Skip to content
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
1 change: 0 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ run --macos_minimum_os=10.12
test --macos_minimum_os=10.12

# FIXME: Temporary workaround for pyo3 stubgen issues. See ticket #45
build --@rules_rust_pyo3//settings:experimental_stubgen=False
112 changes: 77 additions & 35 deletions .github/workflows/_python.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,76 @@ jobs:
# ----------------------------------------------------------------------------

python-build-wheels:
name: Build wheels on ${{ matrix.platform.os }} (${{ matrix.platform.target }})
name: Build wheels on ${{ matrix.platform.runner }}
runs-on: ${{ matrix.platform.runner }}
strategy:
fail-fast: false
matrix:
platform:
# Linux
- { runner: ubuntu-latest, artifact: linux-x86_64 }
- { runner: macos-14, artifact: macos-arm64 }
- { runner: windows-latest, artifact: windows-x86_64 }
python:
- {
os: linux,
runner: ubuntu-latest,
target: x86_64-unknown-linux-gnu,
} # x86 64-bit
version: "3.10",
target: wheel_cp310,
tag: cp310,
dist: dist_cp310,
}
- {
os: linux,
runner: ubuntu-latest,
target: aarch64-unknown-linux-gnu,
} # Arm 64-bit
- {
os: linux,
runner: ubuntu-latest,
target: armv7-unknown-linux-gnueabihf,
} # Arm 32-bit
# macOS
- { os: macos, runner: macos-latest, target: universal2-apple-darwin } # MacOS Universal
# Windows
- {
os: windows,
runner: windows-latest,
target: x86_64-pc-windows-msvc,
} # x86 64-bit
version: "3.11",
target: wheel_cp311,
tag: cp311,
dist: dist_cp311,
}
- { version: "3.12+", target: wheel_abi3, tag: abi3, dist: dist_abi3 }
env:
CI: true
BAZELISK_ALLOW_ENVS: Y
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Python
uses: actions/setup-python@v4
- name: Cache Bazel
uses: actions/cache@v4
with:
python-version: "3.12"
path: |
~/.cache/bazel
C:\bzl
key: bazel-${{ runner.os }}
restore-keys: bazel-${{ runner.os }}

- name: Build wheels (Unix)
if: runner.os != 'Windows'
shell: bash
env:
MSYS2_ARG_CONV_EXCL: "*"
run: |
bazel build //python:${{ matrix.python.target }}.dist
BAZEL_BIN="$(bazel info bazel-bin)"
mkdir -p dist
cp "${BAZEL_BIN}/python/${{ matrix.python.dist }}/"*.whl dist/

- name: Build wheels (Windows)
if: runner.os == 'Windows'
shell: pwsh
run: |
$ErrorActionPreference = 'Stop'
$bazelArgs = @("--output_user_root=C:/bzl")
bazel @bazelArgs build //python:${{ matrix.python.target }}.dist
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
$bazelBin = bazel @bazelArgs info bazel-bin
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
New-Item -ItemType Directory -Force -Path dist | Out-Null
Copy-Item "$bazelBin/python/${{ matrix.python.dist }}/*.whl" dist/

- name: Build wheels
uses: PyO3/maturin-action@v1
with:
working-directory: python
target: ${{ matrix.platform.target }}
args: --release --strip --out dist --compatibility pypi
manylinux: ${{ matrix.platform.os == 'linux' && 'auto' || null }}
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-${{ matrix.platform.os }}-${{ matrix.platform.target }}
path: python/dist/*.whl
name: wheels-${{ matrix.platform.artifact }}-${{ matrix.python.tag }}
path: dist/*.whl

# ----------------------------------------------------------------------------
# Test Python Bindings
Expand All @@ -74,6 +91,14 @@ jobs:
name: Test Python Bindings
runs-on: ubuntu-latest
needs: python-build-wheels
strategy:
fail-fast: false
matrix:
python:
- { version: "3.10", tag: cp310 }
- { version: "3.11", tag: cp311 }
- { version: "3.12", tag: abi3 }
- { version: "3.13", tag: abi3 }
env:
CI: true
BAZELISK_ALLOW_ENVS: Y
Expand All @@ -90,6 +115,23 @@ jobs:
key: bazel-${{ runner.os }}
restore-keys: bazel-${{ runner.os }}

- name: Setup Python ${{ matrix.python.version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python.version }}

- name: Download Linux wheel
uses: actions/download-artifact@v4
with:
name: wheels-linux-x86_64-${{ matrix.python.tag }}
path: dist/

- name: Smoke test built wheel
run: |
python -m pip install --upgrade pip
python -m pip install dist/*.whl
python -c "import dv_py; value = dv_py.DimensionalVariable(10, 'm').value(); print(value)"

- name: Test Python Bindings
run: bazel test //python/... --test_output=errors --keep_going

Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ __pycache__/

# CMake Build Directories
build/
build-*/

# VSCode Settings
.vscode/
.vscode/
6 changes: 3 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ This project is a library in multiple languages for handling units and checking
- Raise and assert `DVError` for invalid operations.

### C/C++ (bindings)
- Headers live in `cpp/include`:
- `dv_c.h` for C API
- `dv.hpp` for C++ RAII wrapper
- The Rust Diplomat bridge lives under `core/ffi/diplomat`.
- Bazel-facing generated bindings live in the language directories: `c/`, `cpp/`, and `python/`.
- `cpp/include/dv.hpp` is a thin compatibility wrapper over the generated Diplomat C++ bindings.
- CMake support is minimal; prefer Bazel for consistency.

### Docs (Docusaurus)
Expand Down
101 changes: 69 additions & 32 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,52 +1,89 @@
cmake_minimum_required(VERSION 3.20)
project(dv LANGUAGES C CXX)

# Options
option(DV_BUILD_SHARED "Build shared library" ON)

# Build Rust C-ABI via Cargo. We use ExternalProject for portability.
include(ExternalProject)
set(DV_RUST_DIR ${CMAKE_CURRENT_LIST_DIR}/cpp/capi)
set(DV_BUILD_CMD cargo build --manifest-path ${DV_RUST_DIR}/Cargo.toml --release)

ExternalProject_Add(dv_capi_ext
SOURCE_DIR ${DV_RUST_DIR}
find_program(BAZEL_EXECUTABLE bazel REQUIRED)
find_program(CARGO_EXECUTABLE cargo REQUIRED)

set(DV_BRIDGE_DIR ${CMAKE_CURRENT_LIST_DIR}/core/ffi/diplomat)
set(DV_BRIDGE_ENTRY ${DV_BRIDGE_DIR}/src/lib.rs)
set(DV_CODEGEN_ROOT ${CMAKE_BINARY_DIR}/generated/diplomat)
set(DV_CODEGEN_C_DIR ${DV_CODEGEN_ROOT}/c)
set(DV_CODEGEN_CPP_DIR ${DV_CODEGEN_ROOT}/cpp)

file(MAKE_DIRECTORY ${DV_CODEGEN_C_DIR} ${DV_CODEGEN_CPP_DIR})

set(DV_GEN_C_HEADERS
${DV_CODEGEN_C_DIR}/BaseUnits.d.h
${DV_CODEGEN_C_DIR}/BaseUnits.h
${DV_CODEGEN_C_DIR}/DvError.d.h
${DV_CODEGEN_C_DIR}/DvError.h
${DV_CODEGEN_C_DIR}/DimensionalVariable.d.h
${DV_CODEGEN_C_DIR}/DimensionalVariable.h
${DV_CODEGEN_C_DIR}/diplomat_runtime.h
)
set(DV_GEN_CPP_HEADERS
${DV_CODEGEN_CPP_DIR}/BaseUnits.d.hpp
${DV_CODEGEN_CPP_DIR}/BaseUnits.hpp
${DV_CODEGEN_CPP_DIR}/DvError.d.hpp
${DV_CODEGEN_CPP_DIR}/DvError.hpp
${DV_CODEGEN_CPP_DIR}/DimensionalVariable.d.hpp
${DV_CODEGEN_CPP_DIR}/DimensionalVariable.hpp
${DV_CODEGEN_CPP_DIR}/diplomat_runtime.hpp
)

add_custom_command(
OUTPUT ${DV_GEN_C_HEADERS}
COMMAND ${CMAKE_COMMAND} -E make_directory ${DV_CODEGEN_C_DIR}
COMMAND ${BAZEL_EXECUTABLE} run //tools/diplomat_codegen:diplomat_codegen -- c ${DV_CODEGEN_C_DIR} --entry ${DV_BRIDGE_ENTRY}
DEPENDS ${DV_BRIDGE_ENTRY}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
VERBATIM
)

add_custom_command(
OUTPUT ${DV_GEN_CPP_HEADERS}
COMMAND ${CMAKE_COMMAND} -E make_directory ${DV_CODEGEN_CPP_DIR}
COMMAND ${BAZEL_EXECUTABLE} run //tools/diplomat_codegen:diplomat_codegen -- cpp ${DV_CODEGEN_CPP_DIR} --entry ${DV_BRIDGE_ENTRY}
DEPENDS ${DV_BRIDGE_ENTRY}
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
VERBATIM
)

add_custom_target(dv_diplomat_codegen DEPENDS ${DV_GEN_C_HEADERS} ${DV_GEN_CPP_HEADERS})

set(DV_CARGO_TARGET_DIR ${DV_BRIDGE_DIR}/target)
set(DV_CARGO_BUILD_CMD
${CARGO_EXECUTABLE} build
--manifest-path ${DV_BRIDGE_DIR}/Cargo.toml
--release
)

ExternalProject_Add(dv_diplomat_bridge_ext
SOURCE_DIR ${DV_BRIDGE_DIR}
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND ""
BUILD_COMMAND ${DV_BUILD_CMD}
BUILD_COMMAND ${DV_CARGO_BUILD_CMD}
INSTALL_COMMAND ""
BUILD_BYPRODUCTS
${DV_RUST_DIR}/target/release/libdv_capi.a
${DV_RUST_DIR}/target/release/libdv_capi.dylib
BUILD_BYPRODUCTS
${DV_BRIDGE_DIR}/target/release/libdv_diplomat_bridge.a
)

# Library artifact path
if(APPLE)
set(DV_CAPI_SHARED ${DV_RUST_DIR}/target/release/libdv_capi.dylib)
elseif(UNIX)
set(DV_CAPI_SHARED ${DV_RUST_DIR}/target/release/libdv_capi.so)
elseif(WIN32)
set(DV_CAPI_SHARED ${DV_RUST_DIR}/target/release/dv_capi.dll)
endif()
set(DV_CAPI_STATIC ${DV_RUST_DIR}/target/release/libdv_capi.a)
set(DV_DIPLOMAT_STATIC ${DV_BRIDGE_DIR}/target/release/libdv_diplomat_bridge.a)

add_library(dv_c STATIC IMPORTED GLOBAL)
set_target_properties(dv_c PROPERTIES
IMPORTED_LOCATION ${DV_CAPI_STATIC}
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_LIST_DIR}/cpp/include
IMPORTED_LOCATION ${DV_DIPLOMAT_STATIC}
INTERFACE_INCLUDE_DIRECTORIES ${DV_CODEGEN_C_DIR}
Comment on lines +69 to +78

Copilot AI Mar 23, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The imported static library path is hard-coded to libdv_diplomat_bridge.a. On Windows, Cargo typically produces a .lib (and different naming conventions), so this CMake build will fail there. Add platform-specific handling for the produced static artifact (and consider wiring CARGO_TARGET_DIR if you want predictable output locations).

Copilot uses AI. Check for mistakes.
)
add_dependencies(dv_c dv_capi_ext)

# Help executables find the dylib at runtime on macOS when running from build tree
if(APPLE)
set_property(TARGET dv_c PROPERTY INTERFACE_LINK_DIRECTORIES ${DV_RUST_DIR}/target/release)
endif()
target_link_libraries(dv_c INTERFACE $<$<PLATFORM_ID:Linux>:m>)
add_dependencies(dv_c dv_diplomat_bridge_ext dv_diplomat_codegen)

add_library(dv_cpp INTERFACE)
target_include_directories(dv_cpp INTERFACE ${CMAKE_CURRENT_LIST_DIR}/cpp/include)
target_include_directories(dv_cpp INTERFACE ${DV_CODEGEN_CPP_DIR})
target_link_libraries(dv_cpp INTERFACE dv_c)

add_subdirectory(cpp)
add_dependencies(dv_cpp dv_diplomat_codegen)

add_subdirectory(examples/c)
add_subdirectory(examples/cpp)
Loading
Loading