OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <sstream> | 6 #include <sstream> |
7 | 7 |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "tools/gn/file_template.h" | 9 #include "tools/gn/file_template.h" |
10 #include "tools/gn/ninja_script_target_writer.h" | 10 #include "tools/gn/ninja_script_target_writer.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 std::vector<OutputFile> output_files; | 29 std::vector<OutputFile> output_files; |
30 writer.WriteOutputFilesForBuildLine(output_template, source, &output_files); | 30 writer.WriteOutputFilesForBuildLine(output_template, source, &output_files); |
31 | 31 |
32 std::string out_str = out.str(); | 32 std::string out_str = out.str(); |
33 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
34 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | 34 std::replace(out_str.begin(), out_str.end(), '\\', '/'); |
35 #endif | 35 #endif |
36 EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out_str); | 36 EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out_str); |
37 } | 37 } |
38 | 38 |
| 39 TEST(NinjaScriptTargetWriter, WriteOutputFilesForBuildLineWithDepfile) { |
| 40 TestWithScope setup; |
| 41 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 42 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 43 |
| 44 target.script_values().set_depfile( |
| 45 SourceFile("//out/Debug/gen/{{source_name_part}}.d")); |
| 46 target.script_values().outputs().push_back( |
| 47 SourceFile("//out/Debug/gen/{{source_name_part}}.h")); |
| 48 target.script_values().outputs().push_back( |
| 49 SourceFile("//out/Debug/gen/{{source_name_part}}.cc")); |
| 50 |
| 51 std::ostringstream out; |
| 52 NinjaScriptTargetWriter writer(&target, setup.toolchain(), out); |
| 53 |
| 54 FileTemplate output_template = writer.GetOutputTemplate(); |
| 55 |
| 56 SourceFile source("//foo/bar.in"); |
| 57 std::vector<OutputFile> output_files; |
| 58 writer.WriteOutputFilesForBuildLine(output_template, source, &output_files); |
| 59 |
| 60 std::string out_str = out.str(); |
| 61 #if defined(OS_WIN) |
| 62 std::replace(out_str.begin(), out_str.end(), '\\', '/'); |
| 63 #endif |
| 64 EXPECT_EQ(" gen/bar.d gen/bar.h gen/bar.cc", out_str); |
| 65 } |
| 66 |
39 TEST(NinjaScriptTargetWriter, WriteArgsSubstitutions) { | 67 TEST(NinjaScriptTargetWriter, WriteArgsSubstitutions) { |
40 TestWithScope setup; | 68 TestWithScope setup; |
41 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 69 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
42 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 70 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
43 | 71 |
44 std::ostringstream out; | 72 std::ostringstream out; |
45 NinjaScriptTargetWriter writer(&target, setup.toolchain(), out); | 73 NinjaScriptTargetWriter writer(&target, setup.toolchain(), out); |
46 | 74 |
47 std::vector<std::string> args; | 75 std::vector<std::string> args; |
48 args.push_back("-i"); | 76 args.push_back("-i"); |
(...skipping 21 matching lines...) Expand all Loading... |
70 target.sources().push_back(SourceFile("//foo/input1.txt")); | 98 target.sources().push_back(SourceFile("//foo/input1.txt")); |
71 target.sources().push_back(SourceFile("//foo/input2.txt")); | 99 target.sources().push_back(SourceFile("//foo/input2.txt")); |
72 | 100 |
73 target.script_values().set_script(SourceFile("//foo/script.py")); | 101 target.script_values().set_script(SourceFile("//foo/script.py")); |
74 | 102 |
75 target.script_values().args().push_back("-i"); | 103 target.script_values().args().push_back("-i"); |
76 target.script_values().args().push_back("{{source}}"); | 104 target.script_values().args().push_back("{{source}}"); |
77 target.script_values().args().push_back( | 105 target.script_values().args().push_back( |
78 "--out=foo bar{{source_name_part}}.o"); | 106 "--out=foo bar{{source_name_part}}.o"); |
79 | 107 |
80 target.script_values().outputs().push_back(SourceFile("//out/Debug/{{source_na
me_part}}.out")); | 108 target.script_values().outputs().push_back( |
| 109 SourceFile("//out/Debug/{{source_name_part}}.out")); |
81 | 110 |
82 target.source_prereqs().push_back(SourceFile("//foo/included.txt")); | 111 target.source_prereqs().push_back(SourceFile("//foo/included.txt")); |
83 | 112 |
84 // Posix. | 113 // Posix. |
85 { | 114 { |
86 setup.settings()->set_target_os(Settings::LINUX); | 115 setup.settings()->set_target_os(Settings::LINUX); |
87 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( | 116 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
88 "/usr/bin/python"))); | 117 "/usr/bin/python"))); |
89 | 118 |
90 std::ostringstream out; | 119 std::ostringstream out; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 " source_name_part = input2\n" | 181 " source_name_part = input2\n" |
153 "\n" | 182 "\n" |
154 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; | 183 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
155 std::string out_str = out.str(); | 184 std::string out_str = out.str(); |
156 #if defined(OS_WIN) | 185 #if defined(OS_WIN) |
157 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | 186 std::replace(out_str.begin(), out_str.end(), '\\', '/'); |
158 #endif | 187 #endif |
159 EXPECT_EQ(expected_win, out_str); | 188 EXPECT_EQ(expected_win, out_str); |
160 } | 189 } |
161 } | 190 } |
| 191 |
| 192 // Tests the "run script over multiple source files" mode, with a depfile. |
| 193 TEST(NinjaScriptTargetWriter, InvokeOverSourcesWithDepfile) { |
| 194 TestWithScope setup; |
| 195 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 196 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 197 target.set_output_type(Target::CUSTOM); |
| 198 |
| 199 target.sources().push_back(SourceFile("//foo/input1.txt")); |
| 200 target.sources().push_back(SourceFile("//foo/input2.txt")); |
| 201 |
| 202 target.script_values().set_script(SourceFile("//foo/script.py")); |
| 203 target.script_values().set_depfile( |
| 204 SourceFile("//out/Debug/gen/{{source_name_part}}.d")); |
| 205 |
| 206 target.script_values().args().push_back("-i"); |
| 207 target.script_values().args().push_back("{{source}}"); |
| 208 target.script_values().args().push_back( |
| 209 "--out=foo bar{{source_name_part}}.o"); |
| 210 |
| 211 target.script_values().outputs().push_back( |
| 212 SourceFile("//out/Debug/{{source_name_part}}.out")); |
| 213 |
| 214 target.source_prereqs().push_back(SourceFile("//foo/included.txt")); |
| 215 |
| 216 // Posix. |
| 217 { |
| 218 setup.settings()->set_target_os(Settings::LINUX); |
| 219 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
| 220 "/usr/bin/python"))); |
| 221 |
| 222 std::ostringstream out; |
| 223 NinjaScriptTargetWriter writer(&target, setup.toolchain(), out); |
| 224 writer.Run(); |
| 225 |
| 226 const char expected_linux[] = |
| 227 "rule __foo_bar___rule\n" |
| 228 " command = /usr/bin/python ../../foo/script.py -i ${source} " |
| 229 "\"--out=foo$ bar${source_name_part}.o\"\n" |
| 230 " description = CUSTOM //foo:bar()\n" |
| 231 " restat = 1\n" |
| 232 "\n" |
| 233 "build gen/input1.d input1.out: __foo_bar___rule ../../foo/input1.txt" |
| 234 " | ../../foo/included.txt\n" |
| 235 " source = ../../foo/input1.txt\n" |
| 236 " source_name_part = input1\n" |
| 237 " depfile = gen/input1.d\n" |
| 238 "build gen/input2.d input2.out: __foo_bar___rule ../../foo/input2.txt" |
| 239 " | ../../foo/included.txt\n" |
| 240 " source = ../../foo/input2.txt\n" |
| 241 " source_name_part = input2\n" |
| 242 " depfile = gen/input2.d\n" |
| 243 "\n" |
| 244 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
| 245 |
| 246 std::string out_str = out.str(); |
| 247 #if defined(OS_WIN) |
| 248 std::replace(out_str.begin(), out_str.end(), '\\', '/'); |
| 249 #endif |
| 250 EXPECT_EQ(expected_linux, out_str); |
| 251 } |
| 252 |
| 253 // Windows. |
| 254 { |
| 255 // Note: we use forward slashes here so that the output will be the same on |
| 256 // Linux and Windows. |
| 257 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
| 258 "C:/python/python.exe"))); |
| 259 setup.settings()->set_target_os(Settings::WIN); |
| 260 |
| 261 std::ostringstream out; |
| 262 NinjaScriptTargetWriter writer(&target, setup.toolchain(), out); |
| 263 writer.Run(); |
| 264 |
| 265 // TODO(brettw) I think we'll need to worry about backslashes here |
| 266 // depending if we're on actual Windows or Linux pretending to be Windows. |
| 267 const char expected_win[] = |
| 268 "rule __foo_bar___rule\n" |
| 269 " command = C:/python/python.exe gyp-win-tool action-wrapper " |
| 270 "environment.x86 __foo_bar___rule.$unique_name.rsp\n" |
| 271 " description = CUSTOM //foo:bar()\n" |
| 272 " restat = 1\n" |
| 273 " rspfile = __foo_bar___rule.$unique_name.rsp\n" |
| 274 " rspfile_content = C:/python/python.exe ../../foo/script.py -i " |
| 275 "${source} \"--out=foo$ bar${source_name_part}.o\"\n" |
| 276 "\n" |
| 277 "build gen/input1.d input1.out: __foo_bar___rule ../../foo/input1.txt" |
| 278 " | ../../foo/included.txt\n" |
| 279 " unique_name = 0\n" |
| 280 " source = ../../foo/input1.txt\n" |
| 281 " source_name_part = input1\n" |
| 282 " depfile = gen/input1.d\n" |
| 283 "build gen/input2.d input2.out: __foo_bar___rule ../../foo/input2.txt" |
| 284 " | ../../foo/included.txt\n" |
| 285 " unique_name = 1\n" |
| 286 " source = ../../foo/input2.txt\n" |
| 287 " source_name_part = input2\n" |
| 288 " depfile = gen/input2.d\n" |
| 289 "\n" |
| 290 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
| 291 std::string out_str = out.str(); |
| 292 #if defined(OS_WIN) |
| 293 std::replace(out_str.begin(), out_str.end(), '\\', '/'); |
| 294 #endif |
| 295 EXPECT_EQ(expected_win, out_str); |
| 296 } |
| 297 } |
OLD | NEW |