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