Skip to content

Migrate sys example to bzlmod #3130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,6 @@ tasks:
rbe_examples_targets: &rbe_examples_targets
- "--" # Allows negative patterns; hack for https://github.com/bazelbuild/continuous-integration/pull/245
- "//..."
# TODO: This requires an updated `rules_foreign_cc`
- "-//sys/..."
# See https://github.com/bazelbuild/bazel/issues/9987
- "-//ffi/rust_calling_c:matrix_dylib_test"
shell_commands:
Expand Down Expand Up @@ -800,6 +798,38 @@ tasks:
# - "@rules_rust//tools/rust_analyzer:gen_rust_project"
# test_targets:
# - "//..."
example_sys_linux:
platform: ubuntu2004
working_directory: examples/bzlmod/sys
name: Sys Example
build_targets:
- "//..."
test_targets:
- "//..."
example_sys_linux_rbe:
platform: ubuntu2004
working_directory: examples/bzlmod/sys
name: Sys Example
build_targets:
- "//..."
test_targets:
- "//..."
example_sys_macos:
platform: macos_arm64
working_directory: examples/bzlmod/sys
name: Sys Example
build_targets:
- "//..."
test_targets:
- "//..."
example_sys_windows:
platform: windows
working_directory: examples/bzlmod/sys
name: Sys Example
build_targets:
- "//..."
test_targets:
- "//..."
ubuntu2004_bzlmod_plus_repo_names_bcr:
name: bzlmod BCR presubmit w/ incompatible flags
# A newer version than is specified in the .bazel_version file is needed for
Expand Down
4 changes: 0 additions & 4 deletions examples/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_depende

rules_foreign_cc_dependencies()

load("//sys:sys_deps.bzl", "sys_deps")

sys_deps()

local_repository(
name = "rules_rust_example_cargo_manifest_dir",
path = "cargo_manifest_dir/external_crate",
Expand Down
67 changes: 67 additions & 0 deletions examples/bzlmod/sys/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
###############################################################################
## Bazel Configuration Flags
##
## `.bazelrc` is a Bazel configuration file.
## https://bazel.build/docs/best-practices#bazelrc-file
###############################################################################

###############################################################################
## Build configuration
###############################################################################

# Don't create bazel-* symlinks in the WORKSPACE directory.
# Instead, set a prefix and put it in .gitignore
# build --symlink_prefix=target-bzl/

###############################################################################
## Test configuration
###############################################################################

# Reduce test output to just error cases
test --test_output=errors
test --verbose_failures

###############################################################################
## Common configuration
###############################################################################

# Enable Bzlmod for every Bazel command
common --enable_bzlmod

# Disable the bzlmod lockfile, so we don't accidentally commit MODULE.bazel.lock
common --lockfile_mode=off

# Write build outputs in a platform-specific directory;
# avoid outputs being wiped and rewritten when switching between platforms.
common --experimental_platform_in_output_dir

# https://github.com/bazelbuild/bazel/issues/8195
build --incompatible_disallow_empty_glob=true

# https://github.com/bazelbuild/bazel/issues/12821
build --nolegacy_external_runfiles

# Required for cargo_build_script support before Bazel 7
build --incompatible_merge_fixed_and_default_shell_env

###############################################################################
## Rust configuration
###############################################################################

# Enable rustfmt for all targets in the workspace
build:rustfmt --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
build:rustfmt --output_groups=+rustfmt_checks

# Enable clippy for all targets in the workspace
build:clippy --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
build:clippy --output_groups=+clippy_checks

###############################################################################
## Custom user flags
##
## This should always be the last thing in the `.bazelrc` file to ensure
## consistent behavior when setting flags in that file as `.bazelrc` files are
## evaluated top to bottom.
###############################################################################

try-import %workspace%/user.bazelrc
1 change: 1 addition & 0 deletions examples/bzlmod/sys/.bazelversion
2 changes: 2 additions & 0 deletions examples/bzlmod/sys/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bazel-*
user.bazelrc
File renamed without changes.
56 changes: 56 additions & 0 deletions examples/bzlmod/sys/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module(
name = "rules_rust_example_sys",
version = "0.0.0",
)

###############################################################################
# B A Z E L C E N T R A L R E G I S T R Y # https://registry.bazel.build/
###############################################################################
# https://github.com/bazelbuild/rules_rust/releases
bazel_dep(name = "rules_rust", version = "0.46.0")
local_path_override(
module_name = "rules_rust",
path = "../../..",
)

bazel_dep(
name = "bazel_skylib",
version = "1.7.1",
)
bazel_dep(
name = "platforms",
version = "0.0.10",
)
bazel_dep(
name = "rules_cc",
version = "0.0.17",
)

deps = use_extension("//:extensions.bzl", "rust_example")
use_repo(
deps,
"basic_sys",
"basic_sys__bzip2-0.3.3",
"complex_sys",
"complex_sys__git2-0.14.4",
"libgit2",
"zlib",
)

###############################################################################
# T O O L C H A I N S
###############################################################################

# Rust toolchain
RUST_EDITION = "2021"

RUST_VERSION = "1.79.0"

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = RUST_EDITION,
versions = [RUST_VERSION],
)
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")
10 changes: 10 additions & 0 deletions examples/bzlmod/sys/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Sys Crate Examples

This repository demonstrates how to use `rules_rust` to build projects that depend on `-sys` crates.

`-sys` crates provide low-level bindings to native libraries, allowing Rust code to interact with C libraries through the Foreign Function Interface (FFI). For more details, see the [Rust FFI documentation](https://doc.rust-lang.org/nomicon/ffi.html) or the [Rust-bindgen project](https://github.com/rust-lang/rust-bindgen).

This workspace includes:

1. **Basic Example**: Using `bzip2-sys` to interface with the `bzip2` compression library.
2. **Complex Example**: Using `libgit2-sys` to interact with the `libgit2` library.
1 change: 1 addition & 0 deletions examples/bzlmod/sys/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "rules_rust_example_sys")
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
# regenerate this file, run the following:
#
# bazel run @//sys/basic/3rdparty:crates_vendor
# bazel run @@//basic/3rdparty:crates_vendor
###############################################################################

package(default_visibility = ["//visibility:public"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
# regenerate this file, run the following:
#
# bazel run @//sys/basic/3rdparty:crates_vendor
# bazel run @@//basic/3rdparty:crates_vendor
###############################################################################

load("@rules_rust//rust:defs.bzl", "rust_library")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
# regenerate this file, run the following:
#
# bazel run @//sys/basic/3rdparty:crates_vendor
# bazel run @@//basic/3rdparty:crates_vendor
###############################################################################

load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
# regenerate this file, run the following:
#
# bazel run @//sys/basic/3rdparty:crates_vendor
# bazel run @@//basic/3rdparty:crates_vendor
###############################################################################

load("@rules_rust//rust:defs.bzl", "rust_library")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
# regenerate this file, run the following:
#
# bazel run @//sys/basic/3rdparty:crates_vendor
# bazel run @@//basic/3rdparty:crates_vendor
###############################################################################

load("@rules_rust//rust:defs.bzl", "rust_library")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
# regenerate this file, run the following:
#
# bazel run @//sys/basic/3rdparty:crates_vendor
# bazel run @@//basic/3rdparty:crates_vendor
###############################################################################

load("@rules_rust//rust:defs.bzl", "rust_library")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

# buildifier: disable=bzl-visibility
load("@examples//sys/basic/3rdparty/crates:defs.bzl", _crate_repositories = "crate_repositories")
load("@rules_rust//crate_universe/private:crates_vendor.bzl", "crates_vendor_remote_repository")

# buildifier: disable=bzl-visibility
load("@rules_rust//crate_universe/private:crates_vendor.bzl", "crates_vendor_remote_repository")
load("//basic/3rdparty/crates:defs.bzl", _crate_repositories = "crate_repositories")

def crate_repositories():
"""Generates repositories for vendored crates.
Expand All @@ -23,8 +23,8 @@ def crate_repositories():
maybe(
crates_vendor_remote_repository,
name = "basic_sys",
build_file = Label("@examples//sys/basic/3rdparty/crates:BUILD.bazel"),
defs_module = Label("@examples//sys/basic/3rdparty/crates:defs.bzl"),
build_file = Label("//basic/3rdparty/crates:BUILD.bazel"),
defs_module = Label("//basic/3rdparty/crates:defs.bzl"),
)

direct_deps = [struct(repo = "basic_sys", is_dev_dep = False)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
# regenerate this file, run the following:
#
# bazel run @//sys/basic/3rdparty:crates_vendor
# bazel run @@//basic/3rdparty:crates_vendor
###############################################################################
"""
# `crates_repository` API
Expand Down Expand Up @@ -409,7 +409,7 @@ def crate_repositories():
type = "tar.gz",
urls = ["https://static.crates.io/crates/bzip2/0.3.3/download"],
strip_prefix = "bzip2-0.3.3",
build_file = Label("@examples//sys/basic/3rdparty/crates:BUILD.bzip2-0.3.3.bazel"),
build_file = Label("//basic/3rdparty/crates:BUILD.bzip2-0.3.3.bazel"),
)

maybe(
Expand All @@ -419,7 +419,7 @@ def crate_repositories():
type = "tar.gz",
urls = ["https://static.crates.io/crates/bzip2-sys/0.1.11+1.0.8/download"],
strip_prefix = "bzip2-sys-0.1.11+1.0.8",
build_file = Label("@examples//sys/basic/3rdparty/crates:BUILD.bzip2-sys-0.1.11+1.0.8.bazel"),
build_file = Label("//basic/3rdparty/crates:BUILD.bzip2-sys-0.1.11+1.0.8.bazel"),
)

maybe(
Expand All @@ -429,7 +429,7 @@ def crate_repositories():
type = "tar.gz",
urls = ["https://static.crates.io/crates/cc/1.0.77/download"],
strip_prefix = "cc-1.0.77",
build_file = Label("@examples//sys/basic/3rdparty/crates:BUILD.cc-1.0.77.bazel"),
build_file = Label("//basic/3rdparty/crates:BUILD.cc-1.0.77.bazel"),
)

maybe(
Expand All @@ -439,7 +439,7 @@ def crate_repositories():
type = "tar.gz",
urls = ["https://static.crates.io/crates/libc/0.2.137/download"],
strip_prefix = "libc-0.2.137",
build_file = Label("@examples//sys/basic/3rdparty/crates:BUILD.libc-0.2.137.bazel"),
build_file = Label("//basic/3rdparty/crates:BUILD.libc-0.2.137.bazel"),
)

maybe(
Expand All @@ -449,7 +449,7 @@ def crate_repositories():
type = "tar.gz",
urls = ["https://static.crates.io/crates/pkg-config/0.3.26/download"],
strip_prefix = "pkg-config-0.3.26",
build_file = Label("@examples//sys/basic/3rdparty/crates:BUILD.pkg-config-0.3.26.bazel"),
build_file = Label("//basic/3rdparty/crates:BUILD.pkg-config-0.3.26.bazel"),
)

return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("@rules_rust//rust:defs.bzl", "rust_binary")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_test")

package(default_visibility = ["//visibility:public"])

Expand All @@ -22,13 +21,17 @@ rust_binary(
srcs = ["src/main.rs"],
edition = "2018",
# Note the `crate_unvierse` dependencies here need to have been loaded
# in the WORKSPACE file. See `//sys:sys_deps.bzl` for rmore details.
deps = ["//sys/basic/3rdparty/crates:bzip2"],
# in the `MODULE.bazel` file.
deps = ["//basic/3rdparty/crates:bzip2"],
)

sh_test(
name = "test",
srcs = ["test.sh"],
args = ["$(location :hello_sys)"],
rust_test(
name = "basic_test",
srcs = ["basic_test.rs"],
data = [":hello_sys"],
edition = "2021",
rustc_env = {"HELLO_SYS_RLOCATIONPATH": "$(rlocationpath :hello_sys)"},
deps = [
"@rules_rust//tools/runfiles",
],
)
Loading
Loading