Fix transitive rust deps.

Use a single out_dir for all rust crates.
This commit is contained in:
Ryan Dahl 2018-08-11 16:55:37 -04:00
parent 2c384f2b07
commit 02540e559d
2 changed files with 63 additions and 98 deletions

View file

@ -35,76 +35,21 @@ config("deno_config") {
} }
main_extern = [ main_extern = [
"$rust_build:futures",
"$rust_build:libc", "$rust_build:libc",
"$rust_build:log", "$rust_build:log",
"$rust_build:sha1", "$rust_build:sha1",
"$rust_build:tempfile", "$rust_build:tempfile",
"$rust_build:tokio", "$rust_build:tokio",
"$rust_build:tokio_current_thread",
"$rust_build:url", "$rust_build:url",
"//build_extra/flatbuffers/rust:flatbuffers", "//build_extra/flatbuffers/rust:flatbuffers",
":msg_rs", ":msg_rs",
# Indirect rust depdendencies also need to be listed here:
# * Linking to `:handlers` or `:libdeno` isn't possible, because they
# already contain some symbols exported by `handlers.rs`. These duplicate
# symbols trip up the linker.
# * The `rust_test` and `rust_executable` templates only produce an object
# file, and then invoke an external linker. Transitive rust depencenies
# are not resolved in either step.
"$rust_build:arrayvec",
"$rust_build:byteorder",
"$rust_build:bytes",
"$rust_build:crossbeam_deque",
"$rust_build:crossbeam_epoch",
"$rust_build:crossbeam_utils",
"$rust_build:futures",
"$rust_build:idna",
"$rust_build:iovec",
"$rust_build:kernel32",
"$rust_build:lazy_static",
"$rust_build:lazycell",
"$rust_build:memoffset",
"$rust_build:mio",
"$rust_build:miow",
"$rust_build:net2",
"$rust_build:nodrop",
"$rust_build:num_cpus",
"$rust_build:percent_encoding",
"$rust_build:rand",
"$rust_build:rand_core",
"$rust_build:remove_dir_all",
"$rust_build:scopeguard",
"$rust_build:slab",
"$rust_build:tokio_executor",
"$rust_build:tokio_fs",
"$rust_build:tokio_io",
"$rust_build:tokio_reactor",
"$rust_build:tokio_tcp",
"$rust_build:tokio_threadpool",
"$rust_build:tokio_current_thread",
"$rust_build:tokio_timer",
"$rust_build:tokio_udp",
"$rust_build:unicode_bidi",
"$rust_build:unicode_normalization",
"$rust_build:winapi",
"$rust_build:ws2_32",
]
# Old version of the 'winapi' crate, required by 'mio', 'miow', and 'iovec'.
# This exceptional! Generally we don't allow multiple versions of a crate.
# TODO: Remove this dependency. https://github.com/denoland/deno/issues/484
main_extern_version = [
{
label = "$rust_build:winapi-0.2"
crate_name = "winapi"
crate_version = "0.2"
},
] ]
rust_executable("deno") { rust_executable("deno") {
source_root = "src/main.rs" source_root = "src/main.rs"
extern = main_extern extern = main_extern
extern_version = main_extern_version
deps = [ deps = [
":libdeno", ":libdeno",
] ]
@ -117,7 +62,6 @@ rust_executable("deno") {
rust_executable("deno_ns") { rust_executable("deno_ns") {
source_root = "src/main.rs" source_root = "src/main.rs"
extern = main_extern extern = main_extern
extern_version = main_extern_version
deps = [ deps = [
":libdeno_nosnapshot", ":libdeno_nosnapshot",
] ]
@ -126,7 +70,6 @@ rust_executable("deno_ns") {
rust_test("test_rs") { rust_test("test_rs") {
source_root = "src/main.rs" source_root = "src/main.rs"
extern = main_extern extern = main_extern
extern_version = main_extern_version
deps = [ deps = [
":libdeno", ":libdeno",
] ]

View file

@ -9,6 +9,11 @@ if (is_win) {
executable_suffix = "" executable_suffix = ""
} }
# 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 # 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: # 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 source sets.
@ -44,8 +49,7 @@ template("run_rustc") {
"crate_type", "crate_type",
"crate_version", "crate_version",
"deps", "deps",
"extern", "extern_infos",
"extern_version",
"features", "features",
"is_test", "is_test",
"source_root", "source_root",
@ -61,7 +65,6 @@ template("run_rustc") {
sources = [ sources = [
source_root, source_root,
] ]
outputs = []
script = "//tools/run_rustc.py" script = "//tools/run_rustc.py"
# TODO: We want to apply "-Dwarnings" only when treat_warnings_as_errors is not false # TODO: We want to apply "-Dwarnings" only when treat_warnings_as_errors is not false
@ -83,17 +86,19 @@ template("run_rustc") {
} }
if (crate_type == "bin") { if (crate_type == "bin") {
output_file = "$target_out_dir/$crate_name_and_version.o" output_file = "$out_dir/$crate_name_and_version.o"
emit_type = "obj" emit_type = "obj"
} else if (crate_type == "rlib") { } else if (crate_type == "rlib") {
output_file = "$target_out_dir/lib$crate_name_and_version.rlib" output_file = "$out_dir/lib$crate_name_and_version.rlib"
emit_type = "link" emit_type = "link"
} }
outputs += [ output_file ] outputs = [
output_file,
]
output_file_rel = rebase_path(output_file, root_build_dir) output_file_rel = rebase_path(output_file, root_build_dir)
args += [ "--emit=$emit_type=$output_file_rel" ] args += [ "--emit=$emit_type=$output_file_rel" ]
depfile = "$target_out_dir/$crate_name_and_version.d" depfile = "$out_dir/$crate_name_and_version.d"
args += [ args += [
"--emit=dep-info=" + rebase_path(depfile, root_build_dir), "--emit=dep-info=" + rebase_path(depfile, root_build_dir),
@ -101,6 +106,10 @@ template("run_rustc") {
# the depfile on the fly. They are not passed thru to rustc. # the depfile on the fly. They are not passed thru to rustc.
"--depfile=" + rebase_path(depfile, root_build_dir), "--depfile=" + rebase_path(depfile, root_build_dir),
"--output_file=" + output_file_rel, "--output_file=" + output_file_rel,
# This is needed for transitive dependencies.
"-L",
"dependency=" + rebase_path(out_dir, root_build_dir),
] ]
if (defined(crate_version)) { if (defined(crate_version)) {
@ -139,41 +148,12 @@ template("run_rustc") {
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. # Build the list of '--extern' arguments from the 'extern_infos' array.
foreach(info, extern_infos) { foreach(info, extern_infos) {
dir = get_label_info(info.label, "target_out_dir") rlib = "$out_dir/lib${info.crate_name_and_version}.rlib"
rlib = "$dir/lib${info.crate_name_and_version}.rlib"
args += [ args += [
"--extern", "--extern",
info.crate_name + "=" + rebase_path(rlib, root_build_dir), 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 ] deps += [ info.label ]
} }
@ -183,6 +163,36 @@ template("run_rustc") {
template("rust_crate") { template("rust_crate") {
rustc_name = target_name + "_rustc" rustc_name = target_name + "_rustc"
rustc_label = ":" + rustc_name rustc_label = ":" + rustc_name
config_name = target_name + "_config"
# Convert all 'extern' and 'extern_version' items to a single format.
extern_infos = []
if (defined(invoker.extern)) {
foreach(label, invoker.extern) {
extern_infos += [
{
label = label
crate_name = get_label_info(label, "name")
crate_name_and_version = crate_name
},
]
}
}
if (defined(invoker.extern_version)) {
foreach(info, invoker.extern_version) {
extern_infos += [
{
forward_variables_from(info,
[
"label",
"crate_name",
"crate_version",
])
crate_name_and_version = "$crate_name-$crate_version"
},
]
}
}
forward_variables_from(invoker, forward_variables_from(invoker,
[ [
@ -202,8 +212,6 @@ template("rust_crate") {
"args", "args",
"crate_version", "crate_version",
"deps", "deps",
"extern",
"extern_version",
"features", "features",
"is_test", "is_test",
"source_root", "source_root",
@ -214,6 +222,19 @@ template("rust_crate") {
crate_outputs = get_target_outputs(rustc_label) crate_outputs = get_target_outputs(rustc_label)
crate_obj = crate_outputs[0] crate_obj = crate_outputs[0]
config(config_name) {
lib_dirs = []
forward_variables_from(invoker, [ "libs" ])
if (!defined(libs)) {
libs = []
}
foreach(info, extern_infos) {
rlib = "$out_dir/lib${info.crate_name_and_version}.rlib"
libs += [ rlib ]
}
lib_dirs = [ out_dir ]
}
source_set(target_name) { source_set(target_name) {
forward_variables_from(invoker, forward_variables_from(invoker,
[ [
@ -229,6 +250,7 @@ template("rust_crate") {
} }
libs += [ crate_obj ] libs += [ crate_obj ]
deps += [ rustc_label ] deps += [ rustc_label ]
all_dependent_configs = [ ":" + config_name ]
} }
} }