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.
5 #ifndef TOOLS_GN_SETUP_H_
6 #define TOOLS_GN_SETUP_H_
10 #include "base/basictypes.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "tools/gn/build_settings.h"
14 #include "tools/gn/builder.h"
15 #include "tools/gn/label_pattern.h"
16 #include "tools/gn/loader.h"
17 #include "tools/gn/scheduler.h"
18 #include "tools/gn/scope.h"
19 #include "tools/gn/settings.h"
20 #include "tools/gn/token.h"
21 #include "tools/gn/toolchain.h"
30 extern const char kDotfile_Help
[];
32 // Helper class to setup the build settings and environment for the various
39 // Configures the build for the current command line. On success returns
40 // true. On failure, prints the error and returns false.
42 // The parameter is the string the user specified for the build directory. We
43 // will try to interpret this as a SourceDir if possible, and will fail if is
46 // With force_create = false, setup will fail if the build directory doesn't
47 // alreay exist with an args file in it. With force_create set to true, the
48 // directory will be created if necessary. Commands explicitly doing
49 // generation should set this to true to create it, but querying commands
50 // should set it to false to prevent creating oddly-named directories in case
51 // the user omits the build directory argument (which is easy to do).
52 bool DoSetup(const std::string
& build_dir
, bool force_create
);
54 // Runs the load, returning true on success. On failure, prints the error
55 // and returns false. This includes both RunPreMessageLoop() and
56 // RunPostMessageLoop().
59 Scheduler
& scheduler() { return scheduler_
; }
61 // Returns the file used to store the build arguments. Note that the path
63 SourceFile
GetBuildArgFile() const;
65 // Sets whether the build arguments should be filled during setup from the
66 // command line/build argument file. This will be true by default. The use
67 // case for setting it to false is when editing build arguments, we don't
68 // want to rely on them being valid.
69 void set_fill_arguments(bool fa
) { fill_arguments_
= fa
; }
71 // After a successful run, setting this will additionally cause the public
72 // headers to be checked. Defaults to false.
73 void set_check_public_headers(bool s
) {
74 check_public_headers_
= s
;
77 // Read from the .gn file, these are the targets to check. If the .gn file
78 // does not specify anything, this will be null. If the .gn file specifies
79 // the empty list, this will be non-null but empty.
80 const std::vector
<LabelPattern
>* check_patterns() const {
81 return check_patterns_
.get();
84 BuildSettings
& build_settings() { return build_settings_
; }
85 Builder
* builder() { return builder_
.get(); }
86 LoaderImpl
* loader() { return loader_
.get(); }
88 // Name of the file in the root build directory that contains the build
90 static const char kBuildArgFileName
[];
93 // Performs the two sets of operations to run the generation before and after
94 // the message loop is run.
95 void RunPreMessageLoop();
96 bool RunPostMessageLoop();
98 // Fills build arguments. Returns true on success.
99 bool FillArguments(const base::CommandLine
& cmdline
);
101 // Fills the build arguments from the command line or from the build arg file.
102 bool FillArgsFromCommandLine(const std::string
& args
);
103 bool FillArgsFromFile();
105 // Given an already-loaded args_input_file_, parses and saves the resulting
106 // arguments. Backend for the different FillArgs variants.
107 bool FillArgsFromArgsInputFile();
109 // Writes the build arguments to the build arg file.
110 bool SaveArgsToFile();
112 // Fills the root directory into the settings. Returns true on success.
113 bool FillSourceDir(const base::CommandLine
& cmdline
);
115 // Fills the build directory given the value the user has specified.
116 // Must happen after FillSourceDir so we can resolve source-relative
117 // paths. If require_exists is false, it will fail if the dir doesn't exist.
118 bool FillBuildDir(const std::string
& build_dir
, bool require_exists
);
120 // Fills the python path portion of the command line. On failure, sets
121 // it to just "python".
122 void FillPythonPath();
125 bool RunConfigFile();
127 bool FillOtherConfig(const base::CommandLine
& cmdline
);
129 BuildSettings build_settings_
;
130 scoped_refptr
<LoaderImpl
> loader_
;
131 scoped_refptr
<Builder
> builder_
;
133 SourceFile root_build_file_
;
135 bool check_public_headers_
;
137 // See getter for info.
138 scoped_ptr
<std::vector
<LabelPattern
>> check_patterns_
;
140 Scheduler scheduler_
;
142 // These settings and toolchain are used to interpret the command line and
144 Settings dotfile_settings_
;
145 Scope dotfile_scope_
;
147 // State for invoking the dotfile.
148 base::FilePath dotfile_name_
;
149 scoped_ptr
<InputFile
> dotfile_input_file_
;
150 std::vector
<Token
> dotfile_tokens_
;
151 scoped_ptr
<ParseNode
> dotfile_root_
;
153 // Set to true when we should populate the build arguments from the command
154 // line or build argument file. See setter above.
155 bool fill_arguments_
;
157 // State for invoking the command line args. We specifically want to keep
158 // this around for the entire run so that Values can blame to the command
159 // line when we issue errors about them.
160 scoped_ptr
<InputFile
> args_input_file_
;
161 std::vector
<Token
> args_tokens_
;
162 scoped_ptr
<ParseNode
> args_root_
;
164 DISALLOW_COPY_AND_ASSIGN(Setup
);
167 #endif // TOOLS_GN_SETUP_H_