1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

Further gn/rust cleanups

Move rust.gni and deno.gni into build_extra/

Removes rust_library which was only an action. This instead defines
rust_component, which is an action plus a gn "component" target
to expose the resulting object file. This simplifies link code in
rust.gni.

Support rust modules that can be linked into C++.
This commit is contained in:
Ryan Dahl 2018-07-06 00:58:09 -04:00
parent bfe08aa462
commit d30664958e
6 changed files with 76 additions and 43 deletions

View file

@ -2,8 +2,8 @@ import("//third_party/v8/gni/v8.gni")
import("//third_party/v8/snapshot_toolchain.gni") import("//third_party/v8/snapshot_toolchain.gni")
import("//third_party/flatbuffers/flatbuffer.gni") import("//third_party/flatbuffers/flatbuffer.gni")
import("//third_party/flatbuffers/ts_flatbuffer.gni") import("//third_party/flatbuffers/ts_flatbuffer.gni")
import("deno.gni") import("//build_extra/deno.gni")
import("rust.gni") import("//build_extra/rust/rust.gni")
config("deno_config") { config("deno_config") {
include_dirs = [ "third_party/v8" ] # This allows us to v8/src/base/ libraries. include_dirs = [ "third_party/v8" ] # This allows us to v8/src/base/ libraries.
@ -18,7 +18,7 @@ rust_executable("deno") {
] ]
} }
rust_library("libc") { rust_component("libc") {
source_root = "third_party/rust_crates/libc/src/lib.rs" source_root = "third_party/rust_crates/libc/src/lib.rs"
cfg = [ cfg = [
"feature=\"default\"", "feature=\"default\"",

15
build_extra/rust/BUILD.gn Normal file
View file

@ -0,0 +1,15 @@
import("rust.gni")
# By compiling an empty file as crate-type=staticlib we get all the code
# for the rust stdlib, which are not included in the object file outputs
# of other libs.
rust_component("stdlib") {
crate_type = "staticlib"
source_root = "empty.rs"
if (current_os == "mac") {
libs = [ "resolv" ]
}
if (current_os == "win") {
libs = [ "userenv.lib" ]
}
}

View file

@ -1,5 +1,8 @@
template("rust_crate") { stdlib_label = "//build_extra/rust:stdlib"
template("run_rustc") {
action(target_name) { action(target_name) {
assert(defined(invoker.source_root), "Must specify source_root")
forward_variables_from(invoker, forward_variables_from(invoker,
[ [
"cfg", "cfg",
@ -8,6 +11,12 @@ template("rust_crate") {
"deps", "deps",
"rust_deps", "rust_deps",
]) ])
if (defined(invoker.crate_name)) {
crate_name = invoker.crate_name
} else {
crate_name = target_name
}
sources = [ sources = [
source_root, source_root,
] ]
@ -18,26 +27,26 @@ template("rust_crate") {
args = [ args = [
"rustc", "rustc",
rebase_path(source_root, root_build_dir), rebase_path(source_root, root_build_dir),
"--crate-name=$target_name", "--crate-name=$crate_name",
"--crate-type=$crate_type", "--crate-type=$crate_type",
"--emit=dep-info=" + rebase_path(depfile, root_build_dir), "--emit=dep-info=" + rebase_path(depfile, root_build_dir),
] ]
# We only use staticlib for the special "empty" lib. # We only use staticlib for the special "empty" lib.
if (crate_type == "staticlib") { if (crate_type == "staticlib") {
staticlib = "$target_out_dir/$target_name.a" staticlib = "$target_out_dir/$crate_name.a"
outputs += [ staticlib ] outputs += [ staticlib ]
args += [ "--emit=link=" + rebase_path(staticlib, root_build_dir) ] args += [ "--emit=link=" + rebase_path(staticlib, root_build_dir) ]
} }
if (crate_type == "rlib" || crate_type == "bin") { if (crate_type == "rlib" || crate_type == "bin") {
obj = "$target_out_dir/$target_name.o" obj = "$target_out_dir/$crate_name.o"
outputs += [ obj ] outputs += [ obj ]
args += [ "--emit=obj=" + rebase_path(obj, root_build_dir) ] args += [ "--emit=obj=" + rebase_path(obj, root_build_dir) ]
} }
if (crate_type == "rlib") { if (crate_type == "rlib") {
rlib = "$target_out_dir/lib$target_name.rlib" rlib = "$target_out_dir/lib$crate_name.rlib"
outputs += [ rlib ] outputs += [ rlib ]
args += [ "--emit=link=" + rebase_path(rlib, root_build_dir) ] args += [ "--emit=link=" + rebase_path(rlib, root_build_dir) ]
} }
@ -78,29 +87,53 @@ template("rust_crate") {
} }
} }
template("rust_library") { template("rust_component") {
rust_crate(target_name) { rustc_name = target_name + "_rustc"
rustc_label = ":" + rustc_name
crate_name = target_name
run_rustc(rustc_name) {
forward_variables_from(invoker,
[
"crate_name",
"crate_type",
"rust_deps",
"cfg",
"source_root",
])
if (!defined(invoker.crate_type)) {
crate_type = "rlib" crate_type = "rlib"
forward_variables_from(invoker, "*") }
}
crate_outputs = get_target_outputs(rustc_label)
crate_obj = crate_outputs[0]
component(target_name) {
forward_variables_from(invoker,
[
"libs",
"deps",
])
if (!defined(deps)) {
deps = []
}
if (!defined(libs)) {
libs = []
}
libs += [ crate_obj ]
deps += [ rustc_label ]
} }
} }
template("rust_executable") { template("rust_executable") {
bin_target = target_name + "_bin" bin_name = target_name + "_bin"
rust_crate(bin_target) { bin_label = ":" + bin_name
rust_component(bin_name) {
crate_type = "bin" crate_type = "bin"
forward_variables_from(invoker, "*") forward_variables_from(invoker, "*")
} }
# By compiling an empty file as crate-type=staticlib we get all the code
# for the rust stdlib, which are not included in the object file outputs
# of other libs.
stdlib_target = target_name + "_stdlib"
rust_crate(stdlib_target) {
crate_type = "staticlib"
source_root = "src/empty.rs"
}
executable(target_name) { executable(target_name) {
forward_variables_from(invoker, "*") forward_variables_from(invoker, "*")
@ -109,28 +142,12 @@ template("rust_executable") {
} }
deps += [ deps += [
":" + bin_target, bin_label,
":" + stdlib_target, stdlib_label,
] ]
libs = get_target_outputs(":" + bin_target) +
get_target_outputs(":" + stdlib_target)
if (defined(rust_deps)) { if (defined(rust_deps)) {
deps += rust_deps deps += rust_deps
foreach(dep_label, rust_deps) {
dep_name = get_label_info(dep_label, "name")
dep_dir = get_label_info(dep_label, "target_out_dir")
dep_obj = "$dep_dir/$dep_name.o"
libs += [ dep_obj ]
}
}
if (current_os == "mac") {
libs += [ "resolv" ]
}
if (current_os == "win") {
libs += [ "userenv.lib" ]
} }
} }
} }

View file

@ -4,8 +4,9 @@ cd `dirname "$0"`/..
clang-format -i -style Google src/*.cc src/*.h clang-format -i -style Google src/*.cc src/*.h
gn format BUILD.gn gn format BUILD.gn
gn format deno.gni gn format build_extra/deno.gni
gn format rust.gni gn format build_extra/rust/rust.gni
gn format build_extra/rust/BUILD.gn
gn format .gn gn format .gn
yapf -i js/*.py yapf -i js/*.py