Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / tools / gn / generate_test_gn_data.cc
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 <fstream>
6 #include <iostream>
7
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"
13
14 // Usage: just run in the directory where you want your test source root to be.
15
16 int files_written = 0;
17 int targets_written = 0;
18
19 base::FilePath UTF8ToFilePath(const std::string& s) {
20 #if defined(OS_WIN)
21   return base::FilePath(base::UTF8ToWide(s));
22 #else
23   return base::FilePath(s);
24 #endif
25 }
26
27 std::string FilePathToUTF8(const base::FilePath& path) {
28 #if defined(OS_WIN)
29   return base::WideToUTF8(path.value());
30 #else
31   return path.value();
32 #endif
33 }
34
35 base::FilePath RepoPathToPathName(const std::vector<int>& repo_path) {
36   base::FilePath ret;
37   for (size_t i = 0; i < repo_path.size(); i++) {
38     ret = ret.Append(UTF8ToFilePath(base::IntToString(repo_path[i])));
39   }
40   return ret;
41 }
42
43 std::string TargetIndexToLetter(int target_index) {
44   char ret[2];
45   ret[0] = 'a' + target_index;
46   ret[1] = 0;
47   return ret;
48 }
49
50 std::string RepoPathToTargetName(const std::vector<int>& repo_path,
51                                  int target_index) {
52   std::string ret;
53   for (size_t i = 0; i < repo_path.size(); i++) {
54     if (i != 0)
55       ret.push_back('_');
56     ret.append(base::IntToString(repo_path[i]));
57   }
58   ret += TargetIndexToLetter(target_index);
59   return ret;
60 }
61
62 std::string RepoPathToFullTargetName(const std::vector<int>& repo_path,
63                                  int target_index) {
64   std::string ret;
65   for (size_t i = 0; i < repo_path.size(); i++) {
66     ret.push_back('/');
67     ret.append(base::IntToString(repo_path[i]));
68   }
69
70   ret += ":" + RepoPathToTargetName(repo_path, target_index);
71   return ret;
72 }
73
74 void WriteLevel(const std::vector<int>& repo_path,
75                 int spread,
76                 int max_depth,
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";
82
83   // Don't keep the file open while recursing.
84   {
85     base::CreateDirectory(dirname);
86
87     std::ofstream file;
88     file.open(FilePathToUTF8(filename).c_str(),
89               std::ios_base::out | std::ios_base::binary);
90     files_written++;
91
92     for (int i = 0; i < targets_per_level; i++) {
93       targets_written++;
94       file << "executable(\"" << RepoPathToTargetName(repo_path, i)
95            << "\") {\n";
96       file << "  sources = [\n";
97       for (int f = 0; f < files_per_target; f++)
98         file << "    \"" << base::IntToString(f) << ".cc\",\n";
99
100       if (repo_path.size() < (size_t)max_depth) {
101         file << "  ]\n";
102         file << "  deps = [\n";
103         for (int d = 0; d < spread; d++) {
104           std::vector<int> cur = repo_path;
105           cur.push_back(d);
106           for (int t = 0; t < targets_per_level; t++)
107             file << "    \"" << RepoPathToFullTargetName(cur, t) << "\",\n";
108         }
109       }
110       file << "  ]\n}\n\n";
111     }
112   }
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;
117       cur.push_back(i);
118       WriteLevel(cur, spread, max_depth, targets_per_level, files_per_target);
119     }
120   }
121 }
122
123 int main() {
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";
128   return 0;
129 }