| 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 "tools/gn/ninja_binary_target_writer.h" |    5 #include "tools/gn/ninja_binary_target_writer.h" | 
|    6  |    6  | 
|    7 #include <sstream> |    7 #include <sstream> | 
|    8 #include <utility> |    8 #include <utility> | 
|    9  |    9  | 
|   10 #include "testing/gtest/include/gtest/gtest.h" |   10 #include "testing/gtest/include/gtest/gtest.h" | 
|   11 #include "tools/gn/scheduler.h" |   11 #include "tools/gn/scheduler.h" | 
|   12 #include "tools/gn/target.h" |   12 #include "tools/gn/target.h" | 
|   13 #include "tools/gn/test_with_scope.h" |   13 #include "tools/gn/test_with_scope.h" | 
|   14  |   14  | 
|   15 TEST(NinjaBinaryTargetWriter, SourceSet) { |   15 TEST(NinjaBinaryTargetWriter, SourceSet) { | 
 |   16   Err err; | 
|   16   TestWithScope setup; |   17   TestWithScope setup; | 
|   17   Err err; |  | 
|   18  |  | 
|   19   setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |  | 
|   20  |   18  | 
|   21   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |   19   Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 
|   22   target.set_output_type(Target::SOURCE_SET); |   20   target.set_output_type(Target::SOURCE_SET); | 
|   23   target.visibility().SetPublic(); |   21   target.visibility().SetPublic(); | 
|   24   target.sources().push_back(SourceFile("//foo/input1.cc")); |   22   target.sources().push_back(SourceFile("//foo/input1.cc")); | 
|   25   target.sources().push_back(SourceFile("//foo/input2.cc")); |   23   target.sources().push_back(SourceFile("//foo/input2.cc")); | 
|   26   // Also test object files, which should be just passed through to the |   24   // Also test object files, which should be just passed through to the | 
|   27   // dependents to link. |   25   // dependents to link. | 
|   28   target.sources().push_back(SourceFile("//foo/input3.o")); |   26   target.sources().push_back(SourceFile("//foo/input3.o")); | 
|   29   target.sources().push_back(SourceFile("//foo/input4.obj")); |   27   target.sources().push_back(SourceFile("//foo/input4.obj")); | 
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  250         "  output_extension = \n" |  248         "  output_extension = \n" | 
|  251         "  output_dir = \n"; |  249         "  output_dir = \n"; | 
|  252     std::string out_str = out.str(); |  250     std::string out_str = out.str(); | 
|  253     EXPECT_EQ(expected, out_str); |  251     EXPECT_EQ(expected, out_str); | 
|  254   } |  252   } | 
|  255 } |  253 } | 
|  256  |  254  | 
|  257 // This tests that output extension and output dir overrides apply, and input |  255 // This tests that output extension and output dir overrides apply, and input | 
|  258 // dependencies are applied. |  256 // dependencies are applied. | 
|  259 TEST(NinjaBinaryTargetWriter, OutputExtensionAndInputDeps) { |  257 TEST(NinjaBinaryTargetWriter, OutputExtensionAndInputDeps) { | 
 |  258   Err err; | 
|  260   TestWithScope setup; |  259   TestWithScope setup; | 
|  261   Err err; |  | 
|  262  |  | 
|  263   setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |  | 
|  264  |  260  | 
|  265   // An action for our library to depend on. |  261   // An action for our library to depend on. | 
|  266   Target action(setup.settings(), Label(SourceDir("//foo/"), "action")); |  262   Target action(setup.settings(), Label(SourceDir("//foo/"), "action")); | 
|  267   action.set_output_type(Target::ACTION_FOREACH); |  263   action.set_output_type(Target::ACTION_FOREACH); | 
|  268   action.visibility().SetPublic(); |  264   action.visibility().SetPublic(); | 
|  269   action.SetToolchain(setup.toolchain()); |  265   action.SetToolchain(setup.toolchain()); | 
|  270   ASSERT_TRUE(action.OnResolved(&err)); |  266   ASSERT_TRUE(action.OnResolved(&err)); | 
|  271  |  267  | 
|  272   // A shared library w/ the output_extension set to a custom value. |  268   // A shared library w/ the output_extension set to a custom value. | 
|  273   Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |  269   Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  307       "  libs =\n" |  303       "  libs =\n" | 
|  308       "  output_extension = .so.6\n" |  304       "  output_extension = .so.6\n" | 
|  309       "  output_dir = foo\n"; |  305       "  output_dir = foo\n"; | 
|  310  |  306  | 
|  311   std::string out_str = out.str(); |  307   std::string out_str = out.str(); | 
|  312   EXPECT_EQ(expected, out_str); |  308   EXPECT_EQ(expected, out_str); | 
|  313 } |  309 } | 
|  314  |  310  | 
|  315 // Tests libs are applied. |  311 // Tests libs are applied. | 
|  316 TEST(NinjaBinaryTargetWriter, LibsAndLibDirs) { |  312 TEST(NinjaBinaryTargetWriter, LibsAndLibDirs) { | 
 |  313   Err err; | 
|  317   TestWithScope setup; |  314   TestWithScope setup; | 
|  318   Err err; |  | 
|  319  |  | 
|  320   setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |  | 
|  321  |  315  | 
|  322   // A shared library w/ libs and lib_dirs. |  316   // A shared library w/ libs and lib_dirs. | 
|  323   Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |  317   Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); | 
|  324   target.set_output_type(Target::SHARED_LIBRARY); |  318   target.set_output_type(Target::SHARED_LIBRARY); | 
|  325   target.config_values().libs().push_back(LibFile(SourceFile("//foo/lib1.a"))); |  319   target.config_values().libs().push_back(LibFile(SourceFile("//foo/lib1.a"))); | 
|  326   target.config_values().libs().push_back(LibFile("foo")); |  320   target.config_values().libs().push_back(LibFile("foo")); | 
|  327   target.config_values().lib_dirs().push_back(SourceDir("//foo/bar/")); |  321   target.config_values().lib_dirs().push_back(SourceDir("//foo/bar/")); | 
|  328   target.SetToolchain(setup.toolchain()); |  322   target.SetToolchain(setup.toolchain()); | 
|  329   ASSERT_TRUE(target.OnResolved(&err)); |  323   ASSERT_TRUE(target.OnResolved(&err)); | 
|  330  |  324  | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
|  344       "  ldflags = -L../../foo/bar\n" |  338       "  ldflags = -L../../foo/bar\n" | 
|  345       "  libs = ../../foo/lib1.a -lfoo\n" |  339       "  libs = ../../foo/lib1.a -lfoo\n" | 
|  346       "  output_extension = .so\n" |  340       "  output_extension = .so\n" | 
|  347       "  output_dir = \n"; |  341       "  output_dir = \n"; | 
|  348  |  342  | 
|  349   std::string out_str = out.str(); |  343   std::string out_str = out.str(); | 
|  350   EXPECT_EQ(expected, out_str); |  344   EXPECT_EQ(expected, out_str); | 
|  351 } |  345 } | 
|  352  |  346  | 
|  353 TEST(NinjaBinaryTargetWriter, EmptyOutputExtension) { |  347 TEST(NinjaBinaryTargetWriter, EmptyOutputExtension) { | 
 |  348   Err err; | 
|  354   TestWithScope setup; |  349   TestWithScope setup; | 
|  355   Err err; |  | 
|  356  |  | 
|  357   setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |  | 
|  358  |  350  | 
|  359   // This test is the same as OutputExtensionAndInputDeps, except that we call |  351   // This test is the same as OutputExtensionAndInputDeps, except that we call | 
|  360   // set_output_extension("") and ensure that we get an empty one and override |  352   // set_output_extension("") and ensure that we get an empty one and override | 
|  361   // the output prefix so that the name matches the target exactly. |  353   // the output prefix so that the name matches the target exactly. | 
|  362   Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |  354   Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); | 
|  363   target.set_output_type(Target::SHARED_LIBRARY); |  355   target.set_output_type(Target::SHARED_LIBRARY); | 
|  364   target.set_output_prefix_override(true); |  356   target.set_output_prefix_override(true); | 
|  365   target.set_output_extension(std::string()); |  357   target.set_output_extension(std::string()); | 
|  366   target.sources().push_back(SourceFile("//foo/input1.cc")); |  358   target.sources().push_back(SourceFile("//foo/input1.cc")); | 
|  367   target.sources().push_back(SourceFile("//foo/input2.cc")); |  359   target.sources().push_back(SourceFile("//foo/input2.cc")); | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
|  390       "  ldflags =\n" |  382       "  ldflags =\n" | 
|  391       "  libs =\n" |  383       "  libs =\n" | 
|  392       "  output_extension = \n" |  384       "  output_extension = \n" | 
|  393       "  output_dir = \n"; |  385       "  output_dir = \n"; | 
|  394  |  386  | 
|  395   std::string out_str = out.str(); |  387   std::string out_str = out.str(); | 
|  396   EXPECT_EQ(expected, out_str); |  388   EXPECT_EQ(expected, out_str); | 
|  397 } |  389 } | 
|  398  |  390  | 
|  399 TEST(NinjaBinaryTargetWriter, SourceSetDataDeps) { |  391 TEST(NinjaBinaryTargetWriter, SourceSetDataDeps) { | 
 |  392   Err err; | 
|  400   TestWithScope setup; |  393   TestWithScope setup; | 
|  401   setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |  | 
|  402  |  | 
|  403   Err err; |  | 
|  404  |  394  | 
|  405   // This target is a data (runtime) dependency of the intermediate target. |  395   // This target is a data (runtime) dependency of the intermediate target. | 
|  406   Target data(setup.settings(), Label(SourceDir("//foo/"), "data_target")); |  396   Target data(setup.settings(), Label(SourceDir("//foo/"), "data_target")); | 
|  407   data.set_output_type(Target::EXECUTABLE); |  397   data.set_output_type(Target::EXECUTABLE); | 
|  408   data.visibility().SetPublic(); |  398   data.visibility().SetPublic(); | 
|  409   data.SetToolchain(setup.toolchain()); |  399   data.SetToolchain(setup.toolchain()); | 
|  410   ASSERT_TRUE(data.OnResolved(&err)); |  400   ASSERT_TRUE(data.OnResolved(&err)); | 
|  411  |  401  | 
|  412   // Intermediate source set target. |  402   // Intermediate source set target. | 
|  413   Target inter(setup.settings(), Label(SourceDir("//foo/"), "inter")); |  403   Target inter(setup.settings(), Label(SourceDir("//foo/"), "inter")); | 
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  472       "build ./exe: link obj/foo/exe.final.o obj/foo/inter.inter.o || " |  462       "build ./exe: link obj/foo/exe.final.o obj/foo/inter.inter.o || " | 
|  473           "obj/foo/inter.stamp\n" |  463           "obj/foo/inter.stamp\n" | 
|  474       "  ldflags =\n" |  464       "  ldflags =\n" | 
|  475       "  libs =\n" |  465       "  libs =\n" | 
|  476       "  output_extension = \n" |  466       "  output_extension = \n" | 
|  477       "  output_dir = \n"; |  467       "  output_dir = \n"; | 
|  478   EXPECT_EQ(final_expected, final_out.str()); |  468   EXPECT_EQ(final_expected, final_out.str()); | 
|  479 } |  469 } | 
|  480  |  470  | 
|  481 TEST(NinjaBinaryTargetWriter, SharedLibraryModuleDefinitionFile) { |  471 TEST(NinjaBinaryTargetWriter, SharedLibraryModuleDefinitionFile) { | 
 |  472   Err err; | 
|  482   TestWithScope setup; |  473   TestWithScope setup; | 
|  483   setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |  | 
|  484  |  474  | 
|  485   Target shared_lib(setup.settings(), Label(SourceDir("//foo/"), "bar")); |  475   Target shared_lib(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 
|  486   shared_lib.set_output_type(Target::SHARED_LIBRARY); |  476   shared_lib.set_output_type(Target::SHARED_LIBRARY); | 
|  487   shared_lib.SetToolchain(setup.toolchain()); |  477   shared_lib.SetToolchain(setup.toolchain()); | 
|  488   shared_lib.sources().push_back(SourceFile("//foo/sources.cc")); |  478   shared_lib.sources().push_back(SourceFile("//foo/sources.cc")); | 
|  489   shared_lib.sources().push_back(SourceFile("//foo/bar.def")); |  479   shared_lib.sources().push_back(SourceFile("//foo/bar.def")); | 
|  490  |  | 
|  491   Err err; |  | 
|  492   ASSERT_TRUE(shared_lib.OnResolved(&err)); |  480   ASSERT_TRUE(shared_lib.OnResolved(&err)); | 
|  493  |  481  | 
|  494   std::ostringstream out; |  482   std::ostringstream out; | 
|  495   NinjaBinaryTargetWriter writer(&shared_lib, out); |  483   NinjaBinaryTargetWriter writer(&shared_lib, out); | 
|  496   writer.Run(); |  484   writer.Run(); | 
|  497  |  485  | 
|  498   const char expected[] = |  486   const char expected[] = | 
|  499       "defines =\n" |  487       "defines =\n" | 
|  500       "include_dirs =\n" |  488       "include_dirs =\n" | 
|  501       "cflags =\n" |  489       "cflags =\n" | 
|  502       "cflags_cc =\n" |  490       "cflags_cc =\n" | 
|  503       "root_out_dir = .\n" |  491       "root_out_dir = .\n" | 
|  504       "target_out_dir = obj/foo\n" |  492       "target_out_dir = obj/foo\n" | 
|  505       "target_output_name = libbar\n" |  493       "target_output_name = libbar\n" | 
|  506       "\n" |  494       "\n" | 
|  507       "build obj/foo/libbar.sources.o: cxx ../../foo/sources.cc\n" |  495       "build obj/foo/libbar.sources.o: cxx ../../foo/sources.cc\n" | 
|  508       "\n" |  496       "\n" | 
|  509       "build ./libbar.so: solink obj/foo/libbar.sources.o | ../../foo/bar.def\n" |  497       "build ./libbar.so: solink obj/foo/libbar.sources.o | ../../foo/bar.def\n" | 
|  510       "  ldflags = /DEF:../../foo/bar.def\n" |  498       "  ldflags = /DEF:../../foo/bar.def\n" | 
|  511       "  libs =\n" |  499       "  libs =\n" | 
|  512       "  output_extension = .so\n" |  500       "  output_extension = .so\n" | 
|  513       "  output_dir = \n"; |  501       "  output_dir = \n"; | 
|  514   EXPECT_EQ(expected, out.str()); |  502   EXPECT_EQ(expected, out.str()); | 
|  515 } |  503 } | 
|  516  |  504  | 
|  517 TEST(NinjaBinaryTargetWriter, LoadableModule) { |  505 TEST(NinjaBinaryTargetWriter, LoadableModule) { | 
 |  506   Err err; | 
|  518   TestWithScope setup; |  507   TestWithScope setup; | 
|  519   setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |  | 
|  520  |  508  | 
|  521   Target loadable_module(setup.settings(), Label(SourceDir("//foo/"), "bar")); |  509   Target loadable_module(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 
|  522   loadable_module.set_output_type(Target::LOADABLE_MODULE); |  510   loadable_module.set_output_type(Target::LOADABLE_MODULE); | 
|  523   loadable_module.visibility().SetPublic(); |  511   loadable_module.visibility().SetPublic(); | 
|  524   loadable_module.SetToolchain(setup.toolchain()); |  512   loadable_module.SetToolchain(setup.toolchain()); | 
|  525   loadable_module.sources().push_back(SourceFile("//foo/sources.cc")); |  513   loadable_module.sources().push_back(SourceFile("//foo/sources.cc")); | 
|  526  |  | 
|  527   Err err; |  | 
|  528   ASSERT_TRUE(loadable_module.OnResolved(&err)) << err.message(); |  514   ASSERT_TRUE(loadable_module.OnResolved(&err)) << err.message(); | 
|  529  |  515  | 
|  530   std::ostringstream out; |  516   std::ostringstream out; | 
|  531   NinjaBinaryTargetWriter writer(&loadable_module, out); |  517   NinjaBinaryTargetWriter writer(&loadable_module, out); | 
|  532   writer.Run(); |  518   writer.Run(); | 
|  533  |  519  | 
|  534   const char loadable_expected[] = |  520   const char loadable_expected[] = | 
|  535       "defines =\n" |  521       "defines =\n" | 
|  536       "include_dirs =\n" |  522       "include_dirs =\n" | 
|  537       "cflags =\n" |  523       "cflags =\n" | 
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  851   NinjaBinaryTargetWriter writer(&target, out); |  837   NinjaBinaryTargetWriter writer(&target, out); | 
|  852   writer.Run(); |  838   writer.Run(); | 
|  853  |  839  | 
|  854   // Should have issued an error. |  840   // Should have issued an error. | 
|  855   EXPECT_TRUE(scheduler.is_failed()); |  841   EXPECT_TRUE(scheduler.is_failed()); | 
|  856 } |  842 } | 
|  857  |  843  | 
|  858 // This tests that output extension and output dir overrides apply, and input |  844 // This tests that output extension and output dir overrides apply, and input | 
|  859 // dependencies are applied. |  845 // dependencies are applied. | 
|  860 TEST(NinjaBinaryTargetWriter, InputFiles) { |  846 TEST(NinjaBinaryTargetWriter, InputFiles) { | 
 |  847   Err err; | 
|  861   TestWithScope setup; |  848   TestWithScope setup; | 
|  862   Err err; |  | 
|  863  |  | 
|  864   setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |  | 
|  865  |  849  | 
|  866   // This target has one input. |  850   // This target has one input. | 
|  867   { |  851   { | 
|  868     Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |  852     Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 
|  869     target.set_output_type(Target::SOURCE_SET); |  853     target.set_output_type(Target::SOURCE_SET); | 
|  870     target.visibility().SetPublic(); |  854     target.visibility().SetPublic(); | 
|  871     target.sources().push_back(SourceFile("//foo/input1.cc")); |  855     target.sources().push_back(SourceFile("//foo/input1.cc")); | 
|  872     target.sources().push_back(SourceFile("//foo/input2.cc")); |  856     target.sources().push_back(SourceFile("//foo/input2.cc")); | 
|  873     target.inputs().push_back(SourceFile("//foo/input.data")); |  857     target.inputs().push_back(SourceFile("//foo/input.data")); | 
|  874     target.SetToolchain(setup.toolchain()); |  858     target.SetToolchain(setup.toolchain()); | 
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  929           " | obj/foo/bar.inputs.stamp\n" |  913           " | obj/foo/bar.inputs.stamp\n" | 
|  930         "build obj/foo/bar.input2.o: cxx ../../foo/input2.cc" |  914         "build obj/foo/bar.input2.o: cxx ../../foo/input2.cc" | 
|  931           " | obj/foo/bar.inputs.stamp\n" |  915           " | obj/foo/bar.inputs.stamp\n" | 
|  932         "\n" |  916         "\n" | 
|  933         "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.o " |  917         "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.o " | 
|  934             "obj/foo/bar.input2.o\n"; |  918             "obj/foo/bar.input2.o\n"; | 
|  935  |  919  | 
|  936     EXPECT_EQ(expected, out.str()); |  920     EXPECT_EQ(expected, out.str()); | 
|  937   } |  921   } | 
|  938 } |  922 } | 
| OLD | NEW |