forked from google/cargo-raze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.rs
271 lines (239 loc) · 8.54 KB
/
settings.rs
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// 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 serde_derive::{Deserialize, Serialize};
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>,
/**
* The arguments given to the patch tool.
*
* Defaults to `-p0`, however `-p1` will usually be needed for patches generated by git.
*
* If multiple `-p` arguments are specified, the last one will take effect.
* If arguments other than `-p` are specified, Bazel will fall back to use patch command line
* tool instead of the Bazel-native patch implementation.
*
* When falling back to `patch` command line tool and `patch_tool` attribute is not specified,
* `patch` will be used.
*/
#[serde(default)]
pub patch_args: Vec<String>,
/**
* Sequence of Bash commands to be applied on Linux/Macos after patches are applied.
*/
#[serde(default)]
pub patch_cmds: Vec<String>,
/**
* Sequence of Powershell commands to be applied on Windows after patches are applied.
*
* If this attribute is not set, patch_cmds will be executed on Windows, which requires Bash
* binary to exist.
*/
#[serde(default)]
pub patch_cmds_win: Vec<String>,
/**
* The `patch(1)` utility to use.
*
* If this is specified, Bazel will use the specifed patch tool instead of the Bazel-native patch
* implementation.
*/
#[serde(default)]
pub patch_tool: Option<String>,
/**
* A list of files that are to be applied as patches after extracting the archive.
*
* By default, it uses the Bazel-native patch implementation which doesn't support fuzz match and
* binary patch, but Bazel will fall back to use patch command line tool if `patch_tool`
* attribute is specified or there are arguments other than `-p` in `patch_args` attribute.
*/
#[serde(default)]
pub patches: Vec<String>,
/**
* Path to a file to be included as part of the generated BUILD file.
*
* For example, some crates include non-Rust code typically built through a build.rs script. They
* can be made compatible by manually writing appropriate Bazel targets, and including them into
* the crate through a combination of additional_build_file and additional_deps.
*/
#[serde(default)]
pub additional_build_file: Option<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() -> Self {
Self {
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(),
patch_args: Vec::new(),
patch_cmds: Vec::new(),
patch_cmds_win: Vec::new(),
patch_tool: None,
patches: Vec::new(),
additional_build_file: None,
}
}
}
#[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
}