From 7e0fafb287a653505a438b725acedda6e58e858a Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Thu, 2 Jan 2025 12:48:20 -0800 Subject: [PATCH] Added rust example (#1354) I think having this example here vs in `rules_rust` will be more visible for consumers of `rules_foreign_cc`. `rules_rust` should otherwise try to demonstrate simple examples or uses of `rules_cc` and leave the complexity of foreign_cc interactions to this repo. --- .bazelci/config.yaml | 4 + examples/MODULE.bazel | 30 ++ examples/WORKSPACE.bazel | 54 ++++ examples/rust/BUILD.bazel | 8 + examples/rust/Cargo.Bazel.lock | 41 +++ examples/rust/cargo-bazel-lock.json | 417 ++++++++++++++++++++++++++++ 6 files changed, 554 insertions(+) create mode 100644 examples/rust/BUILD.bazel create mode 100644 examples/rust/Cargo.Bazel.lock create mode 100644 examples/rust/cargo-bazel-lock.json diff --git a/.bazelci/config.yaml b/.bazelci/config.yaml index 3bf609560..0fc49c264 100644 --- a/.bazelci/config.yaml +++ b/.bazelci/config.yaml @@ -167,6 +167,7 @@ tasks: - "-//cmake_hello_world_lib/static:libhello_example" - "-//cmake_hello_world_lib/static:test_hello" - "-//cmake_with_data/..." + - "-//rust/..." batch_commands: - powershell -noexit "& "".\..\.bazelci\windows-update-certs.ps1""" build_targets: *windows_targets @@ -247,7 +248,10 @@ tasks: platform: ubuntu1804 working_directory: examples min_supported_targets: &min_supported_targets + - "--" - "//..." + # The min supported version of rules_rust is `>=7` + - "-//rust/..." build_targets: *min_supported_targets test_targets: *min_supported_targets diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index b13b893bc..15b17be92 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -13,6 +13,7 @@ use_repo( ) bazel_dep(name = "platforms", version = "0.0.6") +bazel_dep(name = "rules_rust", version = "0.56.0") bazel_dep(name = "rules_swift", version = "1.6.0", repo_name = "build_bazel_rules_swift") bazel_dep(name = "rules_apple", version = "3.4.0", repo_name = "build_bazel_rules_apple") bazel_dep(name = "apple_support", version = "1.12.0", repo_name = "build_bazel_apple_support") @@ -36,6 +37,35 @@ pip.parse( ) use_repo(pip, "pip") +# https://bazelbuild.github.io/rules_rust/crate_universe_bzlmod.html +rust_example = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate") + +inject_repo(rust_example, "rules_foreign_cc") + +rust_example.annotation( + # Setting build_script_data makes the files available on disk when the rule runs. + build_script_data = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"], + build_script_env = { + # The toolchain supplies a value of $(CMAKE) which is an execroot-relative path, so we need to prefix it with `$${pwd}/` + # because build scripts don't typically run in the execroot unlike most bazel rules, for improved compatibility with Cargo. + "CMAKE": "$${pwd}/$(CMAKE)", + }, + # Setting build_script_toolchains makes makefile variable substitution work so that we can reference $(CNAME) in attributes. + build_script_toolchains = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"], + crate = "libz-ng-sys", +) +rust_example.spec( + package = "libz-ng-sys", + version = "=1.1.20", +) +rust_example.from_specs( + name = "rust_example", +) +use_repo( + rust_example, + "rust_example", +) + # bazel_dep(name = "rules_android", version = "0.1.1") # bazel_dep(name = "rules_jvm_external", version = "5.1") # diff --git a/examples/WORKSPACE.bazel b/examples/WORKSPACE.bazel index b6b4b99af..80691c2b8 100644 --- a/examples/WORKSPACE.bazel +++ b/examples/WORKSPACE.bazel @@ -124,3 +124,57 @@ load( ) apple_support_dependencies() + +maybe( + http_archive, + name = "rules_rust", + integrity = "sha256-8TBqrAsli3kN8BrZq8arsN8LZUFsdLTvJ/Sqsph4CmQ=", + urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.56.0/rules_rust-0.56.0.tar.gz"], +) + +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") + +rules_rust_dependencies() + +rust_register_toolchains() + +load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies") + +crate_universe_dependencies() + +load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository", "splicing_config") + +crates_repository( + name = "rust_example", + annotations = { + # zlib-ng-sys's build script invokes cmake, so we need to make cmake available. + # Fortunately, rules_foreign_cc has a cmake toolchain we can use. + "libz-ng-sys": [crate.annotation( + # Setting build_script_data makes the files available on disk when the rule runs. + build_script_data = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"], + build_script_env = { + # The toolchain supplies a value of $(CMAKE) which is an execroot-relative path, so we need to prefix it with $${pwd}/ because build scripts don't typically run in the execroot unlike most bazel rules, for improved compatibility with Cargo. + "CMAKE": "$${pwd}/$(CMAKE)", + }, + # Setting build_script_toolchains makes makefile variable substitution work so that we can reference $(CNAME) in attributes. + build_script_toolchains = ["@rules_foreign_cc//toolchains:current_cmake_toolchain"], + )], + }, + cargo_lockfile = "//rust:Cargo.Bazel.lock", + lockfile = "//rust:cargo-bazel-lock.json", + packages = { + "libz-ng-sys": crate.spec( + version = "=1.1.20", + ), + }, + splicing_config = splicing_config( + resolver_version = "2", + ), +) + +load( + "@rust_example//:defs.bzl", + rust_example_crate_repositories = "crate_repositories", +) + +rust_example_crate_repositories() diff --git a/examples/rust/BUILD.bazel b/examples/rust/BUILD.bazel new file mode 100644 index 000000000..f7f8f4074 --- /dev/null +++ b/examples/rust/BUILD.bazel @@ -0,0 +1,8 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") + +build_test( + name = "build_test", + targets = [ + "@rust_example//:libz-ng-sys", + ], +) diff --git a/examples/rust/Cargo.Bazel.lock b/examples/rust/Cargo.Bazel.lock new file mode 100644 index 000000000..ece3f81fd --- /dev/null +++ b/examples/rust/Cargo.Bazel.lock @@ -0,0 +1,41 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "cc" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" + +[[package]] +name = "cmake" +version = "0.1.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31c789563b815f77f4250caee12365734369f942439b7defd71e18a48197130" +dependencies = [ + "cc", +] + +[[package]] +name = "direct-cargo-bazel-deps" +version = "0.0.1" +dependencies = [ + "libz-ng-sys", +] + +[[package]] +name = "libc" +version = "0.2.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" + +[[package]] +name = "libz-ng-sys" +version = "1.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f0f7295a34685977acb2e8cc8b08ee4a8dffd6cf278eeccddbe1ed55ba815d5" +dependencies = [ + "cmake", + "libc", +] diff --git a/examples/rust/cargo-bazel-lock.json b/examples/rust/cargo-bazel-lock.json new file mode 100644 index 000000000..d6e4322e7 --- /dev/null +++ b/examples/rust/cargo-bazel-lock.json @@ -0,0 +1,417 @@ +{ + "checksum": "08b2f765bceb9d6254850f963890ec6eab219bc219b5decf897b2a74d19b2cdc", + "crates": { + "cc 1.0.95": { + "name": "cc", + "version": "1.0.95", + "package_url": "https://github.com/rust-lang/cc-rs", + "repository": { + "Http": { + "url": "https://static.crates.io/crates/cc/1.0.95/download", + "sha256": "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" + } + }, + "targets": [ + { + "Library": { + "crate_name": "cc", + "crate_root": "src/lib.rs", + "srcs": { + "allow_empty": true, + "include": [ + "**/*.rs" + ] + } + } + } + ], + "library_target_name": "cc", + "common_attrs": { + "compile_data_glob": [ + "**" + ], + "edition": "2018", + "version": "1.0.95" + }, + "license": "MIT OR Apache-2.0", + "license_ids": [ + "Apache-2.0", + "MIT" + ], + "license_file": "LICENSE-APACHE" + }, + "cmake 0.1.50": { + "name": "cmake", + "version": "0.1.50", + "package_url": "https://github.com/rust-lang/cmake-rs", + "repository": { + "Http": { + "url": "https://static.crates.io/crates/cmake/0.1.50/download", + "sha256": "a31c789563b815f77f4250caee12365734369f942439b7defd71e18a48197130" + } + }, + "targets": [ + { + "Library": { + "crate_name": "cmake", + "crate_root": "src/lib.rs", + "srcs": { + "allow_empty": true, + "include": [ + "**/*.rs" + ] + } + } + } + ], + "library_target_name": "cmake", + "common_attrs": { + "compile_data_glob": [ + "**" + ], + "deps": { + "common": [ + { + "id": "cc 1.0.95", + "target": "cc" + } + ], + "selects": {} + }, + "edition": "2021", + "version": "0.1.50" + }, + "license": "MIT OR Apache-2.0", + "license_ids": [ + "Apache-2.0", + "MIT" + ], + "license_file": "LICENSE-APACHE" + }, + "direct-cargo-bazel-deps 0.0.1": { + "name": "direct-cargo-bazel-deps", + "version": "0.0.1", + "package_url": null, + "repository": null, + "targets": [ + { + "Library": { + "crate_name": "direct_cargo_bazel_deps", + "crate_root": ".direct_cargo_bazel_deps.rs", + "srcs": { + "allow_empty": true, + "include": [ + "**/*.rs" + ] + } + } + } + ], + "library_target_name": "direct_cargo_bazel_deps", + "common_attrs": { + "compile_data_glob": [ + "**" + ], + "deps": { + "common": [ + { + "id": "libz-ng-sys 1.1.20", + "target": "libz_ng_sys" + } + ], + "selects": {} + }, + "edition": "2018", + "version": "0.0.1" + }, + "license": null, + "license_ids": [], + "license_file": null + }, + "libc 0.2.154": { + "name": "libc", + "version": "0.2.154", + "package_url": "https://github.com/rust-lang/libc", + "repository": { + "Http": { + "url": "https://static.crates.io/crates/libc/0.2.154/download", + "sha256": "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" + } + }, + "targets": [ + { + "Library": { + "crate_name": "libc", + "crate_root": "src/lib.rs", + "srcs": { + "allow_empty": true, + "include": [ + "**/*.rs" + ] + } + } + }, + { + "BuildScript": { + "crate_name": "build_script_build", + "crate_root": "build.rs", + "srcs": { + "allow_empty": true, + "include": [ + "**/*.rs" + ] + } + } + } + ], + "library_target_name": "libc", + "common_attrs": { + "compile_data_glob": [ + "**" + ], + "crate_features": { + "common": [ + "default", + "std" + ], + "selects": {} + }, + "deps": { + "common": [ + { + "id": "libc 0.2.154", + "target": "build_script_build" + } + ], + "selects": {} + }, + "edition": "2015", + "version": "0.2.154" + }, + "build_script_attrs": { + "compile_data_glob": [ + "**" + ], + "data_glob": [ + "**" + ] + }, + "license": "MIT OR Apache-2.0", + "license_ids": [ + "Apache-2.0", + "MIT" + ], + "license_file": "LICENSE-APACHE" + }, + "libz-ng-sys 1.1.20": { + "name": "libz-ng-sys", + "version": "1.1.20", + "package_url": "https://github.com/rust-lang/libz-sys", + "repository": { + "Http": { + "url": "https://static.crates.io/crates/libz-ng-sys/1.1.20/download", + "sha256": "8f0f7295a34685977acb2e8cc8b08ee4a8dffd6cf278eeccddbe1ed55ba815d5" + } + }, + "targets": [ + { + "Library": { + "crate_name": "libz_ng_sys", + "crate_root": "src/lib.rs", + "srcs": { + "allow_empty": true, + "include": [ + "**/*.rs" + ] + } + } + }, + { + "BuildScript": { + "crate_name": "build_script_cmake", + "crate_root": "zng/cmake.rs", + "srcs": { + "allow_empty": true, + "include": [ + "**/*.rs" + ] + } + } + } + ], + "library_target_name": "libz_ng_sys", + "common_attrs": { + "compile_data_glob": [ + "**" + ], + "deps": { + "common": [ + { + "id": "libc 0.2.154", + "target": "libc" + }, + { + "id": "libz-ng-sys 1.1.20", + "target": "build_script_cmake" + } + ], + "selects": {} + }, + "edition": "2018", + "version": "1.1.20" + }, + "build_script_attrs": { + "compile_data_glob": [ + "**" + ], + "data": { + "common": [ + "@rules_foreign_cc//toolchains:current_cmake_toolchain" + ], + "selects": {} + }, + "data_glob": [ + "**" + ], + "deps": { + "common": [ + { + "id": "cmake 0.1.50", + "target": "cmake" + } + ], + "selects": {} + }, + "build_script_env": { + "common": { + "CMAKE": "$${pwd}/$(CMAKE)" + }, + "selects": {} + }, + "links": "z-ng", + "toolchains": [ + "@rules_foreign_cc//toolchains:current_cmake_toolchain" + ] + }, + "license": "MIT OR Apache-2.0", + "license_ids": [ + "Apache-2.0", + "MIT" + ], + "license_file": "LICENSE-APACHE" + } + }, + "binary_crates": [], + "workspace_members": { + "direct-cargo-bazel-deps 0.0.1": "" + }, + "conditions": { + "aarch64-apple-darwin": [ + "aarch64-apple-darwin" + ], + "aarch64-apple-ios": [ + "aarch64-apple-ios" + ], + "aarch64-apple-ios-sim": [ + "aarch64-apple-ios-sim" + ], + "aarch64-linux-android": [ + "aarch64-linux-android" + ], + "aarch64-pc-windows-msvc": [ + "aarch64-pc-windows-msvc" + ], + "aarch64-unknown-fuchsia": [ + "aarch64-unknown-fuchsia" + ], + "aarch64-unknown-linux-gnu": [ + "aarch64-unknown-linux-gnu" + ], + "aarch64-unknown-nixos-gnu": [ + "aarch64-unknown-nixos-gnu" + ], + "aarch64-unknown-nto-qnx710": [ + "aarch64-unknown-nto-qnx710" + ], + "arm-unknown-linux-gnueabi": [ + "arm-unknown-linux-gnueabi" + ], + "armv7-linux-androideabi": [ + "armv7-linux-androideabi" + ], + "armv7-unknown-linux-gnueabi": [ + "armv7-unknown-linux-gnueabi" + ], + "i686-apple-darwin": [ + "i686-apple-darwin" + ], + "i686-linux-android": [ + "i686-linux-android" + ], + "i686-pc-windows-msvc": [ + "i686-pc-windows-msvc" + ], + "i686-unknown-freebsd": [ + "i686-unknown-freebsd" + ], + "i686-unknown-linux-gnu": [ + "i686-unknown-linux-gnu" + ], + "powerpc-unknown-linux-gnu": [ + "powerpc-unknown-linux-gnu" + ], + "riscv32imc-unknown-none-elf": [ + "riscv32imc-unknown-none-elf" + ], + "riscv64gc-unknown-none-elf": [ + "riscv64gc-unknown-none-elf" + ], + "s390x-unknown-linux-gnu": [ + "s390x-unknown-linux-gnu" + ], + "thumbv7em-none-eabi": [ + "thumbv7em-none-eabi" + ], + "thumbv8m.main-none-eabi": [ + "thumbv8m.main-none-eabi" + ], + "wasm32-unknown-unknown": [ + "wasm32-unknown-unknown" + ], + "wasm32-wasip1": [ + "wasm32-wasip1" + ], + "x86_64-apple-darwin": [ + "x86_64-apple-darwin" + ], + "x86_64-apple-ios": [ + "x86_64-apple-ios" + ], + "x86_64-linux-android": [ + "x86_64-linux-android" + ], + "x86_64-pc-windows-msvc": [ + "x86_64-pc-windows-msvc" + ], + "x86_64-unknown-freebsd": [ + "x86_64-unknown-freebsd" + ], + "x86_64-unknown-fuchsia": [ + "x86_64-unknown-fuchsia" + ], + "x86_64-unknown-linux-gnu": [ + "x86_64-unknown-linux-gnu" + ], + "x86_64-unknown-nixos-gnu": [ + "x86_64-unknown-nixos-gnu" + ], + "x86_64-unknown-none": [ + "x86_64-unknown-none" + ] + }, + "direct_deps": [ + "libz-ng-sys 1.1.20" + ], + "direct_dev_deps": [], + "unused_patches": [] +}