Skip to content

Commit e94b219

Browse files
Copilotcochcoder
andcommitted
Move Rust crate to src/libslic3r/Support/TreeSupportRust/ and add CMake integration
Co-authored-by: cochcoder <103969142+cochcoder@users.noreply.github.com>
1 parent db3c700 commit e94b219

File tree

19 files changed

+135
-26
lines changed

19 files changed

+135
-26
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ test.js
4343
/.cache/
4444
.clangd
4545
internal_docs/
46-
orca-tree-supports/target/
46+
src/libslic3r/Support/TreeSupportRust/target/

src/libslic3r/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,9 @@ add_library(libslic3r STATIC ${lisbslic3r_sources}
486486
${OpenVDBUtils_SOURCES})
487487
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${lisbslic3r_sources})
488488

489+
# Rust tree support library (optional — requires cargo/Rust toolchain)
490+
add_subdirectory(Support/TreeSupportRust)
491+
489492
if (SLIC3R_STATIC)
490493
set(CGAL_Boost_USE_STATIC_LIBS ON CACHE BOOL "" FORCE)
491494
endif ()
@@ -603,6 +606,13 @@ target_link_libraries(libslic3r
603606
OpenSSL::Crypto
604607
)
605608

609+
# Link Rust tree support library if cargo/Rust toolchain is available
610+
if(TARGET orca_tree_supports)
611+
target_link_libraries(libslic3r PRIVATE orca_tree_supports)
612+
target_compile_definitions(libslic3r PRIVATE HAS_RUST_TREE_SUPPORTS=1)
613+
message(STATUS "Rust tree support library enabled")
614+
endif()
615+
606616
if(NOT WIN32)
607617
# Link freetype for OCCT dependency (CAD operations need font rendering)
608618
target_link_libraries(libslic3r PRIVATE ${FREETYPE_LIBRARIES})
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# CMakeLists.txt for the Rust tree support library (orca-tree-supports)
2+
#
3+
# This file integrates the Rust crate into the CMake build system.
4+
# It invokes `cargo build` to compile the Rust static library and
5+
# creates an IMPORTED target that can be linked by libslic3r.
6+
#
7+
# Requirements:
8+
# - Rust toolchain (rustc, cargo) must be installed and on PATH
9+
# - Minimum Rust version: 1.70 (2021 edition)
10+
11+
# Detect cargo
12+
find_program(CARGO_EXECUTABLE cargo HINTS $ENV{HOME}/.cargo/bin)
13+
if(NOT CARGO_EXECUTABLE)
14+
message(WARNING "cargo not found — Rust tree support library will NOT be built. "
15+
"Install Rust from https://rustup.rs/ to enable.")
16+
return()
17+
endif()
18+
19+
message(STATUS "Found cargo: ${CARGO_EXECUTABLE}")
20+
21+
# Determine build type for cargo
22+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
23+
set(CARGO_BUILD_TYPE "debug")
24+
set(CARGO_BUILD_FLAGS "")
25+
else()
26+
set(CARGO_BUILD_TYPE "release")
27+
set(CARGO_BUILD_FLAGS "--release")
28+
endif()
29+
30+
# Paths
31+
set(RUST_CRATE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
32+
set(RUST_TARGET_DIR "${CMAKE_CURRENT_SOURCE_DIR}/target")
33+
set(RUST_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/include/orca_tree_supports.h")
34+
35+
# Determine the static library filename (platform-dependent)
36+
if(WIN32)
37+
set(RUST_LIB_NAME "orca_tree_supports.lib")
38+
elseif(APPLE)
39+
set(RUST_LIB_NAME "liborca_tree_supports.a")
40+
else()
41+
set(RUST_LIB_NAME "liborca_tree_supports.a")
42+
endif()
43+
44+
set(RUST_LIB_PATH "${RUST_TARGET_DIR}/${CARGO_BUILD_TYPE}/${RUST_LIB_NAME}")
45+
46+
# Custom command: build the Rust crate
47+
add_custom_command(
48+
OUTPUT "${RUST_LIB_PATH}"
49+
COMMAND ${CARGO_EXECUTABLE} build ${CARGO_BUILD_FLAGS}
50+
--manifest-path "${RUST_CRATE_DIR}/Cargo.toml"
51+
--target-dir "${RUST_TARGET_DIR}"
52+
WORKING_DIRECTORY "${RUST_CRATE_DIR}"
53+
DEPENDS
54+
"${RUST_CRATE_DIR}/Cargo.toml"
55+
"${RUST_CRATE_DIR}/Cargo.lock"
56+
"${RUST_CRATE_DIR}/src/lib.rs"
57+
"${RUST_CRATE_DIR}/src/config.rs"
58+
"${RUST_CRATE_DIR}/src/mesh.rs"
59+
"${RUST_CRATE_DIR}/src/overhang_detection.rs"
60+
"${RUST_CRATE_DIR}/src/branch_generation.rs"
61+
"${RUST_CRATE_DIR}/src/collision.rs"
62+
"${RUST_CRATE_DIR}/src/interface_layers.rs"
63+
"${RUST_CRATE_DIR}/src/placement.rs"
64+
"${RUST_CRATE_DIR}/src/ffi.rs"
65+
"${RUST_CRATE_DIR}/src/utils.rs"
66+
COMMENT "Building Rust tree support library (${CARGO_BUILD_TYPE})..."
67+
)
68+
69+
# Custom target that triggers the build
70+
add_custom_target(orca_tree_supports_build
71+
DEPENDS "${RUST_LIB_PATH}"
72+
)
73+
74+
# Create an IMPORTED static library target
75+
add_library(orca_tree_supports STATIC IMPORTED GLOBAL)
76+
set_target_properties(orca_tree_supports PROPERTIES
77+
IMPORTED_LOCATION "${RUST_LIB_PATH}"
78+
INTERFACE_INCLUDE_DIRECTORIES "${RUST_CRATE_DIR}/include"
79+
)
80+
add_dependencies(orca_tree_supports orca_tree_supports_build)
81+
82+
# On Unix, Rust static libs need to link against system threading and dl libs
83+
if(UNIX AND NOT APPLE)
84+
set_target_properties(orca_tree_supports PROPERTIES
85+
INTERFACE_LINK_LIBRARIES "pthread;dl;m"
86+
)
87+
elseif(APPLE)
88+
set_target_properties(orca_tree_supports PROPERTIES
89+
INTERFACE_LINK_LIBRARIES "-framework Security;-framework CoreFoundation"
90+
)
91+
elseif(WIN32)
92+
set_target_properties(orca_tree_supports PROPERTIES
93+
INTERFACE_LINK_LIBRARIES "ws2_32;userenv;bcrypt;ntdll"
94+
)
95+
endif()
96+
97+
# Custom target to run Rust tests
98+
add_custom_target(orca_tree_supports_test
99+
COMMAND ${CARGO_EXECUTABLE} test
100+
--manifest-path "${RUST_CRATE_DIR}/Cargo.toml"
101+
--target-dir "${RUST_TARGET_DIR}"
102+
WORKING_DIRECTORY "${RUST_CRATE_DIR}"
103+
COMMENT "Running Rust tree support tests..."
104+
)
File renamed without changes.
File renamed without changes.

orca-tree-supports/README.md renamed to src/libslic3r/Support/TreeSupportRust/README.md

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,31 @@ for FDM 3D printing. It is designed as a drop-in replacement for the C++
6565

6666
### Build the library
6767

68+
The Rust library is automatically built by CMake when a Rust toolchain is available.
69+
It integrates into the standard OrcaSlicer build process:
70+
6871
```bash
69-
cd orca-tree-supports
72+
# Standard OrcaSlicer build (includes Rust tree support if cargo is available)
73+
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
74+
cmake --build build
75+
76+
# Or build standalone for development:
77+
cd src/libslic3r/Support/TreeSupportRust
7078
cargo build --release
7179
```
7280

7381
This produces:
7482
- `target/release/liborca_tree_supports.a` (static library)
75-
- `target/release/liborca_tree_supports.so` (shared library on Linux)
76-
- `target/release/liborca_tree_supports.dylib` (shared library on macOS)
7783
- `include/orca_tree_supports.h` (C header, generated by cbindgen)
7884

7985
### Run tests
8086

8187
```bash
88+
cd src/libslic3r/Support/TreeSupportRust
8289
cargo test
90+
91+
# Or via CMake:
92+
cmake --build build --target orca_tree_supports_test
8393
```
8494

8595
### Generate documentation
@@ -90,28 +100,13 @@ cargo doc --open
90100

91101
## Integration with OrcaSlicer C++
92102

93-
### 1. Link the Rust library
94-
95-
Add to your CMakeLists.txt:
96-
97-
```cmake
98-
# Build the Rust crate
99-
add_custom_command(
100-
OUTPUT ${CMAKE_BINARY_DIR}/liborca_tree_supports.a
101-
COMMAND cargo build --release --manifest-path ${CMAKE_SOURCE_DIR}/orca-tree-supports/Cargo.toml
102-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/orca-tree-supports
103-
)
104-
105-
# Link against the static library
106-
target_link_libraries(libslic3r PRIVATE
107-
${CMAKE_BINARY_DIR}/liborca_tree_supports.a
108-
)
109-
target_include_directories(libslic3r PRIVATE
110-
${CMAKE_SOURCE_DIR}/orca-tree-supports/include
111-
)
112-
```
103+
The Rust tree support library is automatically integrated into the build when a
104+
Rust toolchain (cargo) is detected. CMake will:
105+
1. Build the Rust static library via `cargo build`
106+
2. Link it into `libslic3r`
107+
3. Define `HAS_RUST_TREE_SUPPORTS=1` when available
113108

114-
### 2. Call from C++
109+
### Call from C++
115110

116111
```cpp
117112
#include "orca_tree_supports.h"
File renamed without changes.
File renamed without changes.

orca-tree-supports/include/orca_tree_supports.h renamed to src/libslic3r/Support/TreeSupportRust/include/orca_tree_supports.h

File renamed without changes.

orca-tree-supports/src/branch_generation.rs renamed to src/libslic3r/Support/TreeSupportRust/src/branch_generation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ fn drop_single_branch(
298298
nodes.push(node);
299299
}
300300

301-
let reaches_buildplate = !nodes.is_empty() && nodes.last().is_some_and(|n| n.layer_index == 0);
301+
let reaches_buildplate = nodes.last().is_some_and(|n| n.layer_index == 0);
302302

303303
Branch {
304304
nodes,

0 commit comments

Comments
 (0)