forked from google/cargo-raze
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.rs
More file actions
205 lines (179 loc) · 6.28 KB
/
Copy pathsettings.rs
File metadata and controls
205 lines (179 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
pub type CrateSettingsPerVersion = HashMap<String, CrateSettings>;
/**
* A "deserializable struct" for the whole Cargo.toml
*
* Contains only `raze` settings, (we drop everything else in the toml on the floor).
*/
#[derive(Debug, Clone, Deserialize)]
pub struct CargoToml {
pub raze: RazeSettings,
}
/** The configuration settings for `cargo-raze`, included in Cargo.toml. */
#[derive(Debug, Clone, Deserialize)]
pub struct RazeSettings {
/**
* The path to the Cargo.toml working directory.
*/
pub workspace_path: String,
/**
* The platform target to generate BUILD rules for.
*
* This comes in the form of a "triple", such as "x86_64-unknown-linux-gnu"
*/
#[serde(default = "default_raze_settings_field_target")]
pub target: String,
/** Any crate-specific configuration. See CrateSetings for details. */
#[serde(default)]
pub crates: HashMap<String, CrateSettingsPerVersion>,
/**
* Prefix for generated Bazel workspaces (from workspace_rules)
*
* This is only useful with remote genmode. It prefixes the names of the workspaces for
* dependencies (@PREFIX_crateName_crateVersion) as well as the name of the repository function
* generated in crates.bzl (PREFIX_fetch_remote_crates()).
*
* TODO(acmcarther): Does this have a non-bazel analogue?
*/
#[serde(default = "default_raze_settings_field_gen_workspace_prefix")]
pub gen_workspace_prefix: String,
/** How to generate the dependencies. See GenMode for details. */
#[serde(default = "default_raze_settings_field_genmode")]
pub genmode: GenMode,
/**
* Suffix for generated build files, uses whole value for Vendored mode
* Default: BUILD
*/
#[serde(default = "default_raze_settings_field_output_buildfile_suffix")]
pub output_buildfile_suffix: String,
}
/** Override settings for individual crates (as part of RazeSettings). */
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct CrateSettings {
/**
* Dependencies to be added to a crate.
*
* Importantly, the format of dependency references depends on the genmode.
* Remote: @{gen_workspace_prefix}__{dep_name}__{dep_version_sanitized}/:{dep_name}
* Vendored: //{workspace_path}/vendor/{dep_name}-{dep_version}:{dep_name}
*
* In addition, the added deps must be accessible from a remote workspace under Remote GenMode.
* Usually, this means they _also_ need to be remote, but a "local" build path prefixed with
* "@", in the form "@//something_local" may work.
*/
#[serde(default)]
pub additional_deps: Vec<String>,
/**
* Dependencies to be removed from a crate, in the form "{dep-name}-{dep-version}"
*
* This is applied during Cargo analysis, so it uses Cargo-style labeling
*/
#[serde(default)]
pub skipped_deps: Vec<String>,
/**
* Library targets that should be aliased in the root BUILD file.
*
* This is useful to facilitate using binary utility crates, such as bindgen, as part of genrules.
*/
#[serde(default)]
pub extra_aliased_targets: Vec<String>,
/** Flags to be added to the crate compilation process, in the form "--flag". */
#[serde(default)]
pub additional_flags: Vec<String>,
/**
* Whether or not to generate the build script that goes with this crate.
*
* Many build scripts will not function, as they will still be built hermetically. However, build
* scripts that merely generate files into OUT_DIR may be fully functional.
*/
#[serde(default = "default_crate_settings_field_gen_buildrs")]
pub gen_buildrs: bool,
/**
* The verbatim `data` clause to be included for the generated build targets.
*
* N.B. Build scripts are always provided all crate files for their `data` attr.
*/
#[serde(default = "default_crate_settings_field_data_attr")]
pub data_attr: Option<String>,
/**
* Additional environment variables to add when running the build script.
*
* Values should looks like "KEY=VALUE".
*/
#[serde(default)]
pub buildrs_additional_environment_variables: Vec<String>
}
/**
* Describes how dependencies should be managed in tree. Options are {Remote, Vendored}.
*
* Remote:
* This mode assumes that files are not locally vendored, and generates a workspace-level
* function that can bring them in.
*
* Vendored:
* This mode assumes that files are vendored (into vendor/), and generates BUILD files
* accordingly
*/
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub enum GenMode {
Vendored,
Remote,
}
impl Default for CrateSettings {
fn default() -> CrateSettings {
CrateSettings {
additional_deps: Vec::new(),
skipped_deps: Vec::new(),
extra_aliased_targets: Vec::new(),
additional_flags: Vec::new(),
gen_buildrs: default_crate_settings_field_gen_buildrs(),
data_attr: default_crate_settings_field_data_attr(),
buildrs_additional_environment_variables: Vec::new(),
}
}
}
#[cfg(test)]
pub mod testing {
use super::*;
pub fn dummy_raze_settings() -> RazeSettings {
RazeSettings {
workspace_path: "//cargo".to_owned(),
target: "x86_64-unknown-linux-gnu".to_owned(),
crates: HashMap::new(),
gen_workspace_prefix: "raze_test".to_owned(),
genmode: GenMode::Remote,
output_buildfile_suffix: "BUILD".to_owned(),
}
}
}
fn default_raze_settings_field_target() -> String {
"x86_64-unknown-linux-gnu".to_owned()
}
fn default_raze_settings_field_gen_workspace_prefix() -> String {
"raze".to_owned()
}
fn default_raze_settings_field_genmode() -> GenMode {
GenMode::Vendored
}
fn default_raze_settings_field_output_buildfile_suffix() -> String {
"BUILD".to_owned()
}
fn default_crate_settings_field_gen_buildrs() -> bool {
false
}
fn default_crate_settings_field_data_attr() -> Option<String> {
None
}