etl/docs/source/bazel.md
John Wellbelove afa17c5042 Squashed commit of the following:
commit c5dc331004b4d9611ab996935bedada203676c71
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date:   Sat Jul 11 12:32:20 2026 +0100

    Updated release notes

commit 4bf6ed6ce8d79857658d763e368ee56617542b2c
Author: vcoselev <145198867+vcoselev@users.noreply.github.com>
Date:   Sat Jul 11 12:05:39 2026 +0100

    Fix for 1405: etl::exchange doesn't work with unique_ptr (#1477)

    * Separate bit order and endianness in bit_stream.h (#1495)

    * Factor out ranges tests from test_algorithm.cpp to test_algorithm_ranges.cpp (#1497)

    * Fix exchange to use forwarding reference (U&&) and move

    - etl::exchange was using the copy constructor. Changed for etl::move.
    - The assignment to the new object is now done with forward.
    - Added a test with unique_ptr.

    ---------

    Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>
    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 2ed9019df04d470530c41c1f27d35adc30b9a32d
Author: Moritz Pflanzer <moritz@pflanzer.eu>
Date:   Fri Jul 10 11:42:32 2026 +0200

    Update operator[] docs for map class (#1499)

    * Separate bit order and endianness in bit_stream.h (#1495)

    * Update operator[] docs for map class

    No const variant is implemented for the operator[].

    ---------

    Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>

commit 423e18d96dc645f1c733cb0d8ae6b675b9eb7e83
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Fri Jul 10 11:14:08 2026 +0200

    Factor out ranges tests from test_algorithm.cpp to test_algorithm_ranges.cpp (#1497)

commit 338bfdbb1f4df3c5c08a68a9038f13b8dae1c28b
Author: John Wellbelove <jwellbelove@users.noreply.github.com>
Date:   Fri Jul 10 10:12:04 2026 +0100

    Add coverage report to docs (#1498)

    * Updated release notes

    * Added link to coverage report

    ---------

    Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>

commit 269025424f4c6f61cebac090f4df82985fb3b239
Merge: c004cc9d faa4c15b
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date:   Thu Jul 9 14:18:32 2026 +0100

    Merge branch 'development' of https://github.com/ETLCPP/etl into development

commit c004cc9d508efcf398630758a47aae95b37a7eb8
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date:   Thu Jul 9 10:06:17 2026 +0100

    Updated release notes

commit faa4c15b49c8ad15475c83ae23e00f5a46f803ca
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date:   Thu Jul 9 10:06:17 2026 +0100

    Updated release notes

commit 1523e63e35a15cfd1fa72d230a3c79efa7b9c3e0
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Wed Jul 8 20:48:33 2026 +0200

    Support non-assignable alternatives in variant (#1496)

commit 878a0ba5a28f6b0dcae9f472a6a6a1f107de0a74
Author: John Wellbelove <john.wellbelove@etlcpp.com>
Date:   Wed Jul 8 11:07:01 2026 +0100

    Updated version and release notes

commit 5287fe4b3b9f58330881d2a62abf6ce8e1159b8d
Author: John Wellbelove <jwellbelove@users.noreply.github.com>
Date:   Wed Jul 8 09:06:50 2026 +0100

    Allow etl::vector to store pointers to functions and member functions (#1492)

    * Refactored vector.h

    Deleted ivectorpointer.h

    * Minor changes

    * Fixed code incompatible with C++03

    * Clang-format changes

    * Removed redundant double-qualified name

    * Fixed is_is_object_pointer_v to is_object_pointer_v

    * Added new test files to meson.build

    * Added overflow checks to vector_ext copy and move constructors and assignments

    Added assert throw tests.

    * Added diagnostic pushes to eliminate GCC warning false positives

    * Changed the GCC C++23 Github action to use GCC14

    * Applied clang-format

    * Added guard to pop_heap

    * Added guard to adjust_heap

    * Attempt to fix GCC diagnostic

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * clang-format

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Missing newline at end of file

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    Re-enable ranges algorithm tests

    * Attempt to fix GCC diagnostic

    * clang-format

    * Changed gcc --version to gcc-14 --version

    ---------

    Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
    Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>

commit 40dddd7f9601985ae7350e60a404af9028bc9fe9
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 23:41:17 2026 +0200

    Add optional etl_profile label_flag for Bazel profile injection (#1491)

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 84fca4cd62f72c382c062376ca934803116a20d5
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 21:45:47 2026 +0200

    Add missing memory functions, according the standard (#1490)

    Adding:

    - etl::align
    - etl::assume_aligned
    - etl::is_sufficiently_aligned
    - etl::launder
    - etl::pointer_traits
    - etl::start_lifetime_as

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit dca6653b3759b2cdc79307fec7425589feea478d
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 21:00:09 2026 +0200

    Add missing interfaces to etl::chrono (#1489)

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit c52b2ac7b1f675e6b1d34052239423b7f3634033
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 18:06:05 2026 +0200

    Fix etl::mem_cast cross-size copy constructor and assignment (#1488)

    The templated cross-size copy constructor and assignment operator were
    broken in two ways:

    - They accessed other.buffer / rhs.buffer, which is private in a
      different mem_cast instantiation, so the cross-size overloads failed
      to compile whenever they were actually instantiated.
    - They copied Size_ (the destination size) bytes from a source buffer
      that is only Other_Size bytes large, reading past the end of the
      source when the destination was larger.

    Use the public data() accessor and copy Other_Size bytes. The existing
    static_assert(Size >= Other_Size) guarantees the destination is big
    enough.

    Add test_mem_cast_copy_and_assign_from_smaller regression test.

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 165674cb23dda1fd4f7e5a1182f49554a796bfe8
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 16:41:50 2026 +0200

    Fix etl::pearson::reset() not resetting the first-byte flag (#1487)

    reset() cleared the hash array but left the `first` flag false, so a
    calculator reused after reset() took the XOR update path on its next
    add() instead of the initialisation path. This produced an incorrect
    hash for any pearson object that was reset and reused.

    Reset `first` to true in reset() so a reset calculator behaves like a
    freshly constructed one.

    Add test_pearson_reset regression test.

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 032ec4cc80f1c0f1baa0383bdbc5f9239b68f268
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 13:40:04 2026 +0200

    Fix etl::bitset single-element from_string shift on empty string (#1486)

    The single-element from_string overloads (char, wchar_t, char16_t,
    char32_t) computed element_type(1) << (string_length - 1U) before the
    copy loop. For an empty string, or when active_bits is 0, string_length
    is 0, so the shift count underflows to SIZE_MAX. Shifting by more than
    the element width is undefined behaviour, which also breaks constexpr
    evaluation.

    Guard the mask with a zero-length check so the shift is only performed
    when string_length > 0. The multi-element from_string overloads were
    already safe.

    Add test_construct_from_empty_string regression test.

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 98cb365d4c28e7b72f8547b734a03a4068acd3f5
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Tue Jul 7 12:32:56 2026 +0200

    Fix etl::histogram copy/move not preserving start_index (#1485)

    The run-time-offset specialization of etl::histogram stores a start_index
    member used to map keys to bins (accumulator[key - start_index]). Its copy
    constructor, move constructor, copy assignment, and move assignment copied
    only the accumulator and left start_index uninitialized, so a copied or
    moved histogram indexed the wrong bin in operator[]/add(), causing
    out-of-bounds access (undefined behavior).

    All four special member functions now also copy start_index. The
    compile-time-offset specialization is unaffected, as its start index is a
    template constant rather than a data member.

    Add regression tests covering copy/move construction and assignment.

    Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>

commit 5cd2f28cd54e4019f7be126bcf7394a6a8318eb9
Author: Drew Rife <darife@jlg.com>
Date:   Tue Jul 7 03:14:48 2026 -0400

    Refactor variant::emplace to perfect forward args instead of copy/move construct (#1494)

    * refactor: emplace logic to use do_emplace for in-place construction with forwarded arguments #1493

    * Add test for variant_variadic

    ---------

    Co-authored-by: Roland Reichwein <Roland.Reichwein@bmw.de>

commit a47c0aae81b886259918961d5fcee20faa06fdd0
Author: Roland Reichwein <Roland.Reichwein@bmw.de>
Date:   Mon Jul 6 17:32:26 2026 +0200

    Fix etl::message_timer firing following timers early on unregister (#1484)

    active_list.remove(id, has_expired) adjusts the next timer's delta only
    when has_expired is false. When unregistering an active, non-expired timer,
    unregister_timer() incorrectly passed true, skipping that adjustment, so
    every timer after the removed one in the active list fired early by the
    removed timer's delta.

    Pass false instead, matching the etl::callback_timer family. tick() still
    passes true, since the timer has genuinely expired there. The bug affected
    all four variants: message_timer, message_timer_interrupt,
    message_timer_atomic and message_timer_locked. Existing tests missed it
    because they only ever unregistered the tail timer, where the delta
    adjustment is a no-op.

    Add a regression test that unregisters a non-tail active timer and checks
    the following timer still fires at its original absolute time.
2026-07-11 13:02:42 +01:00

333 lines
10 KiB
Markdown

---
title: "Building ETL with Bazel"
weight: 1
---
ETL provides first-class [Bazel](https://bazel.build/) support, both for developing ETL itself and for consuming it as a dependency in your own projects.
## Prerequisites
- [Bazelisk](https://github.com/bazelbuild/bazelisk) (recommended) or [Bazel](https://bazel.build/install) 7.0 or later (with Bzlmod support)
[Bazelisk](https://github.com/bazelbuild/bazelisk) is a launcher that automatically downloads and runs the Bazel version specified in the `.bazelversion` file at the project root. This ensures all contributors use a consistent Bazel version. Simply install Bazelisk and use `bazel` as usual — it transparently delegates to the correct version.
## Syntax Checks
To validate that every ETL header is well-formed and compiles on its own (equivalent to `test/run-syntax-checks.sh` for CMake):
```sh
bazel build //test/syntax_check:syntax_check
```
This compiles a set of minimal `.t.cpp` files, each of which includes a single ETL header, with strict warning flags enabled.
## Cleaning Build Artifacts
To remove all build outputs and symlinks (`bazel-bin`, `bazel-out`, `bazel-etl`, etc.):
```sh
bazel clean
```
For a full cleanup including the external dependency cache:
```sh
bazel clean --expunge
```
## Running Unit Tests
To run the full test suite:
```sh
bazel test //test:etl_tests
```
You can also pass standard Bazel flags:
```sh
# Run with verbose test output
bazel test //test:etl_tests --test_output=all
# Run tests matching a filter (UnitTest++ subset)
bazel test //test:etl_tests --test_arg=<suite_name>
```
## Using ETL in Your Project
### With Bzlmod (recommended, Bazel 7+)
Add ETL as a dependency in your project's `MODULE.bazel`:
```python
bazel_dep(name = "etl", version = "20.47.1")
git_override(
module_name = "etl",
remote = "https://github.com/ETLCPP/etl.git",
tag = "20.47.1", # or a specific commit
)
```
Then depend on it in your `BUILD.bazel`:
```python
cc_library(
name = "my_library",
srcs = ["my_library.cpp"],
hdrs = ["my_library.h"],
deps = ["@etl//:etl"],
)
```
### With WORKSPACE (legacy)
In your `WORKSPACE` file:
```python
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "etl",
remote = "https://github.com/ETLCPP/etl.git",
tag = "20.47.1",
)
```
Then use `deps = ["@etl//:etl"]` in your targets as shown above.
## Project Structure
| File | Purpose |
|---|---|
| `MODULE.bazel` | Module definition and dependencies |
| `BUILD.bazel` | Exposes ETL as a `cc_library` |
| `.bazelversion` | Bazel version for Bazelisk |
| `.bazelrc` | Default Bazel settings |
| `test/BUILD.bazel` | Unit test target |
| `test/syntax_check/BUILD.bazel` | Header syntax check target |
| `test/UnitTest++/BUILD.bazel` | Vendored UnitTest++ framework |
## Cross-Compilation
Bazel supports cross-compilation through its [platforms](https://bazel.build/extending/platforms) and [toolchains](https://bazel.build/extending/toolchains) system. Since ETL is a header-only library, there is nothing to cross-compile for the library itself. However, when building tests or consuming ETL in an application targeting a different architecture, you need to define a platform and register an appropriate C++ toolchain.
Example platform definition (e.g. in a `platforms/BUILD.bazel`):
```python
platform(
name = "linux_arm64",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:aarch64",
],
)
```
Then build with:
```sh
bazel build //:etl --platforms=//platforms:linux_arm64
```
> **Note:** You must also have a C++ toolchain registered that supports the target platform.
> See the [Bazel toolchains documentation](https://bazel.build/extending/toolchains) for details.
### Running Cross-Compiled Tests under QEMU
Cross-compiled test binaries cannot run natively on the host. Pre-defined configurations in `.bazelrc` select the correct cross-compiler via `--repo_env=CC`, set the build flags to match `.devcontainer/run-tests.sh` (C++23, No-STL, `-O0`), and use `--run_under` to execute the resulting binary under the appropriate QEMU emulator:
```sh
# Cross-build and run tests for ARM (armhf)
bazel test //test:etl_tests --config=armhf
# Other architectures
bazel test //test:etl_tests --config=i386
bazel test //test:etl_tests --config=powerpc
bazel test //test:etl_tests --config=riscv64
bazel test //test:etl_tests --config=s390x
```
These configs are designed to run inside the Docker containers under `.devcontainer/`, which provide the cross-compiler toolchains and QEMU binaries. Each config sets `CC`, `AR`, `LD`, `NM`, `STRIP`, and `OBJDUMP` via `--repo_env` so that Bazel's auto-configured toolchain finds the complete prefixed cross-tool suite.
You can also use `--run_under` directly for custom setups:
```sh
bazel test //test:etl_tests --run_under=/usr/bin/qemu-arm-static
```
## Compiler and Build Configuration
Unlike CMake where options like `ETL_CXX_STANDARD` and `CMAKE_CXX_COMPILER` are set at configure time, Bazel uses command-line flags and `.bazelrc` configurations.
### C++ Standard Version
Use `--cxxopt` to pass the desired standard flag:
```sh
# C++17 (default in .bazelrc)
bazel test //test:etl_tests --cxxopt=-std=c++17
# C++20
bazel test //test:etl_tests --cxxopt=-std=c++20
# C++23
bazel test //test:etl_tests --cxxopt=-std=c++23
# C++14
bazel test //test:etl_tests --cxxopt=-std=c++14
```
### Optimization Level
Use `--compilation_mode` (shorthand `-c`) for standard profiles, or `--copt` for explicit flags:
```sh
# Debug (default) — no optimization, debug symbols
bazel test //test:etl_tests -c dbg
# Optimized — O2 with NDEBUG
bazel test //test:etl_tests -c opt
# Fast build — no optimization, no debug symbols
bazel test //test:etl_tests -c fastbuild
# Custom optimization level
bazel test //test:etl_tests --copt=-O3
bazel test //test:etl_tests --copt=-O1
bazel test //test:etl_tests --copt=-Os
```
### Selecting the Compiler (GCC vs Clang)
Bazel uses the system's default `CC` environment variable. Override it to switch compilers:
```sh
# Use Clang
bazel test //test:etl_tests --repo_env=CC=clang
# Use a specific GCC version
bazel test //test:etl_tests --repo_env=CC=gcc-13
# Use a specific Clang version
bazel test //test:etl_tests --repo_env=CC=clang-18
```
> **Note:** Bazel's auto-configured toolchain infers the C++ compiler from `CC` automatically
> (e.g. `CC=gcc-13` → `g++-13` for C++ compilation). There is no need to set `CXX` separately.
### Combining Options
Flags can be combined freely:
```sh
# Clang, C++20, optimized
bazel test //test:etl_tests --repo_env=CC=clang --cxxopt=-std=c++20 -c opt
# GCC 13, C++23, debug
bazel test //test:etl_tests --repo_env=CC=gcc-13 --cxxopt=-std=c++23 -c dbg
```
### STL vs. No-STL Mode
ETL can operate without the standard library, which is common on bare-metal embedded targets. Use `--copt` to define `ETL_NO_STL`:
```sh
# Build and test without STL
bazel test //test:etl_tests --copt=-DETL_NO_STL
# Build with STL (default, no flag needed)
bazel test //test:etl_tests
```
When `ETL_NO_STL` is defined, ETL provides its own implementations of containers, algorithms, and utilities instead of delegating to `<algorithm>`, `<type_traits>`, etc.
### Type Traits Configuration
ETL supports three type traits strategies, controlled via preprocessor defines:
| Mode | Define | Description |
|---|---|---|
| **STL type traits** | *(default)* | Uses `<type_traits>` from the standard library |
| **Compiler builtins** | `ETL_USE_TYPE_TRAITS_BUILTINS` | Uses compiler intrinsics (`__is_trivially_copyable`, etc.) — useful when STL headers are unavailable or incomplete |
| **User-defined** | `ETL_USER_DEFINED_TYPE_TRAITS` | Uses ETL's own type traits implementations |
```sh
# Use compiler built-in type traits
bazel test //test:etl_tests --copt=-DETL_USE_TYPE_TRAITS_BUILTINS
# Use ETL's own user-defined type traits
bazel test //test:etl_tests --copt=-DETL_USER_DEFINED_TYPE_TRAITS
```
These are mutually exclusive — define at most one. If neither is defined and STL is available, ETL uses `<type_traits>`.
### Other Configuration Defines
Additional defines can be passed the same way via `--copt=-D...`:
| Define | Description |
|---|---|
| `ETL_FORCE_TEST_CPP03_IMPLEMENTATION` | Force C++03 code paths even when a newer standard is available |
| `ETL_MESSAGES_ARE_NOT_VIRTUAL` | Use non-virtual message types |
```sh
# Force C++03 implementation paths
bazel test //test:etl_tests --copt=-DETL_FORCE_TEST_CPP03_IMPLEMENTATION
```
### Using `.bazelrc` Presets
To avoid retyping flags, add configurations to `.bazelrc`:
```
# .bazelrc
# Named configurations
build:clang --repo_env=CC=clang
build:gcc13 --repo_env=CC=gcc-13
build:c++20 --cxxopt=-std=c++20
build:c++23 --cxxopt=-std=c++23
build:release --compilation_mode=opt
```
Then use them with `--config`:
```sh
bazel test //test:etl_tests --config=clang --config=c++20 --config=release
```
### Injecting a Custom Profile Header
Instead of passing many individual `--copt=-D...` flags, ETL can be configured in bulk through a user-provided `etl_profile.h` header. When such a header is reachable on the include path, `include/etl/platform.h` includes it automatically; otherwise ETL falls back to its built-in defaults (`ETL_NO_PROFILE_HEADER`).
Because the header must sit on ETL's include path, `BUILD.bazel` exposes an `etl_profile` [`label_flag`](https://bazel.build/extending/config) so you can inject it as a Bazel target — without patching ETL's `BUILD.bazel`.
First, wrap your profile header in a `cc_library` that places `etl_profile.h` on its include path:
```python
# path/to/BUILD.bazel
cc_library(
name = "my_etl_profile",
hdrs = ["etl_profile.h"],
strip_include_prefix = ".",
)
```
Then point the `etl_profile` flag at that target when building:
```sh
bazel build @etl//:etl --@etl//:etl_profile=//path/to:my_etl_profile
```
The flag defaults to an empty library (`:no_profile`), so ETL uses its built-in defaults unless you override it. As with any flag, the override can be made permanent in `.bazelrc`:
```
# .bazelrc
build --@etl//:etl_profile=//path/to:my_etl_profile
```
> **Note:** Use the repository-qualified form `--@etl//:etl_profile=...` when consuming ETL as an
> external dependency. When building from within the ETL repository itself, drop the repository
> prefix: `--//:etl_profile=...`.