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