2018-07-07 19:28:23 -04:00
|
|
|
declare_args() {
|
|
|
|
# Absolute path of rust build files.
|
|
|
|
rust_build = "//build_extra/rust/"
|
2018-08-13 04:04:18 -04:00
|
|
|
|
2018-09-25 21:45:09 -04:00
|
|
|
# Wrapper executable for rustc invocations. This can be used for a caching
|
|
|
|
# utility, e.g. sccache.
|
|
|
|
rustc_wrapper = ""
|
|
|
|
|
2018-08-13 04:04:18 -04:00
|
|
|
# treat the warnings in rust files as errors
|
|
|
|
rust_treat_warnings_as_errors = true
|
2018-07-07 19:28:23 -04:00
|
|
|
}
|
|
|
|
|
2018-07-10 15:13:29 -04:00
|
|
|
if (is_win) {
|
|
|
|
executable_suffix = ".exe"
|
|
|
|
} else {
|
|
|
|
executable_suffix = ""
|
|
|
|
}
|
|
|
|
|
2018-08-11 16:55:37 -04:00
|
|
|
# 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"
|
|
|
|
|
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).
|
|
|
|
#
|
2018-10-25 14:04:23 -04:00
|
|
|
# The `get_rustc_info` tool outputs the linker flags that are needed to
|
2018-07-17 16:36:11 -04:00
|
|
|
# successfully link rustc object code into an executable.
|
|
|
|
# We generate two sets of ldflags:
|
2018-10-25 14:04:23 -04:00
|
|
|
# `ldflags_bin` : Used for rust_executable targets.
|
|
|
|
# `ldflags_test`: Used for rust_test targets; includes the test harness.
|
2018-07-17 16:36:11 -04:00
|
|
|
#
|
|
|
|
# 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.
|
2018-10-25 14:04:23 -04:00
|
|
|
_rustc_info = exec_script("get_rustc_info.py", [], "json")
|
2018-07-17 16:36:11 -04:00
|
|
|
|
2018-10-20 00:23:00 -04:00
|
|
|
template("rust_crate") {
|
|
|
|
config_name = "${target_name}_config"
|
|
|
|
action_name = "${target_name}_rustc"
|
2018-07-06 00:58:09 -04:00
|
|
|
|
2018-10-20 00:23:00 -04:00
|
|
|
forward_variables_from(invoker,
|
|
|
|
[
|
2019-01-08 13:17:27 -05:00
|
|
|
"cfg",
|
2018-10-20 00:23:00 -04:00
|
|
|
"crate_name",
|
|
|
|
"crate_type",
|
|
|
|
"crate_version",
|
|
|
|
"deps",
|
2018-12-13 16:16:58 -05:00
|
|
|
"inputs",
|
2018-10-20 00:23:00 -04:00
|
|
|
"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(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 = ""
|
|
|
|
}
|
2018-09-25 21:44:23 -04:00
|
|
|
|
2018-10-20 00:23:00 -04:00
|
|
|
# Derive filenames for 'extern' and 'extern_version' linked rust libraries.
|
|
|
|
extern_rlibs = []
|
|
|
|
if (defined(invoker.extern)) {
|
|
|
|
foreach(label, invoker.extern) {
|
|
|
|
extern_rlibs += [
|
|
|
|
{
|
|
|
|
label = label
|
|
|
|
crate_name = get_label_info(label, "name")
|
|
|
|
rlib = "$out_dir/lib$crate_name.rlib"
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (defined(invoker.extern_version)) {
|
|
|
|
foreach(info, invoker.extern_version) {
|
|
|
|
extern_rlibs += [
|
|
|
|
{
|
2018-11-03 01:54:46 -04:00
|
|
|
crate_name = info.crate_name
|
|
|
|
crate_version = info.crate_version
|
2018-10-20 00:23:00 -04:00
|
|
|
crate_suffix = exec_script("//tools/sha256sum.py",
|
|
|
|
[
|
2018-11-03 01:54:46 -04:00
|
|
|
"--input=$crate_version",
|
2018-10-20 00:23:00 -04:00
|
|
|
"--format=-%.8s",
|
|
|
|
],
|
|
|
|
"trim string")
|
2018-11-03 01:54:46 -04:00
|
|
|
label = ":$crate_name-$crate_version"
|
2018-10-20 00:23:00 -04:00
|
|
|
rlib = "$out_dir/lib$crate_name$crate_suffix.rlib"
|
|
|
|
},
|
|
|
|
]
|
2018-08-08 18:42:14 -04:00
|
|
|
}
|
2018-10-20 00:23:00 -04:00
|
|
|
}
|
2018-08-08 18:42:14 -04:00
|
|
|
|
2018-10-20 00:23:00 -04:00
|
|
|
config(config_name) {
|
|
|
|
foreach(extern, extern_rlibs) {
|
|
|
|
libs += [ extern.rlib ]
|
2018-07-17 16:36:11 -04:00
|
|
|
}
|
2018-10-20 00:23:00 -04:00
|
|
|
lib_dirs = [ out_dir ]
|
|
|
|
}
|
|
|
|
|
|
|
|
if (crate_type == "bin") {
|
|
|
|
rustc_output = "$out_dir/$crate_name$crate_suffix.o"
|
|
|
|
emit_type = "obj"
|
|
|
|
} else if (crate_type == "rlib") {
|
|
|
|
rustc_output = "$out_dir/lib$crate_name$crate_suffix.rlib"
|
|
|
|
emit_type = "link"
|
|
|
|
}
|
2018-09-25 21:44:23 -04:00
|
|
|
|
2018-10-20 00:23:00 -04:00
|
|
|
source_set(target_name) {
|
|
|
|
public_deps = [
|
|
|
|
":$action_name",
|
|
|
|
]
|
|
|
|
libs += [ rustc_output ]
|
|
|
|
all_dependent_configs = [ ":$config_name" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
action(action_name) {
|
2018-10-31 03:32:42 -04:00
|
|
|
script = "//build_extra/rust/run.py"
|
2018-09-25 21:44:23 -04:00
|
|
|
sources = [
|
|
|
|
source_root,
|
|
|
|
]
|
2018-08-11 16:55:37 -04:00
|
|
|
outputs = [
|
2018-10-20 00:23:00 -04:00
|
|
|
rustc_output,
|
2018-08-11 16:55:37 -04:00
|
|
|
]
|
2018-09-25 21:44:23 -04:00
|
|
|
depfile = "$out_dir/$crate_name$crate_suffix.d"
|
2018-07-17 16:36:11 -04:00
|
|
|
|
2018-09-25 21:45:09 -04:00
|
|
|
if (rustc_wrapper != "") {
|
|
|
|
args = [ rustc_wrapper ]
|
|
|
|
} else {
|
|
|
|
args = []
|
|
|
|
}
|
|
|
|
|
|
|
|
args += [
|
2018-09-25 21:44:23 -04:00
|
|
|
"rustc",
|
|
|
|
rebase_path(source_root, root_build_dir),
|
|
|
|
"--crate-name=$crate_name",
|
|
|
|
"--crate-type=$crate_type",
|
|
|
|
"--emit=$emit_type,dep-info",
|
|
|
|
"--out-dir=" + rebase_path(out_dir, root_build_dir),
|
2018-07-17 16:36:11 -04:00
|
|
|
|
2018-09-25 21:44:23 -04:00
|
|
|
# This is to disambiguate multiple versions of the same crate.
|
|
|
|
"-Cextra-filename=$crate_suffix",
|
2018-10-25 14:04:23 -04:00
|
|
|
|
|
|
|
# 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}\"",
|
2018-08-11 16:55:37 -04:00
|
|
|
|
|
|
|
# This is needed for transitive dependencies.
|
|
|
|
"-L",
|
|
|
|
"dependency=" + rebase_path(out_dir, root_build_dir),
|
2018-08-26 00:17:02 -04:00
|
|
|
|
2018-09-25 21:44:23 -04:00
|
|
|
# Use colorful output even if stdout is redirected and not a tty.
|
|
|
|
"--color=always",
|
|
|
|
]
|
2018-08-26 00:17:02 -04:00
|
|
|
|
2018-12-13 16:16:58 -05:00
|
|
|
if (defined(crate_version)) {
|
|
|
|
args += [
|
|
|
|
# This is used to set env variables for Cargo build compatibility
|
|
|
|
"--cargo-pkg-version=$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-10-20 00:23:00 -04:00
|
|
|
if (treat_warnings_as_errors) {
|
|
|
|
args += [ "-Dwarnings" ]
|
|
|
|
}
|
|
|
|
if (defined(invoker.args)) {
|
|
|
|
args += invoker.args
|
|
|
|
}
|
2018-07-17 16:36:11 -04:00
|
|
|
|
2019-01-08 13:17:27 -05:00
|
|
|
if (defined(cfg)) {
|
|
|
|
foreach(c, cfg) {
|
|
|
|
args += [
|
|
|
|
"--cfg",
|
|
|
|
"c",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 05:07:54 -04:00
|
|
|
if (defined(features)) {
|
|
|
|
foreach(f, features) {
|
|
|
|
args += [
|
|
|
|
"--cfg",
|
|
|
|
"feature=\"" + f + "\"",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 00:23:00 -04:00
|
|
|
# Build the list of '--extern' arguments from the 'extern_rlibs' array.
|
|
|
|
foreach(extern, extern_rlibs) {
|
2018-08-08 18:42:14 -04:00
|
|
|
args += [
|
|
|
|
"--extern",
|
2018-10-20 00:23:00 -04:00
|
|
|
extern.crate_name + "=" + rebase_path(extern.rlib, root_build_dir),
|
2018-08-11 16:55:37 -04:00
|
|
|
]
|
2018-10-20 00:23:00 -04:00
|
|
|
sources += [ extern.rlib ]
|
|
|
|
deps += [ extern.label ]
|
2018-08-11 16:55:37 -04:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2018-10-25 14:04:23 -04:00
|
|
|
ldflags = _rustc_info.ldflags_test
|
2018-07-17 16:36:11 -04:00
|
|
|
} else {
|
2018-10-25 14:04:23 -04:00
|
|
|
ldflags = _rustc_info.ldflags_bin
|
2018-07-17 16:36:11 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|