| 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" |
| 11 #include "tools/gn/test_with_scope.h" | 11 #include "tools/gn/test_with_scope.h" |
| 12 | 12 |
| 13 TEST(NinjaScriptTargetWriter, WriteOutputFilesForBuildLine) { | 13 TEST(NinjaScriptTargetWriter, 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.script_values().outputs().push_back( | 18 target.script_values().outputs().push_back( |
| 19 SourceFile("//out/Debug/gen/a b{{source_name_part}}.h")); | 19 SourceFile("//out/Debug/gen/a b{{source_name_part}}.h")); |
| 20 target.script_values().outputs().push_back( | 20 target.script_values().outputs().push_back( |
| 21 SourceFile("//out/Debug/gen/{{source_name_part}}.cc")); | 21 SourceFile("//out/Debug/gen/{{source_name_part}}.cc")); |
| 22 | 22 |
| 23 std::ostringstream out; | 23 std::ostringstream out; |
| 24 NinjaScriptTargetWriter writer(&target, out); | 24 NinjaScriptTargetWriter writer(&target, setup.toolchain(), out); |
| 25 | 25 |
| 26 FileTemplate output_template = writer.GetOutputTemplate(); | 26 FileTemplate output_template = writer.GetOutputTemplate(); |
| 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 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, WriteArgsSubstitutions) { | 39 TEST(NinjaScriptTargetWriter, WriteArgsSubstitutions) { |
| 40 TestWithScope setup; | 40 TestWithScope setup; |
| 41 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); | 41 setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/")); |
| 42 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); | 42 Target target(setup.settings(), Label(SourceDir("//foo/"), "bar")); |
| 43 | 43 |
| 44 std::ostringstream out; | 44 std::ostringstream out; |
| 45 NinjaScriptTargetWriter writer(&target, out); | 45 NinjaScriptTargetWriter writer(&target, setup.toolchain(), out); |
| 46 | 46 |
| 47 std::vector<std::string> args; | 47 std::vector<std::string> args; |
| 48 args.push_back("-i"); | 48 args.push_back("-i"); |
| 49 args.push_back("{{source}}"); | 49 args.push_back("{{source}}"); |
| 50 args.push_back("--out=foo bar{{source_name_part}}.o"); | 50 args.push_back("--out=foo bar{{source_name_part}}.o"); |
| 51 FileTemplate args_template(args); | 51 FileTemplate args_template(args); |
| 52 | 52 |
| 53 writer.WriteArgsSubstitutions(SourceFile("//foo/b ar.in"), args_template); | 53 writer.WriteArgsSubstitutions(SourceFile("//foo/b ar.in"), args_template); |
| 54 | 54 |
| 55 std::string out_str = out.str(); | 55 std::string out_str = out.str(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 81 | 81 |
| 82 target.source_prereqs().push_back(SourceFile("//foo/included.txt")); | 82 target.source_prereqs().push_back(SourceFile("//foo/included.txt")); |
| 83 | 83 |
| 84 // Posix. | 84 // Posix. |
| 85 { | 85 { |
| 86 setup.settings()->set_target_os(Settings::LINUX); | 86 setup.settings()->set_target_os(Settings::LINUX); |
| 87 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( | 87 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
| 88 "/usr/bin/python"))); | 88 "/usr/bin/python"))); |
| 89 | 89 |
| 90 std::ostringstream out; | 90 std::ostringstream out; |
| 91 NinjaScriptTargetWriter writer(&target, out); | 91 NinjaScriptTargetWriter writer(&target, setup.toolchain(), out); |
| 92 writer.Run(); | 92 writer.Run(); |
| 93 | 93 |
| 94 const char expected_linux[] = | 94 const char expected_linux[] = |
| 95 "rule __foo_bar___rule\n" | 95 "rule __foo_bar___rule\n" |
| 96 " command = /usr/bin/python ../../foo/script.py -i ${source} " | 96 " command = /usr/bin/python ../../foo/script.py -i ${source} " |
| 97 "\"--out=foo$ bar${source_name_part}.o\"\n" | 97 "\"--out=foo$ bar${source_name_part}.o\"\n" |
| 98 " description = CUSTOM //foo:bar()\n" | 98 " description = CUSTOM //foo:bar()\n" |
| 99 " restat = 1\n" | 99 " restat = 1\n" |
| 100 "\n" | 100 "\n" |
| 101 "build input1.out: __foo_bar___rule ../../foo/input1.txt | " | 101 "build input1.out: __foo_bar___rule ../../foo/input1.txt | " |
| 102 "../../foo/included.txt\n" | 102 "../../foo/included.txt\n" |
| 103 " source = ../../foo/input1.txt\n" | 103 " source = ../../foo/input1.txt\n" |
| 104 " source_name_part = input1\n" | 104 " source_name_part = input1\n" |
| 105 "build input2.out: __foo_bar___rule ../../foo/input2.txt | " | 105 "build input2.out: __foo_bar___rule ../../foo/input2.txt | " |
| 106 "../../foo/included.txt\n" | 106 "../../foo/included.txt\n" |
| 107 " source = ../../foo/input2.txt\n" | 107 " source = ../../foo/input2.txt\n" |
| 108 " source_name_part = input2\n" | 108 " source_name_part = input2\n" |
| 109 "\n" | 109 "\n" |
| 110 "build obj/foo/bar.stamp: tc_stamp input1.out input2.out\n"; | 110 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
| 111 | 111 |
| 112 std::string out_str = out.str(); | 112 std::string out_str = out.str(); |
| 113 #if defined(OS_WIN) | 113 #if defined(OS_WIN) |
| 114 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | 114 std::replace(out_str.begin(), out_str.end(), '\\', '/'); |
| 115 #endif | 115 #endif |
| 116 EXPECT_EQ(expected_linux, out_str); | 116 EXPECT_EQ(expected_linux, out_str); |
| 117 } | 117 } |
| 118 | 118 |
| 119 // Windows. | 119 // Windows. |
| 120 { | 120 { |
| 121 // Note: we use forward slashes here so that the output will be the same on | 121 // Note: we use forward slashes here so that the output will be the same on |
| 122 // Linux and Windows. | 122 // Linux and Windows. |
| 123 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( | 123 setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL( |
| 124 "C:/python/python.exe"))); | 124 "C:/python/python.exe"))); |
| 125 setup.settings()->set_target_os(Settings::WIN); | 125 setup.settings()->set_target_os(Settings::WIN); |
| 126 | 126 |
| 127 std::ostringstream out; | 127 std::ostringstream out; |
| 128 NinjaScriptTargetWriter writer(&target, out); | 128 NinjaScriptTargetWriter writer(&target, setup.toolchain(), out); |
| 129 writer.Run(); | 129 writer.Run(); |
| 130 | 130 |
| 131 // TODO(brettw) I think we'll need to worry about backslashes here | 131 // TODO(brettw) I think we'll need to worry about backslashes here |
| 132 // depending if we're on actual Windows or Linux pretending to be Windows. | 132 // depending if we're on actual Windows or Linux pretending to be Windows. |
| 133 const char expected_win[] = | 133 const char expected_win[] = |
| 134 "rule __foo_bar___rule\n" | 134 "rule __foo_bar___rule\n" |
| 135 " command = C:/python/python.exe gyp-win-tool action-wrapper " | 135 " command = C:/python/python.exe gyp-win-tool action-wrapper " |
| 136 "environment.x86 __foo_bar___rule.$unique_name.rsp\n" | 136 "environment.x86 __foo_bar___rule.$unique_name.rsp\n" |
| 137 " description = CUSTOM //foo:bar()\n" | 137 " description = CUSTOM //foo:bar()\n" |
| 138 " restat = 1\n" | 138 " restat = 1\n" |
| 139 " rspfile = __foo_bar___rule.$unique_name.rsp\n" | 139 " rspfile = __foo_bar___rule.$unique_name.rsp\n" |
| 140 " rspfile_content = C:/python/python.exe ../../foo/script.py -i " | 140 " rspfile_content = C:/python/python.exe ../../foo/script.py -i " |
| 141 "${source} \"--out=foo$ bar${source_name_part}.o\"\n" | 141 "${source} \"--out=foo$ bar${source_name_part}.o\"\n" |
| 142 "\n" | 142 "\n" |
| 143 "build input1.out: __foo_bar___rule ../../foo/input1.txt | " | 143 "build input1.out: __foo_bar___rule ../../foo/input1.txt | " |
| 144 "../../foo/included.txt\n" | 144 "../../foo/included.txt\n" |
| 145 " unique_name = 0\n" | 145 " unique_name = 0\n" |
| 146 " source = ../../foo/input1.txt\n" | 146 " source = ../../foo/input1.txt\n" |
| 147 " source_name_part = input1\n" | 147 " source_name_part = input1\n" |
| 148 "build input2.out: __foo_bar___rule ../../foo/input2.txt | " | 148 "build input2.out: __foo_bar___rule ../../foo/input2.txt | " |
| 149 "../../foo/included.txt\n" | 149 "../../foo/included.txt\n" |
| 150 " unique_name = 1\n" | 150 " unique_name = 1\n" |
| 151 " source = ../../foo/input2.txt\n" | 151 " source = ../../foo/input2.txt\n" |
| 152 " source_name_part = input2\n" | 152 " source_name_part = input2\n" |
| 153 "\n" | 153 "\n" |
| 154 "build obj/foo/bar.stamp: tc_stamp input1.out input2.out\n"; | 154 "build obj/foo/bar.stamp: stamp input1.out input2.out\n"; |
| 155 std::string out_str = out.str(); | 155 std::string out_str = out.str(); |
| 156 #if defined(OS_WIN) | 156 #if defined(OS_WIN) |
| 157 std::replace(out_str.begin(), out_str.end(), '\\', '/'); | 157 std::replace(out_str.begin(), out_str.end(), '\\', '/'); |
| 158 #endif | 158 #endif |
| 159 EXPECT_EQ(expected_win, out_str); | 159 EXPECT_EQ(expected_win, out_str); |
| 160 } | 160 } |
| 161 } | 161 } |
| OLD | NEW |