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/files/file_path.h"
9 #include "base/files/file_util.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
) {
20 return base::FilePath::FromUTF8Unsafe(s
);
23 std::string
FilePathToUTF8(const base::FilePath
& path
) {
25 return base::WideToUTF8(path
.value());
31 base::FilePath
RepoPathToPathName(const std::vector
<int>& repo_path
) {
33 for (const auto& elem
: repo_path
) {
34 ret
= ret
.Append(UTF8ToFilePath(base::IntToString(elem
)));
39 std::string
TargetIndexToLetter(int target_index
) {
41 ret
[0] = static_cast<char>('a' + target_index
);
46 std::string
RepoPathToTargetName(const std::vector
<int>& repo_path
,
49 for (size_t i
= 0; i
< repo_path
.size(); i
++) {
52 ret
.append(base::IntToString(repo_path
[i
]));
54 ret
+= TargetIndexToLetter(target_index
);
58 std::string
RepoPathToFullTargetName(const std::vector
<int>& repo_path
,
61 for (const auto& elem
: repo_path
) {
63 ret
.append(base::IntToString(elem
));
66 ret
+= ":" + RepoPathToTargetName(repo_path
, target_index
);
70 void WriteLevel(const std::vector
<int>& repo_path
,
73 int targets_per_level
,
74 int files_per_target
) {
75 base::FilePath dirname
= RepoPathToPathName(repo_path
);
76 base::FilePath filename
= dirname
.AppendASCII("BUILD.gn");
77 std::cout
<< "Writing " << FilePathToUTF8(filename
) << "\n";
79 // Don't keep the file open while recursing.
81 base::CreateDirectory(dirname
);
84 file
.open(FilePathToUTF8(filename
).c_str(),
85 std::ios_base::out
| std::ios_base::binary
);
88 for (int i
= 0; i
< targets_per_level
; i
++) {
90 file
<< "executable(\"" << RepoPathToTargetName(repo_path
, i
)
92 file
<< " sources = [\n";
93 for (int f
= 0; f
< files_per_target
; f
++)
94 file
<< " \"" << base::IntToString(f
) << ".cc\",\n";
96 if (repo_path
.size() < (size_t)max_depth
) {
98 file
<< " deps = [\n";
99 for (int d
= 0; d
< spread
; d
++) {
100 std::vector
<int> cur
= repo_path
;
102 for (int t
= 0; t
< targets_per_level
; t
++)
103 file
<< " \"" << RepoPathToFullTargetName(cur
, t
) << "\",\n";
109 if (repo_path
.size() < (size_t)max_depth
) {
110 // Recursively generate subdirs.
111 for (int i
= 0; i
< spread
; i
++) {
112 std::vector
<int> cur
= repo_path
;
114 WriteLevel(cur
, spread
, max_depth
, targets_per_level
, files_per_target
);
120 WriteLevel(std::vector
<int>(), 5, 4, 3, 50); // 781 files, 2343 targets
121 //WriteLevel(std::vector<int>(), 6, 4, 2, 50);
122 std::cout
<< "Wrote " << files_written
<< " files and "
123 << targets_written
<< " targets.\n";