-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathrust.gni
364 lines (324 loc) · 10 KB
/
rust.gni
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
declare_args() {
# Absolute path of rust build files.
rust_build = "//build_extra/rust/"
# Wrapper executable for rustc invocations. This can be used for a caching
# utility, e.g. sccache.
rustc_wrapper = ""
# treat the warnings in rust files as errors
rust_treat_warnings_as_errors = true
}
if (is_linux) {
executable_suffix = ""
shared_lib_prefix = "lib"
shared_lib_suffix = ".so"
} else if (is_mac) {
executable_suffix = ""
shared_lib_prefix = "lib"
shared_lib_suffix = ".dylib"
} else if (is_win) {
executable_suffix = ".exe"
shared_lib_prefix = ""
shared_lib_suffix = ".dll"
} else {
assert(false, "Unsupported platform")
}
# To simplify transitive dependency management with gn, we build all rust
# crates into the same directory. We need to be careful not to have crates
# with the same name.
out_dir = "$root_out_dir/rust_crates"
# The official way of building Rust executables is to to let rustc do the
# linking. However, we'd prefer to leave it in the hands of gn/ninja:
# * It allows us to use source sets.
# * It allows us to use the bundled lld that Chromium and V8 use.
# * We have more control over build flags.
# * To sidestep rustc weirdness (e.g. on Windows, it always links with the
# release C runtime library, even for debug builds).
#
# The `get_rustc_info` tool outputs the linker flags that are needed to
# successfully link rustc object code into an executable.
# We generate two sets of ldflags:
# `ldflags_bin` : Used for rust_executable targets.
# `ldflags_test`: Used for rust_test targets; includes the test harness.
#
# The tool works by compiling and linking something with rustc, and analyzing
# the arguments it passes to the system linker. That's what dummy.rs is for.
_rustc_info = exec_script("get_rustc_info.py", [], "json")
template("_rust_crate") {
config_name = "${target_name}_config"
action_name = "${target_name}_rustc"
forward_variables_from(invoker,
[
"cfg",
"crate_name",
"crate_type",
"crate_version",
"deps",
"edition",
"inputs",
"features",
"is_test",
"libs",
"source_root",
"testonly",
"treat_warnings_as_errors",
])
if (!defined(crate_name)) {
crate_name = target_name
}
if (!defined(crate_type)) {
crate_type = "rlib"
}
if (!defined(deps)) {
deps = []
}
if (!defined(edition)) {
edition = "2018"
}
if (!defined(is_test)) {
is_test = false
}
if (!defined(libs)) {
libs = []
}
if (!defined(treat_warnings_as_errors)) {
# Use global setting if not explicitly specified for this target.
treat_warnings_as_errors = rust_treat_warnings_as_errors
}
if (defined(crate_version)) {
# In our build setup, all crates are built in the same directory. To avoid
# file name conflicts between when multiple versions of the same crate are
# built, add a unique suffix to output file names.
# Unfortunately the version number as such can't be used directly:
# everything after the first dot (.) is thrown away by rust, so in case of
# foo-0.2 vs foo-0.3 only the first '0' would be used, and conflicts would
# still occur. Therefore we use a hash of the version number instead.
crate_suffix = exec_script("//tools/sha256sum.py",
[
"--input=$crate_version",
"--format=-%.8s",
],
"trim string")
} else {
# Of most crates we use only one version; no need for all this difficulty.
crate_suffix = ""
}
if (crate_type == "bin") {
out_file = "$crate_name$crate_suffix.o"
emit_type = "obj"
} else if (crate_type == "proc-macro") {
out_file = "$shared_lib_prefix$crate_name$crate_suffix$shared_lib_suffix"
emit_type = "link"
} else if (crate_type == "rlib") {
out_file = "lib$crate_name$crate_suffix.rlib"
emit_type = "link"
}
out_path = "$out_dir/$out_file"
# Merge `invoker.extern` and `invoker.extern_rlib` into a single list.
extern = []
if (defined(invoker.extern)) {
extern += invoker.extern
}
if (defined(invoker.extern_rlib)) {
foreach(extern_crate_name, invoker.extern_rlib) {
extern += [
{
label = "$rust_build:$extern_crate_name"
crate_name = extern_crate_name
crate_type = "rlib"
},
]
}
}
# Add output file info to every entry in the 'extern' list.
extern_outputs = []
foreach(info, extern) {
extern_outputs += [
{
label = info.label
crate_name = info.crate_name
crate_type = info.crate_type
if (!defined(info.crate_version)) {
crate_suffix = ""
} else {
crate_version = info.crate_version
crate_suffix = exec_script("//tools/sha256sum.py",
[
"--input=$crate_version",
"--format=-%.8s",
],
"trim string")
}
if (crate_type == "rlib") {
out_file = "lib$crate_name$crate_suffix.rlib"
} else if (info.crate_type == "proc_macro") {
out_file =
"$shared_lib_prefix$crate_name$crate_suffix$shared_lib_suffix"
}
out_path = "$out_dir/$out_file"
},
]
}
config(config_name) {
foreach(info, extern_outputs) {
if (info.crate_type == "rlib") {
libs += [ info.out_path ]
}
}
lib_dirs = [ out_dir ]
}
source_set(target_name) {
public_deps = [
":$action_name",
]
libs += [ out_path ]
all_dependent_configs = [ ":$config_name" ]
}
action(action_name) {
script = "//build_extra/rust/run.py"
sources = [
source_root,
]
outputs = [
out_path,
]
depfile = "$out_dir/$crate_name$crate_suffix.d"
if (rustc_wrapper != "") {
args = [ rustc_wrapper ]
} else {
args = []
}
args += [
"rustc",
rebase_path(source_root, root_build_dir),
"--crate-name=$crate_name",
"--crate-type=$crate_type",
"--emit=$emit_type,dep-info",
"--edition=$edition",
"--out-dir=" + rebase_path(out_dir, root_build_dir),
# This is to disambiguate multiple versions of the same crate.
"-Cextra-filename=$crate_suffix",
# Appending the rustc version to the crate metadata ensures that they are
# rebuilt when rustc is upgraded, by changing the command line.
"-Cmetadata=\"${crate_suffix}_${_rustc_info.version}\"",
# This is needed for transitive dependencies.
"-L",
"dependency=" + rebase_path(out_dir, root_build_dir),
# Use colorful output even if stdout is redirected and not a tty.
"--color=always",
]
if (defined(crate_version)) {
args += [
# This is used to set env variables for Cargo build compatibility
"--cargo-pkg-version=$crate_version",
]
}
if (is_win) {
# Proc-macro crates need to be linked by rustc itself, because rustc
# doesn't expose all the information necessary to produce the correct
# linker invocation ourselves. However gn's setup creates an environment
# where link.exe doesn't always work, so we direct rustc to use lld-link,
# and explicitly load the proper environment that makes it work in run.py.
args += [
"-Clinker-flavor=lld-link",
"-Clinker=" + rebase_path(
"//third_party/llvm-build/Release+Asserts/bin/lld-link.exe",
root_build_dir),
]
}
if (is_debug) {
args += [ "-g" ]
}
if (is_official_build) {
args += [
"-C",
"opt-level=3",
]
}
if (is_test) {
args += [ "--test" ]
}
if (treat_warnings_as_errors) {
args += [ "-Dwarnings" ]
}
if (defined(invoker.args)) {
args += invoker.args
}
if (defined(cfg)) {
foreach(c, cfg) {
args += [
"--cfg",
c,
]
}
}
if (defined(features)) {
foreach(f, features) {
args += [
"--cfg",
"feature=\"" + f + "\"",
]
}
}
# Build the list of '--extern' arguments from the 'extern_outputs' array.
foreach(info, extern_outputs) {
args += [
"--extern",
info.crate_name + "=" + rebase_path(info.out_path, root_build_dir),
]
sources += [ info.out_path ]
deps += [ info.label ]
}
}
}
template("rust_rlib") {
_rust_crate(target_name) {
forward_variables_from(invoker, "*")
crate_type = "rlib"
}
}
template("rust_proc_macro") {
_rust_crate(target_name) {
forward_variables_from(invoker, "*")
crate_type = "proc-macro"
}
}
template("rust_executable") {
bin_name = target_name + "_bin"
bin_label = ":" + bin_name
_rust_crate(bin_name) {
crate_type = "bin"
forward_variables_from(invoker, "*")
}
executable(target_name) {
forward_variables_from(invoker, "*")
if (defined(is_test) && is_test) {
ldflags = _rustc_info.ldflags_test
} else {
ldflags = _rustc_info.ldflags_bin
}
if (!defined(deps)) {
deps = []
}
deps += [ bin_label ]
if (defined(extern)) {
foreach(info, extern) {
if (info.crate_type == "rlib") {
deps += [ info.label ]
}
}
}
if (defined(extern_rlib)) {
foreach(extern_crate_name, extern_rlib) {
deps += [ "$rust_build:$extern_crate_name" ]
}
}
}
}
template("rust_test") {
rust_executable(target_name) {
forward_variables_from(invoker, "*")
is_test = true
testonly = true
}
}