From e017f7cf50f89fea9ee1d68dba9488faa6fd6dda Mon Sep 17 00:00:00 2001 From: David Skidmore Date: Tue, 29 Jun 2021 14:09:29 -0700 Subject: [PATCH 1/9] Add support for passing a custom target specification. --- cargo/cargo_build_script.bzl | 2 +- rust/private/rustc.bzl | 5 ++++- rust/toolchain.bzl | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/cargo/cargo_build_script.bzl b/cargo/cargo_build_script.bzl index 7500920435..885f4d9302 100644 --- a/cargo/cargo_build_script.bzl +++ b/cargo/cargo_build_script.bzl @@ -77,7 +77,7 @@ def _build_script_impl(ctx): # This isn't exactly right, but Bazel doesn't have exact views of "debug" and "release", so... "PROFILE": {"dbg": "debug", "fastbuild": "debug", "opt": "release"}.get(ctx.var["COMPILATION_MODE"], "unknown"), "RUSTC": toolchain.rustc.path, - "TARGET": toolchain.target_triple, + "TARGET": toolchain.target_json.path if toolchain.target_json != None else toolchain.target_triple, # OUT_DIR is set by the runner itself, rather than on the action. }) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index ea5ba025b0..b331923d8c 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -429,7 +429,10 @@ def construct_arguments( args.add("--emit=" + ",".join(emit_with_paths)) args.add("--color=always") - args.add("--target=" + toolchain.target_triple) + if toolchain.target_json != None: + args.add("--target=" + toolchain.target_json.path) + else: + args.add("--target=" + toolchain.target_triple) if hasattr(attr, "crate_features"): args.add_all(getattr(attr, "crate_features"), before_each = "--cfg", format_each = 'feature="%s"') if linker_script: diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index 95c42591bb..c5f8880c04 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -193,6 +193,7 @@ def _rust_toolchain_impl(ctx): rustfmt = ctx.file.rustfmt, cargo = ctx.file.cargo, clippy_driver = ctx.file.clippy_driver, + target_json = ctx.file.target_json, rustc_lib = ctx.attr.rustc_lib, rustc_srcs = ctx.attr.rustc_srcs, rust_lib = ctx.attr.rust_lib, @@ -296,6 +297,11 @@ rust_toolchain = rule( ), mandatory = True, ), + "target_json": attr.label( + doc = ("Override the target_triple with a custom target specification. " + + "For more details see: https://doc.rust-lang.org/rustc/targets/custom.html"), + allow_single_file = True, + ), "target_triple": attr.string( doc = ( "The platform triple for the toolchains target environment. " + From a6a0e93e9251ed6063726d7cac8783c1eb66465a Mon Sep 17 00:00:00 2001 From: David Skidmore Date: Fri, 16 Jul 2021 15:02:11 -0700 Subject: [PATCH 2/9] Pass the target spec ias a dependency. --- cargo/cargo_build_script.bzl | 2 +- rust/private/rustc.bzl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cargo/cargo_build_script.bzl b/cargo/cargo_build_script.bzl index 885f4d9302..61a57440ea 100644 --- a/cargo/cargo_build_script.bzl +++ b/cargo/cargo_build_script.bzl @@ -129,7 +129,7 @@ def _build_script_impl(ctx): script, ctx.executable._cargo_build_script_runner, toolchain.rustc, - ] + ctx.files.data, + ] + ctx.files.data + ([] if toolchain.target_json == None else [toolchain.target_json]), transitive = toolchain_tools, ) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index b331923d8c..869e42a1b2 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -287,6 +287,7 @@ def collect_inputs( [toolchain.rustc] + toolchain.crosstool_files + ([build_info.rustc_env, build_info.flags] if build_info else []) + + ([] if toolchain.target_json == None else [toolchain.target_json]) + ([] if linker_script == None else [linker_script]), transitive = [ toolchain.rustc_lib.files, From c5d4099afa79e3ac503cb51f2ec5eb70c623cea2 Mon Sep 17 00:00:00 2001 From: David Skidmore Date: Fri, 16 Jul 2021 15:13:56 -0700 Subject: [PATCH 3/9] Created a target_flag_value toolchain info field to avoid recalculating the value. --- cargo/cargo_build_script.bzl | 2 +- rust/private/rustc.bzl | 5 +---- rust/toolchain.bzl | 1 + 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/cargo/cargo_build_script.bzl b/cargo/cargo_build_script.bzl index 61a57440ea..b2f94b8cbe 100644 --- a/cargo/cargo_build_script.bzl +++ b/cargo/cargo_build_script.bzl @@ -77,7 +77,7 @@ def _build_script_impl(ctx): # This isn't exactly right, but Bazel doesn't have exact views of "debug" and "release", so... "PROFILE": {"dbg": "debug", "fastbuild": "debug", "opt": "release"}.get(ctx.var["COMPILATION_MODE"], "unknown"), "RUSTC": toolchain.rustc.path, - "TARGET": toolchain.target_json.path if toolchain.target_json != None else toolchain.target_triple, + "TARGET": toolchain.target_flag_value, # OUT_DIR is set by the runner itself, rather than on the action. }) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 869e42a1b2..4f66815ca4 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -430,10 +430,7 @@ def construct_arguments( args.add("--emit=" + ",".join(emit_with_paths)) args.add("--color=always") - if toolchain.target_json != None: - args.add("--target=" + toolchain.target_json.path) - else: - args.add("--target=" + toolchain.target_triple) + args.add("--target=" + toolchain.target_flag_value) if hasattr(attr, "crate_features"): args.add_all(getattr(attr, "crate_features"), before_each = "--cfg", format_each = 'feature="%s"') if linker_script: diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index c5f8880c04..f428ffbfc4 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -194,6 +194,7 @@ def _rust_toolchain_impl(ctx): cargo = ctx.file.cargo, clippy_driver = ctx.file.clippy_driver, target_json = ctx.file.target_json, + target_flag_value = ctx.file.target_json.path if ctx.file.target_json != None else ctx.attr.target_triple, rustc_lib = ctx.attr.rustc_lib, rustc_srcs = ctx.attr.rustc_srcs, rust_lib = ctx.attr.rust_lib, From a8d80340a412a2aa207044cc040e8afc808d8891 Mon Sep 17 00:00:00 2001 From: David Skidmore Date: Mon, 19 Jul 2021 11:33:44 -0700 Subject: [PATCH 4/9] Prevent passing both target_triple and target_json simultaneously. --- rust/toolchain.bzl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index f428ffbfc4..f6202ff9f6 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -187,6 +187,9 @@ def _rust_toolchain_impl(ctx): if not k in ctx.attr.opt_level: fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k)) + if ctx.attr.target_triple and ctx.file.target_json != None: + fail("Do not specify both target_triple and target_json, either use a builtin triple or provide a custom specification file.") + toolchain = platform_common.ToolchainInfo( rustc = ctx.file.rustc, rust_doc = ctx.file.rust_doc, From 31279574ba527efcad1abb6f1e34a1ec435fa07b Mon Sep 17 00:00:00 2001 From: David Skidmore Date: Tue, 20 Jul 2021 09:44:18 -0700 Subject: [PATCH 5/9] Streamline checks for None. --- cargo/cargo_build_script.bzl | 2 +- rust/private/rustc.bzl | 2 +- rust/toolchain.bzl | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cargo/cargo_build_script.bzl b/cargo/cargo_build_script.bzl index b2f94b8cbe..755ffe81a0 100644 --- a/cargo/cargo_build_script.bzl +++ b/cargo/cargo_build_script.bzl @@ -129,7 +129,7 @@ def _build_script_impl(ctx): script, ctx.executable._cargo_build_script_runner, toolchain.rustc, - ] + ctx.files.data + ([] if toolchain.target_json == None else [toolchain.target_json]), + ] + ctx.files.data + ([toolchain.target_json] if toolchain.target_json else []), transitive = toolchain_tools, ) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 4f66815ca4..eae3772577 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -287,7 +287,7 @@ def collect_inputs( [toolchain.rustc] + toolchain.crosstool_files + ([build_info.rustc_env, build_info.flags] if build_info else []) + - ([] if toolchain.target_json == None else [toolchain.target_json]) + + ([toolchain.target_json] if toolchain.target_json else []) + ([] if linker_script == None else [linker_script]), transitive = [ toolchain.rustc_lib.files, diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index f6202ff9f6..0d04652cd5 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -187,7 +187,7 @@ def _rust_toolchain_impl(ctx): if not k in ctx.attr.opt_level: fail("Compilation mode {} is not defined in opt_level but is defined debug_info".format(k)) - if ctx.attr.target_triple and ctx.file.target_json != None: + if ctx.attr.target_triple and ctx.file.target_json: fail("Do not specify both target_triple and target_json, either use a builtin triple or provide a custom specification file.") toolchain = platform_common.ToolchainInfo( @@ -197,7 +197,7 @@ def _rust_toolchain_impl(ctx): cargo = ctx.file.cargo, clippy_driver = ctx.file.clippy_driver, target_json = ctx.file.target_json, - target_flag_value = ctx.file.target_json.path if ctx.file.target_json != None else ctx.attr.target_triple, + target_flag_value = ctx.file.target_json.path if ctx.file.target_json else ctx.attr.target_triple, rustc_lib = ctx.attr.rustc_lib, rustc_srcs = ctx.attr.rustc_srcs, rust_lib = ctx.attr.rust_lib, From 7ec841ebc4bc7627264191d46c1def38dfdb7b71 Mon Sep 17 00:00:00 2001 From: David Skidmore Date: Tue, 20 Jul 2021 15:08:05 -0700 Subject: [PATCH 6/9] Added unit tests for rust_toolchain rules. --- test/unit/toolchain/BUILD | 3 + test/unit/toolchain/toolchain_test.bzl | 80 +++++++++++++++++++ .../toolchain/x86_64-unknown-linux-gnu.json | 34 ++++++++ 3 files changed, 117 insertions(+) create mode 100644 test/unit/toolchain/BUILD create mode 100644 test/unit/toolchain/toolchain_test.bzl create mode 100644 test/unit/toolchain/x86_64-unknown-linux-gnu.json diff --git a/test/unit/toolchain/BUILD b/test/unit/toolchain/BUILD new file mode 100644 index 0000000000..b42717221a --- /dev/null +++ b/test/unit/toolchain/BUILD @@ -0,0 +1,3 @@ +load(":toolchain_test.bzl", "toolchain_test_suite") + +toolchain_test_suite(name = "toolchain_test_suite") diff --git a/test/unit/toolchain/toolchain_test.bzl b/test/unit/toolchain/toolchain_test.bzl new file mode 100644 index 0000000000..a7a7c08a4a --- /dev/null +++ b/test/unit/toolchain/toolchain_test.bzl @@ -0,0 +1,80 @@ +"""Unit tests for rust toolchains.""" + +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("//rust:toolchain.bzl", "rust_stdlib_filegroup", "rust_toolchain") + +def _toolchain_specifies_target_triple_test_impl(ctx): + env = analysistest.begin(ctx) + toolchain_info = analysistest.target_under_test(env)[platform_common.ToolchainInfo] + + asserts.equals(env, None, toolchain_info.target_json) + asserts.equals(env, "x86_64-unknown-linux-gnu", toolchain_info.target_flag_value) + asserts.equals(env, "x86_64-unknown-linux-gnu", toolchain_info.target_triple) + + return analysistest.end(env) + +def _toolchain_specifies_target_json_test_impl(ctx): + env = analysistest.begin(ctx) + toolchain_info = analysistest.target_under_test(env)[platform_common.ToolchainInfo] + + asserts.equals(env, "x86_64-unknown-linux-gnu.json", toolchain_info.target_json.basename) + asserts.equals(env, "test/unit/toolchain/x86_64-unknown-linux-gnu.json", toolchain_info.target_flag_value) + asserts.equals(env, "", toolchain_info.target_triple) + + return analysistest.end(env) + +toolchain_specifies_target_triple_test = analysistest.make(_toolchain_specifies_target_triple_test_impl) +toolchain_specifies_target_json_test = analysistest.make(_toolchain_specifies_target_json_test_impl) + +def _toolchain_test(): + rust_stdlib_filegroup( + name = "std_libs", + srcs = [], + ) + + native.filegroup( + name = "target_json", + srcs = ["x86_64-unknown-linux-gnu.json"], + ) + + rust_toolchain( + name = "rust_triple_toolchain", + binary_ext = "", + dylib_ext = ".so", + os = "linux", + rust_lib = ":std_libs", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "x86_64-unknown-linux-gnu", + ) + + rust_toolchain( + name = "rust_json_toolchain", + binary_ext = "", + dylib_ext = ".so", + os = "linux", + rust_lib = ":std_libs", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_json = ":target_json", + ) + + toolchain_specifies_target_triple_test( + name = "toolchain_specifies_target_triple_test", + target_under_test = ":rust_triple_toolchain", + ) + toolchain_specifies_target_json_test( + name = "toolchain_specifies_target_json_test", + target_under_test = ":rust_json_toolchain", + ) + +def toolchain_test_suite(name): + _toolchain_test() + + native.test_suite( + name = name, + tests = [ + ":toolchain_specifies_target_triple_test", + ":toolchain_specifies_target_json_test", + ], + ) diff --git a/test/unit/toolchain/x86_64-unknown-linux-gnu.json b/test/unit/toolchain/x86_64-unknown-linux-gnu.json new file mode 100644 index 0000000000..1098428f40 --- /dev/null +++ b/test/unit/toolchain/x86_64-unknown-linux-gnu.json @@ -0,0 +1,34 @@ +{ + "arch": "x86_64", + "cpu": "x86-64", + "crt-static-respected": true, + "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", + "dynamic-linking": true, + "env": "gnu", + "executables": true, + "has-elf-tls": true, + "has-rpath": true, + "llvm-target": "x86_64-unknown-linux-gnu", + "max-atomic-width": 64, + "os": "linux", + "position-independent-executables": true, + "pre-link-args": { + "gcc": [ + "-m64" + ] + }, + "relro-level": "full", + "stack-probes": { + "kind": "call" + }, + "supported-sanitizers": [ + "address", + "leak", + "memory", + "thread" + ], + "target-family": [ + "unix" + ], + "target-pointer-width": "64" +} From f2d8509e04d66aa2968daf8485365b974fd20add Mon Sep 17 00:00:00 2001 From: David Skidmore Date: Tue, 20 Jul 2021 15:23:19 -0700 Subject: [PATCH 7/9] Rename test target spec to avoid collisions with the builtin. --- ...64-unknown-linux-gnu.json => toolchain-test-triple.json} | 0 test/unit/toolchain/toolchain_test.bzl | 6 +++--- 2 files changed, 3 insertions(+), 3 deletions(-) rename test/unit/toolchain/{x86_64-unknown-linux-gnu.json => toolchain-test-triple.json} (100%) diff --git a/test/unit/toolchain/x86_64-unknown-linux-gnu.json b/test/unit/toolchain/toolchain-test-triple.json similarity index 100% rename from test/unit/toolchain/x86_64-unknown-linux-gnu.json rename to test/unit/toolchain/toolchain-test-triple.json diff --git a/test/unit/toolchain/toolchain_test.bzl b/test/unit/toolchain/toolchain_test.bzl index a7a7c08a4a..7f1e6c43b5 100644 --- a/test/unit/toolchain/toolchain_test.bzl +++ b/test/unit/toolchain/toolchain_test.bzl @@ -17,8 +17,8 @@ def _toolchain_specifies_target_json_test_impl(ctx): env = analysistest.begin(ctx) toolchain_info = analysistest.target_under_test(env)[platform_common.ToolchainInfo] - asserts.equals(env, "x86_64-unknown-linux-gnu.json", toolchain_info.target_json.basename) - asserts.equals(env, "test/unit/toolchain/x86_64-unknown-linux-gnu.json", toolchain_info.target_flag_value) + asserts.equals(env, "toolchain-test-triple.json", toolchain_info.target_json.basename) + asserts.equals(env, "test/unit/toolchain/toolchain-test-triple.json", toolchain_info.target_flag_value) asserts.equals(env, "", toolchain_info.target_triple) return analysistest.end(env) @@ -34,7 +34,7 @@ def _toolchain_test(): native.filegroup( name = "target_json", - srcs = ["x86_64-unknown-linux-gnu.json"], + srcs = ["toolchain-test-triple.json"], ) rust_toolchain( From 2eda8dfedce8dbc2d5608c94d6a02ca6d328969c Mon Sep 17 00:00:00 2001 From: David Skidmore Date: Tue, 20 Jul 2021 15:23:59 -0700 Subject: [PATCH 8/9] Regenerate documentation --- docs/flatten.md | 4 +++- docs/rust_repositories.md | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/flatten.md b/docs/flatten.md index aca03640fe..f0466bf722 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1085,7 +1085,8 @@ Run the test with `bazel build //hello_lib:hello_lib_test`.
 rust_toolchain(name, allocator_library, binary_ext, cargo, clippy_driver, debug_info,
                default_edition, dylib_ext, exec_triple, opt_level, os, rust_doc, rust_lib, rustc,
-               rustc_lib, rustc_srcs, rustfmt, staticlib_ext, stdlib_linkflags, target_triple)
+               rustc_lib, rustc_srcs, rustfmt, staticlib_ext, stdlib_linkflags, target_json,
+               target_triple)
 
Declares a Rust toolchain for use. @@ -1153,6 +1154,7 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r | rustfmt | The location of the rustfmt binary. Can be a direct source or a filegroup containing one item. | Label | optional | None | | staticlib_ext | The extension for static libraries created from rustc. | String | required | | | stdlib_linkflags | Additional linker libs used when std lib is linked, see https://github.com/rust-lang/rust/blob/master/src/libstd/build.rs | List of strings | required | | +| target_json | Override the target_triple with a custom target specification. For more details see: https://doc.rust-lang.org/rustc/targets/custom.html | Label | optional | None | | target_triple | The platform triple for the toolchains target environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | optional | "" | diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md index 9da7eeb0ad..5909ee4f36 100644 --- a/docs/rust_repositories.md +++ b/docs/rust_repositories.md @@ -34,7 +34,8 @@ A dedicated filegroup-like rule for Rust stdlib artifacts.
 rust_toolchain(name, allocator_library, binary_ext, cargo, clippy_driver, debug_info,
                default_edition, dylib_ext, exec_triple, opt_level, os, rust_doc, rust_lib, rustc,
-               rustc_lib, rustc_srcs, rustfmt, staticlib_ext, stdlib_linkflags, target_triple)
+               rustc_lib, rustc_srcs, rustfmt, staticlib_ext, stdlib_linkflags, target_json,
+               target_triple)
 
Declares a Rust toolchain for use. @@ -102,6 +103,7 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r | rustfmt | The location of the rustfmt binary. Can be a direct source or a filegroup containing one item. | Label | optional | None | | staticlib_ext | The extension for static libraries created from rustc. | String | required | | | stdlib_linkflags | Additional linker libs used when std lib is linked, see https://github.com/rust-lang/rust/blob/master/src/libstd/build.rs | List of strings | required | | +| target_json | Override the target_triple with a custom target specification. For more details see: https://doc.rust-lang.org/rustc/targets/custom.html | Label | optional | None | | target_triple | The platform triple for the toolchains target environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | optional | "" | From 975271700fb0728454456c0160665ef3cb0863a3 Mon Sep 17 00:00:00 2001 From: David Skidmore Date: Tue, 20 Jul 2021 17:06:30 -0700 Subject: [PATCH 9/9] Rename the triple used in unit tests. --- test/unit/toolchain/toolchain_test.bzl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/toolchain/toolchain_test.bzl b/test/unit/toolchain/toolchain_test.bzl index 7f1e6c43b5..0e44df993d 100644 --- a/test/unit/toolchain/toolchain_test.bzl +++ b/test/unit/toolchain/toolchain_test.bzl @@ -8,8 +8,8 @@ def _toolchain_specifies_target_triple_test_impl(ctx): toolchain_info = analysistest.target_under_test(env)[platform_common.ToolchainInfo] asserts.equals(env, None, toolchain_info.target_json) - asserts.equals(env, "x86_64-unknown-linux-gnu", toolchain_info.target_flag_value) - asserts.equals(env, "x86_64-unknown-linux-gnu", toolchain_info.target_triple) + asserts.equals(env, "toolchain-test-triple", toolchain_info.target_flag_value) + asserts.equals(env, "toolchain-test-triple", toolchain_info.target_triple) return analysistest.end(env) @@ -45,7 +45,7 @@ def _toolchain_test(): rust_lib = ":std_libs", staticlib_ext = ".a", stdlib_linkflags = [], - target_triple = "x86_64-unknown-linux-gnu", + target_triple = "toolchain-test-triple", ) rust_toolchain(