• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 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_RUST_TARGET_VALUES_H_
6 #define TOOLS_GN_RUST_TARGET_VALUES_H_
7 
8 #include <map>
9 
10 #include "base/containers/flat_map.h"
11 #include "gn/inherited_libraries.h"
12 #include "gn/label.h"
13 #include "gn/source_file.h"
14 
15 // Holds the values (outputs, args, script name, etc.) for either an action or
16 // an action_foreach target.
17 class RustValues {
18  public:
19   RustValues();
20   ~RustValues();
21 
22   // Library crate types.
23   //
24   // The default value CRATE_AUTO means the type should be deduced from the
25   // target type (see InferredCrateType() below).
26   //
27   // Shared library crate types must be specified explicitly, all other target
28   // types can be deduced.
29   enum CrateType {
30     CRATE_AUTO = 0,
31     CRATE_BIN,
32     CRATE_CDYLIB,
33     CRATE_DYLIB,
34     CRATE_PROC_MACRO,
35     CRATE_RLIB,
36     CRATE_STATICLIB,
37   };
38 
39   // Name of this crate.
crate_name()40   std::string& crate_name() { return crate_name_; }
crate_name()41   const std::string& crate_name() const { return crate_name_; }
42 
43   // Main source file for this crate.
crate_root()44   const SourceFile& crate_root() const { return crate_root_; }
set_crate_root(SourceFile & s)45   void set_crate_root(SourceFile& s) { crate_root_ = s; }
46 
47   // Crate type for compilation.
crate_type()48   CrateType crate_type() const { return crate_type_; }
set_crate_type(CrateType s)49   void set_crate_type(CrateType s) { crate_type_ = s; }
50 
51   // Same as crate_type(), except attempt to resolve CRATE_AUTO based on the
52   // target type.
53   //
54   // Dylib and cdylib targets should call set_crate_type(CRATE_[C]DYLIB)
55   // explicitly to resolve ambiguity. For shared libraries, this assumes
56   // CRATE_DYLIB by default.
57   //
58   // For unsupported target types and targets without Rust sources,
59   // returns CRATE_AUTO.
60   static CrateType InferredCrateType(const Target* target);
61 
62   // Returns whether this target is a Rust rlib, dylib, or proc macro.
63   //
64   // Notably, this does not include staticlib or cdylib targets that have Rust
65   // source, because they look like native libraries to the Rust compiler.
66   //
67   // It does include proc_macro targets, which are sometimes a special case.
68   // (TODO: Should it?)
69   static bool IsRustLibrary(const Target* target);
70 
71   // Any renamed dependencies for the `extern` flags.
aliased_deps()72   const std::map<Label, std::string>& aliased_deps() const {
73     return aliased_deps_;
74   }
aliased_deps()75   std::map<Label, std::string>& aliased_deps() { return aliased_deps_; }
76 
77  private:
78   std::string crate_name_;
79   SourceFile crate_root_;
80   CrateType crate_type_ = CRATE_AUTO;
81   std::map<Label, std::string> aliased_deps_;
82 
83   RustValues(const RustValues&) = delete;
84   RustValues& operator=(const RustValues&) = delete;
85 };
86 
87 #endif  // TOOLS_GN_RUST_TARGET_VALUES_H_
88