summaryrefslogtreecommitdiffstats
path: root/tools/gn/ninja_action_target_writer_unittest.cc
blob: 8c295c938d92063440b1bae058b9666426819d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// 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 <algorithm>
#include <sstream>

#include "testing/gtest/include/gtest/gtest.h"
#include "tools/gn/ninja_action_target_writer.h"
#include "tools/gn/substitution_list.h"
#include "tools/gn/target.h"
#include "tools/gn/test_with_scope.h"

TEST(NinjaActionTargetWriter, WriteOutputFilesForBuildLine) {
  TestWithScope setup;
  Err err;

  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));

  Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
  target.set_output_type(Target::ACTION_FOREACH);
  target.action_values().outputs() = SubstitutionList::MakeForTest(
      "//out/Debug/gen/a b{{source_name_part}}.h",
      "//out/Debug/gen/{{source_name_part}}.cc");

  target.SetToolchain(setup.toolchain());
  ASSERT_TRUE(target.OnResolved(&err));

  std::ostringstream out;
  NinjaActionTargetWriter writer(&target, out);

  SourceFile source("//foo/bar.in");
  std::vector<OutputFile> output_files;
  writer.WriteOutputFilesForBuildLine(source, &output_files);

  EXPECT_EQ(" gen/a$ bbar.h gen/bar.cc", out.str());
}

// Tests an action with no sources.
TEST(NinjaActionTargetWriter, ActionNoSources) {
  TestWithScope setup;
  Err err;

  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
  Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
  target.set_output_type(Target::ACTION);

  target.action_values().set_script(SourceFile("//foo/script.py"));
  target.inputs().push_back(SourceFile("//foo/included.txt"));

  target.action_values().outputs() =
      SubstitutionList::MakeForTest("//out/Debug/foo.out");

  target.SetToolchain(setup.toolchain());
  ASSERT_TRUE(target.OnResolved(&err));

  setup.settings()->set_target_os(Settings::LINUX);
  setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL(
      "/usr/bin/python")));

  std::ostringstream out;
  NinjaActionTargetWriter writer(&target, out);
  writer.Run();

  const char expected[] =
      "rule __foo_bar___rule\n"
      "  command = /usr/bin/python ../../foo/script.py\n"
      "  description = ACTION //foo:bar()\n"
      "  restat = 1\n"
      "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py "
          "../../foo/included.txt\n"
      "\n"
      "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n"
      "\n"
      "build obj/foo/bar.stamp: stamp foo.out\n";
  EXPECT_EQ(expected, out.str());
}

// Makes sure that we write sources as input dependencies for actions with
// both sources and inputs (ACTION_FOREACH treats the sources differently).
TEST(NinjaActionTargetWriter, ActionWithSources) {
  TestWithScope setup;
  Err err;

  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
  Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
  target.set_output_type(Target::ACTION);

  target.action_values().set_script(SourceFile("//foo/script.py"));

  target.sources().push_back(SourceFile("//foo/source.txt"));
  target.inputs().push_back(SourceFile("//foo/included.txt"));

  target.action_values().outputs() =
      SubstitutionList::MakeForTest("//out/Debug/foo.out");

  target.SetToolchain(setup.toolchain());
  ASSERT_TRUE(target.OnResolved(&err));

  // Posix.
  {
    setup.settings()->set_target_os(Settings::LINUX);
    setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL(
        "/usr/bin/python")));

    std::ostringstream out;
    NinjaActionTargetWriter writer(&target, out);
    writer.Run();

    const char expected_linux[] =
        "rule __foo_bar___rule\n"
        "  command = /usr/bin/python ../../foo/script.py\n"
        "  description = ACTION //foo:bar()\n"
        "  restat = 1\n"
        "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py "
            "../../foo/included.txt ../../foo/source.txt\n"
        "\n"
        "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n"
        "\n"
        "build obj/foo/bar.stamp: stamp foo.out\n";
    EXPECT_EQ(expected_linux, out.str());
  }

  // Windows.
  {
    // Note: we use forward slashes here so that the output will be the same on
    // Linux and Windows.
    setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL(
        "C:/python/python.exe")));
    setup.settings()->set_target_os(Settings::WIN);

    std::ostringstream out;
    NinjaActionTargetWriter writer(&target, out);
    writer.Run();

    const char expected_win[] =
        "rule __foo_bar___rule\n"
        "  command = C$:/python/python.exe gyp-win-tool action-wrapper environment.x86 __foo_bar___rule.$unique_name.rsp\n"
        "  description = ACTION //foo:bar()\n"
        "  restat = 1\n"
        "  rspfile = __foo_bar___rule.$unique_name.rsp\n"
        "  rspfile_content = C$:/python/python.exe ../../foo/script.py\n"
        "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py "
            "../../foo/included.txt ../../foo/source.txt\n"
        "\n"
        "build foo.out: __foo_bar___rule | obj/foo/bar.inputdeps.stamp\n"
        "\n"
        "build obj/foo/bar.stamp: stamp foo.out\n";
    EXPECT_EQ(expected_win, out.str());
  }
}

TEST(NinjaActionTargetWriter, ForEach) {
  TestWithScope setup;
  Err err;

  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));

  // Some dependencies that the action can depend on. Use actions for these
  // so they have a nice platform-independent stamp file that can appear in the
  // output (rather than having to worry about how the current platform names
  // binaries).
  Target dep(setup.settings(), Label(SourceDir("//foo/"), "dep"));
  dep.set_output_type(Target::ACTION);
  dep.visibility().SetPublic();
  dep.SetToolchain(setup.toolchain());
  ASSERT_TRUE(dep.OnResolved(&err));

  Target datadep(setup.settings(), Label(SourceDir("//foo/"), "datadep"));
  datadep.set_output_type(Target::ACTION);
  datadep.visibility().SetPublic();
  datadep.SetToolchain(setup.toolchain());
  ASSERT_TRUE(datadep.OnResolved(&err));

  Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
  target.set_output_type(Target::ACTION_FOREACH);
  target.private_deps().push_back(LabelTargetPair(&dep));
  target.data_deps().push_back(LabelTargetPair(&datadep));

  target.sources().push_back(SourceFile("//foo/input1.txt"));
  target.sources().push_back(SourceFile("//foo/input2.txt"));

  target.action_values().set_script(SourceFile("//foo/script.py"));

  target.action_values().args() = SubstitutionList::MakeForTest(
      "-i",
      "{{source}}",
      "--out=foo bar{{source_name_part}}.o");
  target.action_values().outputs() = SubstitutionList::MakeForTest(
      "//out/Debug/{{source_name_part}}.out");

  target.inputs().push_back(SourceFile("//foo/included.txt"));

  target.SetToolchain(setup.toolchain());
  ASSERT_TRUE(target.OnResolved(&err));

  // Posix.
  {
    setup.settings()->set_target_os(Settings::LINUX);
    setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL(
        "/usr/bin/python")));

    std::ostringstream out;
    NinjaActionTargetWriter writer(&target, out);
    writer.Run();

    const char expected_linux[] =
        "rule __foo_bar___rule\n"
        "  command = /usr/bin/python ../../foo/script.py -i ${in} "
            // Escaping is different between Windows and Posix.
#if defined(OS_WIN)
            "\"--out=foo$ bar${source_name_part}.o\"\n"
#else
            "--out=foo\\$ bar${source_name_part}.o\n"
#endif
        "  description = ACTION //foo:bar()\n"
        "  restat = 1\n"
        "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py "
            "../../foo/included.txt obj/foo/dep.stamp\n"
        "\n"
        "build input1.out: __foo_bar___rule ../../foo/input1.txt | "
            "obj/foo/bar.inputdeps.stamp\n"
        "  source_name_part = input1\n"
        "build input2.out: __foo_bar___rule ../../foo/input2.txt | "
            "obj/foo/bar.inputdeps.stamp\n"
        "  source_name_part = input2\n"
        "\n"
        "build obj/foo/bar.stamp: "
            "stamp input1.out input2.out || obj/foo/datadep.stamp\n";

    std::string out_str = out.str();
#if defined(OS_WIN)
    std::replace(out_str.begin(), out_str.end(), '\\', '/');
#endif
    EXPECT_EQ(expected_linux, out_str);
  }

  // Windows.
  {
    setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL(
        "C:/python/python.exe")));
    setup.settings()->set_target_os(Settings::WIN);

    std::ostringstream out;
    NinjaActionTargetWriter writer(&target, out);
    writer.Run();

    const char expected_win[] =
        "rule __foo_bar___rule\n"
        "  command = C$:/python/python.exe gyp-win-tool action-wrapper "
            "environment.x86 __foo_bar___rule.$unique_name.rsp\n"
        "  description = ACTION //foo:bar()\n"
        "  restat = 1\n"
        "  rspfile = __foo_bar___rule.$unique_name.rsp\n"
        "  rspfile_content = C$:/python/python.exe ../../foo/script.py -i "
#if defined(OS_WIN)
            "${in} \"--out=foo$ bar${source_name_part}.o\"\n"
#else
            "${in} --out=foo\\$ bar${source_name_part}.o\n"
#endif
        "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py "
            "../../foo/included.txt obj/foo/dep.stamp\n"
        "\n"
        "build input1.out: __foo_bar___rule ../../foo/input1.txt | "
            "obj/foo/bar.inputdeps.stamp\n"
        "  unique_name = 0\n"
        "  source_name_part = input1\n"
        "build input2.out: __foo_bar___rule ../../foo/input2.txt | "
            "obj/foo/bar.inputdeps.stamp\n"
        "  unique_name = 1\n"
        "  source_name_part = input2\n"
        "\n"
        "build obj/foo/bar.stamp: "
            "stamp input1.out input2.out || obj/foo/datadep.stamp\n";
    EXPECT_EQ(expected_win, out.str());
  }
}

TEST(NinjaActionTargetWriter, ForEachWithDepfile) {
  TestWithScope setup;
  Err err;

  setup.build_settings()->SetBuildDir(SourceDir("//out/Debug/"));
  Target target(setup.settings(), Label(SourceDir("//foo/"), "bar"));
  target.set_output_type(Target::ACTION_FOREACH);

  target.sources().push_back(SourceFile("//foo/input1.txt"));
  target.sources().push_back(SourceFile("//foo/input2.txt"));

  target.action_values().set_script(SourceFile("//foo/script.py"));

  target.SetToolchain(setup.toolchain());
  ASSERT_TRUE(target.OnResolved(&err));

  SubstitutionPattern depfile;
  ASSERT_TRUE(
      depfile.Parse("//out/Debug/gen/{{source_name_part}}.d", NULL, &err));
  target.action_values().set_depfile(depfile);

  target.action_values().args() = SubstitutionList::MakeForTest(
      "-i",
      "{{source}}",
      "--out=foo bar{{source_name_part}}.o");
  target.action_values().outputs() = SubstitutionList::MakeForTest(
      "//out/Debug/{{source_name_part}}.out");

  target.inputs().push_back(SourceFile("//foo/included.txt"));

  // Posix.
  {
    setup.settings()->set_target_os(Settings::LINUX);
    setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL(
        "/usr/bin/python")));

    std::ostringstream out;
    NinjaActionTargetWriter writer(&target, out);
    writer.Run();

    const char expected_linux[] =
        "rule __foo_bar___rule\n"
        "  command = /usr/bin/python ../../foo/script.py -i ${in} "
#if defined(OS_WIN)
            "\"--out=foo$ bar${source_name_part}.o\"\n"
#else
            "--out=foo\\$ bar${source_name_part}.o\n"
#endif
        "  description = ACTION //foo:bar()\n"
        "  restat = 1\n"
        "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py "
            "../../foo/included.txt\n"
        "\n"
        "build input1.out: __foo_bar___rule ../../foo/input1.txt"
            " | obj/foo/bar.inputdeps.stamp\n"
        "  source_name_part = input1\n"
        "  depfile = gen/input1.d\n"
        "build input2.out: __foo_bar___rule ../../foo/input2.txt"
            " | obj/foo/bar.inputdeps.stamp\n"
        "  source_name_part = input2\n"
        "  depfile = gen/input2.d\n"
        "\n"
        "build obj/foo/bar.stamp: stamp input1.out input2.out\n";
    EXPECT_EQ(expected_linux, out.str());
  }

  // Windows.
  {
    setup.build_settings()->set_python_path(base::FilePath(FILE_PATH_LITERAL(
        "C:/python/python.exe")));
    setup.settings()->set_target_os(Settings::WIN);

    std::ostringstream out;
    NinjaActionTargetWriter writer(&target, out);
    writer.Run();

    const char expected_win[] =
        "rule __foo_bar___rule\n"
        "  command = C$:/python/python.exe gyp-win-tool action-wrapper "
            "environment.x86 __foo_bar___rule.$unique_name.rsp\n"
        "  description = ACTION //foo:bar()\n"
        "  restat = 1\n"
        "  rspfile = __foo_bar___rule.$unique_name.rsp\n"
        "  rspfile_content = C$:/python/python.exe ../../foo/script.py -i "
#if defined(OS_WIN)
            "${in} \"--out=foo$ bar${source_name_part}.o\"\n"
#else
            "${in} --out=foo\\$ bar${source_name_part}.o\n"
#endif
        "build obj/foo/bar.inputdeps.stamp: stamp ../../foo/script.py "
            "../../foo/included.txt\n"
        "\n"
        "build input1.out: __foo_bar___rule ../../foo/input1.txt"
            " | obj/foo/bar.inputdeps.stamp\n"
        "  unique_name = 0\n"
        "  source_name_part = input1\n"
        "  depfile = gen/input1.d\n"
        "build input2.out: __foo_bar___rule ../../foo/input2.txt"
            " | obj/foo/bar.inputdeps.stamp\n"
        "  unique_name = 1\n"
        "  source_name_part = input2\n"
        "  depfile = gen/input2.d\n"
        "\n"
        "build obj/foo/bar.stamp: stamp input1.out input2.out\n";
    EXPECT_EQ(expected_win, out.str());
  }
}