OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stddef.h> | |
6 | |
7 #include <fstream> | |
8 #include <iostream> | |
9 | |
10 #include "base/files/file_path.h" | |
11 #include "base/files/file_util.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "base/strings/utf_string_conversions.h" | |
14 #include "build/build_config.h" | |
15 | |
16 // Usage: just run in the directory where you want your test source root to be. | |
17 | |
18 int files_written = 0; | |
19 int targets_written = 0; | |
20 | |
21 base::FilePath UTF8ToFilePath(const std::string& s) { | |
22 return base::FilePath::FromUTF8Unsafe(s); | |
23 } | |
24 | |
25 std::string FilePathToUTF8(const base::FilePath& path) { | |
26 #if defined(OS_WIN) | |
27 return base::WideToUTF8(path.value()); | |
28 #else | |
29 return path.value(); | |
30 #endif | |
31 } | |
32 | |
33 base::FilePath RepoPathToPathName(const std::vector<int>& repo_path) { | |
34 base::FilePath ret; | |
35 for (const auto& elem : repo_path) { | |
36 ret = ret.Append(UTF8ToFilePath(base::IntToString(elem))); | |
37 } | |
38 return ret; | |
39 } | |
40 | |
41 std::string TargetIndexToLetter(int target_index) { | |
42 char ret[2]; | |
43 ret[0] = static_cast<char>('a' + target_index); | |
44 ret[1] = 0; | |
45 return ret; | |
46 } | |
47 | |
48 std::string RepoPathToTargetName(const std::vector<int>& repo_path, | |
49 int target_index) { | |
50 std::string ret; | |
51 for (size_t i = 0; i < repo_path.size(); i++) { | |
52 if (i != 0) | |
53 ret.push_back('_'); | |
54 ret.append(base::IntToString(repo_path[i])); | |
55 } | |
56 ret += TargetIndexToLetter(target_index); | |
57 return ret; | |
58 } | |
59 | |
60 std::string RepoPathToFullTargetName(const std::vector<int>& repo_path, | |
61 int target_index) { | |
62 std::string ret; | |
63 for (const auto& elem : repo_path) { | |
64 ret.push_back('/'); | |
65 ret.append(base::IntToString(elem)); | |
66 } | |
67 | |
68 ret += ":" + RepoPathToTargetName(repo_path, target_index); | |
69 return ret; | |
70 } | |
71 | |
72 void WriteLevel(const std::vector<int>& repo_path, | |
73 int spread, | |
74 int max_depth, | |
75 int targets_per_level, | |
76 int files_per_target) { | |
77 base::FilePath dirname = RepoPathToPathName(repo_path); | |
78 base::FilePath filename = dirname.AppendASCII("BUILD.gn"); | |
79 std::cout << "Writing " << FilePathToUTF8(filename) << "\n"; | |
80 | |
81 // Don't keep the file open while recursing. | |
82 { | |
83 base::CreateDirectory(dirname); | |
84 | |
85 std::ofstream file; | |
86 file.open(FilePathToUTF8(filename).c_str(), | |
87 std::ios_base::out | std::ios_base::binary); | |
88 files_written++; | |
89 | |
90 for (int i = 0; i < targets_per_level; i++) { | |
91 targets_written++; | |
92 file << "executable(\"" << RepoPathToTargetName(repo_path, i) | |
93 << "\") {\n"; | |
94 file << " sources = [\n"; | |
95 for (int f = 0; f < files_per_target; f++) | |
96 file << " \"" << base::IntToString(f) << ".cc\",\n"; | |
97 | |
98 if (repo_path.size() < (size_t)max_depth) { | |
99 file << " ]\n"; | |
100 file << " deps = [\n"; | |
101 for (int d = 0; d < spread; d++) { | |
102 std::vector<int> cur = repo_path; | |
103 cur.push_back(d); | |
104 for (int t = 0; t < targets_per_level; t++) | |
105 file << " \"" << RepoPathToFullTargetName(cur, t) << "\",\n"; | |
106 } | |
107 } | |
108 file << " ]\n}\n\n"; | |
109 } | |
110 } | |
111 if (repo_path.size() < (size_t)max_depth) { | |
112 // Recursively generate subdirs. | |
113 for (int i = 0; i < spread; i++) { | |
114 std::vector<int> cur = repo_path; | |
115 cur.push_back(i); | |
116 WriteLevel(cur, spread, max_depth, targets_per_level, files_per_target); | |
117 } | |
118 } | |
119 } | |
120 | |
121 int main() { | |
122 WriteLevel(std::vector<int>(), 5, 4, 3, 50); // 781 files, 2343 targets | |
123 //WriteLevel(std::vector<int>(), 6, 4, 2, 50); | |
124 std::cout << "Wrote " << files_written << " files and " | |
125 << targets_written << " targets.\n"; | |
126 return 0; | |
127 } | |
OLD | NEW |