| // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include <sstream> |
| |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "tools/gn/ninja_binary_target_writer.h" |
| #include "tools/gn/target.h" |
| #include "tools/gn/test_with_scope.h" |
| |
| TEST(NinjaBinaryTargetWriter, SourceSet) { |
| TestWithScope setup; |
| Err err; |
| |
| setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| setup.settings()->set_target_os(Settings::WIN); |
| |
| Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| target.set_output_type(Target::SOURCE_SET); |
| target.visibility().SetPublic(); |
| target.sources().push_back(SourceFile("//foo/input1.cc")); |
| target.sources().push_back(SourceFile("//foo/input2.cc")); |
| // Also test object files, which should be just passed through to the |
| // dependents to link. |
| target.sources().push_back(SourceFile("//foo/input3.o")); |
| target.sources().push_back(SourceFile("//foo/input4.obj")); |
| target.SetToolchain(setup.toolchain()); |
| ASSERT_TRUE(target.OnResolved(&err)); |
| |
| // Source set itself. |
| { |
| std::ostringstream out; |
| NinjaBinaryTargetWriter writer(&target, out); |
| writer.Run(); |
| |
| const char expected[] = |
| "defines =\n" |
| "include_dirs =\n" |
| "cflags =\n" |
| "cflags_c =\n" |
| "cflags_cc =\n" |
| "cflags_objc =\n" |
| "cflags_objcc =\n" |
| "root_out_dir = .\n" |
| "target_out_dir = obj/foo\n" |
| "target_output_name = bar\n" |
| "\n" |
| "build obj/foo/bar.input1.o: cxx ../../foo/input1.cc\n" |
| "build obj/foo/bar.input2.o: cxx ../../foo/input2.cc\n" |
| "\n" |
| "build obj/foo/bar.stamp: stamp obj/foo/bar.input1.o " |
| "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj\n"; |
| std::string out_str = out.str(); |
| EXPECT_EQ(expected, out_str); |
| } |
| |
| // A shared library that depends on the source set. |
| Target shlib_target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |
| shlib_target.set_output_type(Target::SHARED_LIBRARY); |
| shlib_target.public_deps().push_back(LabelTargetPair(&target)); |
| shlib_target.SetToolchain(setup.toolchain()); |
| ASSERT_TRUE(shlib_target.OnResolved(&err)); |
| |
| { |
| std::ostringstream out; |
| NinjaBinaryTargetWriter writer(&shlib_target, out); |
| writer.Run(); |
| |
| const char expected[] = |
| "defines =\n" |
| "include_dirs =\n" |
| "cflags =\n" |
| "cflags_c =\n" |
| "cflags_cc =\n" |
| "cflags_objc =\n" |
| "cflags_objcc =\n" |
| "root_out_dir = .\n" |
| "target_out_dir = obj/foo\n" |
| "target_output_name = libshlib\n" |
| "\n" |
| "\n" |
| // Ordering of the obj files here should come out in the order |
| // specified, with the target's first, followed by the source set's, in |
| // order. |
| "build ./libshlib.so: solink obj/foo/bar.input1.o " |
| "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj " |
| "|| obj/foo/bar.stamp\n" |
| " ldflags =\n" |
| " libs =\n" |
| " output_extension = .so\n"; |
| std::string out_str = out.str(); |
| EXPECT_EQ(expected, out_str); |
| } |
| |
| // A static library that depends on the source set (should not link it). |
| Target stlib_target(setup.settings(), Label(SourceDir("//foo/"), "stlib")); |
| stlib_target.set_output_type(Target::STATIC_LIBRARY); |
| stlib_target.public_deps().push_back(LabelTargetPair(&target)); |
| stlib_target.SetToolchain(setup.toolchain()); |
| ASSERT_TRUE(stlib_target.OnResolved(&err)); |
| |
| { |
| std::ostringstream out; |
| NinjaBinaryTargetWriter writer(&stlib_target, out); |
| writer.Run(); |
| |
| const char expected[] = |
| "defines =\n" |
| "include_dirs =\n" |
| "cflags =\n" |
| "cflags_c =\n" |
| "cflags_cc =\n" |
| "cflags_objc =\n" |
| "cflags_objcc =\n" |
| "root_out_dir = .\n" |
| "target_out_dir = obj/foo\n" |
| "target_output_name = libstlib\n" |
| "\n" |
| "\n" |
| // There are no sources so there are no params to alink. (In practice |
| // this will probably fail in the archive tool.) |
| "build obj/foo/libstlib.a: alink || obj/foo/bar.stamp\n" |
| " ldflags =\n" |
| " libs =\n" |
| " output_extension = \n"; |
| std::string out_str = out.str(); |
| EXPECT_EQ(expected, out_str); |
| } |
| |
| // Make the static library 'complete', which means it should be linked. |
| stlib_target.set_complete_static_lib(true); |
| { |
| std::ostringstream out; |
| NinjaBinaryTargetWriter writer(&stlib_target, out); |
| writer.Run(); |
| |
| const char expected[] = |
| "defines =\n" |
| "include_dirs =\n" |
| "cflags =\n" |
| "cflags_c =\n" |
| "cflags_cc =\n" |
| "cflags_objc =\n" |
| "cflags_objcc =\n" |
| "root_out_dir = .\n" |
| "target_out_dir = obj/foo\n" |
| "target_output_name = libstlib\n" |
| "\n" |
| "\n" |
| // Ordering of the obj files here should come out in the order |
| // specified, with the target's first, followed by the source set's, in |
| // order. |
| "build obj/foo/libstlib.a: alink obj/foo/bar.input1.o " |
| "obj/foo/bar.input2.o ../../foo/input3.o ../../foo/input4.obj " |
| "|| obj/foo/bar.stamp\n" |
| " ldflags =\n" |
| " libs =\n" |
| " output_extension = \n"; |
| std::string out_str = out.str(); |
| EXPECT_EQ(expected, out_str); |
| } |
| } |
| |
| // This tests that output extension overrides apply, and input dependencies |
| // are applied. |
| TEST(NinjaBinaryTargetWriter, ProductExtensionAndInputDeps) { |
| TestWithScope setup; |
| Err err; |
| |
| setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| setup.settings()->set_target_os(Settings::LINUX); |
| |
| // An action for our library to depend on. |
| Target action(setup.settings(), Label(SourceDir("//foo/"), "action")); |
| action.set_output_type(Target::ACTION_FOREACH); |
| action.visibility().SetPublic(); |
| action.SetToolchain(setup.toolchain()); |
| ASSERT_TRUE(action.OnResolved(&err)); |
| |
| // A shared library w/ the product_extension set to a custom value. |
| Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |
| target.set_output_type(Target::SHARED_LIBRARY); |
| target.set_output_extension(std::string("so.6")); |
| target.sources().push_back(SourceFile("//foo/input1.cc")); |
| target.sources().push_back(SourceFile("//foo/input2.cc")); |
| target.public_deps().push_back(LabelTargetPair(&action)); |
| target.SetToolchain(setup.toolchain()); |
| ASSERT_TRUE(target.OnResolved(&err)); |
| |
| std::ostringstream out; |
| NinjaBinaryTargetWriter writer(&target, out); |
| writer.Run(); |
| |
| const char expected[] = |
| "defines =\n" |
| "include_dirs =\n" |
| "cflags =\n" |
| "cflags_c =\n" |
| "cflags_cc =\n" |
| "cflags_objc =\n" |
| "cflags_objcc =\n" |
| "root_out_dir = .\n" |
| "target_out_dir = obj/foo\n" |
| "target_output_name = libshlib\n" |
| "\n" |
| "build obj/foo/shlib.inputdeps.stamp: stamp obj/foo/action.stamp\n" |
| "build obj/foo/libshlib.input1.o: cxx ../../foo/input1.cc" |
| " || obj/foo/shlib.inputdeps.stamp\n" |
| "build obj/foo/libshlib.input2.o: cxx ../../foo/input2.cc" |
| " || obj/foo/shlib.inputdeps.stamp\n" |
| "\n" |
| "build ./libshlib.so.6: solink obj/foo/libshlib.input1.o " |
| // The order-only dependency here is stricly unnecessary since the |
| // sources list this as an order-only dep. See discussion in the code |
| // that writes this. |
| "obj/foo/libshlib.input2.o || obj/foo/action.stamp\n" |
| " ldflags =\n" |
| " libs =\n" |
| " output_extension = .so.6\n"; |
| |
| std::string out_str = out.str(); |
| EXPECT_EQ(expected, out_str); |
| } |
| |
| TEST(NinjaBinaryTargetWriter, EmptyProductExtension) { |
| TestWithScope setup; |
| Err err; |
| |
| setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| setup.settings()->set_target_os(Settings::LINUX); |
| |
| // This test is the same as ProductExtension, except that |
| // we call set_output_extension("") and ensure that we still get the default. |
| Target target(setup.settings(), Label(SourceDir("//foo/"), "shlib")); |
| target.set_output_type(Target::SHARED_LIBRARY); |
| target.set_output_extension(std::string()); |
| target.sources().push_back(SourceFile("//foo/input1.cc")); |
| target.sources().push_back(SourceFile("//foo/input2.cc")); |
| |
| target.SetToolchain(setup.toolchain()); |
| ASSERT_TRUE(target.OnResolved(&err)); |
| |
| std::ostringstream out; |
| NinjaBinaryTargetWriter writer(&target, out); |
| writer.Run(); |
| |
| const char expected[] = |
| "defines =\n" |
| "include_dirs =\n" |
| "cflags =\n" |
| "cflags_c =\n" |
| "cflags_cc =\n" |
| "cflags_objc =\n" |
| "cflags_objcc =\n" |
| "root_out_dir = .\n" |
| "target_out_dir = obj/foo\n" |
| "target_output_name = libshlib\n" |
| "\n" |
| "build obj/foo/libshlib.input1.o: cxx ../../foo/input1.cc\n" |
| "build obj/foo/libshlib.input2.o: cxx ../../foo/input2.cc\n" |
| "\n" |
| "build ./libshlib.so: solink obj/foo/libshlib.input1.o " |
| "obj/foo/libshlib.input2.o\n" |
| " ldflags =\n" |
| " libs =\n" |
| " output_extension = .so\n"; |
| |
| std::string out_str = out.str(); |
| EXPECT_EQ(expected, out_str); |
| } |
| |
| TEST(NinjaBinaryTargetWriter, SourceSetDataDeps) { |
| TestWithScope setup; |
| setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| setup.settings()->set_target_os(Settings::LINUX); |
| |
| Err err; |
| |
| // This target is a data (runtime) dependency of the intermediate target. |
| Target data(setup.settings(), Label(SourceDir("//foo/"), "data_target")); |
| data.set_output_type(Target::EXECUTABLE); |
| data.visibility().SetPublic(); |
| data.SetToolchain(setup.toolchain()); |
| ASSERT_TRUE(data.OnResolved(&err)); |
| |
| // Intermediate source set target. |
| Target inter(setup.settings(), Label(SourceDir("//foo/"), "inter")); |
| inter.set_output_type(Target::SOURCE_SET); |
| inter.visibility().SetPublic(); |
| inter.data_deps().push_back(LabelTargetPair(&data)); |
| inter.SetToolchain(setup.toolchain()); |
| inter.sources().push_back(SourceFile("//foo/inter.cc")); |
| ASSERT_TRUE(inter.OnResolved(&err)) << err.message(); |
| |
| // Write out the intermediate target. |
| std::ostringstream inter_out; |
| NinjaBinaryTargetWriter inter_writer(&inter, inter_out); |
| inter_writer.Run(); |
| |
| // The intermediate source set will be a stamp file that depends on the |
| // object files, and will have an order-only dependency on its data dep and |
| // data file. |
| const char inter_expected[] = |
| "defines =\n" |
| "include_dirs =\n" |
| "cflags =\n" |
| "cflags_c =\n" |
| "cflags_cc =\n" |
| "cflags_objc =\n" |
| "cflags_objcc =\n" |
| "root_out_dir = .\n" |
| "target_out_dir = obj/foo\n" |
| "target_output_name = inter\n" |
| "\n" |
| "build obj/foo/inter.inter.o: cxx ../../foo/inter.cc\n" |
| "\n" |
| "build obj/foo/inter.stamp: stamp obj/foo/inter.inter.o || " |
| "./data_target\n"; |
| EXPECT_EQ(inter_expected, inter_out.str()); |
| |
| // Final target. |
| Target exe(setup.settings(), Label(SourceDir("//foo/"), "exe")); |
| exe.set_output_type(Target::EXECUTABLE); |
| exe.public_deps().push_back(LabelTargetPair(&inter)); |
| exe.SetToolchain(setup.toolchain()); |
| exe.sources().push_back(SourceFile("//foo/final.cc")); |
| ASSERT_TRUE(exe.OnResolved(&err)); |
| |
| std::ostringstream final_out; |
| NinjaBinaryTargetWriter final_writer(&exe, final_out); |
| final_writer.Run(); |
| |
| // The final output depends on both object files (one from the final target, |
| // one from the source set) and has an order-only dependency on the source |
| // set's stamp file and the final target's data file. The source set stamp |
| // dependency will create an implicit order-only dependency on the data |
| // target. |
| const char final_expected[] = |
| "defines =\n" |
| "include_dirs =\n" |
| "cflags =\n" |
| "cflags_c =\n" |
| "cflags_cc =\n" |
| "cflags_objc =\n" |
| "cflags_objcc =\n" |
| "root_out_dir = .\n" |
| "target_out_dir = obj/foo\n" |
| "target_output_name = exe\n" |
| "\n" |
| "build obj/foo/exe.final.o: cxx ../../foo/final.cc\n" |
| "\n" |
| "build ./exe: link obj/foo/exe.final.o obj/foo/inter.inter.o || " |
| "obj/foo/inter.stamp\n" |
| " ldflags =\n" |
| " libs =\n" |
| " output_extension = \n"; |
| EXPECT_EQ(final_expected, final_out.str()); |
| } |
| |
| TEST(NinjaBinaryTargetWriter, SharedLibraryModuleDefinitionFile) { |
| TestWithScope setup; |
| setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| setup.settings()->set_target_os(Settings::WIN); |
| |
| Target shared_lib(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| shared_lib.set_output_type(Target::SHARED_LIBRARY); |
| shared_lib.SetToolchain(setup.toolchain()); |
| shared_lib.sources().push_back(SourceFile("//foo/sources.cc")); |
| shared_lib.sources().push_back(SourceFile("//foo/bar.def")); |
| |
| Err err; |
| ASSERT_TRUE(shared_lib.OnResolved(&err)); |
| |
| std::ostringstream out; |
| NinjaBinaryTargetWriter writer(&shared_lib, out); |
| writer.Run(); |
| |
| const char expected[] = |
| "defines =\n" |
| "include_dirs =\n" |
| "cflags =\n" |
| "cflags_c =\n" |
| "cflags_cc =\n" |
| "cflags_objc =\n" |
| "cflags_objcc =\n" |
| "root_out_dir = .\n" |
| "target_out_dir = obj/foo\n" |
| "target_output_name = libbar\n" |
| "\n" |
| "build obj/foo/libbar.sources.o: cxx ../../foo/sources.cc\n" |
| "\n" |
| "build ./libbar.so: solink obj/foo/libbar.sources.o | ../../foo/bar.def\n" |
| " ldflags = /DEF:../../foo/bar.def\n" |
| " libs =\n" |
| " output_extension = .so\n"; |
| EXPECT_EQ(expected, out.str()); |
| } |