From 4b75dd7cf5cd40bf9ac08ca16c63dd60b85d6bdf Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Thu, 9 Aug 2018 00:42:14 +0200 Subject: [PATCH] Support building multiple versions of a rust crate This is only to be used in exceptional cases. Generally we don't allow using multiple versions of a crate. --- build_extra/rust/rust.gni | 97 +++++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 23 deletions(-) diff --git a/build_extra/rust/rust.gni b/build_extra/rust/rust.gni index 1a353bfa0d..f37d235e6b 100644 --- a/build_extra/rust/rust.gni +++ b/build_extra/rust/rust.gni @@ -43,8 +43,10 @@ template("run_rustc") { "cfg", "crate_name", "crate_type", + "crate_version", "deps", "extern", + "extern_version", "features", "is_test", "source_root", @@ -75,21 +77,27 @@ template("run_rustc") { args += [ "--color=always" ] } + if (!defined(crate_version)) { + crate_name_and_version = crate_name + } else { + crate_name_and_version = "$crate_name-$crate_version" + } + if (crate_type == "staticlib") { - output_file = "$target_out_dir/$crate_name.a" + output_file = "$target_out_dir/$crate_name_and_version.a" emit_type = "link" } else if (crate_type == "bin") { - output_file = "$target_out_dir/$crate_name.o" + output_file = "$target_out_dir/$crate_name_and_version.o" emit_type = "obj" } else if (crate_type == "rlib") { - output_file = "$target_out_dir/lib$crate_name.rlib" + output_file = "$target_out_dir/lib$crate_name_and_version.rlib" emit_type = "link" } outputs += [ output_file ] output_file_rel = rebase_path(output_file, root_build_dir) args += [ "--emit=$emit_type=$output_file_rel" ] - depfile = "$target_out_dir/$crate_name.d" + depfile = "$target_out_dir/$crate_name_and_version.d" args += [ "--emit=dep-info=" + rebase_path(depfile, root_build_dir), @@ -99,6 +107,13 @@ template("run_rustc") { "--output_file=" + output_file_rel, ] + if (defined(crate_version)) { + args += [ + "-C", + "metadata=$crate_version", + ] + } + if (is_debug) { args += [ "-g" ] } @@ -137,48 +152,77 @@ template("run_rustc") { deps = [] } + # Convert all 'extern' and 'extern_version' items to a single format. + extern_infos = [] if (defined(extern)) { - deps += extern foreach(label, extern) { - name = get_label_info(label, "name") - dir = get_label_info(label, "target_out_dir") - rlib = "$dir/lib$name.rlib" - args += [ - "--extern", - "$name=" + rebase_path(rlib, root_build_dir), - ] - - # This is needed for transitive dependencies. - args += [ - "-L", - "dependency=" + rebase_path(dir, root_build_dir), + 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. + 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 ] + } } } template("rust_component") { rustc_name = target_name + "_rustc" rustc_label = ":" + rustc_name - crate_name = target_name + + forward_variables_from(invoker, + [ + "crate_name", + "crate_type", + ]) + if (!defined(crate_name)) { + crate_name = target_name + } + if (!defined(crate_type)) { + crate_type = "rlib" + } + run_rustc(rustc_name) { forward_variables_from(invoker, [ "args", "cfg", - "crate_name", - "crate_type", + "crate_version", "deps", "extern", + "extern_version", "features", "is_test", "source_root", "testonly", ]) - if (!defined(invoker.crate_type)) { - crate_type = "rlib" - } } crate_outputs = get_target_outputs(rustc_label) @@ -208,7 +252,9 @@ template("rust_staticlib") { [ "cfg", "crate_name", + "crate_version", "extern", + "extern_version", "features", "source_root", "testonly", @@ -244,6 +290,11 @@ template("rust_executable") { if (defined(extern)) { deps += extern } + if (defined(extern_version)) { + foreach(info, extern_version) { + deps += [ info.label ] + } + } } }