• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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_METADATA_H_
6 #define TOOLS_GN_METADATA_H_
7 
8 #include <memory>
9 
10 #include "gn/build_settings.h"
11 #include "gn/scope.h"
12 #include "gn/source_dir.h"
13 
14 extern const char kMetadata_Help[];
15 
16 // Metadata about a particular target.
17 //
18 // Metadata is a collection of keys and values relating to a particular target.
19 // Generally, these keys will include three categories of strings: ordinary
20 // strings, filenames intended to be rebased according to their particular
21 // source directory, and target labels intended to be used as barriers to the
22 // walk. Verification of these categories occurs at walk time, not creation
23 // time (since it is not clear until the walk which values are intended for
24 // which purpose).
25 //
26 // Represented as a scope in the expression language, here it is reduced to just
27 // the KeyValueMap (since it doesn't need the logical overhead of a full scope).
28 // Values must be lists of strings, as the walking collection logic contatenates
29 // their values across targets.
30 class Metadata {
31  public:
32   using Contents = Scope::KeyValueMap;
33 
34   Metadata() = default;
35 
origin()36   const ParseNode* origin() const { return origin_; }
set_origin(const ParseNode * origin)37   void set_origin(const ParseNode* origin) { origin_ = origin; }
38 
39   // The contents of this metadata variable.
contents()40   const Contents& contents() const { return contents_; }
contents()41   Contents& contents() { return contents_; }
set_contents(Contents && contents)42   void set_contents(Contents&& contents) { contents_ = std::move(contents); }
43 
44   // The relative source directory to use when rebasing.
source_dir()45   const SourceDir& source_dir() const { return source_dir_; }
source_dir()46   SourceDir& source_dir() { return source_dir_; }
set_source_dir(const SourceDir & d)47   void set_source_dir(const SourceDir& d) { source_dir_ = d; }
48 
49   // Collect the specified metadata from this instance.
50   //
51   // Calling this will populate `next_walk_keys` with the values of targets to
52   // be walked next (with the empty string "" indicating that the target should
53   // walk all of its deps and data_deps).
54   bool WalkStep(const BuildSettings* settings,
55                 const std::vector<std::string>& keys_to_extract,
56                 const std::vector<std::string>& keys_to_walk,
57                 const SourceDir& rebase_dir,
58                 std::vector<Value>* next_walk_keys,
59                 std::vector<Value>* result,
60                 Err* err) const;
61 
62  private:
63   const ParseNode* origin_ = nullptr;
64   Contents contents_;
65   SourceDir source_dir_;
66 
67   std::pair<Value, bool> RebaseValue(const BuildSettings* settings,
68                                      const SourceDir& rebase_dir,
69                                      const Value& value,
70                                      Err* err) const;
71 
72   std::pair<Value, bool> RebaseStringValue(const BuildSettings* settings,
73                                            const SourceDir& rebase_dir,
74                                            const Value& value,
75                                            Err* err) const;
76 
77   std::pair<Value, bool> RebaseListValue(const BuildSettings* settings,
78                                          const SourceDir& rebase_dir,
79                                          const Value& value,
80                                          Err* err) const;
81 
82   std::pair<Value, bool> RebaseScopeValue(const BuildSettings* settings,
83                                           const SourceDir& rebase_dir,
84                                           const Value& value,
85                                           Err* err) const;
86 
87   Metadata(const Metadata&) = delete;
88   Metadata& operator=(const Metadata&) = delete;
89 };
90 
91 #endif  // TOOLS_GN_METADATA_H_
92