| 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_action_target_writer.h" | 10 #include "tools/gn/ninja_action_target_writer.h" |
| 11 #include "tools/gn/test_with_scope.h" | 11 #include "tools/gn/test_with_scope.h" |
| 12 | 12 |
| 13 TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) { | 13 TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) { |
| 14 TestWithScope setup; | 14 TestWithScope setup; |
| 15 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 15 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 16 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 16 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 17 | 17 |
| 18 target.action_values().outputs().push_back( | 18 target.action_values().outputs().push_back( |
| 19 SourceFile("//out/Debug/gen/a b{{source_name_part}}.h")); | 19 "//out/Debug/gen/a b{{source_name_part}}.h"); |
| 20 target.action_values().outputs().push_back( | 20 target.action_values().outputs().push_back( |
| 21 SourceFile("//out/Debug/gen/{{source_name_part}}.cc")); | 21 "//out/Debug/gen/{{source_name_part}}.cc"); |
| 22 | 22 |
| 23 std::ostringstream out; | 23 std::ostringstream out; |
| 24 NinjaActionTargetWriter writer(&target, setup.toolchain(), out); | 24 NinjaActionTargetWriter writer(&target, setup.toolchain(), out); |
| 25 | 25 |
| 26 FileTemplate output_template = writer.GetOutputTemplate(); | 26 FileTemplate output_template = FileTemplate::GetForTargetOutputs(&target); |
| 27 | 27 |
| 28 SourceFile source("//foo/bar.in"); | 28 SourceFile source("//foo/bar.in"); |
| 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 EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out.str()); | 32 EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out.str()); |
| 33 } | 33 } |
| 34 | 34 |
| 35 TEST(NinjaActionTargetWriter, WriteArgsSubstitutions) { | 35 TEST(NinjaActionTargetWriter, WriteArgsSubstitutions) { |
| 36 TestWithScope setup; | 36 TestWithScope setup; |
| 37 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 37 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 38 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 38 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 39 | 39 |
| 40 std::ostringstream out; | 40 std::ostringstream out; |
| 41 NinjaActionTargetWriter writer(&target, setup.toolchain(), out); | 41 NinjaActionTargetWriter writer(&target, setup.toolchain(), out); |
| 42 | 42 |
| 43 std::vector<std::string> args; | 43 std::vector<std::string> args; |
| 44 args.push_back("-i"); | 44 args.push_back("-i"); |
| 45 args.push_back("{{source}}"); | 45 args.push_back("{{source}}"); |
| 46 args.push_back("--out=foo bar{{source_name_part}}.o"); | 46 args.push_back("--out=foo bar{{source_name_part}}.o"); |
| 47 FileTemplate args_template(setup.settings(), args); | 47 FileTemplate args_template(setup.settings(), args, |
| 48 FileTemplate::OUTPUT_RELATIVE, |
| 49 setup.settings()->build_settings()->build_dir()); |
| 48 | 50 |
| 49 writer.WriteArgsSubstitutions(SourceFile("//foo/b ar.in"), args_template); | 51 writer.WriteArgsSubstitutions(SourceFile("//foo/b ar.in"), args_template); |
| 50 #if defined(OS_WIN) | 52 #if defined(OS_WIN) |
| 51 EXPECT_EQ(" source = \"../../foo/b$ ar.in\"\n" | 53 EXPECT_EQ(" source = \"../../foo/b$ ar.in\"\n" |
| 52 " source_name_part = \"b$ ar\"\n", | 54 " source_name_part = \"b$ ar\"\n", |
| 53 out.str()); | 55 out.str()); |
| 54 #else | 56 #else |
| 55 EXPECT_EQ(" source = ../../foo/b\\$ ar.in\n" | 57 EXPECT_EQ(" source = ../../foo/b\\$ ar.in\n" |
| 56 " source_name_part = b\\$ ar\n", | 58 " source_name_part = b\\$ ar\n", |
| 57 out.str()); | 59 out.str()); |
| 58 #endif | 60 #endif |
| 59 } | 61 } |
| 60 | 62 |
| 61 // Makes sure that we write sources as input dependencies for actions with | 63 // Makes sure that we write sources as input dependencies for actions with |
| 62 // both sources and inputs (ACTION_FOREACH treats the sources differently). | 64 // both sources and inputs (ACTION_FOREACH treats the sources differently). |
| 63 TEST(NinjaActionTargetWriter, ActionWithSources) { | 65 TEST(NinjaActionTargetWriter, ActionWithSources) { |
| 64 TestWithScope setup; | 66 TestWithScope setup; |
| 65 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 67 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 66 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 68 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 67 target.set_output_type(Target::ACTION); | 69 target.set_output_type(Target::ACTION); |
| 68 | 70 |
| 69 target.action_values().set_script(SourceFile("//foo/script.py")); | 71 target.action_values().set_script(SourceFile("//foo/script.py")); |
| 70 | 72 |
| 71 target.sources().push_back(SourceFile("//foo/source.txt")); | 73 target.sources().push_back(SourceFile("//foo/source.txt")); |
| 72 target.inputs().push_back(SourceFile("//foo/included.txt")); | 74 target.inputs().push_back(SourceFile("//foo/included.txt")); |
| 73 | 75 |
| 74 target.action_values().outputs().push_back( | 76 target.action_values().outputs().push_back("//out/Debug/foo.out"); |
| 75 SourceFile("//out/Debug/foo.out")); | |
| 76 | 77 |
| 77 // Posix. | 78 // Posix. |
| 78 { | 79 { |
| 79 setup.settings()->set_target_os(Settings::LINUX); | 80 setup.settings()->set_target_os(Settings::LINUX); |
| 80 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( | 81 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
| 81 "/usr/bin/python"))); | 82 "/usr/bin/python"))); |
| 82 | 83 |
| 83 std::ostringstream out; | 84 std::ostringstream out; |
| 84 NinjaActionTargetWriter writer(&target, setup.toolchain(), out); | 85 NinjaActionTargetWriter writer(&target, setup.toolchain(), out); |
| 85 writer.Run(); | 86 writer.Run(); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 target.sources().push_back(SourceFile("//foo/input2.txt")); | 150 target.sources().push_back(SourceFile("//foo/input2.txt")); |
| 150 | 151 |
| 151 target.action_values().set_script(SourceFile("//foo/script.py")); | 152 target.action_values().set_script(SourceFile("//foo/script.py")); |
| 152 | 153 |
| 153 target.action_values().args().push_back("-i"); | 154 target.action_values().args().push_back("-i"); |
| 154 target.action_values().args().push_back("{{source}}"); | 155 target.action_values().args().push_back("{{source}}"); |
| 155 target.action_values().args().push_back( | 156 target.action_values().args().push_back( |
| 156 "--out=foo bar{{source_name_part}}.o"); | 157 "--out=foo bar{{source_name_part}}.o"); |
| 157 | 158 |
| 158 target.action_values().outputs().push_back( | 159 target.action_values().outputs().push_back( |
| 159 SourceFile("//out/Debug/{{source_name_part}}.out")); | 160 "//out/Debug/{{source_name_part}}.out"); |
| 160 | 161 |
| 161 target.inputs().push_back(SourceFile("//foo/included.txt")); | 162 target.inputs().push_back(SourceFile("//foo/included.txt")); |
| 162 | 163 |
| 163 // Posix. | 164 // Posix. |
| 164 { | 165 { |
| 165 setup.settings()->set_target_os(Settings::LINUX); | 166 setup.settings()->set_target_os(Settings::LINUX); |
| 166 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( | 167 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
| 167 "/usr/bin/python"))); | 168 "/usr/bin/python"))); |
| 168 | 169 |
| 169 std::ostringstream out; | 170 std::ostringstream out; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 target.action_values().set_script(SourceFile("//foo/script.py")); | 259 target.action_values().set_script(SourceFile("//foo/script.py")); |
| 259 target.action_values().set_depfile( | 260 target.action_values().set_depfile( |
| 260 SourceFile("//out/Debug/gen/{{source_name_part}}.d")); | 261 SourceFile("//out/Debug/gen/{{source_name_part}}.d")); |
| 261 | 262 |
| 262 target.action_values().args().push_back("-i"); | 263 target.action_values().args().push_back("-i"); |
| 263 target.action_values().args().push_back("{{source}}"); | 264 target.action_values().args().push_back("{{source}}"); |
| 264 target.action_values().args().push_back( | 265 target.action_values().args().push_back( |
| 265 "--out=foo bar{{source_name_part}}.o"); | 266 "--out=foo bar{{source_name_part}}.o"); |
| 266 | 267 |
| 267 target.action_values().outputs().push_back( | 268 target.action_values().outputs().push_back( |
| 268 SourceFile("//out/Debug/{{source_name_part}}.out")); | 269 "//out/Debug/{{source_name_part}}.out"); |
| 269 | 270 |
| 270 target.inputs().push_back(SourceFile("//foo/included.txt")); | 271 target.inputs().push_back(SourceFile("//foo/included.txt")); |
| 271 | 272 |
| 272 // Posix. | 273 // Posix. |
| 273 { | 274 { |
| 274 setup.settings()->set_target_os(Settings::LINUX); | 275 setup.settings()->set_target_os(Settings::LINUX); |
| 275 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( | 276 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
| 276 "/usr/bin/python"))); | 277 "/usr/bin/python"))); |
| 277 | 278 |
| 278 std::ostringstream out; | 279 std::ostringstream out; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 " | obj/foo/bar.inputdeps.stamp\n" | 344 " | obj/foo/bar.inputdeps.stamp\n" |
| 344 " unique_name = 1\n" | 345 " unique_name = 1\n" |
| 345 " source = ../../foo/input2.txt\n" | 346 " source = ../../foo/input2.txt\n" |
| 346 " source_name_part = input2\n" | 347 " source_name_part = input2\n" |
| 347 " depfile = gen/input2.d\n" | 348 " depfile = gen/input2.d\n" |
| 348 "\n" | 349 "\n" |
| 349 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; | 350 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
| 350 EXPECT_EQ(expected_win, out.str()); | 351 EXPECT_EQ(expected_win, out.str()); |
| 351 } | 352 } |
| 352 } | 353 } |
| OLD | NEW |