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
3 // found in the LICENSE file.
5 #include "tools/gn/test_with_scope.h"
8 #include "tools/gn/parser.h"
9 #include "tools/gn/tokenizer.h"
11 TestWithScope::TestWithScope()
13 settings_(&build_settings_
, std::string()),
14 toolchain_(&settings_
, Label(SourceDir("//toolchain/"), "default")),
16 scope_progammatic_provider_(&scope_
, true) {
17 build_settings_
.SetBuildDir(SourceDir("//out/Debug/"));
18 build_settings_
.set_print_callback(
19 base::Bind(&TestWithScope::AppendPrintOutput
, base::Unretained(this)));
21 settings_
.set_toolchain_label(toolchain_
.label());
22 settings_
.set_default_toolchain_label(toolchain_
.label());
24 SetupToolchain(&toolchain_
);
27 TestWithScope::~TestWithScope() {
30 Label
TestWithScope::ParseLabel(const std::string
& str
) const {
32 Label result
= Label::Resolve(SourceDir("//"), toolchain_
.label(),
33 Value(nullptr, str
), &err
);
34 CHECK(!err
.has_error());
39 void TestWithScope::SetupToolchain(Toolchain
* toolchain
) {
43 scoped_ptr
<Tool
> cc_tool(new Tool
);
45 "cc {{source}} {{cflags}} {{cflags_c}} {{defines}} {{include_dirs}} "
48 cc_tool
->set_outputs(SubstitutionList::MakeForTest(
49 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"));
50 toolchain
->SetTool(Toolchain::TYPE_CC
, cc_tool
.Pass());
53 scoped_ptr
<Tool
> cxx_tool(new Tool
);
55 "c++ {{source}} {{cflags}} {{cflags_cc}} {{defines}} {{include_dirs}} "
58 cxx_tool
->set_outputs(SubstitutionList::MakeForTest(
59 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"));
60 toolchain
->SetTool(Toolchain::TYPE_CXX
, cxx_tool
.Pass());
63 scoped_ptr
<Tool
> objc_tool(new Tool
);
65 "objcc {{source}} {{cflags}} {{cflags_objc}} {{defines}} "
66 "{{include_dirs}} -o {{output}}",
68 objc_tool
->set_outputs(SubstitutionList::MakeForTest(
69 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"));
70 toolchain
->SetTool(Toolchain::TYPE_OBJC
, objc_tool
.Pass());
73 scoped_ptr
<Tool
> objcxx_tool(new Tool
);
75 "objcxx {{source}} {{cflags}} {{cflags_objcc}} {{defines}} "
76 "{{include_dirs}} -o {{output}}",
78 objcxx_tool
->set_outputs(SubstitutionList::MakeForTest(
79 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"));
80 toolchain
->SetTool(Toolchain::TYPE_OBJCXX
, objcxx_tool
.Pass());
82 // Don't use RC and ASM tools in unit tests yet. Add here if needed.
85 scoped_ptr
<Tool
> alink_tool(new Tool
);
86 SetCommandForTool("ar {{output}} {{source}}", alink_tool
.get());
87 alink_tool
->set_output_prefix("lib");
88 alink_tool
->set_outputs(SubstitutionList::MakeForTest(
89 "{{target_out_dir}}/{{target_output_name}}.a"));
90 toolchain
->SetTool(Toolchain::TYPE_ALINK
, alink_tool
.Pass());
93 scoped_ptr
<Tool
> solink_tool(new Tool
);
94 SetCommandForTool("ld -shared -o {{target_output_name}}.so {{inputs}} "
95 "{{ldflags}} {{libs}}", solink_tool
.get());
96 solink_tool
->set_output_prefix("lib");
97 solink_tool
->set_default_output_extension(".so");
98 solink_tool
->set_outputs(SubstitutionList::MakeForTest(
99 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"));
100 toolchain
->SetTool(Toolchain::TYPE_SOLINK
, solink_tool
.Pass());
103 scoped_ptr
<Tool
> link_tool(new Tool
);
104 SetCommandForTool("ld -o {{target_output_name}} {{source}} "
105 "{{ldflags}} {{libs}}", link_tool
.get());
106 link_tool
->set_outputs(SubstitutionList::MakeForTest(
107 "{{root_out_dir}}/{{target_output_name}}"));
108 toolchain
->SetTool(Toolchain::TYPE_LINK
, link_tool
.Pass());
111 scoped_ptr
<Tool
> stamp_tool(new Tool
);
112 SetCommandForTool("touch {{output}}", stamp_tool
.get());
113 toolchain
->SetTool(Toolchain::TYPE_STAMP
, stamp_tool
.Pass());
116 scoped_ptr
<Tool
> copy_tool(new Tool
);
117 SetCommandForTool("cp {{source}} {{output}}", copy_tool
.get());
118 toolchain
->SetTool(Toolchain::TYPE_COPY
, copy_tool
.Pass());
120 toolchain
->ToolchainSetupComplete();
124 void TestWithScope::SetCommandForTool(const std::string
& cmd
, Tool
* tool
) {
126 SubstitutionPattern command
;
127 command
.Parse(cmd
, nullptr, &err
);
128 CHECK(!err
.has_error())
129 << "Couldn't parse \"" << cmd
<< "\", " << "got " << err
.message();
130 tool
->set_command(command
);
133 void TestWithScope::AppendPrintOutput(const std::string
& str
) {
134 print_output_
.append(str
);
137 TestParseInput::TestParseInput(const std::string
& input
)
138 : input_file_(SourceFile("//test")) {
139 input_file_
.SetContents(input
);
141 tokens_
= Tokenizer::Tokenize(&input_file_
, &parse_err_
);
142 if (!parse_err_
.has_error())
143 parsed_
= Parser::Parse(tokens_
, &parse_err_
);
146 TestParseInput::~TestParseInput() {
149 TestTarget::TestTarget(TestWithScope
& setup
,
150 const std::string
& label_string
,
151 Target::OutputType type
)
152 : Target(setup
.settings(), setup
.ParseLabel(label_string
)) {
153 visibility().SetPublic();
154 set_output_type(type
);
155 SetToolchain(setup
.toolchain());
158 TestTarget::~TestTarget() {