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