2018-07-07 19:28:23 -04:00
|
|
|
declare_args() {
|
|
|
|
# Absolute path of rust build files.
|
|
|
|
rust_build = "//build_extra/rust/"
|
|
|
|
}
|
|
|
|
|
2018-07-10 15:13:29 -04:00
|
|
|
if (is_win) {
|
|
|
|
executable_suffix = ".exe"
|
|
|
|
} else {
|
|
|
|
executable_suffix = ""
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:36:11 -04:00
|
|
|
# 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")
|
|
|
|
|
2018-07-06 00:58:09 -04:00
|
|
|
template("run_rustc") {
|
2018-07-02 04:59:29 -04:00
|
|
|
action(target_name) {
|
2018-07-06 00:58:09 -04:00
|
|
|
assert(defined(invoker.source_root), "Must specify source_root")
|
2018-07-02 04:59:29 -04:00
|
|
|
forward_variables_from(invoker,
|
|
|
|
[
|
2018-07-17 16:36:11 -04:00
|
|
|
"crate_name",
|
2018-07-02 04:59:29 -04:00
|
|
|
"crate_type",
|
2018-08-08 18:42:14 -04:00
|
|
|
"crate_version",
|
2018-07-02 04:59:29 -04:00
|
|
|
"deps",
|
2018-07-06 05:33:32 -04:00
|
|
|
"extern",
|
2018-08-08 18:42:14 -04:00
|
|
|
"extern_version",
|
2018-07-28 05:07:54 -04:00
|
|
|
"features",
|
2018-07-07 16:50:35 -04:00
|
|
|
"is_test",
|
2018-07-28 05:07:54 -04:00
|
|
|
"source_root",
|
2018-07-17 16:36:11 -04:00
|
|
|
"testonly",
|
2018-07-02 04:59:29 -04:00
|
|
|
])
|
2018-07-17 16:36:11 -04:00
|
|
|
if (!defined(crate_name)) {
|
2018-07-06 00:58:09 -04:00
|
|
|
crate_name = target_name
|
|
|
|
}
|
2018-07-17 16:36:11 -04:00
|
|
|
if (!defined(is_test)) {
|
|
|
|
is_test = false
|
|
|
|
}
|
2018-07-06 00:58:09 -04:00
|
|
|
|
2018-07-02 04:59:29 -04:00
|
|
|
sources = [
|
|
|
|
source_root,
|
|
|
|
]
|
|
|
|
outputs = []
|
2018-07-08 05:09:42 -04:00
|
|
|
script = "//tools/run_rustc.py"
|
2018-07-02 04:59:29 -04:00
|
|
|
|
2018-07-21 11:00:09 -04:00
|
|
|
# TODO: We want to apply "-Dwarnings" only when treat_warnings_as_errors is not false
|
2018-08-02 19:13:02 -04:00
|
|
|
# https://github.com/denoland/deno/pull/379
|
2018-07-02 04:59:29 -04:00
|
|
|
args = [
|
|
|
|
rebase_path(source_root, root_build_dir),
|
2018-07-06 00:58:09 -04:00
|
|
|
"--crate-name=$crate_name",
|
2018-07-02 04:59:29 -04:00
|
|
|
"--crate-type=$crate_type",
|
|
|
|
]
|
2018-07-21 11:00:09 -04:00
|
|
|
|
2018-07-12 14:02:03 -04:00
|
|
|
if (!is_win) {
|
|
|
|
args += [ "--color=always" ]
|
|
|
|
}
|
2018-07-02 04:59:29 -04:00
|
|
|
|
2018-08-08 18:42:14 -04:00
|
|
|
if (!defined(crate_version)) {
|
|
|
|
crate_name_and_version = crate_name
|
|
|
|
} else {
|
|
|
|
crate_name_and_version = "$crate_name-$crate_version"
|
|
|
|
}
|
|
|
|
|
2018-08-08 20:06:17 -04:00
|
|
|
if (crate_type == "bin") {
|
2018-08-08 18:42:14 -04:00
|
|
|
output_file = "$target_out_dir/$crate_name_and_version.o"
|
2018-07-17 16:36:11 -04:00
|
|
|
emit_type = "obj"
|
|
|
|
} else if (crate_type == "rlib") {
|
2018-08-08 18:42:14 -04:00
|
|
|
output_file = "$target_out_dir/lib$crate_name_and_version.rlib"
|
2018-07-17 16:36:11 -04:00
|
|
|
emit_type = "link"
|
|
|
|
}
|
|
|
|
outputs += [ output_file ]
|
|
|
|
output_file_rel = rebase_path(output_file, root_build_dir)
|
|
|
|
args += [ "--emit=$emit_type=$output_file_rel" ]
|
|
|
|
|
2018-08-08 18:42:14 -04:00
|
|
|
depfile = "$target_out_dir/$crate_name_and_version.d"
|
2018-07-17 16:36:11 -04:00
|
|
|
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,
|
|
|
|
]
|
2018-07-02 04:59:29 -04:00
|
|
|
|
2018-08-08 18:42:14 -04:00
|
|
|
if (defined(crate_version)) {
|
|
|
|
args += [
|
|
|
|
"-C",
|
|
|
|
"metadata=$crate_version",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2018-07-02 04:59:29 -04:00
|
|
|
if (is_debug) {
|
|
|
|
args += [ "-g" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_official_build) {
|
|
|
|
args += [ "-O" ]
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:36:11 -04:00
|
|
|
if (is_test) {
|
|
|
|
args += [ "--test" ]
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:07:54 -04:00
|
|
|
if (defined(features)) {
|
|
|
|
foreach(f, features) {
|
|
|
|
args += [
|
|
|
|
"--cfg",
|
|
|
|
"feature=\"" + f + "\"",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 11:00:09 -04:00
|
|
|
if (defined(invoker.args)) {
|
|
|
|
args += invoker.args
|
|
|
|
}
|
|
|
|
|
2018-07-02 04:59:29 -04:00
|
|
|
if (!defined(deps)) {
|
|
|
|
deps = []
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:42:14 -04:00
|
|
|
# Convert all 'extern' and 'extern_version' items to a single format.
|
|
|
|
extern_infos = []
|
2018-07-06 05:33:32 -04:00
|
|
|
if (defined(extern)) {
|
|
|
|
foreach(label, extern) {
|
2018-08-08 18:42:14 -04:00
|
|
|
extern_infos += [
|
|
|
|
{
|
|
|
|
label = label
|
|
|
|
crate_name = get_label_info(label, "name")
|
|
|
|
crate_name_and_version = crate_name
|
|
|
|
},
|
2018-07-02 04:59:29 -04:00
|
|
|
]
|
2018-08-08 18:42:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (defined(extern_version)) {
|
|
|
|
foreach(info, extern_version) {
|
|
|
|
extern_infos += [
|
|
|
|
{
|
|
|
|
forward_variables_from(info, "*")
|
|
|
|
crate_name_and_version = "$crate_name-$crate_version"
|
|
|
|
},
|
2018-07-10 14:56:12 -04:00
|
|
|
]
|
2018-07-02 04:59:29 -04:00
|
|
|
}
|
|
|
|
}
|
2018-08-08 18:42:14 -04:00
|
|
|
|
|
|
|
# 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 ]
|
|
|
|
}
|
2018-07-02 04:59:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 16:04:11 -04:00
|
|
|
template("rust_crate") {
|
2018-07-06 00:58:09 -04:00
|
|
|
rustc_name = target_name + "_rustc"
|
|
|
|
rustc_label = ":" + rustc_name
|
2018-08-08 18:42:14 -04:00
|
|
|
|
|
|
|
forward_variables_from(invoker,
|
|
|
|
[
|
|
|
|
"crate_name",
|
|
|
|
"crate_type",
|
|
|
|
])
|
|
|
|
if (!defined(crate_name)) {
|
|
|
|
crate_name = target_name
|
|
|
|
}
|
|
|
|
if (!defined(crate_type)) {
|
|
|
|
crate_type = "rlib"
|
|
|
|
}
|
|
|
|
|
2018-07-06 00:58:09 -04:00
|
|
|
run_rustc(rustc_name) {
|
|
|
|
forward_variables_from(invoker,
|
|
|
|
[
|
2018-07-28 05:07:54 -04:00
|
|
|
"args",
|
2018-08-08 18:42:14 -04:00
|
|
|
"crate_version",
|
2018-07-28 05:07:54 -04:00
|
|
|
"deps",
|
2018-07-06 05:33:32 -04:00
|
|
|
"extern",
|
2018-08-08 18:42:14 -04:00
|
|
|
"extern_version",
|
2018-07-28 05:07:54 -04:00
|
|
|
"features",
|
2018-07-17 16:36:11 -04:00
|
|
|
"is_test",
|
2018-07-28 05:07:54 -04:00
|
|
|
"source_root",
|
2018-07-07 16:50:35 -04:00
|
|
|
"testonly",
|
2018-07-06 00:58:09 -04:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
crate_outputs = get_target_outputs(rustc_label)
|
|
|
|
crate_obj = crate_outputs[0]
|
|
|
|
|
2018-08-11 20:29:52 -04:00
|
|
|
source_set(target_name) {
|
2018-07-06 00:58:09 -04:00
|
|
|
forward_variables_from(invoker,
|
|
|
|
[
|
|
|
|
"deps",
|
2018-07-28 05:07:54 -04:00
|
|
|
"libs",
|
2018-07-07 16:50:35 -04:00
|
|
|
"testonly",
|
2018-07-06 00:58:09 -04:00
|
|
|
])
|
|
|
|
if (!defined(deps)) {
|
|
|
|
deps = []
|
|
|
|
}
|
|
|
|
if (!defined(libs)) {
|
|
|
|
libs = []
|
|
|
|
}
|
|
|
|
libs += [ crate_obj ]
|
|
|
|
deps += [ rustc_label ]
|
2018-07-02 04:59:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template("rust_executable") {
|
2018-07-06 00:58:09 -04:00
|
|
|
bin_name = target_name + "_bin"
|
|
|
|
bin_label = ":" + bin_name
|
|
|
|
|
2018-08-11 16:04:11 -04:00
|
|
|
rust_crate(bin_name) {
|
2018-07-02 04:59:29 -04:00
|
|
|
crate_type = "bin"
|
|
|
|
forward_variables_from(invoker, "*")
|
|
|
|
}
|
|
|
|
|
|
|
|
executable(target_name) {
|
|
|
|
forward_variables_from(invoker, "*")
|
|
|
|
|
2018-07-17 16:36:11 -04:00
|
|
|
if (defined(is_test) && is_test) {
|
|
|
|
ldflags = rust_test_ldflags
|
|
|
|
} else {
|
|
|
|
ldflags = rust_bin_ldflags
|
|
|
|
}
|
|
|
|
|
2018-07-02 04:59:29 -04:00
|
|
|
if (!defined(deps)) {
|
|
|
|
deps = []
|
|
|
|
}
|
|
|
|
|
2018-07-17 16:36:11 -04:00
|
|
|
deps += [ bin_label ]
|
2018-07-02 04:59:29 -04:00
|
|
|
|
2018-07-06 05:33:32 -04:00
|
|
|
if (defined(extern)) {
|
|
|
|
deps += extern
|
2018-07-02 04:59:29 -04:00
|
|
|
}
|
2018-08-08 18:42:14 -04:00
|
|
|
if (defined(extern_version)) {
|
|
|
|
foreach(info, extern_version) {
|
|
|
|
deps += [ info.label ]
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 04:59:29 -04:00
|
|
|
}
|
|
|
|
}
|
2018-07-07 16:50:35 -04:00
|
|
|
|
|
|
|
template("rust_test") {
|
2018-07-17 16:36:11 -04:00
|
|
|
rust_executable(target_name) {
|
|
|
|
forward_variables_from(invoker, "*")
|
2018-07-07 16:50:35 -04:00
|
|
|
is_test = true
|
2018-07-17 16:36:11 -04:00
|
|
|
testonly = true
|
2018-07-07 16:50:35 -04:00
|
|
|
}
|
|
|
|
}
|