ApplicationImpl cleanup, part 1:
[chromium-blink-merge.git] / tools / gn / test_with_scope.cc
blobd37b72246247151f4de7715dc4a15cdd0851733d
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 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 {
30 Err err;
31 Label result = Label::Resolve(SourceDir("//"), toolchain_.label(),
32 Value(nullptr, str), &err);
33 CHECK(!err.has_error());
34 return result;
37 // static
38 void TestWithScope::SetupToolchain(Toolchain* toolchain) {
39 Err err;
41 // CC
42 scoped_ptr<Tool> cc_tool(new Tool);
43 SetCommandForTool(
44 "cc {{source}} {{cflags}} {{cflags_c}} {{defines}} {{include_dirs}} "
45 "-o {{output}}",
46 cc_tool.get());
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());
51 // CXX
52 scoped_ptr<Tool> cxx_tool(new Tool);
53 SetCommandForTool(
54 "c++ {{source}} {{cflags}} {{cflags_cc}} {{defines}} {{include_dirs}} "
55 "-o {{output}}",
56 cxx_tool.get());
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());
61 // OBJC
62 scoped_ptr<Tool> objc_tool(new Tool);
63 SetCommandForTool(
64 "objcc {{source}} {{cflags}} {{cflags_objc}} {{defines}} "
65 "{{include_dirs}} -o {{output}}",
66 objc_tool.get());
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());
71 // OBJC
72 scoped_ptr<Tool> objcxx_tool(new Tool);
73 SetCommandForTool(
74 "objcxx {{source}} {{cflags}} {{cflags_objcc}} {{defines}} "
75 "{{include_dirs}} -o {{output}}",
76 objcxx_tool.get());
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.
83 // ALINK
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());
91 // SOLINK
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());
101 // LINK
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());
109 // STAMP
110 scoped_ptr<Tool> stamp_tool(new Tool);
111 SetCommandForTool("touch {{output}}", stamp_tool.get());
112 toolchain->SetTool(Toolchain::TYPE_STAMP, stamp_tool.Pass());
114 // COPY
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();
122 // static
123 void TestWithScope::SetCommandForTool(const std::string& cmd, Tool* tool) {
124 Err err;
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() {