From 01153b6efaec26474cf13f6e337b27c37e2900c6 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Sat, 17 Feb 2024 20:58:47 -0800 Subject: [PATCH] Support switching Rust library backend with cargo features (#93) --- .github/workflows/build-rust.yml | 16 ++-------- rust/Cargo.toml | 5 ++++ rust/build.rs | 50 ++++++++++++++++++++------------ 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/.github/workflows/build-rust.yml b/.github/workflows/build-rust.yml index 5de1d942..b817d014 100644 --- a/.github/workflows/build-rust.yml +++ b/.github/workflows/build-rust.yml @@ -70,23 +70,11 @@ jobs: version: latest platform: x64 - - name: Select optimizer backend (non-macOS) - if: ${{ !startsWith(matrix.os, 'macOS') }} - run: | - sed -i 's/let optimizer_backend = "[a-z]\+"/let optimizer_backend = "${{ matrix.optimizer_backend }}"/' rust/build.rs - grep 'let optimizer_backend' rust/build.rs - - - name: Select optimizer backend (macOS) - if: startsWith(matrix.os, 'macOS') - run: | - sed -i '' 's/let optimizer_backend = "[a-z]\+"/let optimizer_backend = "${{ matrix.optimizer_backend }}"/' rust/build.rs - grep 'let optimizer_backend' rust/build.rs - - name: Build Rust working-directory: rust - run: cargo build --target ${{ matrix.rust-target }} + run: cargo build --target ${{ matrix.rust-target }} --features ${{ matrix.optimizer_backend }} - name: Run Rust if: matrix.rust-target != 'aarch64-apple-darwin' && matrix.rust-target != 'x86_64-pc-windows-gnu' working-directory: rust - run: cargo run --example swerve --target ${{ matrix.rust-target }} + run: cargo run --example swerve --target ${{ matrix.rust-target }} --features ${{ matrix.optimizer_backend }} diff --git a/rust/Cargo.toml b/rust/Cargo.toml index dcd65f59..b2f68114 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -16,5 +16,10 @@ cmake = "0.1" [lib] name = "trajoptlib" +[features] +default = [] +sleipnir = [] +casadi = [] + [[example]] name = "swerve" diff --git a/rust/build.rs b/rust/build.rs index 1558bcfa..b98259ba 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -2,15 +2,34 @@ use cmake::Config; fn main() -> miette::Result<()> { let mut cmake_config = Config::new(".."); - let optimizer_backend = "casadi"; cmake_config .profile("RelWithDebInfo") - .define("OPTIMIZER_BACKEND", optimizer_backend) .define("BUILD_TESTING", "OFF"); - if cfg!(target_os = "windows") { - if optimizer_backend == "casadi" { + if cfg!(feature = "sleipnir") && cfg!(feature = "casadi") { + panic!("Only select one optimizer backend via cargo `--features sleipnir` or `--features casadi`."); + } + + if cfg!(feature = "sleipnir") { + cmake_config + .define("OPTIMIZER_BACKEND", "sleipnir") + .define("BUILD_SHARED_LIBS", "OFF"); + + if cfg!(target_os = "windows") { + cmake_config + .generator("Visual Studio 17 2022") + .define("CMAKE_GENERATOR_PLATFORM", "x64") + .cxxflag("/EHsc"); + } else if cfg!(target_os = "linux") { + cmake_config + .define("CMAKE_CXX_COMPILER", "g++") + .define("CMAKE_C_COMPILER", "gcc"); + } + } else if cfg!(feature = "casadi") { + cmake_config.define("OPTIMIZER_BACKEND", "casadi"); + + if cfg!(target_os = "windows") { cmake_config .generator("MinGW Makefiles") .define("CMAKE_CXX_COMPILER", "x86_64-w64-mingw32-g++") @@ -20,22 +39,15 @@ fn main() -> miette::Result<()> { "-static-libgcc -static-libstdc++", ) .define("CMAKE_EXE_LINKER_FLAGS", "-static-libgcc -static-libstdc++"); - } else if optimizer_backend == "sleipnir" { + } else if cfg!(target_os = "linux") { cmake_config - .generator("Visual Studio 17 2022") - .define("CMAKE_GENERATOR_PLATFORM", "x64") - .cxxflag("/EHsc"); + .define("CMAKE_CXX_COMPILER", "g++") + .define("CMAKE_C_COMPILER", "gcc"); } - } - - if cfg!(target_os = "linux") { - cmake_config - .define("CMAKE_CXX_COMPILER", "g++") - .define("CMAKE_C_COMPILER", "gcc"); - } - - if optimizer_backend == "sleipnir" { - cmake_config.define("BUILD_SHARED_LIBS", "OFF"); + } else { + panic!( + "Select an optimizer backend via cargo `--features sleipnir` or `--features casadi`." + ); } let dst = cmake_config.build(); @@ -43,7 +55,7 @@ fn main() -> miette::Result<()> { println!("cargo:rustc-link-search=native={}/bin", dst.display()); println!("cargo:rustc-link-search=native={}/lib", dst.display()); println!("cargo:rustc-link-lib=TrajoptLib"); - if optimizer_backend == "sleipnir" { + if cfg!(feature = "sleipnir") { println!("cargo:rustc-link-lib=Sleipnir"); println!("cargo:rustc-link-lib=fmt"); }