Popular sites on the NTP: Favicon improvements
[chromium-blink-merge.git] / tools / gn / test_with_scope.cc
blob20551f19475d278c5e68e60a08dba209b772c4e1
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"
7 #include "base/bind.h"
8 #include "tools/gn/parser.h"
9 #include "tools/gn/tokenizer.h"
11 TestWithScope::TestWithScope()
12 : build_settings_(),
13 settings_(&build_settings_, std::string()),
14 toolchain_(&settings_, Label(SourceDir("//toolchain/"), "default")),
15 scope_(&settings_),
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 {
31 Err err;
32 Label result = Label::Resolve(SourceDir("//"), toolchain_.label(),
33 Value(nullptr, str), &err);
34 CHECK(!err.has_error());
35 return result;
38 // static
39 void TestWithScope::SetupToolchain(Toolchain* toolchain) {
40 Err err;
42 // CC
43 scoped_ptr<Tool> cc_tool(new Tool);
44 SetCommandForTool(
45 "cc {{source}} {{cflags}} {{cflags_c}} {{defines}} {{include_dirs}} "
46 "-o {{output}}",
47 cc_tool.get());
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());
52 // CXX
53 scoped_ptr<Tool> cxx_tool(new Tool);
54 SetCommandForTool(
55 "c++ {{source}} {{cflags}} {{cflags_cc}} {{defines}} {{include_dirs}} "
56 "-o {{output}}",
57 cxx_tool.get());
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());
62 // OBJC
63 scoped_ptr<Tool> objc_tool(new Tool);
64 SetCommandForTool(
65 "objcc {{source}} {{cflags}} {{cflags_objc}} {{defines}} "
66 "{{include_dirs}} -o {{output}}",
67 objc_tool.get());
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());
72 // OBJC
73 scoped_ptr<Tool> objcxx_tool(new Tool);
74 SetCommandForTool(
75 "objcxx {{source}} {{cflags}} {{cflags_objcc}} {{defines}} "
76 "{{include_dirs}} -o {{output}}",
77 objcxx_tool.get());
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.
84 // ALINK
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());
92 // SOLINK
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());
102 // LINK
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());
110 // STAMP
111 scoped_ptr<Tool> stamp_tool(new Tool);
112 SetCommandForTool("touch {{output}}", stamp_tool.get());
113 toolchain->SetTool(Toolchain::TYPE_STAMP, stamp_tool.Pass());
115 // COPY
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();
123 // static
124 void TestWithScope::SetCommandForTool(const std::string& cmd, Tool* tool) {
125 Err err;
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() {