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_ARGS_H_
      6 #define TOOLS_GN_ARGS_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/containers/hash_tables.h"
     10 #include "base/synchronization/lock.h"
     11 #include "tools/gn/err.h"
     12 #include "tools/gn/scope.h"
     13 
     14 extern const char kBuildArgs_Help[];
     15 
     16 // Manages build arguments. It stores the global arguments specified on the
     17 // command line, and sets up the root scope with the proper values.
     18 //
     19 // This class tracks accesses so we can report errors about unused variables.
     20 // The use case is if the user specifies an override on the command line, but
     21 // no buildfile actually uses that variable. We want to be able to report that
     22 // the argument was unused.
     23 class Args {
     24  public:
     25   Args();
     26   Args(const Args& other);
     27   ~Args();
     28 
     29   // Specifies overrides of the build arguments. These are normally specified
     30   // on the command line.
     31   void AddArgOverride(const char* name, const Value& value);
     32   void AddArgOverrides(const Scope::KeyValueMap& overrides);
     33 
     34   // Sets up the root scope for a toolchain. This applies the default system
     35   // flags, then any overrides stored in this object, then applies any
     36   // toolchain overrides specified in the argument.
     37   void SetupRootScope(Scope* dest,
     38                       const Scope::KeyValueMap& toolchain_overrides) const;
     39 
     40   // Sets up the given scope with arguments passed in.
     41   //
     42   // If the values specified in the args are not already set, the values in
     43   // the args list will be used (which are assumed to be the defaults), but
     44   // they will not override the system defaults or the current overrides.
     45   //
     46   // All args specified in the input will be marked as "used".
     47   //
     48   // On failure, the err will be set and it will return false.
     49   bool DeclareArgs(const Scope::KeyValueMap& args,
     50                    Scope* scope_to_set,
     51                    Err* err) const;
     52 
     53   // Checks to see if any of the overrides ever used were never declared as
     54   // arguments. If there are, this returns false and sets the error.
     55   bool VerifyAllOverridesUsed(Err* err) const;
     56 
     57   // This function is not threadsafe, it must only be used when
     58   // single-threaded. It's used to implement the "args" command.
     59   const Scope::KeyValueMap& declared_arguments() const {
     60     return declared_arguments_;
     61   }
     62 
     63  private:
     64   // Sets the default config based on the current system.
     65   void SetSystemVars(Scope* scope) const;
     66 
     67   // Sets the given vars on the given scope.
     68   void ApplyOverrides(const Scope::KeyValueMap& values, Scope* scope) const;
     69 
     70   void SaveOverrideRecord(const Scope::KeyValueMap& values) const;
     71 
     72   // Since this is called during setup which we assume is single-threaded,
     73   // this is not protected by the lock. It should be set only during init.
     74   Scope::KeyValueMap overrides_;
     75 
     76   mutable base::Lock lock_;
     77 
     78   // Maintains a list of all overrides we've ever seen. This is the main
     79   // |overrides_| as well as toolchain overrides. Tracking this allows us to
     80   // check for overrides that were specified but never used.
     81   mutable Scope::KeyValueMap all_overrides_;
     82 
     83   // Tracks all variables declared in any buildfile. This is so we can see if
     84   // the user set variables on the command line that are not used anywhere.
     85   mutable Scope::KeyValueMap declared_arguments_;
     86 
     87   Args& operator=(const Args& other);  // Disallow assignment.
     88 };
     89 
     90 #endif  // TOOLS_GN_ARGS_H_
     91