[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / tools / gn / generate_test_gn_data.cc
blob11dbf3619bfd2d2a040deca7b0606ee174b26ae9
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.
5 #include <fstream>
6 #include <iostream>
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) {
24 #if defined(OS_WIN)
25 return base::WideToUTF8(path.value());
26 #else
27 return path.value();
28 #endif
31 base::FilePath RepoPathToPathName(const std::vector<int>& repo_path) {
32 base::FilePath ret;
33 for (const auto& elem : repo_path) {
34 ret = ret.Append(UTF8ToFilePath(base::IntToString(elem)));
36 return ret;
39 std::string TargetIndexToLetter(int target_index) {
40 char ret[2];
41 ret[0] = static_cast<char>('a' + target_index);
42 ret[1] = 0;
43 return ret;
46 std::string RepoPathToTargetName(const std::vector<int>& repo_path,
47 int target_index) {
48 std::string ret;
49 for (size_t i = 0; i < repo_path.size(); i++) {
50 if (i != 0)
51 ret.push_back('_');
52 ret.append(base::IntToString(repo_path[i]));
54 ret += TargetIndexToLetter(target_index);
55 return ret;
58 std::string RepoPathToFullTargetName(const std::vector<int>& repo_path,
59 int target_index) {
60 std::string ret;
61 for (const auto& elem : repo_path) {
62 ret.push_back('/');
63 ret.append(base::IntToString(elem));
66 ret += ":" + RepoPathToTargetName(repo_path, target_index);
67 return ret;
70 void WriteLevel(const std::vector<int>& repo_path,
71 int spread,
72 int max_depth,
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);
83 std::ofstream file;
84 file.open(FilePathToUTF8(filename).c_str(),
85 std::ios_base::out | std::ios_base::binary);
86 files_written++;
88 for (int i = 0; i < targets_per_level; i++) {
89 targets_written++;
90 file << "executable(\"" << RepoPathToTargetName(repo_path, i)
91 << "\") {\n";
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) {
97 file << " ]\n";
98 file << " deps = [\n";
99 for (int d = 0; d < spread; d++) {
100 std::vector<int> cur = repo_path;
101 cur.push_back(d);
102 for (int t = 0; t < targets_per_level; t++)
103 file << " \"" << RepoPathToFullTargetName(cur, t) << "\",\n";
106 file << " ]\n}\n\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;
113 cur.push_back(i);
114 WriteLevel(cur, spread, max_depth, targets_per_level, files_per_target);
119 int main() {
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";
124 return 0;