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 CommandLine;
     24 class InputFile;
     25 class ParseNode;
     26 
     27 extern const char kDotfile_Help[];
     28 
     29 // Base class for code shared between Setup and DependentSetup.
     30 class CommonSetup {
     31  public:
     32   virtual ~CommonSetup();
     33 
     34   // Returns the scheduler. This is virtual since only the main setup has a
     35   // scheduler and the derived ones just store pointers.
     36   virtual Scheduler* GetScheduler() = 0;
     37 
     38   // When true (the default), Run() will check for unresolved dependencies and
     39   // cycles upon completion. When false, such errors will be ignored.
     40   void set_check_for_bad_items(bool s) { check_for_bad_items_ = s; }
     41 
     42   BuildSettings& build_settings() { return build_settings_; }
     43   Builder* builder() { return builder_.get(); }
     44   LoaderImpl* loader() { return loader_.get(); }
     45 
     46  protected:
     47   CommonSetup();
     48   CommonSetup(const CommonSetup& other);
     49 
     50   // Performs the two sets of operations to run the generation before and after
     51   // the message loop is run.
     52   void RunPreMessageLoop();
     53   bool RunPostMessageLoop();
     54 
     55   BuildSettings build_settings_;
     56   scoped_refptr<LoaderImpl> loader_;
     57   scoped_refptr<Builder> builder_;
     58 
     59   bool check_for_bad_items_;
     60 
     61  private:
     62   CommonSetup& operator=(const CommonSetup& other);  // Disallow.
     63 };
     64 
     65 // Helper class to setup the build settings and environment for the various
     66 // commands to run.
     67 class Setup : public CommonSetup {
     68  public:
     69   Setup();
     70   virtual ~Setup();
     71 
     72   // Configures the build for the current command line. On success returns
     73   // true. On failure, prints the error and returns false.
     74   bool DoSetup();
     75 
     76   // Runs the load, returning true on success. On failure, prints the error
     77   // and returns false. This includes both RunPreMessageLoop() and
     78   // RunPostMessageLoop().
     79   bool Run();
     80 
     81   Scheduler& scheduler() { return scheduler_; }
     82 
     83   virtual Scheduler* GetScheduler() OVERRIDE;
     84 
     85  private:
     86   // Fills build arguments. Returns true on success.
     87   bool FillArguments(const CommandLine& cmdline);
     88 
     89   // Fills the root directory into the settings. Returns true on success.
     90   bool FillSourceDir(const CommandLine& cmdline);
     91 
     92   // Fills the python path portion of the command line. On failure, sets
     93   // it to just "python".
     94   void FillPythonPath();
     95 
     96   // Run config file.
     97   bool RunConfigFile();
     98 
     99   bool FillOtherConfig(const CommandLine& cmdline);
    100 
    101   Scheduler scheduler_;
    102 
    103   // These empty settings and toolchain are used to interpret the command line
    104   // and dot file.
    105   BuildSettings empty_build_settings_;
    106   Settings empty_settings_;
    107   Scope dotfile_scope_;
    108 
    109   // State for invoking the dotfile.
    110   base::FilePath dotfile_name_;
    111   scoped_ptr<InputFile> dotfile_input_file_;
    112   std::vector<Token> dotfile_tokens_;
    113   scoped_ptr<ParseNode> dotfile_root_;
    114 
    115   // State for invoking the command line args. We specifically want to keep
    116   // this around for the entire run so that Values can blame to the command
    117   // line when we issue errors about them.
    118   scoped_ptr<InputFile> args_input_file_;
    119   std::vector<Token> args_tokens_;
    120   scoped_ptr<ParseNode> args_root_;
    121 
    122   DISALLOW_COPY_AND_ASSIGN(Setup);
    123 };
    124 
    125 // A dependent setup allows one to do more than one build at a time. You would
    126 // make a dependent setup which clones the state of the main one, make any
    127 // necessary changes, and then run it.
    128 //
    129 // The way to run both at the same time is:
    130 //   dependent_setup.RunPreMessageLoop();
    131 //   main_setup.Run();
    132 //   dependent_setup.RunPostMessageLoop();
    133 // so that the main setup executes the message loop, but both are run.
    134 class DependentSetup : public CommonSetup {
    135  public:
    136   // Note: this could be one function that takes a CommonSetup*, but then
    137   // the compiler can get confused what to call, since it also matches the
    138   // default copy constructor.
    139   DependentSetup(Setup* derive_from);
    140   DependentSetup(DependentSetup* derive_from);
    141   virtual ~DependentSetup();
    142 
    143   // These are the two parts of Run() in the regular setup, not including the
    144   // call to actually run the message loop.
    145   void RunPreMessageLoop();
    146   bool RunPostMessageLoop();
    147 
    148   virtual Scheduler* GetScheduler() OVERRIDE;
    149 
    150  private:
    151   Scheduler* scheduler_;
    152 };
    153 
    154 #endif  // TOOLS_GN_SETUP_H_
    155