Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Commit

Permalink
Support switching Rust library backend with cargo features (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Feb 18, 2024
1 parent 14c0a49 commit 01153b6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
16 changes: 2 additions & 14 deletions .github/workflows/build-rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
5 changes: 5 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ cmake = "0.1"
[lib]
name = "trajoptlib"

[features]
default = []
sleipnir = []
casadi = []

[[example]]
name = "swerve"
50 changes: 31 additions & 19 deletions rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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++")
Expand All @@ -20,30 +39,23 @@ 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();

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");
}
Expand Down

0 comments on commit 01153b6

Please sign in to comment.