diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ae42f5..d9ef7ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -56,7 +62,10 @@ 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) @@ -64,3 +73,13 @@ 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) diff --git a/rust_wrapper/CMakeLists.txt b/rust_wrapper/CMakeLists.txt index 7cd7671..3ad17ac 100644 --- a/rust_wrapper/CMakeLists.txt +++ b/rust_wrapper/CMakeLists.txt @@ -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 "") @@ -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) @@ -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 @@ -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")