1// Copyright 2014 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.
4
5#ifndef TOOLS_GN_TOOL_H_
6#define TOOLS_GN_TOOL_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/logging.h"
12#include "tools/gn/substitution_list.h"
13#include "tools/gn/substitution_pattern.h"
14
15class Tool {
16 public:
17  enum DepsFormat {
18    DEPS_GCC = 0,
19    DEPS_MSVC = 1
20  };
21
22  Tool();
23  ~Tool();
24
25  // Getters/setters ----------------------------------------------------------
26  //
27  // After the tool has had its attributes set, the caller must call
28  // SetComplete(), at which point no other changes can be made.
29
30  // Command to run.
31  const SubstitutionPattern& command() const {
32    return command_;
33  }
34  void set_command(const SubstitutionPattern& cmd) {
35    DCHECK(!complete_);
36    command_ = cmd;
37  }
38
39  // Should include a leading "." if nonempty.
40  const std::string& default_output_extension() const {
41    return default_output_extension_;
42  }
43  void set_default_output_extension(const std::string& ext) {
44    DCHECK(!complete_);
45    DCHECK(ext.empty() || ext[0] == '.');
46    default_output_extension_ = ext;
47  }
48
49  // Dependency file (if supported).
50  const SubstitutionPattern& depfile() const {
51    return depfile_;
52  }
53  void set_depfile(const SubstitutionPattern& df) {
54    DCHECK(!complete_);
55    depfile_ = df;
56  }
57
58  DepsFormat depsformat() const {
59    return depsformat_;
60  }
61  void set_depsformat(DepsFormat f) {
62    DCHECK(!complete_);
63    depsformat_ = f;
64  }
65
66  const SubstitutionPattern& description() const {
67    return description_;
68  }
69  void set_description(const SubstitutionPattern& desc) {
70    DCHECK(!complete_);
71    description_ = desc;
72  }
73
74  const std::string& lib_switch() const {
75    return lib_switch_;
76  }
77  void set_lib_switch(const std::string& s) {
78    DCHECK(!complete_);
79    lib_switch_ = s;
80  }
81
82  const std::string& lib_dir_switch() const {
83    return lib_dir_switch_;
84  }
85  void set_lib_dir_switch(const std::string& s) {
86    DCHECK(!complete_);
87    lib_dir_switch_ = s;
88  }
89
90  const SubstitutionList& outputs() const {
91    return outputs_;
92  }
93  void set_outputs(const SubstitutionList& out) {
94    DCHECK(!complete_);
95    outputs_ = out;
96  }
97
98  // Should match files in the outputs() if nonempty.
99  const SubstitutionPattern& link_output() const {
100    return link_output_;
101  }
102  void set_link_output(const SubstitutionPattern& link_out) {
103    DCHECK(!complete_);
104    link_output_ = link_out;
105  }
106
107  const SubstitutionPattern& depend_output() const {
108    return depend_output_;
109  }
110  void set_depend_output(const SubstitutionPattern& dep_out) {
111    DCHECK(!complete_);
112    depend_output_ = dep_out;
113  }
114
115  const std::string& output_prefix() const {
116    return output_prefix_;
117  }
118  void set_output_prefix(const std::string& s) {
119    DCHECK(!complete_);
120    output_prefix_ = s;
121  }
122
123  bool restat() const {
124    return restat_;
125  }
126  void set_restat(bool r) {
127    DCHECK(!complete_);
128    restat_ = r;
129  }
130
131  const SubstitutionPattern& rspfile() const {
132    return rspfile_;
133  }
134  void set_rspfile(const SubstitutionPattern& rsp) {
135    DCHECK(!complete_);
136    rspfile_ = rsp;
137  }
138
139  const SubstitutionPattern& rspfile_content() const {
140    return rspfile_content_;
141  }
142  void set_rspfile_content(const SubstitutionPattern& content) {
143    DCHECK(!complete_);
144    rspfile_content_ = content;
145  }
146
147  // Other functions ----------------------------------------------------------
148
149  // Called when the toolchain is saving this tool, after everything is filled
150  // in.
151  void SetComplete();
152
153  // Returns true if this tool has separate outputs for dependency tracking
154  // and linking.
155  bool has_separate_solink_files() const {
156    return !link_output_.empty() || !depend_output_.empty();
157  }
158
159  // Substitutions required by this tool.
160  const SubstitutionBits& substitution_bits() const {
161    DCHECK(complete_);
162    return substitution_bits_;
163  }
164
165 public:
166  SubstitutionPattern command_;
167  std::string default_output_extension_;
168  SubstitutionPattern depfile_;
169  DepsFormat depsformat_;
170  SubstitutionPattern description_;
171  std::string lib_switch_;
172  std::string lib_dir_switch_;
173  SubstitutionList outputs_;
174  SubstitutionPattern link_output_;
175  SubstitutionPattern depend_output_;
176  std::string output_prefix_;
177  bool restat_;
178  SubstitutionPattern rspfile_;
179  SubstitutionPattern rspfile_content_;
180
181  bool complete_;
182
183  SubstitutionBits substitution_bits_;
184
185  DISALLOW_COPY_AND_ASSIGN(Tool);
186};
187
188#endif  // TOOLS_GN_TOOL_H_
189