From d30664958ec1031a99373414e17c124b4fa468fa Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 6 Jul 2018 00:58:09 -0400 Subject: 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++. --- BUILD.gn | 6 +- build_extra/deno.gni | 52 ++++++++++++++++ build_extra/rust/BUILD.gn | 15 +++++ build_extra/rust/empty.rs | 1 + build_extra/rust/rust.gni | 153 ++++++++++++++++++++++++++++++++++++++++++++++ deno.gni | 52 ---------------- rust.gni | 136 ----------------------------------------- src/empty.rs | 1 - tools/format.sh | 5 +- 9 files changed, 227 insertions(+), 194 deletions(-) create mode 100644 build_extra/deno.gni create mode 100644 build_extra/rust/BUILD.gn create mode 100644 build_extra/rust/empty.rs create mode 100644 build_extra/rust/rust.gni delete mode 100644 deno.gni delete mode 100644 rust.gni delete mode 100644 src/empty.rs diff --git a/BUILD.gn b/BUILD.gn index 999d41465..242d836b0 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -2,8 +2,8 @@ import("//third_party/v8/gni/v8.gni") import("//third_party/v8/snapshot_toolchain.gni") import("//third_party/flatbuffers/flatbuffer.gni") import("//third_party/flatbuffers/ts_flatbuffer.gni") -import("deno.gni") -import("rust.gni") +import("//build_extra/deno.gni") +import("//build_extra/rust/rust.gni") config("deno_config") { 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" cfg = [ "feature=\"default\"", diff --git a/build_extra/deno.gni b/build_extra/deno.gni new file mode 100644 index 000000000..a8804e9f9 --- /dev/null +++ b/build_extra/deno.gni @@ -0,0 +1,52 @@ +template("run_node") { + action(target_name) { + forward_variables_from(invoker, "*") + script = "//js/run_node.py" + } +} + +# Template to generate different V8 snapshots based on different runtime flags. +# Can be invoked with run_mksnapshot(). The target will resolve to +# run_mksnapshot_. If is "default", no file suffixes will be used. +# Otherwise files are suffixed, e.g. embedded_.cc and +# snapshot_blob_.bin. +# +# The template exposes the variables: +# args: additional flags for mksnapshots +# embedded_suffix: a camel case suffix for method names in the embedded +# snapshot. +template("create_snapshot") { + name = target_name + suffix = "_$name" + action("create_snapshot_" + name) { + forward_variables_from(invoker, + [ + "testonly", + "deps", + ]) + visibility = [ ":*" ] # Only targets in this file can depend on this. + deps += [ ":snapshot_creator" ] + script = "//third_party/v8/tools/run.py" + data = [] + exe = rebase_path(get_label_info(":snapshot_creator", "root_out_dir") + + "/snapshot_creator") + snapshot_out_cc = "$target_gen_dir/snapshot${suffix}.cc" + sources = [ + invoker.js, + ] + outputs = [ + snapshot_out_cc, + ] + args = [ + exe, + rebase_path(invoker.js, root_build_dir), + rebase_path(snapshot_out_cc, root_build_dir), + ] + + # To debug snapshotting problems: + # args += ["--trace-serializer"] + data = [ + invoker.js, + ] + } +} diff --git a/build_extra/rust/BUILD.gn b/build_extra/rust/BUILD.gn new file mode 100644 index 000000000..e5a4fec16 --- /dev/null +++ b/build_extra/rust/BUILD.gn @@ -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" ] + } +} diff --git a/build_extra/rust/empty.rs b/build_extra/rust/empty.rs new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/build_extra/rust/empty.rs @@ -0,0 +1 @@ + diff --git a/build_extra/rust/rust.gni b/build_extra/rust/rust.gni new file mode 100644 index 000000000..e3c412be2 --- /dev/null +++ b/build_extra/rust/rust.gni @@ -0,0 +1,153 @@ +stdlib_label = "//build_extra/rust:stdlib" + +template("run_rustc") { + action(target_name) { + assert(defined(invoker.source_root), "Must specify source_root") + forward_variables_from(invoker, + [ + "cfg", + "crate_type", + "source_root", + "deps", + "rust_deps", + ]) + if (defined(invoker.crate_name)) { + crate_name = invoker.crate_name + } else { + crate_name = target_name + } + + sources = [ + source_root, + ] + outputs = [] + depfile = "$target_out_dir/$target_name.d" + script = "//third_party/v8/tools/run.py" + + args = [ + "rustc", + rebase_path(source_root, root_build_dir), + "--crate-name=$crate_name", + "--crate-type=$crate_type", + "--emit=dep-info=" + rebase_path(depfile, root_build_dir), + ] + + # We only use staticlib for the special "empty" lib. + if (crate_type == "staticlib") { + staticlib = "$target_out_dir/$crate_name.a" + outputs += [ staticlib ] + args += [ "--emit=link=" + rebase_path(staticlib, root_build_dir) ] + } + + if (crate_type == "rlib" || crate_type == "bin") { + obj = "$target_out_dir/$crate_name.o" + outputs += [ obj ] + args += [ "--emit=obj=" + rebase_path(obj, root_build_dir) ] + } + + if (crate_type == "rlib") { + rlib = "$target_out_dir/lib$crate_name.rlib" + outputs += [ rlib ] + args += [ "--emit=link=" + rebase_path(rlib, root_build_dir) ] + } + + if (is_debug) { + args += [ "-g" ] + } + + if (is_official_build) { + args += [ "-O" ] + } + + if (defined(cfg)) { + foreach(c, cfg) { + args += [ + "--cfg", + c, + ] + } + } + + if (!defined(deps)) { + deps = [] + } + + if (defined(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_rlib = "$dep_dir/lib$dep_name.rlib" + args += [ + "--extern", + "$dep_name=" + rebase_path(dep_rlib, root_build_dir), + ] + } + } + } +} + +template("rust_component") { + 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_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") { + 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(deps)) { + deps = [] + } + + deps += [ + bin_label, + stdlib_label, + ] + + if (defined(rust_deps)) { + deps += rust_deps + } + } +} diff --git a/deno.gni b/deno.gni deleted file mode 100644 index a8804e9f9..000000000 --- a/deno.gni +++ /dev/null @@ -1,52 +0,0 @@ -template("run_node") { - action(target_name) { - forward_variables_from(invoker, "*") - script = "//js/run_node.py" - } -} - -# Template to generate different V8 snapshots based on different runtime flags. -# Can be invoked with run_mksnapshot(). The target will resolve to -# run_mksnapshot_. If is "default", no file suffixes will be used. -# Otherwise files are suffixed, e.g. embedded_.cc and -# snapshot_blob_.bin. -# -# The template exposes the variables: -# args: additional flags for mksnapshots -# embedded_suffix: a camel case suffix for method names in the embedded -# snapshot. -template("create_snapshot") { - name = target_name - suffix = "_$name" - action("create_snapshot_" + name) { - forward_variables_from(invoker, - [ - "testonly", - "deps", - ]) - visibility = [ ":*" ] # Only targets in this file can depend on this. - deps += [ ":snapshot_creator" ] - script = "//third_party/v8/tools/run.py" - data = [] - exe = rebase_path(get_label_info(":snapshot_creator", "root_out_dir") + - "/snapshot_creator") - snapshot_out_cc = "$target_gen_dir/snapshot${suffix}.cc" - sources = [ - invoker.js, - ] - outputs = [ - snapshot_out_cc, - ] - args = [ - exe, - rebase_path(invoker.js, root_build_dir), - rebase_path(snapshot_out_cc, root_build_dir), - ] - - # To debug snapshotting problems: - # args += ["--trace-serializer"] - data = [ - invoker.js, - ] - } -} diff --git a/rust.gni b/rust.gni deleted file mode 100644 index 218ab1299..000000000 --- a/rust.gni +++ /dev/null @@ -1,136 +0,0 @@ -template("rust_crate") { - action(target_name) { - forward_variables_from(invoker, - [ - "cfg", - "crate_type", - "source_root", - "deps", - "rust_deps", - ]) - sources = [ - source_root, - ] - outputs = [] - depfile = "$target_out_dir/$target_name.d" - script = "//third_party/v8/tools/run.py" - - args = [ - "rustc", - rebase_path(source_root, root_build_dir), - "--crate-name=$target_name", - "--crate-type=$crate_type", - "--emit=dep-info=" + rebase_path(depfile, root_build_dir), - ] - - # We only use staticlib for the special "empty" lib. - if (crate_type == "staticlib") { - staticlib = "$target_out_dir/$target_name.a" - outputs += [ staticlib ] - args += [ "--emit=link=" + rebase_path(staticlib, root_build_dir) ] - } - - if (crate_type == "rlib" || crate_type == "bin") { - obj = "$target_out_dir/$target_name.o" - outputs += [ obj ] - args += [ "--emit=obj=" + rebase_path(obj, root_build_dir) ] - } - - if (crate_type == "rlib") { - rlib = "$target_out_dir/lib$target_name.rlib" - outputs += [ rlib ] - args += [ "--emit=link=" + rebase_path(rlib, root_build_dir) ] - } - - if (is_debug) { - args += [ "-g" ] - } - - if (is_official_build) { - args += [ "-O" ] - } - - if (defined(cfg)) { - foreach(c, cfg) { - args += [ - "--cfg", - c, - ] - } - } - - if (!defined(deps)) { - deps = [] - } - - if (defined(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_rlib = "$dep_dir/lib$dep_name.rlib" - args += [ - "--extern", - "$dep_name=" + rebase_path(dep_rlib, root_build_dir), - ] - } - } - } -} - -template("rust_library") { - rust_crate(target_name) { - crate_type = "rlib" - forward_variables_from(invoker, "*") - } -} - -template("rust_executable") { - bin_target = target_name + "_bin" - rust_crate(bin_target) { - crate_type = "bin" - 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) { - forward_variables_from(invoker, "*") - - if (!defined(deps)) { - deps = [] - } - - deps += [ - ":" + bin_target, - ":" + stdlib_target, - ] - - libs = get_target_outputs(":" + bin_target) + - get_target_outputs(":" + stdlib_target) - - if (defined(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" ] - } - } -} diff --git a/src/empty.rs b/src/empty.rs deleted file mode 100644 index 8b1378917..000000000 --- a/src/empty.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/tools/format.sh b/tools/format.sh index f0c7ec0a1..44ca815ff 100755 --- a/tools/format.sh +++ b/tools/format.sh @@ -4,8 +4,9 @@ cd `dirname "$0"`/.. clang-format -i -style Google src/*.cc src/*.h gn format BUILD.gn -gn format deno.gni -gn format rust.gni +gn format build_extra/deno.gni +gn format build_extra/rust/rust.gni +gn format build_extra/rust/BUILD.gn gn format .gn yapf -i js/*.py -- cgit v1.2.3