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
3 // found in the LICENSE file.
4 
5 #include <sstream>
6 
7 #include "tools/gn/c_substitution_type.h"
8 #include "tools/gn/err.h"
9 #include "tools/gn/escape.h"
10 #include "tools/gn/substitution_list.h"
11 #include "tools/gn/substitution_pattern.h"
12 #include "tools/gn/substitution_writer.h"
13 #include "tools/gn/target.h"
14 #include "tools/gn/test_with_scope.h"
15 #include "util/build_config.h"
16 #include "util/test/test.h"
17 
TEST(SubstitutionWriter,GetListAs)18 TEST(SubstitutionWriter, GetListAs) {
19   TestWithScope setup;
20 
21   SubstitutionList list =
22       SubstitutionList::MakeForTest("//foo/bar/a.cc", "//foo/bar/b.cc");
23 
24   std::vector<SourceFile> sources;
25   SubstitutionWriter::GetListAsSourceFiles(list, &sources);
26   ASSERT_EQ(2u, sources.size());
27   EXPECT_EQ("//foo/bar/a.cc", sources[0].value());
28   EXPECT_EQ("//foo/bar/b.cc", sources[1].value());
29 
30   std::vector<OutputFile> outputs;
31   SubstitutionWriter::GetListAsOutputFiles(setup.settings(), list, &outputs);
32   ASSERT_EQ(2u, outputs.size());
33   EXPECT_EQ("../../foo/bar/a.cc", outputs[0].value());
34   EXPECT_EQ("../../foo/bar/b.cc", outputs[1].value());
35 }
36 
TEST(SubstitutionWriter,ApplyPatternToSource)37 TEST(SubstitutionWriter, ApplyPatternToSource) {
38   TestWithScope setup;
39 
40   SubstitutionPattern pattern;
41   Err err;
42   ASSERT_TRUE(pattern.Parse("{{source_gen_dir}}/{{source_name_part}}.tmp",
43                             nullptr, &err));
44 
45   SourceFile result = SubstitutionWriter::ApplyPatternToSource(
46       nullptr, setup.settings(), pattern, SourceFile("//foo/bar/myfile.txt"));
47   ASSERT_EQ("//out/Debug/gen/foo/bar/myfile.tmp", result.value());
48 }
49 
TEST(SubstitutionWriter,ApplyPatternToSourceAsOutputFile)50 TEST(SubstitutionWriter, ApplyPatternToSourceAsOutputFile) {
51   TestWithScope setup;
52 
53   SubstitutionPattern pattern;
54   Err err;
55   ASSERT_TRUE(pattern.Parse("{{source_gen_dir}}/{{source_name_part}}.tmp",
56                             nullptr, &err));
57 
58   OutputFile result = SubstitutionWriter::ApplyPatternToSourceAsOutputFile(
59       nullptr, setup.settings(), pattern, SourceFile("//foo/bar/myfile.txt"));
60   ASSERT_EQ("gen/foo/bar/myfile.tmp", result.value());
61 }
62 
TEST(SubstitutionWriter,WriteNinjaVariablesForSource)63 TEST(SubstitutionWriter, WriteNinjaVariablesForSource) {
64   TestWithScope setup;
65 
66   std::vector<const Substitution*> types;
67   types.push_back(&SubstitutionSource);
68   types.push_back(&SubstitutionSourceNamePart);
69   types.push_back(&SubstitutionSourceDir);
70 
71   EscapeOptions options;
72   options.mode = ESCAPE_NONE;
73 
74   std::ostringstream out;
75   SubstitutionWriter::WriteNinjaVariablesForSource(
76       nullptr, setup.settings(), SourceFile("//foo/bar/baz.txt"), types,
77       options, out);
78 
79   // The "source" should be skipped since that will expand to $in which is
80   // implicit.
81   EXPECT_EQ(
82       "  source_name_part = baz\n"
83       "  source_dir = ../../foo/bar\n",
84       out.str());
85 }
86 
TEST(SubstitutionWriter,WriteWithNinjaVariables)87 TEST(SubstitutionWriter, WriteWithNinjaVariables) {
88   Err err;
89   SubstitutionPattern pattern;
90   ASSERT_TRUE(pattern.Parse("-i {{source}} --out=bar\"{{source_name_part}}\".o",
91                             nullptr, &err));
92   EXPECT_FALSE(err.has_error());
93 
94   EscapeOptions options;
95   options.mode = ESCAPE_NONE;
96 
97   std::ostringstream out;
98   SubstitutionWriter::WriteWithNinjaVariables(pattern, options, out);
99 
100   EXPECT_EQ("-i ${in} --out=bar\"${source_name_part}\".o", out.str());
101 }
102 
TEST(SubstitutionWriter,SourceSubstitutions)103 TEST(SubstitutionWriter, SourceSubstitutions) {
104   TestWithScope setup;
105   Err err;
106 
107   Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz"));
108   target.set_output_type(Target::STATIC_LIBRARY);
109   target.SetToolchain(setup.toolchain());
110   ASSERT_TRUE(target.OnResolved(&err));
111 
112 // Call to get substitutions relative to the build dir.
113 #define GetRelSubst(str, what)                          \
114   SubstitutionWriter::GetSourceSubstitution(            \
115       &target, setup.settings(), SourceFile(str), what, \
116       SubstitutionWriter::OUTPUT_RELATIVE,              \
117       setup.settings()->build_settings()->build_dir())
118 
119 // Call to get absolute directory substitutions.
120 #define GetAbsSubst(str, what)                          \
121   SubstitutionWriter::GetSourceSubstitution(            \
122       &target, setup.settings(), SourceFile(str), what, \
123       SubstitutionWriter::OUTPUT_ABSOLUTE, SourceDir())
124 
125   // Try all possible templates with a normal looking string.
126   EXPECT_EQ("../../foo/bar/baz.txt",
127             GetRelSubst("//foo/bar/baz.txt", &SubstitutionSource));
128   EXPECT_EQ("//foo/bar/baz.txt",
129             GetAbsSubst("//foo/bar/baz.txt", &SubstitutionSource));
130 
131   EXPECT_EQ("baz",
132             GetRelSubst("//foo/bar/baz.txt", &SubstitutionSourceNamePart));
133   EXPECT_EQ("baz",
134             GetAbsSubst("//foo/bar/baz.txt", &SubstitutionSourceNamePart));
135 
136   EXPECT_EQ("baz.txt",
137             GetRelSubst("//foo/bar/baz.txt", &SubstitutionSourceFilePart));
138   EXPECT_EQ("baz.txt",
139             GetAbsSubst("//foo/bar/baz.txt", &SubstitutionSourceFilePart));
140 
141   EXPECT_EQ("../../foo/bar",
142             GetRelSubst("//foo/bar/baz.txt", &SubstitutionSourceDir));
143   EXPECT_EQ("//foo/bar",
144             GetAbsSubst("//foo/bar/baz.txt", &SubstitutionSourceDir));
145 
146   EXPECT_EQ("foo/bar", GetRelSubst("//foo/bar/baz.txt",
147                                    &SubstitutionSourceRootRelativeDir));
148   EXPECT_EQ("foo/bar", GetAbsSubst("//foo/bar/baz.txt",
149                                    &SubstitutionSourceRootRelativeDir));
150 
151   EXPECT_EQ("gen/foo/bar",
152             GetRelSubst("//foo/bar/baz.txt", &SubstitutionSourceGenDir));
153   EXPECT_EQ("//out/Debug/gen/foo/bar",
154             GetAbsSubst("//foo/bar/baz.txt", &SubstitutionSourceGenDir));
155 
156   EXPECT_EQ("obj/foo/bar",
157             GetRelSubst("//foo/bar/baz.txt", &SubstitutionSourceOutDir));
158   EXPECT_EQ("//out/Debug/obj/foo/bar",
159             GetAbsSubst("//foo/bar/baz.txt", &SubstitutionSourceOutDir));
160 
161   // Operations on an absolute path.
162   EXPECT_EQ("/baz.txt", GetRelSubst("/baz.txt", &SubstitutionSource));
163   EXPECT_EQ("/.", GetRelSubst("/baz.txt", &SubstitutionSourceDir));
164   EXPECT_EQ("gen/ABS_PATH", GetRelSubst("/baz.txt", &SubstitutionSourceGenDir));
165   EXPECT_EQ("obj/ABS_PATH", GetRelSubst("/baz.txt", &SubstitutionSourceOutDir));
166 #if defined(OS_WIN)
167   EXPECT_EQ("gen/ABS_PATH/C",
168             GetRelSubst("/C:/baz.txt", &SubstitutionSourceGenDir));
169   EXPECT_EQ("obj/ABS_PATH/C",
170             GetRelSubst("/C:/baz.txt", &SubstitutionSourceOutDir));
171 #endif
172 
173   EXPECT_EQ(".", GetRelSubst("//baz.txt", &SubstitutionSourceRootRelativeDir));
174 
175   EXPECT_EQ("baz.txt", GetRelSubst("//foo/bar/baz.txt",
176                                    &SubstitutionSourceTargetRelative));
177   EXPECT_EQ("baz.txt", GetAbsSubst("//foo/bar/baz.txt",
178                                    &SubstitutionSourceTargetRelative));
179 
180 #undef GetAbsSubst
181 #undef GetRelSubst
182 }
183 
TEST(SubstitutionWriter,TargetSubstitutions)184 TEST(SubstitutionWriter, TargetSubstitutions) {
185   TestWithScope setup;
186   Err err;
187 
188   Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz"));
189   target.set_output_type(Target::STATIC_LIBRARY);
190   target.SetToolchain(setup.toolchain());
191   ASSERT_TRUE(target.OnResolved(&err));
192 
193   std::string result;
194   EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
195       &target, &SubstitutionLabel, &result));
196   EXPECT_EQ("//foo/bar:baz", result);
197 
198   EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
199       &target, &SubstitutionLabelName, &result));
200   EXPECT_EQ("baz", result);
201 
202   EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
203       &target, &SubstitutionRootGenDir, &result));
204   EXPECT_EQ("gen", result);
205 
206   EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
207       &target, &SubstitutionRootOutDir, &result));
208   EXPECT_EQ(".", result);
209 
210   EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
211       &target, &SubstitutionTargetGenDir, &result));
212   EXPECT_EQ("gen/foo/bar", result);
213 
214   EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
215       &target, &SubstitutionTargetOutDir, &result));
216   EXPECT_EQ("obj/foo/bar", result);
217 
218   EXPECT_TRUE(SubstitutionWriter::GetTargetSubstitution(
219       &target, &SubstitutionTargetOutputName, &result));
220   EXPECT_EQ("libbaz", result);
221 }
222 
TEST(SubstitutionWriter,CompilerSubstitutions)223 TEST(SubstitutionWriter, CompilerSubstitutions) {
224   TestWithScope setup;
225   Err err;
226 
227   Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz"));
228   target.set_output_type(Target::STATIC_LIBRARY);
229   target.SetToolchain(setup.toolchain());
230   ASSERT_TRUE(target.OnResolved(&err));
231 
232   // The compiler substitution is just source + target combined. So test one
233   // of each of those classes of things to make sure this is hooked up.
234   EXPECT_EQ("file", SubstitutionWriter::GetCompilerSubstitution(
235                         &target, SourceFile("//foo/bar/file.txt"),
236                         &SubstitutionSourceNamePart));
237   EXPECT_EQ("gen/foo/bar", SubstitutionWriter::GetCompilerSubstitution(
238                                &target, SourceFile("//foo/bar/file.txt"),
239                                &SubstitutionTargetGenDir));
240 }
241 
TEST(SubstitutionWriter,LinkerSubstitutions)242 TEST(SubstitutionWriter, LinkerSubstitutions) {
243   TestWithScope setup;
244   Err err;
245 
246   Target target(setup.settings(), Label(SourceDir("//foo/bar/"), "baz"));
247   target.set_output_type(Target::SHARED_LIBRARY);
248   target.SetToolchain(setup.toolchain());
249   ASSERT_TRUE(target.OnResolved(&err));
250 
251   const Tool* tool = setup.toolchain()->GetToolForTargetFinalOutput(&target);
252 
253   // The compiler substitution is just target + OUTPUT_EXTENSION combined. So
254   // test one target one plus the output extension.
255   EXPECT_EQ(".so", SubstitutionWriter::GetLinkerSubstitution(
256                        &target, tool, &CSubstitutionOutputExtension));
257   EXPECT_EQ("gen/foo/bar", SubstitutionWriter::GetLinkerSubstitution(
258                                &target, tool, &SubstitutionTargetGenDir));
259 
260   // Test that we handle paths that end up in the root build dir properly
261   // (no leading "./" or "/").
262   SubstitutionPattern pattern;
263   ASSERT_TRUE(pattern.Parse("{{root_out_dir}}/{{target_output_name}}.so",
264                             nullptr, &err));
265 
266   OutputFile output = SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
267       &target, tool, pattern);
268   EXPECT_EQ("./libbaz.so", output.value());
269 
270   // Output extensions can be overridden.
271   target.set_output_extension("extension");
272   EXPECT_EQ(".extension", SubstitutionWriter::GetLinkerSubstitution(
273                               &target, tool, &CSubstitutionOutputExtension));
274   target.set_output_extension("");
275   EXPECT_EQ("", SubstitutionWriter::GetLinkerSubstitution(
276                     &target, tool, &CSubstitutionOutputExtension));
277 
278   // Output directory is tested in a separate test below.
279 }
280 
TEST(SubstitutionWriter,OutputDir)281 TEST(SubstitutionWriter, OutputDir) {
282   TestWithScope setup;
283   Err err;
284 
285   // This tool has an output directory pattern and uses that for the
286   // output name.
287   std::unique_ptr<Tool> tool = Tool::CreateTool(CTool::kCToolLink);
288   SubstitutionPattern out_dir_pattern;
289   ASSERT_TRUE(out_dir_pattern.Parse("{{root_out_dir}}/{{target_output_name}}",
290                                     nullptr, &err));
291   tool->set_default_output_dir(out_dir_pattern);
292   tool->SetComplete();
293 
294   // Default target with no output dir overrides.
295   Target target(setup.settings(), Label(SourceDir("//foo/"), "baz"));
296   target.set_output_type(Target::EXECUTABLE);
297   target.SetToolchain(setup.toolchain());
298   ASSERT_TRUE(target.OnResolved(&err));
299 
300   // The output should expand the default from the patterns in the tool.
301   SubstitutionPattern output_name;
302   ASSERT_TRUE(output_name.Parse("{{output_dir}}/{{target_output_name}}.exe",
303                                 nullptr, &err));
304   EXPECT_EQ("./baz/baz.exe",
305             SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
306                 &target, tool.get(), output_name)
307                 .value());
308 
309   // Override the output name to the root build dir.
310   target.set_output_dir(SourceDir("//out/Debug/"));
311   EXPECT_EQ("./baz.exe", SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
312                              &target, tool.get(), output_name)
313                              .value());
314 
315   // Override the output name to a new subdirectory.
316   target.set_output_dir(SourceDir("//out/Debug/foo/bar"));
317   EXPECT_EQ("foo/bar/baz.exe",
318             SubstitutionWriter::ApplyPatternToLinkerAsOutputFile(
319                 &target, tool.get(), output_name)
320                 .value());
321 }
322