Skip to content

Commit

Permalink
Enable the C++ build in the CI pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
Notgnoshi committed Feb 25, 2024
1 parent c0c25a9 commit fb2f7d9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 47 deletions.
25 changes: 15 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Tests
on: [push]

jobs:
test:
python-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -22,12 +22,14 @@ jobs:
RUSTFLAGS: -D warnings
CARGO_TERM_COLOR: always
NUM_JOBS: 2
# TODO: Update submodules and perform C++ build
GENERATIVE_CARGO_ENABLE_CMAKE_BUILD: "NO"
steps:
- uses: actions/checkout@v3
# - run: |
# sudo apt-get install cmake build-essential
- uses: actions/checkout@v4
with:
submodules: "recursive"
- name: Install C++ build dependencies
run: |
sudo apt-get update
sudo apt-get install cmake build-essential ninja-build
- name: Set up Rust stable
uses: actions-rs/toolchain@v1
with:
Expand All @@ -45,14 +47,17 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: build
args: --release --all-targets
args: --release --all-targets --all-features -vv
- name: Lint
uses: actions-rs/cargo@v1
with:
command: clippy
args: --release --all-targets
- name: Test
args: --release --all-targets --all-features
- name: Rust Tests
uses: actions-rs/cargo@v1
with:
command: test
args: --release --all-targets
args: --release --all-targets --all-features
- name: C++ Tests
run: |
./target/release/cxx-tests
18 changes: 12 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ wkb = "0.7"
wkt = "0.10"

[build-dependencies]
cc = {version="*", features=["parallel"]}
cmake = "0.1"
cc = {version="*", features=["parallel"], optional=true}
cmake = {version="0.1", optional=true}
cxx-build = {version="1.0", optional=true}
fs_extra = "1.3"
glob = "0.3"
fs_extra = {version="1.3", optional=true}
glob = {version="0.3", optional=true}

[dev-dependencies]
ctor = "0.2"
Expand All @@ -101,5 +101,11 @@ float-cmp = "0.9"
# Tests can dump WKT for ease of visualization
# cargo test --all-features $test_name -- --nocapture | ./tools/render.py
test-io = []
geom2graph-bindings = ["cxx", "cxx-build"]
default = ["geom2graph-bindings"]
# Whether to also build the C++ libgenerative
cxx = ["dep:cmake", "dep:cc", "dep:fs_extra", "dep:glob"]
# Whether to also build the C++ libgenerative tests
cxx-tests = ["cxx"]
# Whether to build the Rust bindings for libgenerative (geom2graph)
cxx-bindings = ["cxx", "dep:cxx", "dep:cxx-build"]

default = ["cxx-bindings"]
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ This is a mixed Python, C++, and Rust project that uses submodules to satisfy th
```
* **C++** - a C++17 compiler and CMake
```shell
sudo apt install build-essential cmake
sudo apt install build-essential cmake, ninja-build
git submodule update --init --recursive
```
* **Python**
Expand All @@ -72,6 +72,11 @@ The Rust build has been configured to also perform the C++ CMake build, so all y
cargo build
```

The C++ CMake build can be disabled by disabling the default `cxx` feature with
```shell
cargo build --no-default-features
```

## How to test

To run the Python tests:
Expand All @@ -85,6 +90,8 @@ To run the Rust tests:
cargo test
```

The C++ tests, if enabled, are copied to `target/debug/cxx-tests`

# The tools

## A note on composability
Expand Down
40 changes: 13 additions & 27 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
#[cfg(not(feature = "cxx"))]
fn main() {}

#[cfg(feature = "cxx")]
fn main() {
// Options
let enable_cmake_build = if cfg!(feature = "geom2graph-bindings") {
String::from("ON")
} else {
std::env::var("GENERATIVE_CARGO_ENABLE_CMAKE_BUILD").unwrap_or_else(|_| "ON".to_string())
};
let enable_doxygen = std::env::var("GENERATIVE_CARGO_ENABLE_CMAKE_DOXYGEN")
.unwrap_or_else(|_| "OFF".to_string());
let enable_pch =
std::env::var("GENERATIVE_CARGO_ENABLE_CMAKE_PCH").unwrap_or_else(|_| "OFF".to_string());
let enable_lto =
std::env::var("GENERATIVE_CARGO_ENABLE_CMAKE_LTO").unwrap_or_else(|_| "OFF".to_string());
let enable_tests =
std::env::var("GENERATIVE_CARGO_ENABLE_CMAKE_TESTS").unwrap_or_else(|_| "ON".to_string());
if enable_cmake_build.is_empty()
|| enable_cmake_build == "OFF"
|| enable_cmake_build == "NO"
|| enable_cmake_build == "FALSE"
{
return;
}
#[rustfmt::skip]
let enable_tests = if cfg!(feature = "cxx-tests") { "ON" } else { "OFF" };

// Rebuild if any of the C++ code changes
println!("cargo:rerun-if-changed=tools/geom2graph.cpp");
println!("cargo:rerun-if-changed=CMakeLists.txt");

// TODO: Remove tools/ when geom2graph.rs is swapped in
for allow_dir in ["generative", "tests", "tools"] {
for cmakelist in glob::glob(format!("{allow_dir}/**/CMakeLists.txt").as_str()).unwrap() {
println!("cargo:rerun-if-changed={}", cmakelist.unwrap().display());
Expand All @@ -46,10 +31,10 @@ fn main() {
.define("CMAKE_EXPORT_COMPILE_COMMANDS", "ON")
.define("CMAKE_INSTALL_LIBDIR", "lib")
.define("CMAKE_GENERATOR", "Ninja")
.define("GENERATIVE_BUILD_DOCS", enable_doxygen)
.define("GENERATIVE_ENABLE_PCH", enable_pch)
.define("GENERATIVE_ENABLE_LTO", enable_lto)
.define("GENERATIVE_ENABLE_TESTING", &enable_tests)
.define("GENERATIVE_BUILD_DOCS", "OFF")
.define("GENERATIVE_ENABLE_PCH", "OFF")
.define("GENERATIVE_ENABLE_LTO", "OFF")
.define("GENERATIVE_ENABLE_TESTING", enable_tests)
.define("GENERATIVE_TOOL_INSTALL_RPATH", "$ORIGIN/lib") // binaries just stashed in /target/debug/
.build();

Expand All @@ -60,6 +45,7 @@ fn main() {
options.overwrite = true;
fs_extra::dir::copy(src, dest, &options).unwrap();

// TODO: Remove in favor of geom2graph.rs
let geom2graph = format!("{}/bin/geom2graph", install_dir.display());
let dest = format!("{}/../../../geom2graph", &out_dir);
std::fs::copy(geom2graph, dest).unwrap();
Expand All @@ -78,7 +64,7 @@ fn main() {
let dest = format!("{manifest_dir}/compile_commands.json");
std::fs::copy(database, dest).unwrap();

#[cfg(feature = "geom2graph-bindings")]
#[cfg(feature = "cxx-bindings")]
{
println!("cargo:rustc-link-search=native={}/../../../lib/", &out_dir);
println!("cargo:rustc-link-lib=static=generative");
Expand Down
2 changes: 1 addition & 1 deletion generative/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub type NodeIndex = usize;
pub type GeometryGraph<Direction = petgraph::Undirected> =
petgraph::Graph<NodeData, EdgeWeight, Direction, NodeIndex>;

#[cfg(feature = "geom2graph-bindings")]
#[cfg(feature = "cxx-bindings")]
mod wrapper {
use super::*;
use crate::cxxbridge::{GeometryGraphShim, GraphEdge};
Expand Down
4 changes: 2 additions & 2 deletions generative/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[cfg(feature = "geom2graph-bindings")]
#[cfg(feature = "cxx-bindings")]
mod cxxbridge;
pub mod dla;
pub mod flatten;
mod geometry_mut_map;
pub mod graph;
pub mod io;
#[cfg(feature = "geom2graph-bindings")]
#[cfg(feature = "cxx-bindings")]
pub mod noding;
pub mod snap;
pub mod triangulation;
Expand Down

0 comments on commit fb2f7d9

Please sign in to comment.