Home | History | Annotate | Download | only in gn
      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_SETUP_H_
      6 #define TOOLS_GN_SETUP_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/files/file_path.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "tools/gn/build_settings.h"
     15 #include "tools/gn/builder.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"
     22 
     23 class InputFile;
     24 class ParseNode;
     25 
     26 namespace base {
     27 class CommandLine;
     28 }
     29 
     30 extern const char kDotfile_Help[];
     31 
     32 // Base class for code shared between Setup and DependentSetup.
     33 class CommonSetup {
     34  public:
     35   virtual ~CommonSetup();
     36 
     37   // Returns the scheduler. This is virtual since only the main setup has a
     38   // scheduler and the derived ones just store pointers.
     39   virtual Scheduler* GetScheduler() = 0;
     40 
     41   // When true (the default), Run() will check for unresolved dependencies and
     42   // cycles upon completion. When false, such errors will be ignored.
     43   void set_check_for_bad_items(bool s) { check_for_bad_items_ = s; }
     44 
     45   // When true (the default), RunPostMessageLoop will check for overrides that
     46   // were specified but not used. When false, such errors will be ignored.
     47   void set_check_for_unused_overrides(bool s) {
     48     check_for_unused_overrides_ = s;
     49   }
     50 
     51   // After a successful run, setting this will additionally cause the public
     52   // headers to be checked. Defaults to false.
     53   void set_check_public_headers(bool s) {
     54     check_public_headers_ = s;
     55   }
     56 
     57   BuildSettings& build_settings() { return build_settings_; }
     58   Builder* builder() { return builder_.get(); }
     59   LoaderImpl* loader() { return loader_.get(); }
     60 
     61   // Name of the file in the root build directory that contains the build
     62   // arguements.
     63   static const char kBuildArgFileName[];
     64 
     65  protected:
     66   CommonSetup();
     67   CommonSetup(const CommonSetup& other);
     68 
     69   // Performs the two sets of operations to run the generation before and after
     70   // the message loop is run.
     71   void RunPreMessageLoop();
     72   bool RunPostMessageLoop();
     73 
     74   BuildSettings build_settings_;
     75   scoped_refptr<LoaderImpl> loader_;
     76   scoped_refptr<Builder> builder_;
     77 
     78   SourceFile root_build_file_;
     79 
     80   bool check_for_bad_items_;
     81   bool check_for_unused_overrides_;
     82   bool check_public_headers_;
     83 
     84  private:
     85   CommonSetup& operator=(const CommonSetup& other);  // Disallow.
     86 };
     87 
     88 // Helper class to setup the build settings and environment for the various
     89 // commands to run.
     90 class Setup : public CommonSetup {
     91  public:
     92   Setup();
     93   virtual ~Setup();
     94 
     95   // Configures the build for the current command line. On success returns
     96   // true. On failure, prints the error and returns false.
     97   //
     98   // The parameter is the string the user specified for the build directory. We
     99   // will try to interpret this as a SourceDir if possible, and will fail if is
    100   // is malformed.
    101   //
    102   // With force_create = false, setup will fail if the build directory doesn't
    103   // alreay exist with an args file in it. With force_create set to true, the
    104   // directory will be created if necessary. Commands explicitly doing
    105   // generation should set this to true to create it, but querying commands
    106   // should set it to false to prevent creating oddly-named directories in case
    107   // the user omits the build directory argument (which is easy to do).
    108   bool DoSetup(const std::string& build_dir, bool force_create);
    109 
    110   // Runs the load, returning true on success. On failure, prints the error
    111   // and returns false. This includes both RunPreMessageLoop() and
    112   // RunPostMessageLoop().
    113   bool Run();
    114 
    115   Scheduler& scheduler() { return scheduler_; }
    116 
    117   virtual Scheduler* GetScheduler() OVERRIDE;
    118 
    119   // Returns the file used to store the build arguments. Note that the path
    120   // might not exist.
    121   SourceFile GetBuildArgFile() const;
    122 
    123   // Sets whether the build arguments should be filled during setup from the
    124   // command line/build argument file. This will be true by default. The use
    125   // case for setting it to false is when editing build arguments, we don't
    126   // want to rely on them being valid.
    127   void set_fill_arguments(bool fa) { fill_arguments_ = fa; }
    128 
    129  private:
    130   // Fills build arguments. Returns true on success.
    131   bool FillArguments(const base::CommandLine& cmdline);
    132 
    133   // Fills the build arguments from the command line or from the build arg file.
    134   bool FillArgsFromCommandLine(const std::string& args);
    135   bool FillArgsFromFile();
    136 
    137   // Given an already-loaded args_input_file_, parses and saves the resulting
    138   // arguments. Backend for the different FillArgs variants.
    139   bool FillArgsFromArgsInputFile();
    140 
    141   // Writes the build arguments to the build arg file.
    142   bool SaveArgsToFile();
    143 
    144   // Fills the root directory into the settings. Returns true on success.
    145   bool FillSourceDir(const base::CommandLine& cmdline);
    146 
    147   // Fills the build directory given the value the user has specified.
    148   // Must happen after FillSourceDir so we can resolve source-relative
    149   // paths. If require_exists is false, it will fail if the dir doesn't exist.
    150   bool FillBuildDir(const std::string& build_dir, bool require_exists);
    151 
    152   // Fills the python path portion of the command line. On failure, sets
    153   // it to just "python".
    154   void FillPythonPath();
    155 
    156   // Run config file.
    157   bool RunConfigFile();
    158 
    159   bool FillOtherConfig(const base::CommandLine& cmdline);
    160 
    161   Scheduler scheduler_;
    162 
    163   // These empty settings and toolchain are used to interpret the command line
    164   // and dot file.
    165   BuildSettings empty_build_settings_;
    166   Settings empty_settings_;
    167   Scope dotfile_scope_;
    168 
    169   // State for invoking the dotfile.
    170   base::FilePath dotfile_name_;
    171   scoped_ptr<InputFile> dotfile_input_file_;
    172   std::vector<Token> dotfile_tokens_;
    173   scoped_ptr<ParseNode> dotfile_root_;
    174 
    175   // Set to true when we should populate the build arguments from the command
    176   // line or build argument file. See setter above.
    177   bool fill_arguments_;
    178 
    179   // State for invoking the command line args. We specifically want to keep
    180   // this around for the entire run so that Values can blame to the command
    181   // line when we issue errors about them.
    182   scoped_ptr<InputFile> args_input_file_;
    183   std::vector<Token> args_tokens_;
    184   scoped_ptr<ParseNode> args_root_;
    185 
    186   DISALLOW_COPY_AND_ASSIGN(Setup);
    187 };
    188 
    189 // A dependent setup allows one to do more than one build at a time. You would
    190 // make a dependent setup which clones the state of the main one, make any
    191 // necessary changes, and then run it.
    192 //
    193 // The way to run both at the same time is:
    194 //   dependent_setup.RunPreMessageLoop();
    195 //   main_setup.Run();
    196 //   dependent_setup.RunPostMessageLoop();
    197 // so that the main setup executes the message loop, but both are run.
    198 class DependentSetup : public CommonSetup {
    199  public:
    200   // Note: this could be one function that takes a CommonSetup*, but then
    201   // the compiler can get confused what to call, since it also matches the
    202   // default copy constructor.
    203   DependentSetup(Setup* derive_from);
    204   DependentSetup(DependentSetup* derive_from);
    205   virtual ~DependentSetup();
    206 
    207   // These are the two parts of Run() in the regular setup, not including the
    208   // call to actually run the message loop.
    209   void RunPreMessageLoop();
    210   bool RunPostMessageLoop();
    211 
    212   virtual Scheduler* GetScheduler() OVERRIDE;
    213 
    214  private:
    215   Scheduler* scheduler_;
    216 };
    217 
    218 #endif  // TOOLS_GN_SETUP_H_
    219