From 31143247f9c2ab78aee92c07dd7e664904e8b4f2 Mon Sep 17 00:00:00 2001 From: David Chen Date: Mon, 27 Jul 2015 08:40:47 +0000 Subject: Add tools dependencies for Rust rules. TESTED=manual -- MOS_MIGRATED_REVID=99161344 --- tools/build_rules/rust/README.md | 5 +- tools/build_rules/rust/rust-darwin-x86_64.BUILD | 23 ++++++ tools/build_rules/rust/rust-linux-x86_64.BUILD | 23 ++++++ tools/build_rules/rust/rust.WORKSPACE | 13 ++++ tools/build_rules/rust/rust.bzl | 95 ++++++++++++++++--------- 5 files changed, 123 insertions(+), 36 deletions(-) create mode 100644 tools/build_rules/rust/rust-darwin-x86_64.BUILD create mode 100644 tools/build_rules/rust/rust-linux-x86_64.BUILD create mode 100644 tools/build_rules/rust/rust.WORKSPACE (limited to 'tools/build_rules') diff --git a/tools/build_rules/rust/README.md b/tools/build_rules/rust/README.md index 4021477e20..369ca5e9e4 100644 --- a/tools/build_rules/rust/README.md +++ b/tools/build_rules/rust/README.md @@ -17,9 +17,8 @@ These build rules are used for building [Rust][rust] projects with Bazel. ## Setup -Install Rust following the instructions on the [Rust website][rust]. The version -of Rust currently supported by these build rules is Rust 1.0.0. These build -rules also assume that Rust is installed either either `/usr` or `/usr/local`. +To use the Rust rules, simply copy the contents of `rust.WORKSPACE` to your +`WORKSPACE` file. ## Basic Example diff --git a/tools/build_rules/rust/rust-darwin-x86_64.BUILD b/tools/build_rules/rust/rust-darwin-x86_64.BUILD new file mode 100644 index 0000000000..2f067e135b --- /dev/null +++ b/tools/build_rules/rust/rust-darwin-x86_64.BUILD @@ -0,0 +1,23 @@ +BASE_DIR = "rust-1.1.0-x86_64-apple-darwin/" + +filegroup( + name = "rustc", + srcs = [BASE_DIR + "rustc/bin/rustc"], + visibility = ["//visibility:public"], +) + +filegroup( + name = "rustdoc", + srcs = [BASE_DIR + "rustc/bin/rustdoc"], + visibility = ["//visibility:public"], +) + +filegroup( + name = "rustlib", + srcs = glob([ + BASE_DIR + "rustc/lib/rustlib/x86_64-apple-darwin/lib/*.rlib", + BASE_DIR + "rustc/lib/rustlib/x86_64-apple-darwin/lib/*.dylib", + BASE_DIR + "rustc/lib/rustlib/x86_64-apple-darwin/lib/*.a", + ]), + visibility = ["//visibility:public"], +) diff --git a/tools/build_rules/rust/rust-linux-x86_64.BUILD b/tools/build_rules/rust/rust-linux-x86_64.BUILD new file mode 100644 index 0000000000..2ec77289a3 --- /dev/null +++ b/tools/build_rules/rust/rust-linux-x86_64.BUILD @@ -0,0 +1,23 @@ +BASE_DIR = "rust-1.1.0-x86_64-unknown-linux-gnu/" + +filegroup( + name = "rustc", + srcs = [BASE_DIR + "rustc/bin/rustc"], + visibility = ["//visibility:public"], +) + +filegroup( + name = "rustdoc", + srcs = [BASE_DIR + "rustc/bin/rustdoc"], + visibility = ["//visibility:public"], +) + +filegroup( + name = "rustlib", + srcs = glob([ + BASE_DIR + "rustc/lib/rustlib/x86_64-unknown-linux-gnu/lib/*.rlib", + BASE_DIR + "rustc/lib/rustlib/x86_64-unknown-linux-gnu/lib/*.so", + BASE_DIR + "rustc/lib/rustlib/x86_64-unknown-linux-gnu/lib/*.a", + ]), + visibility = ["//visibility:public"], +) diff --git a/tools/build_rules/rust/rust.WORKSPACE b/tools/build_rules/rust/rust.WORKSPACE new file mode 100644 index 0000000000..3b307d264a --- /dev/null +++ b/tools/build_rules/rust/rust.WORKSPACE @@ -0,0 +1,13 @@ +new_http_archive( + name = "rust-linux-x86_64", + url = "https://static.rust-lang.org/dist/rust-1.1.0-x86_64-unknown-linux-gnu.tar.gz", + sha256 = "5a8b1c4bb254a698a69cd05734909a3933567be6996422ff53f947fd115372e6", + build_file = "tools/build_rules/rust/rust-linux-x86_64.BUILD", +) + +new_http_archive( + name = "rust-darwin-x86_64", + url = "https://static.rust-lang.org/dist/rust-1.1.0-x86_64-apple-darwin.tar.gz", + sha256 = "ac802916da3f9c431377c00b864a517bc356859495b7a8a123ce2c532ee8fa83", + build_file = "tools/build_rules/rust/rust-darwin-x86_64.BUILD", +) diff --git a/tools/build_rules/rust/rust.bzl b/tools/build_rules/rust/rust.bzl index 5e84cc8491..c5c505783e 100644 --- a/tools/build_rules/rust/rust.bzl +++ b/tools/build_rules/rust/rust.bzl @@ -112,6 +112,50 @@ def _get_features_flags(features): features_flags += [" --cfg feature=\\\"" + feature + "\\\""] return features_flags +def _build_rustc_command(ctx, crate_type, src, output_dir, depinfo, + extra_flags=[]): + """ + Builds the rustc command + """ + + # Paths to the Rust compiler and standard libraries. + rustc_path = ctx.file._rustc.path + rustlib_path = ctx.files._rustlib[0].dirname + + # Paths to cc (for linker) and ar + cpp_fragment = ctx.configuration.fragment(cpp) + cc = cpp_fragment.compiler_executable + ar = cpp_fragment.ar_executable + # Currently, the CROSSTOOL config for darwin sets ar to "libtool". Because + # rust uses ar-specific flags, use /usr/bin/ar in this case. + # TODO(dzc): This is not ideal. Remove this workaround once ar_executable + # always points to an ar binary. + ar_str = "%s" % ar + if ar_str.find("libtool", 0) != -1: + ar = "/usr/bin/ar" + + # Construct features flags + features_flags = _get_features_flags(ctx.attr.features) + + return " ".join([ + "set -e;", + " ".join(depinfo.setup_cmd), + rustc_path + " " + src, + "--crate-name " + ctx.label.name, + "--crate-type " + crate_type, + "-g", + "--codegen ar=%s" % ar, + "--codegen linker=%s" % cc, + "-L all=" + rustlib_path, + " ".join(extra_flags), + " ".join(features_flags), + "--out-dir " + output_dir, + "--emit=dep-info,link", + " ".join(depinfo.search_flags), + " ".join(depinfo.link_flags), + " ".join(ctx.attr.rustc_flags), + ]) + def _rust_library_impl(ctx): """ Implementation for rust_library Skylark rule. @@ -134,24 +178,14 @@ def _rust_library_impl(ctx): # Dependencies depinfo = _setup_deps(ctx.attr.deps, ctx.label.name, output_dir) - features_flags = _get_features_flags(ctx.attr.features) # Build rustc command - # TODO(dzc): There is a tools dependency on rustc. Use a remote repository - # mechanism to fetch rustc. - cmd = ( - "set -e;export PATH=/usr/bin:/usr/local/bin:$PATH;" + - " " + " ".join(depinfo.setup_cmd) + - "rustc " + lib_rs + - " --crate-name " + ctx.label.name + - " --crate-type lib -g" + - " " + " ".join(features_flags) + - " --out-dir " + output_dir + - " --emit=dep-info,link" + - " " + " ".join(depinfo.search_flags) + - " " + " ".join(depinfo.link_flags) + - " " + " ".join(ctx.attr.rustc_flags) - ) + cmd = _build_rustc_command( + ctx = ctx, + crate_type = "lib", + src = lib_rs, + output_dir = output_dir, + depinfo = depinfo) # Compile action. ctx.action( @@ -191,25 +225,15 @@ def _rust_binary_impl_common(ctx, extra_flags = []): # Dependencies depinfo = _setup_deps(ctx.attr.deps, ctx.label.name, output_dir) - features_flags = _get_features_flags(ctx.attr.features) # Build rustc command. - # TODO(dzc): There is a tools dependency on rustc. Use a remote repository - # mechanism to fetch rustc. - cmd = ( - "set -e;export PATH=/usr/bin:/usr/local/bin:$PATH;" + - " " + " ".join(depinfo.setup_cmd) + - "rustc " + main_rs + - " --crate-name " + ctx.label.name + - " --crate-type bin -g" + - " " + " ".join(extra_flags) + - " " + " ".join(features_flags) + - " --out-dir " + output_dir + - " --emit=dep-info,link" + - " " + " ".join(depinfo.search_flags) + - " " + " ".join(depinfo.link_flags) + - " " + " ".join(ctx.attr.rustc_flags) - ) + cmd = _build_rustc_command( + ctx = ctx, + crate_type = "bin", + src = main_rs, + output_dir = output_dir, + depinfo = depinfo, + extra_flags = extra_flags) # Compile action. ctx.action( @@ -239,6 +263,11 @@ _rust_common_attrs = { "deps": attr.label_list(), "features": attr.string_list(), "rustc_flags": attr.string_list(), + "_rustc": attr.label( + default = Label("//tools/rust:rustc"), + executable = True, + single_file = True), + "_rustlib": attr.label(default = Label("//tools/rust:rustlib")), } rust_library = rule( -- cgit v1.2.3