|
| 1 | +# Use iceoryx2 with bazel |
| 2 | + |
| 3 | +On Linux, iceoryx2 can be build with bazel. Other OSes are not yet supported. |
| 4 | + |
| 5 | +## Instructions for iceoryx2 users |
| 6 | + |
| 7 | +To use iceoryx2 as an external dependency, ensure the following setup is present |
| 8 | +in your `WORKSPACE` file: |
| 9 | + |
| 10 | +```bazel |
| 11 | +ICEORYX2_VERSION = "branch-tag-or-commit-hash" |
| 12 | + |
| 13 | +http_archive( |
| 14 | + name = "iceoryx2", |
| 15 | + sha256 = "add-the-correct-sha256-sum", |
| 16 | + strip_prefix = "iceoryx2-{}".format(ICEORYX2_VERSION), |
| 17 | + urls = [ |
| 18 | + "https://github.com/eclipse-iceoryx/iceoryx2/archive/{}.tar.gz".format(ICEORYX2_VERSION), |
| 19 | + ], |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +# Load Rust rules |
| 24 | +# Use v0.26 to support bazel v6.2 |
| 25 | +maybe( |
| 26 | + name = "rules_rust", |
| 27 | + repo_rule = http_archive, |
| 28 | + sha256 = "9d04e658878d23f4b00163a72da3db03ddb451273eb347df7d7c50838d698f49", |
| 29 | + urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.26.0/rules_rust-v0.26.0.tar.gz"], |
| 30 | +) |
| 31 | + |
| 32 | +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") |
| 33 | + |
| 34 | +rules_rust_dependencies() |
| 35 | +rust_register_toolchains( |
| 36 | + edition = "2021", |
| 37 | + versions = [ |
| 38 | + "1.80.0" |
| 39 | + ], |
| 40 | +) |
| 41 | + |
| 42 | + |
| 43 | +# Load prebuilt bindgen |
| 44 | +maybe( |
| 45 | + name = "bindgen", |
| 46 | + repo_rule = http_archive, |
| 47 | + sha256 = "b7e2321ee8c617f14ccc5b9f39b3a804db173ee217e924ad93ed16af6bc62b1d", |
| 48 | + strip_prefix = "bindgen-cli-x86_64-unknown-linux-gnu", |
| 49 | + urls = ["https://github.com/rust-lang/rust-bindgen/releases/download/v0.69.5/bindgen-cli-x86_64-unknown-linux-gnu.tar.xz"], |
| 50 | + build_file_content = """ |
| 51 | +filegroup( |
| 52 | + name = "bindgen-cli", |
| 53 | + srcs = ["bindgen"], |
| 54 | + visibility = ["//visibility:public"], |
| 55 | +) |
| 56 | + """, |
| 57 | +) |
| 58 | + |
| 59 | +# Load prebuilt cbindgen |
| 60 | +maybe( |
| 61 | + name = "cbindgen", |
| 62 | + repo_rule = http_file, |
| 63 | + sha256 = "521836d00863cb129283054e5090eb17563614e6328b7a1610e30949a05feaea", |
| 64 | + urls = ["https://github.com/mozilla/cbindgen/releases/download/0.26.0/cbindgen"], |
| 65 | + executable = True, |
| 66 | +) |
| 67 | + |
| 68 | +# Load external crates |
| 69 | +load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies") |
| 70 | + |
| 71 | +crate_universe_dependencies() |
| 72 | + |
| 73 | +load("@rules_rust//crate_universe:defs.bzl", "crates_repository") |
| 74 | + |
| 75 | +maybe( |
| 76 | + name = "crate_index", |
| 77 | + repo_rule = crates_repository, |
| 78 | + cargo_lockfile = "@iceoryx2//:Cargo.lock", |
| 79 | + lockfile = "@iceoryx2//:Cargo.Bazel.lock", |
| 80 | + manifests = [ |
| 81 | + "@iceoryx2//:Cargo.toml", |
| 82 | + "@iceoryx2//:benchmarks/event/Cargo.toml", |
| 83 | + "@iceoryx2//:benchmarks/publish-subscribe/Cargo.toml", |
| 84 | + "@iceoryx2//:examples/Cargo.toml", |
| 85 | + "@iceoryx2//:iceoryx2/Cargo.toml", |
| 86 | + "@iceoryx2//:iceoryx2-bb/container/Cargo.toml", |
| 87 | + "@iceoryx2//:iceoryx2-bb/derive-macros/Cargo.toml", |
| 88 | + "@iceoryx2//:iceoryx2-bb/elementary/Cargo.toml", |
| 89 | + "@iceoryx2//:iceoryx2-bb/lock-free/Cargo.toml", |
| 90 | + "@iceoryx2//:iceoryx2-bb/log/Cargo.toml", |
| 91 | + "@iceoryx2//:iceoryx2-bb/memory/Cargo.toml", |
| 92 | + "@iceoryx2//:iceoryx2-bb/posix/Cargo.toml", |
| 93 | + "@iceoryx2//:iceoryx2-bb/system-types/Cargo.toml", |
| 94 | + "@iceoryx2//:iceoryx2-bb/testing/Cargo.toml", |
| 95 | + "@iceoryx2//:iceoryx2-bb/threadsafe/Cargo.toml", |
| 96 | + "@iceoryx2//:iceoryx2-bb/trait-tests/Cargo.toml", |
| 97 | + "@iceoryx2//:iceoryx2-cal/Cargo.toml", |
| 98 | + "@iceoryx2//:iceoryx2-cli/Cargo.toml", |
| 99 | + "@iceoryx2//:iceoryx2-ffi/ffi-macros/Cargo.toml", |
| 100 | + "@iceoryx2//:iceoryx2-ffi/ffi/Cargo.toml", |
| 101 | + "@iceoryx2//:iceoryx2-pal/concurrency-sync/Cargo.toml", |
| 102 | + "@iceoryx2//:iceoryx2-pal/configuration/Cargo.toml", |
| 103 | + "@iceoryx2//:iceoryx2-pal/posix/Cargo.toml", |
| 104 | + ], |
| 105 | +) |
| 106 | + |
| 107 | +load("@crate_index//:defs.bzl", "crate_repositories") |
| 108 | + |
| 109 | +crate_repositories() |
| 110 | +``` |
| 111 | + |
| 112 | +### Syncing Dependencies |
| 113 | + |
| 114 | +For the initial build, you must sync dependencies with crates.io. |
| 115 | +This can be done by running one of the following commands: |
| 116 | + |
| 117 | +```bash |
| 118 | +CARGO_BAZEL_REPIN=1 bazel sync --only=crate_index |
| 119 | +``` |
| 120 | +or |
| 121 | + |
| 122 | +```bash |
| 123 | +CARGO_BAZEL_REPIN=1 bazel build //... |
| 124 | +``` |
| 125 | + |
| 126 | +For more details, refer to the |
| 127 | +[Crate Universe Repinning Guide](https://bazelbuild.github.io/rules_rust/crate_universe.html#repinning--updating-dependencies-1). |
| 128 | + |
| 129 | +### Linking Iceoryx2 in Your `BUILD.bazel` |
| 130 | + |
| 131 | +To use iceoryx2 in your Bazel project, link the appropriate static or shared library. For example, in `BUILD.bazel`: |
| 132 | + |
| 133 | +```bazel |
| 134 | +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") |
| 135 | + |
| 136 | +cc_binary( |
| 137 | + name = "main", |
| 138 | + srcs = [ |
| 139 | + "main.cpp", |
| 140 | + ], |
| 141 | + deps = [ |
| 142 | + "@iceoryx2//:iceoryx2-cxx-static", |
| 143 | + ], |
| 144 | +) |
| 145 | +``` |
| 146 | + |
| 147 | +- **For Rust**: Use `@iceoryx2//:iceoryx2`. |
| 148 | +- **For C**: Use `@iceoryx2//:iceoryx2-c-static` or `@iceoryx2//:iceoryx2-c-shared`. |
| 149 | +- **For C++**: Use `@iceoryx2//:iceoryx2-cxx-static` or `@iceoryx2//:iceoryx2-cxx-shared`. |
| 150 | + |
| 151 | +## Instructions for iceoryx2 Developers |
| 152 | + |
| 153 | +When working with Bazel and Cargo in this project, ensure the following steps are |
| 154 | +followed to maintain consistency between both build systems: |
| 155 | + |
| 156 | +### Adding Crates to Targets |
| 157 | + |
| 158 | +1. **In `Cargo.toml`**: When a new crate is added to a target's `Cargo.toml` file, |
| 159 | +the same crate must also be referenced in the corresponding target within the `BUILD.bazel` file. |
| 160 | + |
| 161 | +2. **In `WORKSPACE.bazel`**: If a new crate is added to the root `Cargo.toml` file, |
| 162 | +it must also be included in the `crate_index` target located in the `WORKSPACE.bazel` file. |
| 163 | + |
| 164 | +### Updating Dependencies |
| 165 | + |
| 166 | +Any time a dependency is added or changed, the `Cargo.Bazel.lock` file must be updated by running: |
| 167 | + |
| 168 | +```bash |
| 169 | +CARGO_BAZEL_REPIN=1 bazel build //... |
| 170 | +``` |
| 171 | + |
| 172 | +### Common Pitfalls |
| 173 | + |
| 174 | +1. **Handling `iceoryx2-ffi-cbindgen` Target**: |
| 175 | + |
| 176 | +The `iceoryx2-ffi-cbindgen` target requires access to all crates listed in the root `Cargo.toml`. |
| 177 | +Due to the sandboxed nature of Bazel builds, this can cause issues if not properly configured. |
| 178 | + |
| 179 | +2. **Managing Source Files**: |
| 180 | + |
| 181 | +Every `BUILD.bazel` file includes an `all_srcs` filegroup to manage the source files |
| 182 | +within the sandbox. |
| 183 | +The root `BUILD.bazel` file has an `all_srcs` filegroup that references all sub-packages. |
| 184 | +When a new package is added, it must also be included in this `all_srcs` filegroup. |
0 commit comments