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 build_settings_
.SetBuildDir(SourceDir("//out/Debug/"));
17 build_settings_
.set_print_callback(
18 base::Bind(&TestWithScope::AppendPrintOutput
, base::Unretained(this)));
20 settings_
.set_toolchain_label(toolchain_
.label());
21 settings_
.set_default_toolchain_label(toolchain_
.label());
23 SetupToolchain(&toolchain_
);
26 TestWithScope::~TestWithScope() {
29 Label
TestWithScope::ParseLabel(const std::string
& str
) const {
31 Label result
= Label::Resolve(SourceDir("//"), toolchain_
.label(),
32 Value(nullptr, str
), &err
);
33 CHECK(!err
.has_error());
38 void TestWithScope::SetupToolchain(Toolchain
* toolchain
) {
42 scoped_ptr
<Tool
> cc_tool(new Tool
);
44 "cc {{source}} {{cflags}} {{cflags_c}} {{defines}} {{include_dirs}} "
47 cc_tool
->set_outputs(SubstitutionList::MakeForTest(
48 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"));
49 toolchain
->SetTool(Toolchain::TYPE_CC
, cc_tool
.Pass());
52 scoped_ptr
<Tool
> cxx_tool(new Tool
);
54 "c++ {{source}} {{cflags}} {{cflags_cc}} {{defines}} {{include_dirs}} "
57 cxx_tool
->set_outputs(SubstitutionList::MakeForTest(
58 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"));
59 toolchain
->SetTool(Toolchain::TYPE_CXX
, cxx_tool
.Pass());
62 scoped_ptr
<Tool
> objc_tool(new Tool
);
64 "objcc {{source}} {{cflags}} {{cflags_objc}} {{defines}} "
65 "{{include_dirs}} -o {{output}}",
67 objc_tool
->set_outputs(SubstitutionList::MakeForTest(
68 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"));
69 toolchain
->SetTool(Toolchain::TYPE_OBJC
, objc_tool
.Pass());
72 scoped_ptr
<Tool
> objcxx_tool(new Tool
);
74 "objcxx {{source}} {{cflags}} {{cflags_objcc}} {{defines}} "
75 "{{include_dirs}} -o {{output}}",
77 objcxx_tool
->set_outputs(SubstitutionList::MakeForTest(
78 "{{source_out_dir}}/{{target_output_name}}.{{source_name_part}}.o"));
79 toolchain
->SetTool(Toolchain::TYPE_OBJCXX
, objcxx_tool
.Pass());
81 // Don't use RC and ASM tools in unit tests yet. Add here if needed.
84 scoped_ptr
<Tool
> alink_tool(new Tool
);
85 SetCommandForTool("ar {{output}} {{source}}", alink_tool
.get());
86 alink_tool
->set_output_prefix("lib");
87 alink_tool
->set_outputs(SubstitutionList::MakeForTest(
88 "{{target_out_dir}}/{{target_output_name}}.a"));
89 toolchain
->SetTool(Toolchain::TYPE_ALINK
, alink_tool
.Pass());
92 scoped_ptr
<Tool
> solink_tool(new Tool
);
93 SetCommandForTool("ld -shared -o {{target_output_name}}.so {{inputs}} "
94 "{{ldflags}} {{libs}}", solink_tool
.get());
95 solink_tool
->set_output_prefix("lib");
96 solink_tool
->set_default_output_extension(".so");
97 solink_tool
->set_outputs(SubstitutionList::MakeForTest(
98 "{{root_out_dir}}/{{target_output_name}}{{output_extension}}"));
99 toolchain
->SetTool(Toolchain::TYPE_SOLINK
, solink_tool
.Pass());
102 scoped_ptr
<Tool
> link_tool(new Tool
);
103 SetCommandForTool("ld -o {{target_output_name}} {{source}} "
104 "{{ldflags}} {{libs}}", link_tool
.get());
105 link_tool
->set_outputs(SubstitutionList::MakeForTest(
106 "{{root_out_dir}}/{{target_output_name}}"));
107 toolchain
->SetTool(Toolchain::TYPE_LINK
, link_tool
.Pass());
110 scoped_ptr
<Tool
> stamp_tool(new Tool
);
111 SetCommandForTool("touch {{output}}", stamp_tool
.get());
112 toolchain
->SetTool(Toolchain::TYPE_STAMP
, stamp_tool
.Pass());
115 scoped_ptr
<Tool
> copy_tool(new Tool
);
116 SetCommandForTool("cp {{source}} {{output}}", copy_tool
.get());
117 toolchain
->SetTool(Toolchain::TYPE_COPY
, copy_tool
.Pass());
119 toolchain
->ToolchainSetupComplete();
123 void TestWithScope::SetCommandForTool(const std::string
& cmd
, Tool
* tool
) {
125 SubstitutionPattern command
;
126 command
.Parse(cmd
, nullptr, &err
);
127 CHECK(!err
.has_error())
128 << "Couldn't parse \"" << cmd
<< "\", " << "got " << err
.message();
129 tool
->set_command(command
);
132 void TestWithScope::AppendPrintOutput(const std::string
& str
) {
133 print_output_
.append(str
);
136 TestParseInput::TestParseInput(const std::string
& input
)
137 : input_file_(SourceFile("//test")) {
138 input_file_
.SetContents(input
);
140 tokens_
= Tokenizer::Tokenize(&input_file_
, &parse_err_
);
141 if (!parse_err_
.has_error())
142 parsed_
= Parser::Parse(tokens_
, &parse_err_
);
145 TestParseInput::~TestParseInput() {
148 TestTarget::TestTarget(TestWithScope
& setup
,
149 const std::string
& label_string
,
150 Target::OutputType type
)
151 : Target(setup
.settings(), setup
.ParseLabel(label_string
)) {
152 visibility().SetPublic();
153 set_output_type(type
);
154 SetToolchain(setup
.toolchain());
157 TestTarget::~TestTarget() {