From e7b99b7c34b39cf0a06d3ff6baba9fe2beb3090d Mon Sep 17 00:00:00 2001 From: xieyuschen Date: Wed, 23 Oct 2024 16:59:48 +0800 Subject: [PATCH] [#486] support bazel in macos * select different releases for different os * build the cbindgen because its release doesn't support macos * setup cxxopts for testing and macos --- .bazelrc | 4 ++ Cargo.Bazel.lock | 82 +++++++++++----------------------- MODULE.bazel.lock | 12 ++--- WORKSPACE.bazel | 52 ++++++++++++++++++--- iceoryx2-ffi/ffi/BUILD.bazel | 28 +++++++++++- iceoryx2-pal/posix/BUILD.bazel | 29 +++++++++++- 6 files changed, 133 insertions(+), 74 deletions(-) diff --git a/.bazelrc b/.bazelrc index e8a8d9178..be4430958 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,6 +1,7 @@ build --enable_platform_specific_config build:linux --cxxopt="-std=c++17" build:windows --cxxopt="/std:c++17" +build:macos --cxxopt="-std=c++17" # this has to be specified manually build:mingw --cxxopt="-std=c++17" @@ -15,6 +16,9 @@ build --action_env=CARGO_BAZEL_REPIN=true test --test_output=streamed test --nocache_test_results test --action_env=RUST_TEST_THREADS=1 +build:linux --cxxopt="-std=c++17" +build:windows --cxxopt="/std:c++17" +build:macos --cxxopt="-std=c++17" # # feature flags diff --git a/Cargo.Bazel.lock b/Cargo.Bazel.lock index 646eff1d8..f72dba1ba 100644 --- a/Cargo.Bazel.lock +++ b/Cargo.Bazel.lock @@ -1,5 +1,5 @@ { - "checksum": "d7b1759c5d28cbeb3fff8b9da5ca81261aa8c593891b9eff7d3c554d07b4c8dd", + "checksum": "0e06dfd9a08fcb09e35b008c85e91abf975fe7a4846366aa3bc10c530cffc688", "crates": { "addr2line 0.24.2": { "name": "addr2line", @@ -3938,61 +3938,10 @@ "crate_features": { "common": [ "default", + "extra_traits", "std" ], - "selects": { - "aarch64-apple-darwin": [ - "extra_traits" - ], - "aarch64-apple-ios": [ - "extra_traits" - ], - "aarch64-apple-ios-sim": [ - "extra_traits" - ], - "aarch64-fuchsia": [ - "extra_traits" - ], - "aarch64-linux-android": [ - "extra_traits" - ], - "armv7-linux-androideabi": [ - "extra_traits" - ], - "i686-apple-darwin": [ - "extra_traits" - ], - "i686-linux-android": [ - "extra_traits" - ], - "i686-unknown-freebsd": [ - "extra_traits" - ], - "powerpc-unknown-linux-gnu": [ - "extra_traits" - ], - "s390x-unknown-linux-gnu": [ - "extra_traits" - ], - "wasm32-wasi": [ - "extra_traits" - ], - "x86_64-apple-darwin": [ - "extra_traits" - ], - "x86_64-apple-ios": [ - "extra_traits" - ], - "x86_64-fuchsia": [ - "extra_traits" - ], - "x86_64-linux-android": [ - "extra_traits" - ], - "x86_64-unknown-freebsd": [ - "extra_traits" - ] - } + "selects": {} }, "deps": { "common": [ @@ -4087,13 +4036,32 @@ ], "crate_features": { "common": [ - "elf", - "errno", "general", "ioctl", "no_std" ], - "selects": {} + "selects": { + "aarch64-unknown-linux-gnu": [ + "elf", + "errno" + ], + "arm-unknown-linux-gnueabi": [ + "elf", + "errno" + ], + "armv7-unknown-linux-gnueabi": [ + "elf", + "errno" + ], + "i686-unknown-linux-gnu": [ + "elf", + "errno" + ], + "x86_64-unknown-linux-gnu": [ + "elf", + "errno" + ] + } }, "edition": "2021", "version": "0.4.14" diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 21b358eb8..d62a47c0f 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -64,19 +64,19 @@ "@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": { "general": { "bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=", - "usagesDigest": "aLmqbvowmHkkBPve05yyDNGN7oh7QE9kBADr3QIZTZs=", + "usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, "generatedRepoSpecs": { - "local_config_apple_cc": { + "local_config_apple_cc_toolchains": { "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf", + "ruleClassName": "_apple_cc_autoconf_toolchains", "attributes": {} }, - "local_config_apple_cc_toolchains": { + "local_config_apple_cc": { "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf_toolchains", + "ruleClassName": "_apple_cc_autoconf", "attributes": {} } }, @@ -92,7 +92,7 @@ "@@platforms//host:extension.bzl%host_platform": { "general": { "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", - "usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=", + "usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index 79f89ca68..fd1d94aa3 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -26,6 +26,8 @@ maybe( ) +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + # Load Rust rules # Use v0.26 to support bazel v6.2 maybe( @@ -38,6 +40,7 @@ maybe( load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") rules_rust_dependencies() + rust_register_toolchains( edition = "2021", versions = [ @@ -47,6 +50,19 @@ rust_register_toolchains( # Load prebuilt bindgen +maybe( + name = "bindgen-macos", + repo_rule = http_archive, + strip_prefix = "bindgen-cli-aarch64-apple-darwin", + urls = ["https://github.com/rust-lang/rust-bindgen/releases/download/v0.69.5/bindgen-cli-aarch64-apple-darwin.tar.xz"], + build_file_content = """ +filegroup( + name = "bindgen-cli", + srcs = ["bindgen"], + visibility = ["//visibility:public"], +) + """, +) maybe( name = "bindgen", repo_rule = http_archive, @@ -61,16 +77,38 @@ filegroup( ) """, ) +load("@rules_rust//rust:repositories.bzl", "rust_repositories") -# Load prebuilt cbindgen -maybe( - name = "cbindgen", - repo_rule = http_file, - sha256 = "521836d00863cb129283054e5090eb17563614e6328b7a1610e30949a05feaea", - urls = ["https://github.com/mozilla/cbindgen/releases/download/0.26.0/cbindgen"], - executable = True, +rust_repositories() + +http_archive( + name = "cbindgen_source", + urls = ["https://github.com/mozilla/cbindgen/archive/refs/tags/0.26.0.tar.gz"], + strip_prefix = "cbindgen-0.26.0", + build_file_content = """ +filegroup( + name = "srcs", + srcs = glob(["src/**/*.rs", "build.rs", "Cargo.toml", "Cargo.lock"]), + visibility = ["//visibility:public"], + +) +exports_files(["Cargo.toml", "Cargo.lock"], visibility = ["//visibility:public"]) +""" ) +load("@rules_rust//rust:defs.bzl", "rust_library") +load("@rules_rust//crate_universe:defs.bzl", "crates_repository") + +crates_repository( + name = "cbindgen_deps", + cargo_lockfile = "@cbindgen_source//:Cargo.lock", + manifests = ["@cbindgen_source//:Cargo.toml"], +) + +load("@cbindgen_deps//:defs.bzl", cbindgen_deps = "crate_repositories") + +cbindgen_deps() + # Load external crates load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies") diff --git a/iceoryx2-ffi/ffi/BUILD.bazel b/iceoryx2-ffi/ffi/BUILD.bazel index 372a57729..8a52dcea7 100644 --- a/iceoryx2-ffi/ffi/BUILD.bazel +++ b/iceoryx2-ffi/ffi/BUILD.bazel @@ -20,9 +20,33 @@ filegroup( srcs = glob(["**"]), ) -filegroup( + +load("@rules_rust//rust:defs.bzl", "rust_binary") + +# load("@cbindgen_source//:BUILD.bazel", "srcs") +rust_binary( name = "cbindgen-cli", - srcs = ["@cbindgen//file"], + srcs = [ + "@cbindgen_source//:srcs", + ], + data = [ + "@cbindgen_source//:Cargo.toml", + "@cbindgen_source//:Cargo.lock", + ], + edition = "2018", + deps = [ + "@cbindgen_deps//:clap", + "@cbindgen_deps//:indexmap", + "@cbindgen_deps//:log", + "@cbindgen_deps//:serde", + "@cbindgen_deps//:serde_json", + "@cbindgen_deps//:tempfile", + "@cbindgen_deps//:toml", + "@cbindgen_deps//:proc-macro2", + "@cbindgen_deps//:quote", + "@cbindgen_deps//:heck", + "@cbindgen_deps//:syn", + ], ) genrule( diff --git a/iceoryx2-pal/posix/BUILD.bazel b/iceoryx2-pal/posix/BUILD.bazel index 031247b8b..8201e59c6 100644 --- a/iceoryx2-pal/posix/BUILD.bazel +++ b/iceoryx2-pal/posix/BUILD.bazel @@ -35,15 +35,39 @@ cc_library( visibility = ["//visibility:public"], ) + +load("@rules_rust//rust:defs.bzl", "rust_binary") + # Generate the Rust binding file +# genrule( +# name = "iceoryx2-pal-posix-bindgen", +# srcs = [ +# "src/c/posix.h", +# ] + select({ +# "@bazel_tools//src/conditions:windows": ["@bindgen//:bindgen-cli"], +# "@bazel_tools//src/conditions:darwin": ["@bindgen-macos//:bindgen-cli"], +# "//conditions:default": ["@bindgen//:bindgen-cli"], +# }), +# outs = ["posix_generated.rs"], +# cmd = "$(execpath :bindgen-cli) --use-core --blocklist-type max_align_t $(location src/c/posix.h) --output $(OUTS)", +# ) + +alias( + name = "bindgen-cli", + actual = select({ + "@bazel_tools//src/conditions:darwin": "@bindgen-macos//:bindgen-cli", + "//conditions:default": "@bindgen//:bindgen-cli", + }), +) + genrule( name = "iceoryx2-pal-posix-bindgen", srcs = [ "src/c/posix.h", - "@bindgen//:bindgen-cli", ], outs = ["posix_generated.rs"], - cmd = "$(execpath @bindgen//:bindgen-cli) --use-core --blocklist-type max_align_t $(location src/c/posix.h) --output $(OUTS)", + cmd = "$(execpath :bindgen-cli) --use-core --blocklist-type max_align_t $(location src/c/posix.h) --output $(OUTS)", + tools = [":bindgen-cli"], ) cargo_build_script( @@ -75,6 +99,7 @@ rust_library( rust_test_suite( name = "iceoryx2-pal-posix-tests", srcs = glob(["tests/**/*.rs"]), + crate_features = [ "testing" ], deps = [ ":iceoryx2-pal-posix", "//iceoryx2-bb/container:iceoryx2-bb-container",