Skip to content

Commit

Permalink
Updates to cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
austinschneider committed Oct 12, 2024
1 parent 7a34462 commit 00164a1
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 24 deletions.
94 changes: 71 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.3.2 FATAL_ERROR)
cmake_policy(VERSION 3.3.2)
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
cmake_policy(VERSION 3.20)

if(${CMAKE_HOST_SYSTEM_NAME} MATCHES "Darwin")
set(MACOSX TRUE)
Expand All @@ -11,13 +11,14 @@ SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake ${CMAKE_CURRENT_LIST_DIR}/
message(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")
message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")

set(CIBUILDWHEEL $ENV{CIBUILDWHEEL})
set(CIBUILDWHEEL "$ENV{CIBUILDWHEEL}")
message(STATUS "CIBUILDWHEEL: ${CIBUILDWHEEL}")
if(${CIBUILDWHEEL})
set(CI_INSTALL_PREFIX $ENV{CI_INSTALL_PREFIX})
if(CIBUILDWHEEL STREQUAL "1")
set(CI_INSTALL_PREFIX "$ENV{CI_INSTALL_PREFIX}")
message(STATUS "CI_INSTALL_PREFIX: ${CI_INSTALL_PREFIX}")
endif()


# parse pyproject.toml for the version
include(pyproject)

Expand All @@ -31,21 +32,31 @@ SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_C_STANDARD 99)

# set the build type and appropriate flags
option(CMAKE_BUILD_TYPE "" "Release")
set(_FLAGS "-O2 -Wall -fPIC")
set(_FLAGS_DEBUG "-g -O0 -Wall -fPIC")
set(_FLAGS_RELEASE "-O2 -Wall -fPIC -s")
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()

# Create an interface library for SIREN compile options
add_library(siren_compile_options INTERFACE)

# Specify the compile options
target_compile_options(siren_compile_options INTERFACE
-O2
-Wall
-fPIC
$<$<CONFIG:Debug>:-g>
$<$<CONFIG:Debug>:-O0>
$<$<CONFIG:Release>:-O2>
$<$<CONFIG:Release>:-s>
)

# Conditionally add -stdlib=libc++ for Clang
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(_FLAGS "${_FLAGS} -stdlib=libc++")
set(_FLAGS_DEBUG "${_FLAGS_DEBUG} -stdlib=libc++")
set(_FLAGS_RELEASE "${_FLAGS_RELEASE} -stdlib=libc++")
target_compile_options(siren_compile_options INTERFACE -stdlib=libc++)
endif()
set(CMAKE_CXX_FLAGS ${_FLAGS})
set(CMAKE_CXX_FLAGS_DEBUG ${_FLAGS_DEBUG})
set(CMAKE_CXX_FLAGS_RELEASE ${_FLAGS_RELEASE})

# override install locations when building python extensions
if(DEFINED SKBUILD)
if(DEFINED SKBUILD_PLATLIB_DIR)
cmake_path(RELATIVE_PATH SKBUILD_HEADERS_DIR BASE_DIRECTORY ${SKBUILD_PLATLIB_DIR} OUTPUT_VARIABLE CMAKE_INSTALL_INCLUDEDIR)
cmake_path(RELATIVE_PATH SKBUILD_PLATLIB_DIR BASE_DIRECTORY ${SKBUILD_PLATLIB_DIR} OUTPUT_VARIABLE CMAKE_INSTALL_LIBDIR)
message(STATUS "Setting include dir to: ${CMAKE_INSTALL_INCLUDEDIR}")
Expand All @@ -63,12 +74,42 @@ include(pybind11)

# load project dependencies
include(rk)
if(TARGET rk_static)
target_link_libraries(rk_static INTERFACE siren_compile_options)
endif()
if(TARGET rk_shared)
target_link_libraries(rk_shared INTERFACE siren_compile_options)
endif()
include(cereal)
if(TARGET cereal)
target_link_libraries(cereal INTERFACE siren_compile_options)
endif()
include(delabella)
if(TARGET delabella_static)
target_link_libraries(delabella_static INTERFACE siren_compile_options)
endif()
if(TARGET delabella_shared)
target_link_libraries(delabella_shared INTERFACE siren_compile_options)
endif()
include(CFITSIO)
include(photospline)
if(TARGET photospline)
target_link_libraries(photospline INTERFACE siren_compile_options)
endif()
include(googletest)
if(TARGET gtest)
target_link_libraries(gtest INTERFACE siren_compile_options)
endif()
if(TARGET gtest_main)
target_link_libraries(gtest_main INTERFACE siren_compile_options)
endif()
if(TARGET gmock)
target_link_libraries(gmock INTERFACE siren_compile_options)
endif()
include(NamedType)
if(TARGET NamedType)
target_link_libraries(NamedType INTERFACE siren_compile_options)
endif()

# load macros for googletest
include(testing)
Expand All @@ -87,8 +128,9 @@ add_subdirectory(projects/injection)
# define the target library
add_library(SIREN SHARED)
set_property(TARGET SIREN PROPERTY POSITION_INDEPENDENT_CODE ON)
target_link_libraries(SIREN INTERFACE siren_compile_options)

if(${MACOSX})
if(DEFINED MACOSX AND MACOSX)
if(CMAKE_VERSION VERSION_LESS 3.13)
target_link_libraries(SIREN PUBLIC "$<$<PLATFORM_ID:Darwin>:LINKER:-undefined,dynamic_lookup>")
else()
Expand Down Expand Up @@ -127,12 +169,15 @@ target_link_libraries(SIREN
)
endif()

# Export siren_compile_options
install(TARGETS siren_compile_options EXPORT ${PROJECT_NAME}Config)

# define the install path normally or for python package
if(DEFINED SKBUILD)
if(DEFINED SKBUILD_PLATLIB_DIR)
set_target_properties(SIREN PROPERTIES
BUILD_WITH_INSTALL_RPATH FALSE
LINK_FLAGS "-Wl,-rpath,\\\$ORIGIN")
if(${CIBUILDWHEEL})
if(DEFINED CIBUILDWHEEL AND CIBUILDWHEEL)
message(STATUS "Setting SIREN install lib dir to: ${CI_INSTALL_PREFIX}/lib")
message(STATUS "Setting SIREN install include dir to: ${CI_INSTALL_PREFIX}/include")
install(TARGETS SIREN
Expand Down Expand Up @@ -183,7 +228,7 @@ else()
endif()

# optionally package runtime dependencies
if((DEFINED SKBUILD) AND (PACKAGE_SHARED_DEPS))
if((DEFINED SKBUILD_PLATLIB_DIR) AND (PACKAGE_SHARED_DEPS))
install(CODE "set(SIREN_LIB_FILE \"${PROJECT_BINARY_DIR}/${CMAKE_SHARED_MODULE_PREFIX}SIREN${CMAKE_SHARED_MODULE_SUFFIX}\")")
install(CODE "set(PYTHON_DEP_LIB_DESTINATION \"${SKBUILD_PLATLIB_DIR}/siren.libs/\")")
install(CODE [[
Expand All @@ -209,7 +254,7 @@ if((DEFINED SKBUILD) AND (PACKAGE_SHARED_DEPS))
endif()

# install the python extensions
if(DEFINED SKBUILD)
if(DEFINED SKBUILD_PLATLIB_DIR)
install(TARGETS utilities
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/siren)
install(TARGETS math
Expand Down Expand Up @@ -241,15 +286,18 @@ write_basic_package_version_file(
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
export(EXPORT ${PROJECT_NAME}Config FILE ${PROJECT_NAME}Config.cmake)
export(EXPORT ${PROJECT_NAME}Config FILE "${PROJECT_NAME}Config.cmake"
NAMESPACE ${PROJECT_NAME}::)

# Make importable from install location
set(_config_dir share/${PROJECT_NAME}/cmake)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION ${_config_dir}
)
install(EXPORT ${PROJECT_NAME}Config
DESTINATION ${_config_dir}
FILE "${PROJECT_NAME}Config.cmake"
NAMESPACE ${PROJECT_NAME}::
DESTINATION "${_config_dir}"
)

MESSAGE("")
Expand Down
5 changes: 4 additions & 1 deletion projects/dataclasses/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ target_include_directories(SIREN_dataclasses PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SIREN/dataclasses/>
)

target_link_libraries(SIREN_dataclasses PUBLIC
target_link_libraries(SIREN_dataclasses
INTERFACE
siren_compile_options
PUBLIC
photospline
SIREN_serialization
SIREN_utilities
Expand Down
2 changes: 2 additions & 0 deletions projects/detector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ target_include_directories(SIREN_detector PUBLIC
)

target_link_libraries(SIREN_detector
INTERFACE
siren_compile_options
PRIVATE
$<BUILD_INTERFACE:rk_static>
PUBLIC
Expand Down
2 changes: 2 additions & 0 deletions projects/distributions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ target_include_directories(SIREN_distributions PUBLIC
)

target_link_libraries(SIREN_distributions
INTERFACE
siren_compile_options
PRIVATE
$<BUILD_INTERFACE:rk_static>
pybind11::embed
Expand Down
2 changes: 2 additions & 0 deletions projects/geometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ target_include_directories(SIREN_geometry PUBLIC
)

target_link_libraries(SIREN_geometry
INTERFACE
siren_compile_options
PRIVATE
$<BUILD_INTERFACE:rk_static>
PUBLIC
Expand Down
2 changes: 2 additions & 0 deletions projects/injection/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ target_include_directories(SIREN_injection PUBLIC
)

target_link_libraries(SIREN_injection
INTERFACE
siren_compile_options
PRIVATE
$<BUILD_INTERFACE:rk_static>
pybind11::embed
Expand Down
2 changes: 2 additions & 0 deletions projects/interactions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ target_include_directories(SIREN_interactions PUBLIC
)

target_link_libraries(SIREN_interactions
INTERFACE
siren_compile_options
PRIVATE
$<BUILD_INTERFACE:rk_static>
pybind11::embed
Expand Down
4 changes: 4 additions & 0 deletions projects/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ target_include_directories(SIREN_math PUBLIC

if(${MACOSX})
target_link_libraries(SIREN_math
INTERFACE
siren_compile_options
PUBLIC
photospline
delabella_shared
Expand All @@ -26,6 +28,8 @@ target_link_libraries(SIREN_math
)
else()
target_link_libraries(SIREN_math
INTERFACE
siren_compile_options
PUBLIC
photospline
delabella_shared
Expand Down
5 changes: 5 additions & 0 deletions projects/serialization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ target_include_directories(SIREN_serialization INTERFACE
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SIREN/serialization/>
)

target_link_libraries(SIREN_serialization
INTERFACE
siren_compile_options
)

install(DIRECTORY "${PROJECT_SOURCE_DIR}/projects/serialization/public/"
EXPORT ${PROJECT_NAME}Config
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
Expand Down
5 changes: 5 additions & 0 deletions projects/utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ target_include_directories(SIREN_utilities PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SIREN/utilities/>
)

target_link_libraries(SIREN_utilities
INTERFACE
siren_compile_options
)

install(DIRECTORY "${PROJECT_SOURCE_DIR}/projects/utilities/public/"
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING
Expand Down

0 comments on commit 00164a1

Please sign in to comment.