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.
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h"
14 // Usage: just run in the directory where you want your test source root to be.
16 int files_written
= 0;
17 int targets_written
= 0;
19 base::FilePath
UTF8ToFilePath(const std::string
& s
) {
21 return base::FilePath(base::UTF8ToWide(s
));
23 return base::FilePath(s
);
27 std::string
FilePathToUTF8(const base::FilePath
& path
) {
29 return base::WideToUTF8(path
.value());
35 base::FilePath
RepoPathToPathName(const std::vector
<int>& repo_path
) {
37 for (size_t i
= 0; i
< repo_path
.size(); i
++) {
38 ret
= ret
.Append(UTF8ToFilePath(base::IntToString(repo_path
[i
])));
43 std::string
TargetIndexToLetter(int target_index
) {
45 ret
[0] = 'a' + target_index
;
50 std::string
RepoPathToTargetName(const std::vector
<int>& repo_path
,
53 for (size_t i
= 0; i
< repo_path
.size(); i
++) {
56 ret
.append(base::IntToString(repo_path
[i
]));
58 ret
+= TargetIndexToLetter(target_index
);
62 std::string
RepoPathToFullTargetName(const std::vector
<int>& repo_path
,
65 for (size_t i
= 0; i
< repo_path
.size(); i
++) {
67 ret
.append(base::IntToString(repo_path
[i
]));
70 ret
+= ":" + RepoPathToTargetName(repo_path
, target_index
);
74 void WriteLevel(const std::vector
<int>& repo_path
,
77 int targets_per_level
,
78 int files_per_target
) {
79 base::FilePath dirname
= RepoPathToPathName(repo_path
);
80 base::FilePath filename
= dirname
.AppendASCII("BUILD.gn");
81 std::cout
<< "Writing " << FilePathToUTF8(filename
) << "\n";
83 // Don't keep the file open while recursing.
85 base::CreateDirectory(dirname
);
88 file
.open(FilePathToUTF8(filename
).c_str(),
89 std::ios_base::out
| std::ios_base::binary
);
92 for (int i
= 0; i
< targets_per_level
; i
++) {
94 file
<< "executable(\"" << RepoPathToTargetName(repo_path
, i
)
96 file
<< " sources = [\n";
97 for (int f
= 0; f
< files_per_target
; f
++)
98 file
<< " \"" << base::IntToString(f
) << ".cc\",\n";
100 if (repo_path
.size() < (size_t)max_depth
) {
102 file
<< " deps = [\n";
103 for (int d
= 0; d
< spread
; d
++) {
104 std::vector
<int> cur
= repo_path
;
106 for (int t
= 0; t
< targets_per_level
; t
++)
107 file
<< " \"" << RepoPathToFullTargetName(cur
, t
) << "\",\n";
113 if (repo_path
.size() < (size_t)max_depth
) {
114 // Recursively generate subdirs.
115 for (int i
= 0; i
< spread
; i
++) {
116 std::vector
<int> cur
= repo_path
;
118 WriteLevel(cur
, spread
, max_depth
, targets_per_level
, files_per_target
);
124 WriteLevel(std::vector
<int>(), 5, 4, 3, 50); // 781 files, 2343 targets
125 //WriteLevel(std::vector<int>(), 6, 4, 2, 50);
126 std::cout
<< "Wrote " << files_written
<< " files and "
127 << targets_written
<< " targets.\n";