• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef TOOLS_GN_BUILD_SETTINGS_H_
6 #define TOOLS_GN_BUILD_SETTINGS_H_
7 
8 #include <functional>
9 #include <map>
10 #include <memory>
11 #include <set>
12 #include <utility>
13 
14 #include "base/files/file_path.h"
15 #include "gn/args.h"
16 #include "gn/label.h"
17 #include "gn/label_pattern.h"
18 #include "gn/scope.h"
19 #include "gn/source_dir.h"
20 #include "gn/source_file.h"
21 #include "gn/version.h"
22 
23 class Item;
24 class OhosComponent;
25 class OhosComponents;
26 
27 // Settings for one build, which is one toplevel output directory. There
28 // may be multiple Settings objects that refer to this, one for each toolchain.
29 class BuildSettings {
30  public:
31   using ItemDefinedCallback = std::function<void(std::unique_ptr<Item>)>;
32   using PrintCallback = std::function<void(const std::string&)>;
33 
34   BuildSettings();
35   BuildSettings(const BuildSettings& other);
36 
37   // Root target label.
root_target_label()38   const Label& root_target_label() const { return root_target_label_; }
39   void SetRootTargetLabel(const Label& r);
40 
41   // Root target label patterns.
root_patterns()42   const std::vector<LabelPattern>& root_patterns() const {
43     return root_patterns_;
44   }
45   void SetRootPatterns(std::vector<LabelPattern>&& root_patterns);
46 
47   // Absolute path of the source root on the local system. Everything is
48   // relative to this. Does not end in a [back]slash.
root_path()49   const base::FilePath& root_path() const { return root_path_; }
dotfile_name()50   const base::FilePath& dotfile_name() const { return dotfile_name_; }
root_path_utf8()51   const std::string& root_path_utf8() const { return root_path_utf8_; }
52   void SetRootPath(const base::FilePath& r);
set_dotfile_name(const base::FilePath & d)53   void set_dotfile_name(const base::FilePath& d) { dotfile_name_ = d; }
54 
55   // When nonempty, specifies a parallel directory higherarchy in which to
56   // search for buildfiles if they're not found in the root higherarchy. This
57   // allows us to keep buildfiles in a separate tree during development.
secondary_source_path()58   const base::FilePath& secondary_source_path() const {
59     return secondary_source_path_;
60   }
61   void SetSecondarySourcePath(const SourceDir& d);
62 
63   // Path of the python executable to run scripts with.
python_path()64   base::FilePath python_path() const { return python_path_; }
set_python_path(const base::FilePath & p)65   void set_python_path(const base::FilePath& p) { python_path_ = p; }
66 
67   // OpenHarmony components manager.
68   void SetOhosComponentsInfo(OhosComponents *ohos_components);
69   bool GetExternalDepsLabel(const Value& external_dep, std::string& label,
70     const Label& current_toolchain, int &whole_status, Err* err) const;
71   bool GetPrivateDepsLabel(const Value &dep, std::string &label,
72     const Label& current_toolchain, int &whole_status, Err *err) const;
73   bool is_ohos_components_enabled() const;
74   const OhosComponent *GetOhosComponent(const std::string& label) const;
75 
76   // Required Ninja version.
ninja_required_version()77   const Version& ninja_required_version() const {
78     return ninja_required_version_;
79   }
set_ninja_required_version(Version v)80   void set_ninja_required_version(Version v) { ninja_required_version_ = v; }
81 
82   // The 'no_stamp_files' boolean flag can be set to generate Ninja files
83   // that use phony rules instead of stamp files in most cases. This reduces
84   // the size of the generated Ninja build plans, but requires Ninja 1.11
85   // or greater to properly process them.
no_stamp_files()86   bool no_stamp_files() const { return no_stamp_files_; }
set_no_stamp_files(bool no_stamp_files)87   void set_no_stamp_files(bool no_stamp_files) {
88     no_stamp_files_ = no_stamp_files;
89   }
90 
build_config_file()91   const SourceFile& build_config_file() const { return build_config_file_; }
set_build_config_file(const SourceFile & f)92   void set_build_config_file(const SourceFile& f) { build_config_file_ = f; }
93 
94   // Path to a file containing the default text to use when running `gn args`.
arg_file_template_path()95   const SourceFile& arg_file_template_path() const {
96     return arg_file_template_path_;
97   }
set_arg_file_template_path(const SourceFile & f)98   void set_arg_file_template_path(const SourceFile& f) {
99     arg_file_template_path_ = f;
100   }
101 
102   // The build directory is the root of all output files. The default toolchain
103   // files go into here, and non-default toolchains will have separate
104   // toolchain-specific root directories inside this.
build_dir()105   const SourceDir& build_dir() const { return build_dir_; }
106   void SetBuildDir(const SourceDir& dir);
107 
108   // The build args are normally specified on the command-line.
build_args()109   Args& build_args() { return build_args_; }
build_args()110   const Args& build_args() const { return build_args_; }
111 
112   // Returns the full absolute OS path cooresponding to the given file in the
113   // root source tree.
114   base::FilePath GetFullPath(const SourceFile& file) const;
115   base::FilePath GetFullPath(const SourceDir& dir) const;
116   // Works the same way as other GetFullPath.
117   // Parameter as_file defines whether path should be treated as a
118   // SourceFile or SourceDir value.
119   base::FilePath GetFullPath(const std::string& path, bool as_file) const;
120 
121   // Returns the absolute OS path inside the secondary source path. Will return
122   // an empty FilePath if the secondary source path is empty. When loading a
123   // buildfile, the GetFullPath should always be consulted first.
124   base::FilePath GetFullPathSecondary(const SourceFile& file) const;
125   base::FilePath GetFullPathSecondary(const SourceDir& dir) const;
126   // Works the same way as other GetFullPathSecondary.
127   // Parameter as_file defines whether path should be treated as a
128   // SourceFile or SourceDir value.
129   base::FilePath GetFullPathSecondary(const std::string& path,
130                                       bool as_file) const;
131 
132   // Called when an item is defined from a background thread.
133   void ItemDefined(std::unique_ptr<Item> item) const;
set_item_defined_callback(ItemDefinedCallback cb)134   void set_item_defined_callback(ItemDefinedCallback cb) {
135     item_defined_callback_ = cb;
136   }
137 
138   // Defines a callback that will be used to override the behavior of the
139   // print function. This is used in tests to collect print output. If the
140   // callback is is_null() (the default) the output will be printed to the
141   // console.
print_callback()142   const PrintCallback& print_callback() const { return print_callback_; }
set_print_callback(const PrintCallback & cb)143   void set_print_callback(const PrintCallback& cb) { print_callback_ = cb; }
144 
145   // A list of files that can call exec_script(). If the returned pointer is
146   // null, exec_script may be called from anywhere.
exec_script_whitelist()147   const SourceFileSet* exec_script_whitelist() const {
148     return exec_script_whitelist_.get();
149   }
set_exec_script_whitelist(std::unique_ptr<SourceFileSet> list)150   void set_exec_script_whitelist(std::unique_ptr<SourceFileSet> list) {
151     exec_script_whitelist_ = std::move(list);
152   }
153 
154   const OhosComponent *GetOhosComponentByName(const std::string &component_name) const;
155 
156   bool isOhosIndepCompilerEnable() const;
157 
158 private:
159   Label root_target_label_;
160   std::vector<LabelPattern> root_patterns_;
161   base::FilePath dotfile_name_;
162   base::FilePath root_path_;
163   std::string root_path_utf8_;
164   base::FilePath secondary_source_path_;
165   base::FilePath python_path_;
166 
167   OhosComponents *ohos_components_ = nullptr;
168 
169   // See 40045b9 for the reason behind using 1.7.2 as the default version.
170   Version ninja_required_version_{1, 7, 2};
171   bool no_stamp_files_ = false;
172 
173   SourceFile build_config_file_;
174   SourceFile arg_file_template_path_;
175   SourceDir build_dir_;
176   Args build_args_;
177 
178   ItemDefinedCallback item_defined_callback_;
179   PrintCallback print_callback_;
180 
181   std::unique_ptr<SourceFileSet> exec_script_whitelist_;
182 
183   BuildSettings& operator=(const BuildSettings&) = delete;
184 };
185 
186 #endif  // TOOLS_GN_BUILD_SETTINGS_H_
187