• 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 "base/macros.h"
16 #include "gn/args.h"
17 #include "gn/label.h"
18 #include "gn/scope.h"
19 #include "gn/source_dir.h"
20 #include "gn/source_file.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 
build_config_file()58   const SourceFile& build_config_file() const { return build_config_file_; }
set_build_config_file(const SourceFile & f)59   void set_build_config_file(const SourceFile& f) { build_config_file_ = f; }
60 
61   // Path to a file containing the default text to use when running `gn args`.
arg_file_template_path()62   const SourceFile& arg_file_template_path() const {
63     return arg_file_template_path_;
64   }
set_arg_file_template_path(const SourceFile & f)65   void set_arg_file_template_path(const SourceFile& f) {
66     arg_file_template_path_ = f;
67   }
68 
69   // The build directory is the root of all output files. The default toolchain
70   // files go into here, and non-default toolchains will have separate
71   // toolchain-specific root directories inside this.
build_dir()72   const SourceDir& build_dir() const { return build_dir_; }
73   void SetBuildDir(const SourceDir& dir);
74 
75   // The build args are normally specified on the command-line.
build_args()76   Args& build_args() { return build_args_; }
build_args()77   const Args& build_args() const { return build_args_; }
78 
79   // Returns the full absolute OS path cooresponding to the given file in the
80   // root source tree.
81   base::FilePath GetFullPath(const SourceFile& file) const;
82   base::FilePath GetFullPath(const SourceDir& dir) const;
83   // Works the same way as other GetFullPath.
84   // Parameter as_file defines whether path should be treated as a
85   // SourceFile or SourceDir value.
86   base::FilePath GetFullPath(const std::string& path, bool as_file) const;
87 
88   // Returns the absolute OS path inside the secondary source path. Will return
89   // an empty FilePath if the secondary source path is empty. When loading a
90   // buildfile, the GetFullPath should always be consulted first.
91   base::FilePath GetFullPathSecondary(const SourceFile& file) const;
92   base::FilePath GetFullPathSecondary(const SourceDir& dir) const;
93   // Works the same way as other GetFullPathSecondary.
94   // Parameter as_file defines whether path should be treated as a
95   // SourceFile or SourceDir value.
96   base::FilePath GetFullPathSecondary(const std::string& path,
97                                       bool as_file) const;
98 
99   // Called when an item is defined from a background thread.
100   void ItemDefined(std::unique_ptr<Item> item) const;
set_item_defined_callback(ItemDefinedCallback cb)101   void set_item_defined_callback(ItemDefinedCallback cb) {
102     item_defined_callback_ = cb;
103   }
104 
105   // Defines a callback that will be used to override the behavior of the
106   // print function. This is used in tests to collect print output. If the
107   // callback is is_null() (the default) the output will be printed to the
108   // console.
print_callback()109   const PrintCallback& print_callback() const { return print_callback_; }
set_print_callback(const PrintCallback & cb)110   void set_print_callback(const PrintCallback& cb) { print_callback_ = cb; }
111 
112   // A list of files that can call exec_script(). If the returned pointer is
113   // null, exec_script may be called from anywhere.
exec_script_whitelist()114   const SourceFileSet* exec_script_whitelist() const {
115     return exec_script_whitelist_.get();
116   }
set_exec_script_whitelist(std::unique_ptr<SourceFileSet> list)117   void set_exec_script_whitelist(std::unique_ptr<SourceFileSet> list) {
118     exec_script_whitelist_ = std::move(list);
119   }
120 
121  private:
122   Label root_target_label_;
123   base::FilePath dotfile_name_;
124   base::FilePath root_path_;
125   std::string root_path_utf8_;
126   base::FilePath secondary_source_path_;
127   base::FilePath python_path_;
128 
129   SourceFile build_config_file_;
130   SourceFile arg_file_template_path_;
131   SourceDir build_dir_;
132   Args build_args_;
133 
134   ItemDefinedCallback item_defined_callback_;
135   PrintCallback print_callback_;
136 
137   std::unique_ptr<SourceFileSet> exec_script_whitelist_;
138 
139   DISALLOW_ASSIGN(BuildSettings);
140 };
141 
142 #endif  // TOOLS_GN_BUILD_SETTINGS_H_
143