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
|
declare_args() {
# Absolute path of rust build files.
rust_build = "//build_extra/rust/"
}
if (is_win) {
executable_suffix = ".exe"
} else {
executable_suffix = ""
}
# 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_rust_ldflags` tool outputs the linker flags that are needed to
# successfully link rustc object code into an executable.
# We generate two sets of ldflags:
# `rust_bin_ldflags`: Used for rust_executable targets.
# `rust_test_ldflags`: 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.
dummy_rs_path = rebase_path("dummy.rs", root_build_dir)
rust_bin_ldflags =
exec_script("get_rust_ldflags.py", [ dummy_rs_path ], "list lines")
rust_test_ldflags = exec_script("get_rust_ldflags.py",
[
dummy_rs_path,
"--test",
],
"list lines")
template("run_rustc") {
action(target_name) {
assert(defined(invoker.source_root), "Must specify source_root")
forward_variables_from(invoker,
[
"crate_name",
"crate_type",
"crate_version",
"deps",
"extern",
"extern_version",
"features",
"is_test",
"source_root",
"testonly",
])
if (!defined(crate_name)) {
crate_name = target_name
}
if (!defined(is_test)) {
is_test = false
}
sources = [
source_root,
]
outputs = []
script = "//tools/run_rustc.py"
# TODO: We want to apply "-Dwarnings" only when treat_warnings_as_errors is not false
# https://github.com/denoland/deno/pull/379
args = [
rebase_path(source_root, root_build_dir),
"--crate-name=$crate_name",
"--crate-type=$crate_type",
]
if (!is_win) {
args += [ "--color=always" ]
}
if (!defined(crate_version)) {
crate_name_and_version = crate_name
} else {
crate_name_and_version = "$crate_name-$crate_version"
}
if (crate_type == "bin") {
output_file = "$target_out_dir/$crate_name_and_version.o"
emit_type = "obj"
} else if (crate_type == "rlib") {
output_file = "$target_out_dir/lib$crate_name_and_version.rlib"
emit_type = "link"
}
outputs += [ output_file ]
output_file_rel = rebase_path(output_file, root_build_dir)
args += [ "--emit=$emit_type=$output_file_rel" ]
depfile = "$target_out_dir/$crate_name_and_version.d"
args += [
"--emit=dep-info=" + rebase_path(depfile, root_build_dir),
# The following two args are used by run_rustc.py to fix
# the depfile on the fly. They are not passed thru to rustc.
"--depfile=" + rebase_path(depfile, root_build_dir),
"--output_file=" + output_file_rel,
]
if (defined(crate_version)) {
args += [
"-C",
"metadata=$crate_version",
]
}
if (is_debug) {
args += [ "-g" ]
}
if (is_official_build) {
args += [ "-O" ]
}
if (is_test) {
args += [ "--test" ]
}
if (defined(features)) {
foreach(f, features) {
args += [
"--cfg",
"feature=\"" + f + "\"",
]
}
}
if (defined(invoker.args)) {
args += invoker.args
}
if (!defined(deps)) {
deps = []
}
# Convert all 'extern' and 'extern_version' items to a single format.
extern_infos = []
if (defined(extern)) {
foreach(label, extern) {
extern_infos += [
{
label = label
crate_name = get_label_info(label, "name")
crate_name_and_version = crate_name
},
]
}
}
if (defined(extern_version)) {
foreach(info, extern_version) {
extern_infos += [
{
forward_variables_from(info, "*")
crate_name_and_version = "$crate_name-$crate_version"
},
]
}
}
# Build the list of '--extern' arguments from the 'extern_infos' array.
foreach(info, extern_infos) {
dir = get_label_info(info.label, "target_out_dir")
rlib = "$dir/lib${info.crate_name_and_version}.rlib"
args += [
"--extern",
info.crate_name + "=" + rebase_path(rlib, root_build_dir),
# This is needed for transitive dependencies.
"-L",
"dependency=" + rebase_path(dir, root_build_dir),
]
deps += [ info.label ]
}
}
}
template("rust_component") {
rustc_name = target_name + "_rustc"
rustc_label = ":" + rustc_name
forward_variables_from(invoker,
[
"crate_name",
"crate_type",
])
if (!defined(crate_name)) {
crate_name = target_name
}
if (!defined(crate_type)) {
crate_type = "rlib"
}
run_rustc(rustc_name) {
forward_variables_from(invoker,
[
"args",
"crate_version",
"deps",
"extern",
"extern_version",
"features",
"is_test",
"source_root",
"testonly",
])
}
crate_outputs = get_target_outputs(rustc_label)
crate_obj = crate_outputs[0]
component(target_name) {
forward_variables_from(invoker,
[
"deps",
"libs",
"testonly",
])
if (!defined(deps)) {
deps = []
}
if (!defined(libs)) {
libs = []
}
libs += [ crate_obj ]
deps += [ rustc_label ]
}
}
template("rust_executable") {
bin_name = target_name + "_bin"
bin_label = ":" + bin_name
rust_component(bin_name) {
crate_type = "bin"
forward_variables_from(invoker, "*")
}
executable(target_name) {
forward_variables_from(invoker, "*")
if (defined(is_test) && is_test) {
ldflags = rust_test_ldflags
} else {
ldflags = rust_bin_ldflags
}
if (!defined(deps)) {
deps = []
}
deps += [ bin_label ]
if (defined(extern)) {
deps += extern
}
if (defined(extern_version)) {
foreach(info, extern_version) {
deps += [ info.label ]
}
}
}
}
template("rust_test") {
rust_executable(target_name) {
forward_variables_from(invoker, "*")
is_test = true
testonly = true
}
}
|