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 <sstream> | 5 #include <sstream> |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "tools/gn/ninja_binary_target_writer.h" | 8 #include "tools/gn/ninja_binary_target_writer.h" |
9 #include "tools/gn/target.h" | 9 #include "tools/gn/target.h" |
10 #include "tools/gn/test_with_scope.h" | 10 #include "tools/gn/test_with_scope.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 // Source set itself. | 31 // Source set itself. |
32 { | 32 { |
33 std::ostringstream out; | 33 std::ostringstream out; |
34 NinjaBinaryTargetWriter writer(&target, out); | 34 NinjaBinaryTargetWriter writer(&target, out); |
35 writer.Run(); | 35 writer.Run(); |
36 | 36 |
37 const char expected[] = | 37 const char expected[] = |
38 "defines =\n" | 38 "defines =\n" |
39 "include_dirs =\n" | 39 "include_dirs =\n" |
40 "cflags =\n" | 40 "cflags =\n" |
41 "cflags_c =\n" | |
brettw
2015/06/29 21:36:10
These got deleted because there are no sources of
| |
42 "cflags_cc =\n" | 41 "cflags_cc =\n" |
43 "cflags_objc =\n" | |
44 "cflags_objcc =\n" | |
45 "root_out_dir = .\n" | 42 "root_out_dir = .\n" |
46 "target_out_dir = obj/foo\n" | 43 "target_out_dir = obj/foo\n" |
47 "target_output_name = bar\n" | 44 "target_output_name = bar\n" |
48 "\n" | 45 "\n" |
49 "build obj/foo/bar.input1.o: cxx ../../foo/input1.cc\n" | 46 "build obj/foo/bar.input1.o: cxx ../../foo/input1.cc\n" |
50 "build obj/foo/bar.input2.o: cxx ../../foo/input2.cc\n" | 47 "build obj/foo/bar.input2.o: cxx ../../foo/input2.cc\n" |
51 "\n" | 48 "\n" |
52 "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.o " | 49 "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.o " |
53 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj\n"; | 50 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj\n"; |
54 std::string out_str = out.str(); | 51 std::string out_str = out.str(); |
55 EXPECT_EQ(expected, out_str); | 52 EXPECT_EQ(expected, out_str); |
56 } | 53 } |
57 | 54 |
58 // A shared library that depends on the source set. | 55 // A shared library that depends on the source set. |
59 Target shlib_target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); | 56 Target shlib_target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |
60 shlib_target.set_output_type(Target::SHARED_LIBRARY); | 57 shlib_target.set_output_type(Target::SHARED_LIBRARY); |
61 shlib_target.public_deps().push_back(LabelTargetPair(&target)); | 58 shlib_target.public_deps().push_back(LabelTargetPair(&target)); |
62 shlib_target.SetToolchain(setup.toolchain()); | 59 shlib_target.SetToolchain(setup.toolchain()); |
63 ASSERT_TRUE(shlib_target.OnResolved(&err)); | 60 ASSERT_TRUE(shlib_target.OnResolved(&err)); |
64 | 61 |
65 { | 62 { |
66 std::ostringstream out; | 63 std::ostringstream out; |
67 NinjaBinaryTargetWriter writer(&shlib_target, out); | 64 NinjaBinaryTargetWriter writer(&shlib_target, out); |
68 writer.Run(); | 65 writer.Run(); |
69 | 66 |
70 const char expected[] = | 67 const char expected[] = |
71 "defines =\n" | 68 "defines =\n" |
72 "include_dirs =\n" | 69 "include_dirs =\n" |
73 "cflags =\n" | |
74 "cflags_c =\n" | |
75 "cflags_cc =\n" | |
76 "cflags_objc =\n" | |
77 "cflags_objcc =\n" | |
78 "root_out_dir = .\n" | 70 "root_out_dir = .\n" |
79 "target_out_dir = obj/foo\n" | 71 "target_out_dir = obj/foo\n" |
80 "target_output_name = libshlib\n" | 72 "target_output_name = libshlib\n" |
81 "\n" | 73 "\n" |
82 "\n" | 74 "\n" |
83 // Ordering of the obj files here should come out in the order | 75 // Ordering of the obj files here should come out in the order |
84 // specified, with the target's first, followed by the source set's, in | 76 // specified, with the target's first, followed by the source set's, in |
85 // order. | 77 // order. |
86 "build ./libshlib.so: solink obj/foo/bar.input1.o " | 78 "build ./libshlib.so: solink obj/foo/bar.input1.o " |
87 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj " | 79 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj " |
(...skipping 13 matching lines...) Expand all Loading... | |
101 ASSERT_TRUE(stlib_target.OnResolved(&err)); | 93 ASSERT_TRUE(stlib_target.OnResolved(&err)); |
102 | 94 |
103 { | 95 { |
104 std::ostringstream out; | 96 std::ostringstream out; |
105 NinjaBinaryTargetWriter writer(&stlib_target, out); | 97 NinjaBinaryTargetWriter writer(&stlib_target, out); |
106 writer.Run(); | 98 writer.Run(); |
107 | 99 |
108 const char expected[] = | 100 const char expected[] = |
109 "defines =\n" | 101 "defines =\n" |
110 "include_dirs =\n" | 102 "include_dirs =\n" |
111 "cflags =\n" | |
112 "cflags_c =\n" | |
113 "cflags_cc =\n" | |
114 "cflags_objc =\n" | |
115 "cflags_objcc =\n" | |
116 "root_out_dir = .\n" | 103 "root_out_dir = .\n" |
117 "target_out_dir = obj/foo\n" | 104 "target_out_dir = obj/foo\n" |
118 "target_output_name = libstlib\n" | 105 "target_output_name = libstlib\n" |
119 "\n" | 106 "\n" |
120 "\n" | 107 "\n" |
121 // There are no sources so there are no params to alink. (In practice | 108 // There are no sources so there are no params to alink. (In practice |
122 // this will probably fail in the archive tool.) | 109 // this will probably fail in the archive tool.) |
123 "build obj/foo/libstlib.a: alink || obj/foo/bar.stamp\n" | 110 "build obj/foo/libstlib.a: alink || obj/foo/bar.stamp\n" |
124 " ldflags =\n" | 111 " ldflags =\n" |
125 " libs =\n" | 112 " libs =\n" |
126 " output_extension = \n"; | 113 " output_extension = \n"; |
127 std::string out_str = out.str(); | 114 std::string out_str = out.str(); |
128 EXPECT_EQ(expected, out_str); | 115 EXPECT_EQ(expected, out_str); |
129 } | 116 } |
130 | 117 |
131 // Make the static library 'complete', which means it should be linked. | 118 // Make the static library 'complete', which means it should be linked. |
132 stlib_target.set_complete_static_lib(true); | 119 stlib_target.set_complete_static_lib(true); |
133 { | 120 { |
134 std::ostringstream out; | 121 std::ostringstream out; |
135 NinjaBinaryTargetWriter writer(&stlib_target, out); | 122 NinjaBinaryTargetWriter writer(&stlib_target, out); |
136 writer.Run(); | 123 writer.Run(); |
137 | 124 |
138 const char expected[] = | 125 const char expected[] = |
139 "defines =\n" | 126 "defines =\n" |
140 "include_dirs =\n" | 127 "include_dirs =\n" |
141 "cflags =\n" | |
142 "cflags_c =\n" | |
143 "cflags_cc =\n" | |
144 "cflags_objc =\n" | |
145 "cflags_objcc =\n" | |
146 "root_out_dir = .\n" | 128 "root_out_dir = .\n" |
147 "target_out_dir = obj/foo\n" | 129 "target_out_dir = obj/foo\n" |
148 "target_output_name = libstlib\n" | 130 "target_output_name = libstlib\n" |
149 "\n" | 131 "\n" |
150 "\n" | 132 "\n" |
151 // Ordering of the obj files here should come out in the order | 133 // Ordering of the obj files here should come out in the order |
152 // specified, with the target's first, followed by the source set's, in | 134 // specified, with the target's first, followed by the source set's, in |
153 // order. | 135 // order. |
154 "build obj/foo/libstlib.a: alink obj/foo/bar.input1.o " | 136 "build obj/foo/libstlib.a: alink obj/foo/bar.input1.o " |
155 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj " | 137 "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj " |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 ASSERT_TRUE(target.OnResolved(&err)); | 171 ASSERT_TRUE(target.OnResolved(&err)); |
190 | 172 |
191 std::ostringstream out; | 173 std::ostringstream out; |
192 NinjaBinaryTargetWriter writer(&target, out); | 174 NinjaBinaryTargetWriter writer(&target, out); |
193 writer.Run(); | 175 writer.Run(); |
194 | 176 |
195 const char expected[] = | 177 const char expected[] = |
196 "defines =\n" | 178 "defines =\n" |
197 "include_dirs =\n" | 179 "include_dirs =\n" |
198 "cflags =\n" | 180 "cflags =\n" |
199 "cflags_c =\n" | |
200 "cflags_cc =\n" | 181 "cflags_cc =\n" |
201 "cflags_objc =\n" | |
202 "cflags_objcc =\n" | |
203 "root_out_dir = .\n" | 182 "root_out_dir = .\n" |
204 "target_out_dir = obj/foo\n" | 183 "target_out_dir = obj/foo\n" |
205 "target_output_name = libshlib\n" | 184 "target_output_name = libshlib\n" |
206 "\n" | 185 "\n" |
207 "build obj/foo/shlib.inputdeps.stamp: stamp obj/foo/action.stamp\n" | 186 "build obj/foo/shlib.inputdeps.stamp: stamp obj/foo/action.stamp\n" |
208 "build obj/foo/libshlib.input1.o: cxx ../../foo/input1.cc" | 187 "build obj/foo/libshlib.input1.o: cxx ../../foo/input1.cc" |
209 " || obj/foo/shlib.inputdeps.stamp\n" | 188 " || obj/foo/shlib.inputdeps.stamp\n" |
210 "build obj/foo/libshlib.input2.o: cxx ../../foo/input2.cc" | 189 "build obj/foo/libshlib.input2.o: cxx ../../foo/input2.cc" |
211 " || obj/foo/shlib.inputdeps.stamp\n" | 190 " || obj/foo/shlib.inputdeps.stamp\n" |
212 "\n" | 191 "\n" |
(...skipping 29 matching lines...) Expand all Loading... | |
242 ASSERT_TRUE(target.OnResolved(&err)); | 221 ASSERT_TRUE(target.OnResolved(&err)); |
243 | 222 |
244 std::ostringstream out; | 223 std::ostringstream out; |
245 NinjaBinaryTargetWriter writer(&target, out); | 224 NinjaBinaryTargetWriter writer(&target, out); |
246 writer.Run(); | 225 writer.Run(); |
247 | 226 |
248 const char expected[] = | 227 const char expected[] = |
249 "defines =\n" | 228 "defines =\n" |
250 "include_dirs =\n" | 229 "include_dirs =\n" |
251 "cflags =\n" | 230 "cflags =\n" |
252 "cflags_c =\n" | |
253 "cflags_cc =\n" | 231 "cflags_cc =\n" |
254 "cflags_objc =\n" | |
255 "cflags_objcc =\n" | |
256 "root_out_dir = .\n" | 232 "root_out_dir = .\n" |
257 "target_out_dir = obj/foo\n" | 233 "target_out_dir = obj/foo\n" |
258 "target_output_name = libshlib\n" | 234 "target_output_name = libshlib\n" |
259 "\n" | 235 "\n" |
260 "build obj/foo/libshlib.input1.o: cxx ../../foo/input1.cc\n" | 236 "build obj/foo/libshlib.input1.o: cxx ../../foo/input1.cc\n" |
261 "build obj/foo/libshlib.input2.o: cxx ../../foo/input2.cc\n" | 237 "build obj/foo/libshlib.input2.o: cxx ../../foo/input2.cc\n" |
262 "\n" | 238 "\n" |
263 "build ./libshlib.so: solink obj/foo/libshlib.input1.o " | 239 "build ./libshlib.so: solink obj/foo/libshlib.input1.o " |
264 "obj/foo/libshlib.input2.o\n" | 240 "obj/foo/libshlib.input2.o\n" |
265 " ldflags =\n" | 241 " ldflags =\n" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 NinjaBinaryTargetWriter inter_writer(&inter, inter_out); | 274 NinjaBinaryTargetWriter inter_writer(&inter, inter_out); |
299 inter_writer.Run(); | 275 inter_writer.Run(); |
300 | 276 |
301 // The intermediate source set will be a stamp file that depends on the | 277 // The intermediate source set will be a stamp file that depends on the |
302 // object files, and will have an order-only dependency on its data dep and | 278 // object files, and will have an order-only dependency on its data dep and |
303 // data file. | 279 // data file. |
304 const char inter_expected[] = | 280 const char inter_expected[] = |
305 "defines =\n" | 281 "defines =\n" |
306 "include_dirs =\n" | 282 "include_dirs =\n" |
307 "cflags =\n" | 283 "cflags =\n" |
308 "cflags_c =\n" | |
309 "cflags_cc =\n" | 284 "cflags_cc =\n" |
310 "cflags_objc =\n" | |
311 "cflags_objcc =\n" | |
312 "root_out_dir = .\n" | 285 "root_out_dir = .\n" |
313 "target_out_dir = obj/foo\n" | 286 "target_out_dir = obj/foo\n" |
314 "target_output_name = inter\n" | 287 "target_output_name = inter\n" |
315 "\n" | 288 "\n" |
316 "build obj/foo/inter.inter.o: cxx ../../foo/inter.cc\n" | 289 "build obj/foo/inter.inter.o: cxx ../../foo/inter.cc\n" |
317 "\n" | 290 "\n" |
318 "build obj/foo/inter.stamp: stamp obj/foo/inter.inter.o || " | 291 "build obj/foo/inter.stamp: stamp obj/foo/inter.inter.o || " |
319 "./data_target\n"; | 292 "./data_target\n"; |
320 EXPECT_EQ(inter_expected, inter_out.str()); | 293 EXPECT_EQ(inter_expected, inter_out.str()); |
321 | 294 |
(...skipping 11 matching lines...) Expand all Loading... | |
333 | 306 |
334 // The final output depends on both object files (one from the final target, | 307 // The final output depends on both object files (one from the final target, |
335 // one from the source set) and has an order-only dependency on the source | 308 // one from the source set) and has an order-only dependency on the source |
336 // set's stamp file and the final target's data file. The source set stamp | 309 // set's stamp file and the final target's data file. The source set stamp |
337 // dependency will create an implicit order-only dependency on the data | 310 // dependency will create an implicit order-only dependency on the data |
338 // target. | 311 // target. |
339 const char final_expected[] = | 312 const char final_expected[] = |
340 "defines =\n" | 313 "defines =\n" |
341 "include_dirs =\n" | 314 "include_dirs =\n" |
342 "cflags =\n" | 315 "cflags =\n" |
343 "cflags_c =\n" | |
344 "cflags_cc =\n" | 316 "cflags_cc =\n" |
345 "cflags_objc =\n" | |
346 "cflags_objcc =\n" | |
347 "root_out_dir = .\n" | 317 "root_out_dir = .\n" |
348 "target_out_dir = obj/foo\n" | 318 "target_out_dir = obj/foo\n" |
349 "target_output_name = exe\n" | 319 "target_output_name = exe\n" |
350 "\n" | 320 "\n" |
351 "build obj/foo/exe.final.o: cxx ../../foo/final.cc\n" | 321 "build obj/foo/exe.final.o: cxx ../../foo/final.cc\n" |
352 "\n" | 322 "\n" |
353 "build ./exe: link obj/foo/exe.final.o obj/foo/inter.inter.o || " | 323 "build ./exe: link obj/foo/exe.final.o obj/foo/inter.inter.o || " |
354 "obj/foo/inter.stamp\n" | 324 "obj/foo/inter.stamp\n" |
355 " ldflags =\n" | 325 " ldflags =\n" |
356 " libs =\n" | 326 " libs =\n" |
(...skipping 16 matching lines...) Expand all Loading... | |
373 ASSERT_TRUE(shared_lib.OnResolved(&err)); | 343 ASSERT_TRUE(shared_lib.OnResolved(&err)); |
374 | 344 |
375 std::ostringstream out; | 345 std::ostringstream out; |
376 NinjaBinaryTargetWriter writer(&shared_lib, out); | 346 NinjaBinaryTargetWriter writer(&shared_lib, out); |
377 writer.Run(); | 347 writer.Run(); |
378 | 348 |
379 const char expected[] = | 349 const char expected[] = |
380 "defines =\n" | 350 "defines =\n" |
381 "include_dirs =\n" | 351 "include_dirs =\n" |
382 "cflags =\n" | 352 "cflags =\n" |
383 "cflags_c =\n" | |
384 "cflags_cc =\n" | 353 "cflags_cc =\n" |
385 "cflags_objc =\n" | |
386 "cflags_objcc =\n" | |
387 "root_out_dir = .\n" | 354 "root_out_dir = .\n" |
388 "target_out_dir = obj/foo\n" | 355 "target_out_dir = obj/foo\n" |
389 "target_output_name = libbar\n" | 356 "target_output_name = libbar\n" |
390 "\n" | 357 "\n" |
391 "build obj/foo/libbar.sources.o: cxx ../../foo/sources.cc\n" | 358 "build obj/foo/libbar.sources.o: cxx ../../foo/sources.cc\n" |
392 "\n" | 359 "\n" |
393 "build ./libbar.so: solink obj/foo/libbar.sources.o | ../../foo/bar.def\n" | 360 "build ./libbar.so: solink obj/foo/libbar.sources.o | ../../foo/bar.def\n" |
394 " ldflags = /DEF:../../foo/bar.def\n" | 361 " ldflags = /DEF:../../foo/bar.def\n" |
395 " libs =\n" | 362 " libs =\n" |
396 " output_extension = .so\n"; | 363 " output_extension = .so\n"; |
397 EXPECT_EQ(expected, out.str()); | 364 EXPECT_EQ(expected, out.str()); |
398 } | 365 } |
366 | |
367 TEST(NinjaBinaryTargetWriter, WinPrecompiledHeaders) { | |
368 Err err; | |
369 | |
370 // This setup's toolchain does not have precompiled headers defined. | |
371 TestWithScope setup; | |
372 | |
373 // A precompiled header toolchain. | |
374 Settings pch_settings(setup.build_settings(), "withpch/"); | |
375 Toolchain pch_toolchain(&pch_settings, | |
376 Label(SourceDir("//toolchain/"), "withpch")); | |
377 | |
378 // Declare a C++ compiler that supports PCH. | |
379 scoped_ptr<Tool> cxx_tool(new Tool); | |
380 TestWithScope::SetCommandForTool( | |
381 "c++ {{source}} {{cflags}} {{cflags_cc}} {{defines}} {{include_dirs}} " | |
382 "-o {{output}}", | |
383 cxx_tool.get()); | |
384 cxx_tool->set_outputs(SubstitutionList::MakeForTest( | |
385 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o")); | |
386 cxx_tool->set_precompiled_header_type(Tool::PCH_MSVC); | |
387 pch_toolchain.SetTool(Toolchain::TYPE_CXX, cxx_tool.Pass()); | |
388 pch_toolchain.ToolchainSetupComplete(); | |
389 | |
390 // This target doesn't specify precompiled headers. | |
391 { | |
392 Target no_pch_target(&pch_settings, | |
393 Label(SourceDir("//foo/"), "no_pch_target")); | |
394 no_pch_target.set_output_type(Target::SOURCE_SET); | |
395 no_pch_target.visibility().SetPublic(); | |
396 no_pch_target.sources().push_back(SourceFile("//foo/input1.cc")); | |
397 no_pch_target.SetToolchain(&pch_toolchain); | |
398 ASSERT_TRUE(no_pch_target.OnResolved(&err)); | |
399 | |
400 std::ostringstream out; | |
401 NinjaBinaryTargetWriter writer(&no_pch_target, out); | |
402 writer.Run(); | |
403 | |
404 const char no_pch_expected[] = | |
405 "defines =\n" | |
406 "include_dirs =\n" | |
407 "cflags =\n" | |
408 "cflags_cc =\n" | |
409 "target_output_name = no_pch_target\n" | |
410 "\n" | |
411 "build withpch/obj/foo/no_pch_target.input1.o: " | |
412 "cxx ../../foo/input1.cc\n" | |
413 "\n" | |
414 "build withpch/obj/foo/no_pch_target.stamp: " | |
415 "stamp withpch/obj/foo/no_pch_target.input1.o\n"; | |
416 EXPECT_EQ(no_pch_expected, out.str()); | |
417 } | |
418 | |
419 // This target specifies PCH. | |
420 { | |
421 Target pch_target(&pch_settings, | |
422 Label(SourceDir("//foo/"), "pch_target")); | |
423 pch_target.config_values().set_precompiled_header("build/precompile.h"); | |
424 pch_target.config_values().set_precompiled_source( | |
425 SourceFile("//build/precompile.cc")); | |
426 pch_target.set_output_type(Target::SOURCE_SET); | |
427 pch_target.visibility().SetPublic(); | |
428 pch_target.sources().push_back(SourceFile("//foo/input1.cc")); | |
429 pch_target.SetToolchain(&pch_toolchain); | |
430 ASSERT_TRUE(pch_target.OnResolved(&err)); | |
431 | |
432 std::ostringstream out; | |
433 NinjaBinaryTargetWriter writer(&pch_target, out); | |
434 writer.Run(); | |
435 | |
436 const char pch_win_expected[] = | |
437 "defines =\n" | |
438 "include_dirs =\n" | |
439 "cflags =\n" | |
440 // There should only be one .pch file created, for C++ files. | |
441 "cflags_cc = /Fpwithpch/obj/foo/pch_target_cc.pch " | |
442 "/Yubuild/precompile.h\n" | |
443 "target_output_name = pch_target\n" | |
444 "\n" | |
445 // Compile the precompiled source file with /Yc. | |
446 "build withpch/obj/build/pch_target.precompile.cc.o: " | |
447 "cxx ../../build/precompile.cc\n" | |
448 " cflags_cc = ${cflags_cc} /Ycbuild/precompile.h\n" | |
449 "\n" | |
450 "build withpch/obj/foo/pch_target.input1.o: " | |
451 "cxx ../../foo/input1.cc | " | |
452 // Explicit dependency on the PCH build step. | |
453 "withpch/obj/build/pch_target.precompile.cc.o\n" | |
454 "\n" | |
455 "build withpch/obj/foo/pch_target.stamp: " | |
456 "stamp withpch/obj/foo/pch_target.input1.o " | |
457 // The precompiled object file was added to the outputs. | |
458 "withpch/obj/build/pch_target.precompile.cc.o\n"; | |
459 EXPECT_EQ(pch_win_expected, out.str()); | |
460 } | |
461 } | |
OLD | NEW |