Skip to content

Commit

Permalink
Add interface library for project-wide compile options
Browse files Browse the repository at this point in the history
Previously we were relying on the use of add_compile_options and
add_compile_definitions for defining compiler definitions and options for
the entire project. This commit replaces that behaviour by defining a new
mrtrix::common INTERFACE library that links to all targets in the
project (the library is linked to mrtrix::core). This is more idiomatic
and prevents the propagation of compiler definitions to targets that do
not link to mrtrix::core (useful when compiling MRtrix3 as a subproject
of anothe project).
  • Loading branch information
daljit46 committed Jul 30, 2024
1 parent aa448a7 commit 804842f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
40 changes: 20 additions & 20 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,35 +91,35 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git AND NOT EXISTS ${CMAKE_CURRENT_SOURCE
"and then run `pre-commit install` from the ${CMAKE_CURRENT_SOURCE_DIR} directory.")
endif()


# Allow compilation of big object of files in debug mode on MINGW
if(MINGW AND CMAKE_BUILD_TYPE MATCHES "Debug")
add_compile_options(-Wa,-mbig-obj)
endif()

add_compile_definitions(
add_library(mrtrix-common INTERFACE)
add_library(mrtrix::common ALIAS mrtrix-common)
target_compile_definitions(mrtrix-common INTERFACE
MRTRIX_BUILD_TYPE="${CMAKE_BUILD_TYPE}"
$<$<PLATFORM_ID:Windows>:MRTRIX_WINDOWS>
$<$<PLATFORM_ID:Darwin>:MRTRIX_MACOSX>
$<$<PLATFORM_ID:FreeBSD>:MRTRIX_FREEBSD>
)

if(MRTRIX_STL_DEBUGGING AND $<NOT:$<CONFIG:Debug>>)
if(MSVC)
add_compile_definitions(_ITERATOR_DEBUG_LEVEL=1)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(_LIBCPP_DEBUG)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
add_compile_options(_GLIBCXX_DEBUG _GLIBCXX_DEBUG_PEDANTIC)
endif()
if(MRTRIX_STL_DEBUGGING AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
message(STATUS "Enabling STL debug mode")
target_compile_definitions(mrtrix-common INTERFACE
$<$<CXX_COMPILER_ID:MSVC>:_ITERATOR_DEBUG_LEVEL=1>
$<$<CXX_COMPILER_ID:GNU>:_GLIBCXX_DEBUG, _GLIBCXX_DEBUG_PEDANTIC>
$<$<CXX_COMPILER_ID:Clang>:-D_LIBCPP_DEBUG>
)
endif()

if(MRTRIX_WARNINGS_AS_ERRORS)
if (MSVC)
add_compile_options(/WX)
else()
add_compile_options(-Werror)
endif()
message(STATUS "Enabling warnings as errors")
target_compile_options(mrtrix-common INTERFACE
$<$<CXX_COMPILER_ID:MSVC>:/WX>
$<$<CXX_COMPILER_ID:GNU,Clang>:-Werror>
)
endif()

# Allow compilation of big object of files in debug mode on MINGW
if(MINGW AND CMAKE_BUILD_TYPE MATCHES "Debug")
target_compile_options(mrtrix-common INTERFACE -Wa,-mbig-obj)
endif()


Expand Down
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ target_link_libraries(mrtrix-core PUBLIC
ZLIB::ZLIB
${FFTW_LIBRARIES}
mrtrix::core-version-lib
mrtrix::common
Threads::Threads
${FFTW_LIBRARIES}
)
Expand Down

0 comments on commit 804842f

Please sign in to comment.