| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "tools/gn/err.h" | 9 #include "tools/gn/err.h" |
| 10 #include "tools/gn/escape.h" | 10 #include "tools/gn/escape.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 TEST(SubstitutionWriter, ApplyPatternToSource) { | 37 TEST(SubstitutionWriter, ApplyPatternToSource) { |
| 38 TestWithScope setup; | 38 TestWithScope setup; |
| 39 | 39 |
| 40 SubstitutionPattern pattern; | 40 SubstitutionPattern pattern; |
| 41 Err err; | 41 Err err; |
| 42 ASSERT_TRUE(pattern.Parse("{{source_gen_dir}}/{{source_name_part}}.tmp", | 42 ASSERT_TRUE(pattern.Parse("{{source_gen_dir}}/{{source_name_part}}.tmp", |
| 43 nullptr, &err)); | 43 nullptr, &err)); |
| 44 | 44 |
| 45 SourceFile result = SubstitutionWriter::ApplyPatternToSource( | 45 SourceFile result = SubstitutionWriter::ApplyPatternToSource( |
| 46 setup.settings(), pattern, SourceFile("//foo/bar/myfile.txt")); | 46 nullptr, setup.settings(), pattern, SourceFile("//foo/bar/myfile.txt")); |
| 47 ASSERT_EQ("//out/Debug/gen/foo/bar/myfile.tmp", result.value()); | 47 ASSERT_EQ("//out/Debug/gen/foo/bar/myfile.tmp", result.value()); |
| 48 } | 48 } |
| 49 | 49 |
| 50 TEST(SubstitutionWriter, ApplyPatternToSourceAsOutputFile) { | 50 TEST(SubstitutionWriter, ApplyPatternToSourceAsOutputFile) { |
| 51 TestWithScope setup; | 51 TestWithScope setup; |
| 52 | 52 |
| 53 SubstitutionPattern pattern; | 53 SubstitutionPattern pattern; |
| 54 Err err; | 54 Err err; |
| 55 ASSERT_TRUE(pattern.Parse("{{source_gen_dir}}/{{source_name_part}}.tmp", | 55 ASSERT_TRUE(pattern.Parse("{{source_gen_dir}}/{{source_name_part}}.tmp", |
| 56 nullptr, &err)); | 56 nullptr, &err)); |
| 57 | 57 |
| 58 OutputFile result = SubstitutionWriter::ApplyPatternToSourceAsOutputFile( | 58 OutputFile result = SubstitutionWriter::ApplyPatternToSourceAsOutputFile( |
| 59 setup.settings(), pattern, SourceFile("//foo/bar/myfile.txt")); | 59 nullptr, setup.settings(), pattern, SourceFile("//foo/bar/myfile.txt")); |
| 60 ASSERT_EQ("gen/foo/bar/myfile.tmp", result.value()); | 60 ASSERT_EQ("gen/foo/bar/myfile.tmp", result.value()); |
| 61 } | 61 } |
| 62 | 62 |
| 63 TEST(SubstitutionWriter, WriteNinjaVariablesForSource) { | 63 TEST(SubstitutionWriter, WriteNinjaVariablesForSource) { |
| 64 TestWithScope setup; | 64 TestWithScope setup; |
| 65 | 65 |
| 66 std::vector<SubstitutionType> types; | 66 std::vector<SubstitutionType> types; |
| 67 types.push_back(SUBSTITUTION_SOURCE); | 67 types.push_back(SUBSTITUTION_SOURCE); |
| 68 types.push_back(SUBSTITUTION_SOURCE_NAME_PART); | 68 types.push_back(SUBSTITUTION_SOURCE_NAME_PART); |
| 69 types.push_back(SUBSTITUTION_SOURCE_DIR); | 69 types.push_back(SUBSTITUTION_SOURCE_DIR); |
| 70 | 70 |
| 71 EscapeOptions options; | 71 EscapeOptions options; |
| 72 options.mode = ESCAPE_NONE; | 72 options.mode = ESCAPE_NONE; |
| 73 | 73 |
| 74 std::ostringstream out; | 74 std::ostringstream out; |
| 75 SubstitutionWriter::WriteNinjaVariablesForSource( | 75 SubstitutionWriter::WriteNinjaVariablesForSource( |
| 76 setup.settings(), SourceFile("//foo/bar/baz.txt"), types, options, out); | 76 nullptr, setup.settings(), SourceFile("//foo/bar/baz.txt"), types, |
| 77 options, out); |
| 77 | 78 |
| 78 // The "source" should be skipped since that will expand to $in which is | 79 // The "source" should be skipped since that will expand to $in which is |
| 79 // implicit. | 80 // implicit. |
| 80 EXPECT_EQ( | 81 EXPECT_EQ( |
| 81 " source_name_part = baz\n" | 82 " source_name_part = baz\n" |
| 82 " source_dir = ../../foo/bar\n", | 83 " source_dir = ../../foo/bar\n", |
| 83 out.str()); | 84 out.str()); |
| 84 } | 85 } |
| 85 | 86 |
| 86 TEST(SubstitutionWriter, WriteWithNinjaVariables) { | 87 TEST(SubstitutionWriter, WriteWithNinjaVariables) { |
| 87 Err err; | 88 Err err; |
| 88 SubstitutionPattern pattern; | 89 SubstitutionPattern pattern; |
| 89 ASSERT_TRUE(pattern.Parse("-i {{source}} --out=bar\"{{source_name_part}}\".o", | 90 ASSERT_TRUE(pattern.Parse("-i {{source}} --out=bar\"{{source_name_part}}\".o", |
| 90 nullptr, &err)); | 91 nullptr, &err)); |
| 91 EXPECT_FALSE(err.has_error()); | 92 EXPECT_FALSE(err.has_error()); |
| 92 | 93 |
| 93 EscapeOptions options; | 94 EscapeOptions options; |
| 94 options.mode = ESCAPE_NONE; | 95 options.mode = ESCAPE_NONE; |
| 95 | 96 |
| 96 std::ostringstream out; | 97 std::ostringstream out; |
| 97 SubstitutionWriter::WriteWithNinjaVariables(pattern, options, out); | 98 SubstitutionWriter::WriteWithNinjaVariables(pattern, options, out); |
| 98 | 99 |
| 99 EXPECT_EQ( | 100 EXPECT_EQ( |
| 100 "-i ${in} --out=bar\"${source_name_part}\".o", | 101 "-i ${in} --out=bar\"${source_name_part}\".o", |
| 101 out.str()); | 102 out.str()); |
| 102 } | 103 } |
| 103 | 104 |
| 104 TEST(SubstitutionWriter, SourceSubstitutions) { | 105 TEST(SubstitutionWriter, SourceSubstitutions) { |
| 105 TestWithScope setup; | 106 TestWithScope setup; |
| 107 Err err; |
| 108 |
| 109 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz")); |
| 110 target.set_output_type(Target::STATIC_LIBRARY); |
| 111 target.SetToolchain(setup.toolchain()); |
| 112 ASSERT_TRUE(target.OnResolved(&err)); |
| 106 | 113 |
| 107 // Call to get substitutions relative to the build dir. | 114 // Call to get substitutions relative to the build dir. |
| 108 #define GetRelSubst(str, what) \ | 115 #define GetRelSubst(str, what) \ |
| 109 SubstitutionWriter::GetSourceSubstitution( \ | 116 SubstitutionWriter::GetSourceSubstitution( \ |
| 117 &target, \ |
| 110 setup.settings(), \ | 118 setup.settings(), \ |
| 111 SourceFile(str), \ | 119 SourceFile(str), \ |
| 112 what, \ | 120 what, \ |
| 113 SubstitutionWriter::OUTPUT_RELATIVE, \ | 121 SubstitutionWriter::OUTPUT_RELATIVE, \ |
| 114 setup.settings()->build_settings()->build_dir()) | 122 setup.settings()->build_settings()->build_dir()) |
| 115 | 123 |
| 116 // Call to get absolute directory substitutions. | 124 // Call to get absolute directory substitutions. |
| 117 #define GetAbsSubst(str, what) \ | 125 #define GetAbsSubst(str, what) \ |
| 118 SubstitutionWriter::GetSourceSubstitution( \ | 126 SubstitutionWriter::GetSourceSubstitution( \ |
| 127 &target, \ |
| 119 setup.settings(), \ | 128 setup.settings(), \ |
| 120 SourceFile(str), \ | 129 SourceFile(str), \ |
| 121 what, \ | 130 what, \ |
| 122 SubstitutionWriter::OUTPUT_ABSOLUTE, \ | 131 SubstitutionWriter::OUTPUT_ABSOLUTE, \ |
| 123 SourceDir()) | 132 SourceDir()) |
| 124 | 133 |
| 125 // Try all possible templates with a normal looking string. | 134 // Try all possible templates with a normal looking string. |
| 126 EXPECT_EQ("../../foo/bar/baz.txt", | 135 EXPECT_EQ("../../foo/bar/baz.txt", |
| 127 GetRelSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE)); | 136 GetRelSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE)); |
| 128 EXPECT_EQ("//foo/bar/baz.txt", | 137 EXPECT_EQ("//foo/bar/baz.txt", |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 #if defined(OS_WIN) | 177 #if defined(OS_WIN) |
| 169 EXPECT_EQ("gen/ABS_PATH/C", | 178 EXPECT_EQ("gen/ABS_PATH/C", |
| 170 GetRelSubst("/C:/baz.txt", SUBSTITUTION_SOURCE_GEN_DIR)); | 179 GetRelSubst("/C:/baz.txt", SUBSTITUTION_SOURCE_GEN_DIR)); |
| 171 EXPECT_EQ("obj/ABS_PATH/C", | 180 EXPECT_EQ("obj/ABS_PATH/C", |
| 172 GetRelSubst("/C:/baz.txt", SUBSTITUTION_SOURCE_OUT_DIR)); | 181 GetRelSubst("/C:/baz.txt", SUBSTITUTION_SOURCE_OUT_DIR)); |
| 173 #endif | 182 #endif |
| 174 | 183 |
| 175 EXPECT_EQ(".", | 184 EXPECT_EQ(".", |
| 176 GetRelSubst("//baz.txt", SUBSTITUTION_SOURCE_ROOT_RELATIVE_DIR)); | 185 GetRelSubst("//baz.txt", SUBSTITUTION_SOURCE_ROOT_RELATIVE_DIR)); |
| 177 | 186 |
| 187 EXPECT_EQ("baz.txt", |
| 188 GetRelSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_TARGET_RELATIVE)); |
| 189 EXPECT_EQ("baz.txt", |
| 190 GetAbsSubst("//foo/bar/baz.txt", SUBSTITUTION_SOURCE_TARGET_RELATIVE)); |
| 191 |
| 178 #undef GetAbsSubst | 192 #undef GetAbsSubst |
| 179 #undef GetRelSubst | 193 #undef GetRelSubst |
| 180 } | 194 } |
| 181 | 195 |
| 182 TEST(SubstitutionWriter, TargetSubstitutions) { | 196 TEST(SubstitutionWriter, TargetSubstitutions) { |
| 183 TestWithScope setup; | 197 TestWithScope setup; |
| 184 Err err; | 198 Err err; |
| 185 | 199 |
| 186 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz")); | 200 Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz")); |
| 187 target.set_output_type(Target::STATIC_LIBRARY); | 201 target.set_output_type(Target::STATIC_LIBRARY); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 EXPECT_EQ("./baz.exe", | 328 EXPECT_EQ("./baz.exe", |
| 315 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( | 329 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( |
| 316 &target, &tool, output_name).value()); | 330 &target, &tool, output_name).value()); |
| 317 | 331 |
| 318 // Override the output name to a new subdirectory. | 332 // Override the output name to a new subdirectory. |
| 319 target.set_output_dir(SourceDir("//out/Debug/foo/bar")); | 333 target.set_output_dir(SourceDir("//out/Debug/foo/bar")); |
| 320 EXPECT_EQ("foo/bar/baz.exe", | 334 EXPECT_EQ("foo/bar/baz.exe", |
| 321 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( | 335 SubstitutionWriter::ApplyPatternToLinkerAsOutputFile( |
| 322 &target, &tool, output_name).value()); | 336 &target, &tool, output_name).value()); |
| 323 } | 337 } |
| OLD | NEW |