Skip to content

Compiling Clarabel.cpp in larger CMake project #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ set(CLARABEL_ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
#----------------------------------------------
# Clarabel feature configuration
#----------------------------------------------

# Include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include/c)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

# SDP feature configuration
set(CLARABEL_FEATURE_SDP "none" CACHE STRING "Package for SDP to be selected")
set_property(CACHE CLARABEL_FEATURE_SDP PROPERTY STRINGS
none
Expand Down Expand Up @@ -56,11 +62,24 @@ endif()
add_subdirectory(rust_wrapper)

# Add other subdirectories
add_subdirectory(examples)
option(CLARABEL_BUILD_EXAMPLES "Build examples for Clarabel.cpp" true)
if(CLARABEL_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

# Add tests
option(CLARABEL_BUILD_TESTS "Build the unit tests for Clarabel.cpp" false)
if(CLARABEL_BUILD_TESTS)
enable_testing()
add_subdirectory(tests)
endif()


message(STATUS "Clarabel.cpp: CMAKE_INSTALL_LIBDIR = ${CMAKE_INSTALL_LIBDIR}")
message(STATUS "Clarabel.cpp: LIBRARY = ${LIBRARY}")
message(STATUS "Clarabel.cpp: PROJECT_NAME = ${PROJECT_NAME}")
install(TARGETS libclarabel_c_shared EXPORT ${PROJECT_NAME}
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)

install(EXPORT ${PROJECT_NAME} DESTINATION cmake)
70 changes: 47 additions & 23 deletions rust_wrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ 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 "${CLARABEL_ROOT_DIR}/rust_wrapper/target/release")
set(clarabel_c_output_directory "${CMAKE_PROJECT_DIR}/rust_wrapper/target/release")
else()
set(clarabel_c_build_flags "")
set(clarabel_c_output_directory "${CLARABEL_ROOT_DIR}/rust_wrapper/target/debug")
set(clarabel_c_output_directory "${CMAKE_PROJECT_DIR}/rust_wrapper/target/debug")
endif()

if (CLARABEL_C_OUTPUT_DIR)
message(STATUS "using CLARABEL_C_OUTPUT_DIR ${CLARABEL_C_OUTPUT_DIR}")
else()
set(CLARABEL_C_OUTPUT_DIR ${clarabel_c_output_directory} PARENT_SCOPE)
set(CLARABEL_C_OUTPUT_DIR ${clarabel_c_output_directory})
message(STATUS "setting CLARABEL_C_OUTPUT_DIR to ${clarabel_c_output_directory}")
endif()

set(CLARABEL_C_OUTPUT_DIR ${clarabel_c_output_directory} PARENT_SCOPE)

if(NOT DEFINED CLARABEL_CARGO_FEATURES)
set(CLARABEL_CARGO_FEATURES "")
Expand Down Expand Up @@ -108,6 +115,8 @@ endif()

rust_features_has_sdp(CONFIG_SDP)
if(CONFIG_SDP)
# Set the Rust feature flag
set(clarabel_c_build_flags "${clarabel_c_build_flags};--features;${CLARABEL_FEATURE_SDP}")
# Define the FEATURE_SDP flag for all targets that link against clarabel_c
target_compile_definitions(libclarabel_c_static INTERFACE FEATURE_SDP)
target_compile_definitions(libclarabel_c_shared INTERFACE FEATURE_SDP)
Expand Down Expand Up @@ -165,35 +174,48 @@ message("-- Cargo options: " "${clarabel_c_build_flags}")
# -------------------------------------
# -------------------------------------

# Add the cargo project as a custom target
add_custom_target(
libclarabel_c
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
# Commands for building the Rust library
COMMAND cargo build ${clarabel_c_build_flags}
COMMAND cargo install cbindgen --version 0.24.5
# Generate the C header
COMMAND cbindgen --config cbindgen.toml --crate clarabel_c --output ./headers/clarabel.h --lang c
# Generate the C++ header
COMMAND cbindgen --config cbindgen.toml --crate clarabel_c --output ./headers/clarabel.hpp
COMMAND cp ${PROJECT_SOURCE_DIR}/target/release/lib* ${CLARABEL_C_OUTPUT_DIR}
)

# Get the path to the Rust library for linking
if(APPLE)
set(LIBCLARABEL_C_SHARED_PATH "${clarabel_c_output_directory}/libclarabel_c.dylib")
set(LIBCLARABEL_C_STATIC_PATH "${clarabel_c_output_directory}/libclarabel_c.a")
set(LIBCLARABEL_C_SHARED_PATH "${CLARABEL_C_OUTPUT_DIR}/libclarabel_c.dylib")
set(LIBCLARABEL_C_STATIC_PATH "${CLARABEL_C_OUTPUT_DIR}/libclarabel_c.a")
elseif(UNIX)
set(LIBCLARABEL_C_SHARED_PATH "${clarabel_c_output_directory}/libclarabel_c.so")
set(LIBCLARABEL_C_STATIC_PATH "${clarabel_c_output_directory}/libclarabel_c.a")
set(LIBCLARABEL_C_SHARED_PATH "${CLARABEL_C_OUTPUT_DIR}/libclarabel_c.so")
set(LIBCLARABEL_C_STATIC_PATH "${CLARABEL_C_OUTPUT_DIR}/libclarabel_c.a")
elseif(WIN32)
set(LIBCLARABEL_C_SHARED_PATH "${clarabel_c_output_directory}/clarabel_c.dll.lib")
set(LIBCLARABEL_C_STATIC_PATH "${clarabel_c_output_directory}/clarabel_c.lib")
set(LIBCLARABEL_C_SHARED_PATH "${CLARABEL_C_OUTPUT_DIR}/clarabel_c.dll.lib")
set(LIBCLARABEL_C_STATIC_PATH "${CLARABEL_C_OUTPUT_DIR}/clarabel_c.lib")
endif()


# Add the cargo project as a custom target
add_custom_target(
libclarabel_c
WORKING_DIRECTORY ${CLARABEL_ROOT_DIR}/rust_wrapper
# Commands for building the Rust library
COMMAND cargo build ${clarabel_c_build_flags}
COMMAND cargo install cbindgen
# Generate the C header
COMMAND cbindgen --config cbindgen.toml --quiet --crate clarabel_c --output ./headers/clarabel.h --lang c
# Generate the C++ header
COMMAND cbindgen --config cbindgen.toml --quiet --crate clarabel_c --output ./headers/clarabel.hpp
BYPRODUCTS
"${LIBCLARABEL_C_SHARED_PATH}"
"${LIBCLARABEL_C_STATIC_PATH}"
)
# add_custom_target(
# libclarabel_c
# WORKING_DIRECTORY ${CLARABEL_ROOT_DIR}/rust_wrapper
# # Commands for building the Rust library
# COMMAND cargo build ${clarabel_c_build_flags}
# COMMAND cargo install cbindgen
# # Generate the C header
# COMMAND cbindgen --config cbindgen.toml --quiet --crate clarabel_c --output ./headers/clarabel.h --lang c
# # Generate the C++ header
# COMMAND cbindgen --config cbindgen.toml --quiet --crate clarabel_c --output ./headers/clarabel.hpp
# BYPRODUCTS
# "${LIBCLARABEL_C_SHARED_PATH}"
# "${LIBCLARABEL_C_STATIC_PATH}"
# )


# Wrap the Rust library in a CMake library target
Expand All @@ -207,3 +229,5 @@ add_dependencies(libclarabel_c_static libclarabel_c)
target_link_libraries(libclarabel_c_shared INTERFACE ${LIBCLARABEL_C_SHARED_PATH})
target_include_directories(libclarabel_c_shared INTERFACE ${CLARABEL_ROOT_DIR}/include)
add_dependencies(libclarabel_c_shared libclarabel_c)

install(FILES "${CLARABEL_C_OUTPUT_DIR}/libclarabel_c.so" DESTINATION "lib")