Skip to content

Commit

Permalink
fix issue oxfordcontrol#46 - support use via add_subdirectory
Browse files Browse the repository at this point in the history
This commit allows Clarabel.cpp to be consumed by other CMake projects
via a call to `add_subdirectory`:

```cmake

add_subdirectory(path/to/clarabel.cpp)
```

Other projects might have Clarabel.cpp as a git submodule, and this
makes it easier to do so.
  • Loading branch information
codeinred committed Jun 6, 2024
1 parent 1333c1a commit 144af58
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED True)

# Set the root directory of the project. This is needed
# so that Clarabel.cpp can be consumed by other projects via
# `add_subdirectory`
set(CLARABEL_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")

#----------------------------------------------
# Clarabel feature configuration
#----------------------------------------------
set(CLARABEL_FEATURE_SDP "none" CACHE STRING "Package for SDP to be selected")
set_property(CACHE CLARABEL_FEATURE_SDP PROPERTY STRINGS
set_property(CACHE CLARABEL_FEATURE_SDP PROPERTY STRINGS
none
sdp-accelerate
sdp-netlib
Expand Down Expand Up @@ -55,4 +60,4 @@ option(CLARABEL_BUILD_TESTS "Build the unit tests for Clarabel.cpp" false)
if(CLARABEL_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
endif()
10 changes: 5 additions & 5 deletions rust_wrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ add_library(libclarabel_c_shared INTERFACE)
# Debug/Release flags
if(CMAKE_BUILD_TYPE MATCHES Release)
set(clarabel_c_build_flags "--release")
set(clarabel_c_output_directory "${CMAKE_SOURCE_DIR}/rust_wrapper/target/release")
set(clarabel_c_output_directory "${CLARABEL_ROOT_DIR}/rust_wrapper/target/release")
else()
set(clarabel_c_build_flags "")
set(clarabel_c_output_directory "${CMAKE_SOURCE_DIR}/rust_wrapper/target/debug")
set(clarabel_c_output_directory "${CLARABEL_ROOT_DIR}/rust_wrapper/target/debug")
endif()

set(CLARABEL_C_OUTPUT_DIR ${clarabel_c_output_directory} PARENT_SCOPE)
Expand Down Expand Up @@ -86,7 +86,7 @@ endif()
# Add the cargo project as a custom target
add_custom_target(
libclarabel_c
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/rust_wrapper
WORKING_DIRECTORY ${CLARABEL_ROOT_DIR}/rust_wrapper
# Commands for building the Rust library
COMMAND cargo build ${clarabel_c_build_flags}
COMMAND cargo install cbindgen --version 0.24.5
Expand All @@ -104,10 +104,10 @@ add_custom_target(

# Static library
target_link_libraries(libclarabel_c_static INTERFACE ${LIBCLARABEL_C_STATIC_PATH})
target_include_directories(libclarabel_c_static INTERFACE ${CMAKE_SOURCE_DIR}/include)
target_include_directories(libclarabel_c_static INTERFACE ${CLARABEL_ROOT_DIR}/include)
add_dependencies(libclarabel_c_static libclarabel_c)

# Shared library
target_link_libraries(libclarabel_c_shared INTERFACE ${LIBCLARABEL_C_SHARED_PATH})
target_include_directories(libclarabel_c_shared INTERFACE ${CMAKE_SOURCE_DIR}/include)
target_include_directories(libclarabel_c_shared INTERFACE ${CLARABEL_ROOT_DIR}/include)
add_dependencies(libclarabel_c_shared libclarabel_c)

0 comments on commit 144af58

Please sign in to comment.