From a04ff4182c4e7049a98a207a81f844fd89dbacaa Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Tue, 27 Apr 2021 10:15:18 -0700 Subject: [PATCH] Added crate_universe examples to CI (#707) * Added crate_universe examples to CI * Update crate_universe/bootstrap.bzl Co-authored-by: Daniel Wagner-Hall * Updated docs * Remove crate_universe.bazelrc * Updated attribute names * Deleted example lockfile Co-authored-by: Daniel Wagner-Hall --- .bazelci/presubmit.yml | 48 +- .bazelrc | 3 - .gitignore | 1 - WORKSPACE.bazel | 7 +- bootstrap.sh | 35 - crate_universe/BUILD.bazel | 10 + crate_universe/bootstrap.bzl | 103 +++ crate_universe/defs.bzl | 156 ++--- crate_universe/private/bootstrap/build.sh | 17 +- crate_universe/private/util.bzl | 97 +++ examples/crate_universe/.bazelrc | 3 - examples/crate_universe/.gitignore | 1 - examples/crate_universe/WORKSPACE.bazel | 35 + examples/crate_universe/basic/BUILD.bazel | 2 +- .../has_aliased_deps/BUILD.openssl.bazel | 6 +- .../uses_sys_crate/lockfile.lock | 603 ------------------ .../uses_sys_crate/workspace.bzl | 1 - 17 files changed, 342 insertions(+), 786 deletions(-) delete mode 100644 .bazelrc delete mode 100755 bootstrap.sh create mode 100644 crate_universe/bootstrap.bzl create mode 100644 crate_universe/private/util.bzl delete mode 100644 examples/crate_universe/.bazelrc delete mode 100644 examples/crate_universe/uses_sys_crate/lockfile.lock diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 4b22c882c8..493cb2c94f 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -109,7 +109,53 @@ tasks: # - "--linkopt=-fuse-ld=lld" build_targets: *default_linux_targets test_targets: *default_linux_targets - + crate_universe_examples_ubuntu2004: + name: Crate Universe Examples + platform: ubuntu2004 + environment: + RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP: true + working_directory: examples/crate_universe + build_targets: + - "//..." + test_targets: + - "//..." + crate_universe_rbe_ubuntu1604: + name: Crate Universe Examples + platform: rbe_ubuntu1604 + environment: + RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP: true + working_directory: examples/crate_universe + build_targets: + - "//..." + test_targets: + - "//..." + crate_universe_examples_macos: + name: Crate Universe Examples + platform: macos + environment: + RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP: true + working_directory: examples/crate_universe + build_targets: + - "//..." + test_targets: + - "//..." + crate_universe_examples_windows: + name: Crate Universe Examples + platform: windows + environment: + RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP: true + working_directory: examples/crate_universe + crate_universe_windows_targets: &crate_universe_windows_targets + - "//..." + # TODO: There are windows specific build issues in the generated + # depednencies. This should be fixed and all testing enabled. + - "-//basic:test" + - "-//has_aliased_deps/..." + - "-//uses_proc_macro/..." + - "-//uses_sys_crate/..." + build_targets: *crate_universe_windows_targets + # TODO: fix test targets in `crate_universe_windows_targets` + # test_targets: *crate_universe_windows_targets buildifier: version: latest warnings: "all" diff --git a/.bazelrc b/.bazelrc deleted file mode 100644 index 5410c6ec86..0000000000 --- a/.bazelrc +++ /dev/null @@ -1,3 +0,0 @@ -# When building in CI, a bazelrc file may be generated for overriding -# the binary used by crate_universe -try-import %workspace%/crate_universe.bazelrc diff --git a/.gitignore b/.gitignore index 25f75fe373..2f8bfb1f1d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,6 @@ /examples/bazel-* /examples/crate_universe/*/bazel-* /docs/bazel-* -crate_universe.bazelrc # rustfmt *.rs.bk diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 67c7f5117c..32ac4264a4 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -34,11 +34,10 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "bazel_toolchains", - sha256 = "d8c2f20deb2f6143bac792d210db1a4872102d81529fe0ea3476c1696addd7ff", - strip_prefix = "bazel-toolchains-0.28.3", + sha256 = "1adf7a8e9901287c644dcf9ca08dd8d67a69df94bedbd57a841490a84dc1e9ed", + strip_prefix = "bazel-toolchains-5.0.0", urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/bazel-toolchains/archive/0.28.3.tar.gz", - "https://github.com/bazelbuild/bazel-toolchains/archive/0.28.3.tar.gz", + "https://github.com/bazelbuild/bazel-toolchains/archive/refs/tags/v5.0.0.tar.gz", ], ) diff --git a/bootstrap.sh b/bootstrap.sh deleted file mode 100755 index b1fca6f218..0000000000 --- a/bootstrap.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -####################################################################################################################### -# Crate Universe -####################################################################################################################### - -# Find the location of the script -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" -BOOTSTRAP_DIR="${SCRIPT_DIR}/crate_universe/private/bootstrap" - -# Go to root of rules_rust -pushd "${SCRIPT_DIR}" &> /dev/null -has_changes="$(git diff --name-only origin/main -- crate_universe bootstrap.sh)" -popd &> /dev/null - -# Only bootstrap the binary if there are changes -if [[ -n "${has_changes}" ]] || git ls-files --others --exclude-standard | grep 'crate_universe'; then - pushd "${BOOTSTRAP_DIR}" &> /dev/null - bazel run //:build && bazel run //:install && bazel clean - popd &> /dev/null - - # Generate a bazelrc file for ensuring we always use the resolver binary for the current host - cat << EOF > "${SCRIPT_DIR}/crate_universe.bazelrc" -common --announce_rc -common --repo_env="RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE=file://${BOOTSTRAP_DIR}/file/resolver" -EOF - - cp ${SCRIPT_DIR}/crate_universe.bazelrc ${SCRIPT_DIR}/examples/crate_universe/crate_universe.bazelrc -else - echo "No changes to crate_universe. Nothing to do" -fi - -####################################################################################################################### diff --git a/crate_universe/BUILD.bazel b/crate_universe/BUILD.bazel index 9aa1da8f0f..5b5bf15ab7 100644 --- a/crate_universe/BUILD.bazel +++ b/crate_universe/BUILD.bazel @@ -2,8 +2,18 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") package(default_visibility = ["//visibility:public"]) +exports_files([ + "Cargo.toml", + "Cargo.lock", +]) + bzl_library( name = "rules", srcs = glob(["**/*.bzl"]), deps = ["//crate_universe/private:bzl_srcs"], ) + +filegroup( + name = "resolver_srcs", + srcs = glob(["srcs/**"]), +) diff --git a/crate_universe/bootstrap.bzl b/crate_universe/bootstrap.bzl new file mode 100644 index 0000000000..d755b7d59a --- /dev/null +++ b/crate_universe/bootstrap.bzl @@ -0,0 +1,103 @@ +"""A module for declaraing a repository for bootstrapping crate_universe""" + +load("//crate_universe/private:util.bzl", "get_host_info") + +BOOTSTRAP_ENV_VAR = "RULES_RUST_CRATE_UNIVERSE_BOOTSTRAP" + +_INSTALL_SCRIPT_CONTENT = """\ +#!/bin/bash + +set -euo pipefail + +cp "${CRATE_RESOLVER_BIN}" "$@" +""" + +_BUILD_FILE_CONTENT = """\ +package(default_visibility = ["//visibility:public"]) + +exports_files(["release/crate_universe_resolver{ext}"]) + +sh_binary( + name = "install", + data = [ + ":release/crate_universe_resolver{ext}", + ], + env = {{ + "CRATE_RESOLVER_BIN": "$(execpath :release/crate_universe_resolver{ext})", + }}, + srcs = ["install.sh"], +) +""" + +def _crate_universe_resolver_bootstrapping_impl(repository_ctx): + # no-op if there has been no request for bootstrapping + if BOOTSTRAP_ENV_VAR not in repository_ctx.os.environ: + repository_ctx.file("BUILD.bazel") + return + + resolver_triple, toolchain_repo, extension = get_host_info(repository_ctx) + + cargo_path = repository_ctx.path(Label(toolchain_repo + "//:bin/cargo" + extension)) + rustc_path = repository_ctx.path(Label(toolchain_repo + "//:bin/rustc" + extension)) + + repository_dir = repository_ctx.path(".") + resolver_path = repository_ctx.path("release/crate_universe_resolver" + extension) + + args = [ + cargo_path, + "build", + "--release", + "--locked", + "--target-dir", + repository_dir, + "--manifest-path", + repository_ctx.path(repository_ctx.attr.cargo_toml), + ] + + repository_ctx.report_progress("bootstrapping crate_universe_resolver") + result = repository_ctx.execute( + args, + environment = { + "RUSTC": str(rustc_path), + }, + quiet = False, + ) + + if result.return_code != 0: + fail("exit_code: {}".format( + result.return_code, + )) + + repository_ctx.file("install.sh", _INSTALL_SCRIPT_CONTENT) + + repository_ctx.file("BUILD.bazel", _BUILD_FILE_CONTENT.format( + ext = extension, + )) + +_crate_universe_resolver_bootstrapping = repository_rule( + doc = "A rule for bootstrapping a crate_universe_resolver binary using [Cargo](https://doc.rust-lang.org/cargo/)", + implementation = _crate_universe_resolver_bootstrapping_impl, + attrs = { + "cargo_lockfile": attr.label( + doc = "The lockfile of the crate_universe resolver", + allow_single_file = ["Cargo.lock"], + default = Label("//crate_universe:Cargo.lock"), + ), + "cargo_toml": attr.label( + doc = "The path of the crate_universe resolver manifest (`Cargo.toml` file)", + allow_single_file = ["Cargo.toml"], + default = Label("//crate_universe:Cargo.toml"), + ), + "srcs": attr.label( + doc = "Souces to the crate_universe resolver", + allow_files = True, + default = Label("//crate_universe:resolver_srcs"), + ), + }, + environ = [BOOTSTRAP_ENV_VAR], +) + +def crate_universe_bootstrap(): + _crate_universe_resolver_bootstrapping( + name = "rules_rust_crate_universe_bootstrap", + ) diff --git a/crate_universe/defs.bzl b/crate_universe/defs.bzl index e3d2db4542..99b9acc296 100644 --- a/crate_universe/defs.bzl +++ b/crate_universe/defs.bzl @@ -1,19 +1,13 @@ """A module defining the `crate_universe` rule""" load("//crate_universe/private:defaults.bzl", "DEFAULT_SHA256_CHECKSUMS", "DEFAULT_URL_TEMPLATE") +load("//crate_universe/private:util.bzl", "get_host_info") load("//rust:repositories.bzl", "DEFAULT_TOOLCHAIN_TRIPLES") +load(":bootstrap.bzl", "BOOTSTRAP_ENV_VAR") DEFAULT_CRATE_REGISTRY_TEMPLATE = "https://crates.io/api/v1/crates/{crate}/{version}/download" -_CPU_ARCH_ERROR_MSG = """\ -Command failed with exit code '{code}': {args} -----------stdout: -{stdout} -----------stderr: -{stderr} -""" - -def _input_content_template(ctx, name, packages, cargo_toml_files, overrides, registry_template, targets, cargo_bin_label): +def _input_content_template(ctx, name, packages, cargo_toml_files, overrides, registry_template, targets, cargo_bin_path): """Generate json encoded dependency info for the crate resolver. Args: @@ -24,7 +18,7 @@ def _input_content_template(ctx, name, packages, cargo_toml_files, overrides, re overrides (dict): A dict of crate name (`str`) to json encoded `crate.override` data. registry_template (str): A crate registry url template targets (list): A list of target platform triples - cargo_bin_label (Label): The label of a Cargo binary. + cargo_bin_path (path): The label of a Cargo binary. Returns: str: Json encoded config data for the resolver @@ -48,7 +42,7 @@ def _input_content_template(ctx, name, packages, cargo_toml_files, overrides, re return "{}\n".format( json.encode_indent( struct( - cargo = str(ctx.path(cargo_bin_label)), + cargo = str(cargo_bin_path), cargo_toml_files = encodable_cargo_toml_files, crate_registry_template = registry_template, overrides = encodable_overrides, @@ -60,88 +54,6 @@ def _input_content_template(ctx, name, packages, cargo_toml_files, overrides, re ), ) -def _query_cpu_architecture(repository_ctx, expected_archs, is_windows = False): - """Detect the host CPU architecture - - Args: - repository_ctx (repository_ctx): The repository rule's context object - expected_archs (list): A list of expected architecture strings - is_windows (bool, optional): If true, the cpu lookup will use the windows method (`wmic` vs `uname`) - - Returns: - str: The host's CPU architecture - """ - if is_windows: - arguments = ["wmic", "os", "get", "osarchitecture"] - else: - arguments = ["uname", "-m"] - - result = repository_ctx.execute(arguments) - - if result.return_code: - fail(_CPU_ARCH_ERROR_MSG.format( - code = result.return_code, - args = arguments, - stdout = result.stdout, - stderr = result.stderr, - )) - - if is_windows: - # Example output: - # OSArchitecture - # 64-bit - lines = result.stdout.split("\n") - arch = lines[1] - else: - arch = result.stdout.strip("\n") - - if not arch in expected_archs: - fail("{} is not a expected cpu architecture. {}".format( - arch, - expected_archs, - )) - - return arch - -def _get_host_info(repository_ctx): - """Query host information for the appropriate triple and toolchain repo name - - Args: - repository_ctx (repository_ctx): The rule's repository_ctx - - Returns: - tuple: A tuple containing a triple (str) and repository name (str) - """ - - # Detect the host's cpu architecture - - supported_architectures = { - "linux": ["aarch64", "x86_64"], - "macos": ["aarch64", "x86_64"], - "windows": ["x86_64"], - } - - # The expected file extension of crate resolver binaries - extension = "" - - if "linux" in repository_ctx.os.name: - cpu = _query_cpu_architecture(repository_ctx, supported_architectures["linux"]) - resolver_triple = "{}-unknown-linux-gnu".format(cpu) - toolchain_repo = "@rust_linux_{}".format(cpu) - elif "mac" in repository_ctx.os.name: - cpu = _query_cpu_architecture(repository_ctx, supported_architectures["macos"]) - resolver_triple = "{}-apple-darwin".format(cpu) - toolchain_repo = "@rust_darwin_{}".format(cpu) - elif "win" in repository_ctx.os.name: - cpu = _query_cpu_architecture(repository_ctx, supported_architectures["windows"], True) - resolver_triple = "{}-pc-windows-gnu".format(cpu) - toolchain_repo = "@rust_windows_{}".format(cpu) - extension = ".exe" - else: - fail("Could not locate resolver for OS " + repository_ctx.os.name) - - return (resolver_triple, toolchain_repo, extension) - def _crate_universe_resolve_impl(repository_ctx): """Entry-point repository to manage rust dependencies. @@ -153,29 +65,36 @@ def _crate_universe_resolve_impl(repository_ctx): """ # Get info about the current host's tool locations - resolver_triple, toolchain_repo, extension = _get_host_info(repository_ctx) - - cargo_label = Label(toolchain_repo + "//:bin/cargo") - rustc_label = Label(toolchain_repo + "//:bin/rustc") - - # Allow for an an override environment variable to define a url to a binary - resolver_url = repository_ctx.os.environ.get("RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE", None) - resolver_sha = repository_ctx.os.environ.get("RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE_SHA256", None) - if resolver_url: - if resolver_url.startswith("file://"): - sha256_result = repository_ctx.execute(["sha256sum", resolver_url[7:]]) - resolver_sha = sha256_result.stdout[:64] + resolver_triple, toolchain_repo, extension = get_host_info(repository_ctx) + + cargo_path = repository_ctx.path(Label(toolchain_repo + "//:bin/cargo" + extension)) + rustc_path = repository_ctx.path(Label(toolchain_repo + "//:bin/rustc" + extension)) + + if BOOTSTRAP_ENV_VAR in repository_ctx.os.environ and not "RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE" in repository_ctx.os.environ: + resolver_label = Label("@rules_rust_crate_universe_bootstrap//:release/crate_universe_resolver" + extension) + resolver_path = repository_ctx.path(resolver_label) else: - resolver_url = repository_ctx.attr.resolver_download_url_template.format(host_triple = resolver_triple, extension = extension) - resolver_sha = repository_ctx.attr.resolver_sha256s.get(resolver_triple, None) - - resolver_path = repository_ctx.path("resolver") - repository_ctx.download( - url = resolver_url, - sha256 = resolver_sha, - output = resolver_path, - executable = True, - ) + # Allow for an an override environment variable to define a url to a binary + resolver_url = repository_ctx.os.environ.get("RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE", None) + resolver_sha = repository_ctx.os.environ.get("RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE_SHA256", None) + if resolver_url: + if resolver_url.startswith("file://"): + sha256_result = repository_ctx.execute(["sha256sum", resolver_url[7:]]) + resolver_sha = sha256_result.stdout[:64] + else: + resolver_url = repository_ctx.attr.resolver_download_url_template.format( + host_triple = resolver_triple, + extension = extension, + ) + resolver_sha = repository_ctx.attr.resolver_sha256s.get(resolver_triple, None) + + resolver_path = repository_ctx.path("resolver") + repository_ctx.download( + url = resolver_url, + sha256 = resolver_sha, + output = resolver_path, + executable = True, + ) lockfile_path = None if repository_ctx.attr.lockfile: @@ -189,7 +108,7 @@ def _crate_universe_resolve_impl(repository_ctx): overrides = repository_ctx.attr.overrides, registry_template = repository_ctx.attr.crate_registry_template, targets = repository_ctx.attr.supported_targets, - cargo_bin_label = cargo_label, + cargo_bin_path = cargo_path, ) input_path = "{name}.resolver_config.json".format(name = repository_ctx.attr.name) @@ -216,8 +135,8 @@ def _crate_universe_resolve_impl(repository_ctx): environment = { # The resolver invokes `cargo metadata` which relies on `rustc` being on the $PATH # See https://github.com/rust-lang/cargo/issues/8219 - "CARGO": str(repository_ctx.path(cargo_label)), - "RUSTC": str(repository_ctx.path(rustc_label)), + "CARGO": str(cargo_path), + "RUSTC": str(rustc_path), "RUST_LOG": "info", }, ) @@ -294,6 +213,7 @@ Environment Variables: environ = [ "RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE", "RULES_RUST_CRATE_UNIVERSE_RESOLVER_URL_OVERRIDE_SHA256", + BOOTSTRAP_ENV_VAR, ], ) diff --git a/crate_universe/private/bootstrap/build.sh b/crate_universe/private/bootstrap/build.sh index e1dd773ad9..c5aab4c767 100755 --- a/crate_universe/private/bootstrap/build.sh +++ b/crate_universe/private/bootstrap/build.sh @@ -1,6 +1,6 @@ #!/bin/bash -set -euo pipefail +set -xeuo pipefail # Find the location of the script if [[ -n "${BUILD_WORKSPACE_DIRECTORY:-}" ]]; then @@ -29,16 +29,9 @@ echo "TARGETS=${TARGETS[@]}" # Specify the path to the cargo manifest MANIFEST="${SCRIPT_DIR}/../../Cargo.toml" -if [[ "${IS_WINDOWS:-}" == "true" ]]; then - WIN_PWD="$(type -t cygpath > /dev/null && cygpath $(pwd) -w || pwd -W)" - # Resolve absolute paths that could potentially be in the cargo and rustc vars - CARGO="$(echo ${CARGO-cargo} | sed "s^\${PWD}^${WIN_PWD}^")" - RUSTC="$(echo ${RUSTC-rustc} | sed "s^\${PWD}^${WIN_PWD}^")" -else - # Resolve absolute paths that could potentially be in the cargo and rustc vars - CARGO="$(echo ${CARGO-cargo} | sed "s^\${PWD}^${PWD}^")" - RUSTC="$(echo ${RUSTC-rustc} | sed "s^\${PWD}^${PWD}^")" -fi +# Resolve absolute paths that could potentially be in the cargo and rustc vars +CARGO="$(echo ${CARGO-cargo} | sed "s^\${PWD}^${PWD}^")" +RUSTC="$(echo ${RUSTC-rustc} | sed "s^\${PWD}^${PWD}^")" # If there are multiple targets or we're in github CI, ensure `cross` is installed if [[ "${#TARGETS[@]}" != 1 || -n "${GITHUB_WORKFLOW:-}" ]]; then @@ -59,7 +52,7 @@ fi # Fetch cargo dependencies in advance to streamline the build process echo "Fetch cargo dependencies" -${CARGO} fetch --manifest-path=${MANIFEST} +${CARGO} fetch --manifest-path="${MANIFEST}" if [[ -z "${OUT_DIR:-}" ]]; then OUT_DIR="${SCRIPT_DIR}/bin" diff --git a/crate_universe/private/util.bzl b/crate_universe/private/util.bzl new file mode 100644 index 0000000000..1d74d35800 --- /dev/null +++ b/crate_universe/private/util.bzl @@ -0,0 +1,97 @@ +"""Utility functions for the crate_universe resolver""" + +_CPU_ARCH_ERROR_MSG = """\ +Command failed with exit code '{code}': {args} +----------stdout: +{stdout} +----------stderr: +{stderr} +""" + +def _query_cpu_architecture(repository_ctx, expected_archs, is_windows = False): + """Detect the host CPU architecture + + Args: + repository_ctx (repository_ctx): The repository rule's context object + expected_archs (list): A list of expected architecture strings + is_windows (bool, optional): If true, the cpu lookup will use the windows method (`wmic` vs `uname`) + + Returns: + str: The host's CPU architecture + """ + if is_windows: + arguments = ["wmic", "os", "get", "osarchitecture"] + else: + arguments = ["uname", "-m"] + + result = repository_ctx.execute(arguments) + + if result.return_code: + fail(_CPU_ARCH_ERROR_MSG.format( + code = result.return_code, + args = arguments, + stdout = result.stdout, + stderr = result.stderr, + )) + + if is_windows: + # Example output: + # OSArchitecture + # 64-bit + lines = result.stdout.split("\n") + arch = lines[1].strip() + + # Translate 64-bit to a compatible rust platform + # https://doc.rust-lang.org/nightly/rustc/platform-support.html + if arch == "64-bit": + arch = "x86_64" + else: + arch = result.stdout.strip("\n") + + if not arch in expected_archs: + fail("{} is not a expected cpu architecture {}\n{}".format( + arch, + expected_archs, + result.stdout, + )) + + return arch + +def get_host_info(repository_ctx): + """Query host information for the appropriate triple and toolchain repo name + + Args: + repository_ctx (repository_ctx): The rule's repository_ctx + + Returns: + tuple: A tuple containing a triple (str) and repository name (str) + """ + + # Detect the host's cpu architecture + + supported_architectures = { + "linux": ["aarch64", "x86_64"], + "macos": ["aarch64", "x86_64"], + "windows": ["x86_64"], + } + + # The expected file extension of crate resolver binaries + extension = "" + + if "linux" in repository_ctx.os.name: + cpu = _query_cpu_architecture(repository_ctx, supported_architectures["linux"]) + resolver_triple = "{}-unknown-linux-gnu".format(cpu) + toolchain_repo = "@rust_linux_{}".format(cpu) + elif "mac" in repository_ctx.os.name: + cpu = _query_cpu_architecture(repository_ctx, supported_architectures["macos"]) + resolver_triple = "{}-apple-darwin".format(cpu) + toolchain_repo = "@rust_darwin_{}".format(cpu) + elif "win" in repository_ctx.os.name: + cpu = _query_cpu_architecture(repository_ctx, supported_architectures["windows"], True) + resolver_triple = "{}-pc-windows-gnu".format(cpu) + toolchain_repo = "@rust_windows_{}".format(cpu) + extension = ".exe" + else: + fail("Could not locate resolver for OS " + repository_ctx.os.name) + + return (resolver_triple, toolchain_repo, extension) diff --git a/examples/crate_universe/.bazelrc b/examples/crate_universe/.bazelrc deleted file mode 100644 index 5410c6ec86..0000000000 --- a/examples/crate_universe/.bazelrc +++ /dev/null @@ -1,3 +0,0 @@ -# When building in CI, a bazelrc file may be generated for overriding -# the binary used by crate_universe -try-import %workspace%/crate_universe.bazelrc diff --git a/examples/crate_universe/.gitignore b/examples/crate_universe/.gitignore index d63036d5b8..ac51a054d2 100644 --- a/examples/crate_universe/.gitignore +++ b/examples/crate_universe/.gitignore @@ -1,2 +1 @@ bazel-* -crate_universe.bazelrc diff --git a/examples/crate_universe/WORKSPACE.bazel b/examples/crate_universe/WORKSPACE.bazel index a591ba8faf..4d41a584ff 100644 --- a/examples/crate_universe/WORKSPACE.bazel +++ b/examples/crate_universe/WORKSPACE.bazel @@ -9,6 +9,10 @@ load("@rules_rust//rust:repositories.bzl", "rust_repositories") rust_repositories() +load("@rules_rust//crate_universe:bootstrap.bzl", "crate_universe_bootstrap") + +crate_universe_bootstrap() + load("//basic:workspace.bzl", basic_deps = "deps") basic_deps() @@ -40,3 +44,34 @@ uses_sys_crate_deps() load("@uses_sys_crate_deps//:defs.bzl", uses_sys_crate_deps_pinned_rust_install = "pinned_rust_install") uses_sys_crate_deps_pinned_rust_install() + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +# Used for Bazel CI +http_archive( + name = "bazel_toolchains", + sha256 = "1adf7a8e9901287c644dcf9ca08dd8d67a69df94bedbd57a841490a84dc1e9ed", + strip_prefix = "bazel-toolchains-5.0.0", + urls = [ + "https://github.com/bazelbuild/bazel-toolchains/archive/refs/tags/v5.0.0.tar.gz", + ], +) + +load("@bazel_toolchains//rules:rbe_repo.bzl", "rbe_autoconfig") + +# Creates toolchain configuration for remote execution with BuildKite CI +# for rbe_ubuntu1604 +rbe_autoconfig( + name = "buildkite_config", +) + +http_archive( + name = "rules_foreign_cc", + sha256 = "d39b6930a21aed518bf5403dbf333fff731de6314bd0d528f4df62366350a312", + strip_prefix = "rules_foreign_cc-98c88de5e0f57dd2a896dd595469ccd6bf97ceab", + url = "https://github.com/bazelbuild/rules_foreign_cc/archive/98c88de5e0f57dd2a896dd595469ccd6bf97ceab.tar.gz", +) + +load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies") + +rules_foreign_cc_dependencies(register_preinstalled_tools = False) diff --git a/examples/crate_universe/basic/BUILD.bazel b/examples/crate_universe/basic/BUILD.bazel index c20467a074..19cb580fc2 100644 --- a/examples/crate_universe/basic/BUILD.bazel +++ b/examples/crate_universe/basic/BUILD.bazel @@ -13,6 +13,6 @@ rust_binary( sh_test( name = "test", srcs = ["test.sh"], - args = ["$(location :basic)"], + args = ["$(rootpath :basic)"], data = [":basic"], ) diff --git a/examples/crate_universe/has_aliased_deps/BUILD.openssl.bazel b/examples/crate_universe/has_aliased_deps/BUILD.openssl.bazel index de4b3e29ff..77f0f8b0c4 100644 --- a/examples/crate_universe/has_aliased_deps/BUILD.openssl.bazel +++ b/examples/crate_universe/has_aliased_deps/BUILD.openssl.bazel @@ -1,4 +1,4 @@ -load("@rules_foreign_cc//tools/build_defs:configure.bzl", "configure_make") +load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make") config_setting( name = "darwin", @@ -33,7 +33,7 @@ configure_make( ":linux": {}, }), lib_source = ":srcs", - shared_libraries = select({ + out_shared_libs = select({ ":darwin": [ "libcrypto.dylib", "libssl.dylib", @@ -43,7 +43,7 @@ configure_make( "libssl.so", ], }), - static_libraries = [ + out_static_libs = [ "libcrypto.a", "libssl.a", ], diff --git a/examples/crate_universe/uses_sys_crate/lockfile.lock b/examples/crate_universe/uses_sys_crate/lockfile.lock deleted file mode 100644 index d15f7ec94d..0000000000 --- a/examples/crate_universe/uses_sys_crate/lockfile.lock +++ /dev/null @@ -1,603 +0,0 @@ -{ - "config": { - "repo_rule_name": "uses_sys_crate_deps", - "crate_registry_template": "https://crates.io/api/v1/crates/{crate}/{version}/download", - "rules_rust_workspace_name": "rules_rust" - }, - "hash": "b916e44c6b9880f343f36afe6306a62a91143212d5a24f94ad1170df9b5f6a9e", - "transitive_renderable_packages": [ - { - "crate_context": { - "pkg_name": "bzip2", - "pkg_version": "0.3.3", - "edition": "2015", - "raze_settings": { - "additional_deps": [], - "skipped_deps": [], - "extra_aliased_targets": [], - "additional_flags": [], - "additional_env": {}, - "gen_buildrs": null, - "data_attr": null, - "data_dependencies": [], - "compile_data_attr": null, - "build_data_dependencies": [], - "buildrs_additional_environment_variables": {}, - "buildrs_additional_deps": [], - "patch_args": [], - "patch_cmds": [], - "patch_cmds_win": [], - "patch_tool": null, - "patches": [], - "additional_build_file": null - }, - "canonical_additional_build_file": null, - "default_deps": { - "dependencies": [ - { - "buildable_target": "@uses_sys_crate_deps__bzip2_sys__0_1_10_1_0_8//:bzip2_sys", - "name": "bzip2-sys", - "version": "0.1.10+1.0.8", - "is_proc_macro": false - }, - { - "buildable_target": "@uses_sys_crate_deps__libc__0_2_76//:libc", - "name": "libc", - "version": "0.2.76", - "is_proc_macro": false - } - ], - "proc_macro_dependencies": [], - "data_dependencies": [], - "build_dependencies": [ - { - "buildable_target": "@uses_sys_crate_deps__bzip2_sys__0_1_10_1_0_8//:bzip2_sys", - "name": "bzip2-sys", - "version": "0.1.10+1.0.8", - "is_proc_macro": false - } - ], - "build_proc_macro_dependencies": [], - "build_data_dependencies": [], - "dev_dependencies": [], - "aliased_dependencies": [] - }, - "targeted_deps": [], - "license": { - "name": "MIT from expression \"MIT OR Apache-2.0\"", - "rating": "notice" - }, - "features": [], - "workspace_path_to_crate": "@uses_sys_crate_deps__bzip2__0_3_3//", - "workspace_member_dependents": [ - "" - ], - "workspace_member_dev_dependents": [], - "workspace_member_build_dependents": [], - "is_workspace_member_dependency": true, - "is_binary_dependency": false, - "targets": [ - { - "kind": "lib", - "name": "bzip2", - "path": "src/lib.rs", - "edition": "2015" - }, - { - "kind": "test", - "name": "tokio", - "path": "tests/tokio.rs", - "edition": "2015" - } - ], - "build_script_target": null, - "links": null, - "source_details": { - "git_data": null - }, - "sha256": "42b7c3cbf0fa9c1b82308d57191728ca0256cb821220f4e2fd410a72ade26e3b", - "registry_url": "https://crates.io/api/v1/crates/bzip2/0.3.3/download", - "expected_build_path": "remote/BUILD.bzip2-0.3.3.bazel", - "lib_target_name": "bzip2", - "is_proc_macro": false - }, - "per_triple_metadata": { - "//conditions:default": { - "target": "Default", - "deps": { - "dependencies": [ - { - "buildable_target": "@uses_sys_crate_deps__bzip2_sys__0_1_10_1_0_8//:bzip2_sys", - "name": "bzip2-sys", - "version": "0.1.10+1.0.8", - "is_proc_macro": false - }, - { - "buildable_target": "@uses_sys_crate_deps__libc__0_2_76//:libc", - "name": "libc", - "version": "0.2.76", - "is_proc_macro": false - } - ], - "proc_macro_dependencies": [], - "data_dependencies": [], - "build_dependencies": [ - { - "buildable_target": "@uses_sys_crate_deps__bzip2_sys__0_1_10_1_0_8//:bzip2_sys", - "name": "bzip2-sys", - "version": "0.1.10+1.0.8", - "is_proc_macro": false - } - ], - "build_proc_macro_dependencies": [], - "build_data_dependencies": [], - "dev_dependencies": [], - "aliased_dependencies": [] - }, - "conditions": [ - "//conditions:default" - ] - } - }, - "is_proc_macro": false - }, - { - "crate_context": { - "pkg_name": "bzip2-sys", - "pkg_version": "0.1.10+1.0.8", - "edition": "2015", - "raze_settings": { - "additional_deps": [], - "skipped_deps": [], - "extra_aliased_targets": [], - "additional_flags": [], - "additional_env": {}, - "gen_buildrs": null, - "data_attr": null, - "data_dependencies": [], - "compile_data_attr": null, - "build_data_dependencies": [], - "buildrs_additional_environment_variables": {}, - "buildrs_additional_deps": [], - "patch_args": [], - "patch_cmds": [], - "patch_cmds_win": [], - "patch_tool": null, - "patches": [], - "additional_build_file": null - }, - "canonical_additional_build_file": null, - "default_deps": { - "dependencies": [ - { - "buildable_target": "@uses_sys_crate_deps__libc__0_2_76//:libc", - "name": "libc", - "version": "0.2.76", - "is_proc_macro": false - } - ], - "proc_macro_dependencies": [], - "data_dependencies": [], - "build_dependencies": [ - { - "buildable_target": "@uses_sys_crate_deps__cc__1_0_67//:cc", - "name": "cc", - "version": "1.0.67", - "is_proc_macro": false - }, - { - "buildable_target": "@uses_sys_crate_deps__pkg_config__0_3_19//:pkg_config", - "name": "pkg-config", - "version": "0.3.19", - "is_proc_macro": false - } - ], - "build_proc_macro_dependencies": [], - "build_data_dependencies": [], - "dev_dependencies": [], - "aliased_dependencies": [] - }, - "targeted_deps": [], - "license": { - "name": "MIT from expression \"MIT OR Apache-2.0\"", - "rating": "notice" - }, - "features": [], - "workspace_path_to_crate": "@uses_sys_crate_deps__bzip2_sys__0_1_10_1_0_8//", - "workspace_member_dependents": [], - "workspace_member_dev_dependents": [], - "workspace_member_build_dependents": [], - "is_workspace_member_dependency": false, - "is_binary_dependency": false, - "targets": [ - { - "kind": "lib", - "name": "bzip2_sys", - "path": "lib.rs", - "edition": "2015" - } - ], - "build_script_target": { - "kind": "custom-build", - "name": "build-script-build", - "path": "build.rs", - "edition": "2015" - }, - "links": "bzip2", - "source_details": { - "git_data": null - }, - "sha256": "17fa3d1ac1ca21c5c4e36a97f3c3eb25084576f6fc47bf0139c1123434216c6c", - "registry_url": "https://crates.io/api/v1/crates/bzip2-sys/0.1.10+1.0.8/download", - "expected_build_path": "remote/BUILD.bzip2-sys-0.1.10+1.0.8.bazel", - "lib_target_name": "bzip2_sys", - "is_proc_macro": false - }, - "per_triple_metadata": { - "//conditions:default": { - "target": "Default", - "deps": { - "dependencies": [ - { - "buildable_target": "@uses_sys_crate_deps__libc__0_2_76//:libc", - "name": "libc", - "version": "0.2.76", - "is_proc_macro": false - } - ], - "proc_macro_dependencies": [], - "data_dependencies": [], - "build_dependencies": [ - { - "buildable_target": "@uses_sys_crate_deps__cc__1_0_67//:cc", - "name": "cc", - "version": "1.0.67", - "is_proc_macro": false - }, - { - "buildable_target": "@uses_sys_crate_deps__pkg_config__0_3_19//:pkg_config", - "name": "pkg-config", - "version": "0.3.19", - "is_proc_macro": false - } - ], - "build_proc_macro_dependencies": [], - "build_data_dependencies": [], - "dev_dependencies": [], - "aliased_dependencies": [] - }, - "conditions": [ - "//conditions:default" - ] - } - }, - "is_proc_macro": false - }, - { - "crate_context": { - "pkg_name": "cc", - "pkg_version": "1.0.67", - "edition": "2018", - "raze_settings": { - "additional_deps": [], - "skipped_deps": [], - "extra_aliased_targets": [], - "additional_flags": [], - "additional_env": {}, - "gen_buildrs": null, - "data_attr": null, - "data_dependencies": [], - "compile_data_attr": null, - "build_data_dependencies": [], - "buildrs_additional_environment_variables": {}, - "buildrs_additional_deps": [], - "patch_args": [], - "patch_cmds": [], - "patch_cmds_win": [], - "patch_tool": null, - "patches": [], - "additional_build_file": null - }, - "canonical_additional_build_file": null, - "default_deps": { - "dependencies": [], - "proc_macro_dependencies": [], - "data_dependencies": [], - "build_dependencies": [], - "build_proc_macro_dependencies": [], - "build_data_dependencies": [], - "dev_dependencies": [], - "aliased_dependencies": [] - }, - "targeted_deps": [], - "license": { - "name": "MIT from expression \"MIT OR Apache-2.0\"", - "rating": "notice" - }, - "features": [], - "workspace_path_to_crate": "@uses_sys_crate_deps__cc__1_0_67//", - "workspace_member_dependents": [], - "workspace_member_dev_dependents": [], - "workspace_member_build_dependents": [], - "is_workspace_member_dependency": false, - "is_binary_dependency": false, - "targets": [ - { - "kind": "bin", - "name": "gcc-shim", - "path": "src/bin/gcc-shim.rs", - "edition": "2018" - }, - { - "kind": "lib", - "name": "cc", - "path": "src/lib.rs", - "edition": "2018" - }, - { - "kind": "test", - "name": "cc_env", - "path": "tests/cc_env.rs", - "edition": "2018" - }, - { - "kind": "test", - "name": "cflags", - "path": "tests/cflags.rs", - "edition": "2018" - }, - { - "kind": "test", - "name": "cxxflags", - "path": "tests/cxxflags.rs", - "edition": "2018" - }, - { - "kind": "test", - "name": "test", - "path": "tests/test.rs", - "edition": "2018" - } - ], - "build_script_target": null, - "links": null, - "source_details": { - "git_data": null - }, - "sha256": "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd", - "registry_url": "https://crates.io/api/v1/crates/cc/1.0.67/download", - "expected_build_path": "remote/BUILD.cc-1.0.67.bazel", - "lib_target_name": "cc", - "is_proc_macro": false - }, - "per_triple_metadata": { - "//conditions:default": { - "target": "Default", - "deps": { - "dependencies": [], - "proc_macro_dependencies": [], - "data_dependencies": [], - "build_dependencies": [], - "build_proc_macro_dependencies": [], - "build_data_dependencies": [], - "dev_dependencies": [], - "aliased_dependencies": [] - }, - "conditions": [ - "//conditions:default" - ] - } - }, - "is_proc_macro": false - }, - { - "crate_context": { - "pkg_name": "libc", - "pkg_version": "0.2.76", - "edition": "2015", - "raze_settings": { - "additional_deps": [], - "skipped_deps": [], - "extra_aliased_targets": [], - "additional_flags": [], - "additional_env": {}, - "gen_buildrs": null, - "data_attr": null, - "data_dependencies": [], - "compile_data_attr": null, - "build_data_dependencies": [], - "buildrs_additional_environment_variables": {}, - "buildrs_additional_deps": [], - "patch_args": [], - "patch_cmds": [], - "patch_cmds_win": [], - "patch_tool": null, - "patches": [], - "additional_build_file": null - }, - "canonical_additional_build_file": null, - "default_deps": { - "dependencies": [], - "proc_macro_dependencies": [], - "data_dependencies": [], - "build_dependencies": [], - "build_proc_macro_dependencies": [], - "build_data_dependencies": [], - "dev_dependencies": [], - "aliased_dependencies": [] - }, - "targeted_deps": [], - "license": { - "name": "MIT from expression \"MIT OR Apache-2.0\"", - "rating": "notice" - }, - "features": [ - "default", - "std" - ], - "workspace_path_to_crate": "@uses_sys_crate_deps__libc__0_2_76//", - "workspace_member_dependents": [ - "" - ], - "workspace_member_dev_dependents": [], - "workspace_member_build_dependents": [], - "is_workspace_member_dependency": true, - "is_binary_dependency": false, - "targets": [ - { - "kind": "lib", - "name": "libc", - "path": "src/lib.rs", - "edition": "2015" - }, - { - "kind": "test", - "name": "const_fn", - "path": "tests/const_fn.rs", - "edition": "2015" - } - ], - "build_script_target": { - "kind": "custom-build", - "name": "build-script-build", - "path": "build.rs", - "edition": "2015" - }, - "links": null, - "source_details": { - "git_data": null - }, - "sha256": "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3", - "registry_url": "https://crates.io/api/v1/crates/libc/0.2.76/download", - "expected_build_path": "remote/BUILD.libc-0.2.76.bazel", - "lib_target_name": "libc", - "is_proc_macro": false - }, - "per_triple_metadata": { - "//conditions:default": { - "target": "Default", - "deps": { - "dependencies": [], - "proc_macro_dependencies": [], - "data_dependencies": [], - "build_dependencies": [], - "build_proc_macro_dependencies": [], - "build_data_dependencies": [], - "dev_dependencies": [], - "aliased_dependencies": [] - }, - "conditions": [ - "//conditions:default" - ] - } - }, - "is_proc_macro": false - }, - { - "crate_context": { - "pkg_name": "pkg-config", - "pkg_version": "0.3.19", - "edition": "2015", - "raze_settings": { - "additional_deps": [], - "skipped_deps": [], - "extra_aliased_targets": [], - "additional_flags": [], - "additional_env": {}, - "gen_buildrs": null, - "data_attr": null, - "data_dependencies": [], - "compile_data_attr": null, - "build_data_dependencies": [], - "buildrs_additional_environment_variables": {}, - "buildrs_additional_deps": [], - "patch_args": [], - "patch_cmds": [], - "patch_cmds_win": [], - "patch_tool": null, - "patches": [], - "additional_build_file": null - }, - "canonical_additional_build_file": null, - "default_deps": { - "dependencies": [], - "proc_macro_dependencies": [], - "data_dependencies": [], - "build_dependencies": [], - "build_proc_macro_dependencies": [], - "build_data_dependencies": [], - "dev_dependencies": [], - "aliased_dependencies": [] - }, - "targeted_deps": [], - "license": { - "name": "MIT from expression \"MIT OR Apache-2.0\"", - "rating": "notice" - }, - "features": [], - "workspace_path_to_crate": "@uses_sys_crate_deps__pkg_config__0_3_19//", - "workspace_member_dependents": [], - "workspace_member_dev_dependents": [], - "workspace_member_build_dependents": [], - "is_workspace_member_dependency": false, - "is_binary_dependency": false, - "targets": [ - { - "kind": "lib", - "name": "pkg-config", - "path": "src/lib.rs", - "edition": "2015" - }, - { - "kind": "test", - "name": "test", - "path": "tests/test.rs", - "edition": "2015" - } - ], - "build_script_target": null, - "links": null, - "source_details": { - "git_data": null - }, - "sha256": "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c", - "registry_url": "https://crates.io/api/v1/crates/pkg-config/0.3.19/download", - "expected_build_path": "remote/BUILD.pkg-config-0.3.19.bazel", - "lib_target_name": "pkg-config", - "is_proc_macro": false - }, - "per_triple_metadata": { - "//conditions:default": { - "target": "Default", - "deps": { - "dependencies": [], - "proc_macro_dependencies": [], - "data_dependencies": [], - "build_dependencies": [], - "build_proc_macro_dependencies": [], - "build_data_dependencies": [], - "dev_dependencies": [], - "aliased_dependencies": [] - }, - "conditions": [ - "//conditions:default" - ] - } - }, - "is_proc_macro": false - } - ], - "member_packages_version_mapping": { - "normal": { - "bzip2": "0.3.3", - "libc": "0.2.76" - }, - "build": {}, - "dev": {} - }, - "label_to_crates": { - "//uses_sys_crate:Cargo.toml": [ - "bzip2" - ] - } -} diff --git a/examples/crate_universe/uses_sys_crate/workspace.bzl b/examples/crate_universe/uses_sys_crate/workspace.bzl index 6e7ded9e3a..6026f2232a 100644 --- a/examples/crate_universe/uses_sys_crate/workspace.bzl +++ b/examples/crate_universe/uses_sys_crate/workspace.bzl @@ -6,7 +6,6 @@ def deps(): crate_universe( name = "uses_sys_crate_deps", cargo_toml_files = ["//uses_sys_crate:Cargo.toml"], - lockfile = "//uses_sys_crate:lockfile.lock", packages = [ crate.spec( name = "libc",