Skip to content

Commit d11d9fa

Browse files
committed
CMake cleanup
1 parent fb1d6d6 commit d11d9fa

3 files changed

Lines changed: 43 additions & 48 deletions

File tree

CMakeLists.txt

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.15)
1+
cmake_minimum_required(VERSION 3.24)
22
project(samseg LANGUAGES C CXX)
33

44
include(FetchContent)
@@ -8,23 +8,12 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
88
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Default build type" FORCE)
99
endif()
1010

11-
find_package(pybind11 QUIET)
12-
if(NOT pybind11_FOUND)
13-
message(STATUS "pybind11 not found, fetching from source...")
14-
FetchContent_Declare(pybind11
15-
GIT_REPOSITORY https://github.com/pybind/pybind11.git
16-
GIT_TAG stable
17-
GIT_SHALLOW TRUE
18-
)
19-
FetchContent_MakeAvailable(pybind11)
20-
endif()
21-
22-
find_package(ITK QUIET)
23-
2411
set(CMAKE_CXX_STANDARD 17)
2512
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2613
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
2714

15+
option(PROFILING "Enable profiling with -pg" OFF)
16+
2817
# Warning flags applied only to our own targets via INTERFACE library
2918
# so that third-party dependencies (ITK, GDCM, etc.) are not affected.
3019
add_library(samseg_warnings INTERFACE)
@@ -33,21 +22,22 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
3322
-Wall -Wextra
3423
-Wno-sign-compare -Wno-unused-parameter
3524
)
25+
elseif(MSVC)
26+
target_compile_options(samseg_warnings INTERFACE /W3)
3627
endif()
3728

38-
if(APPLE)
39-
add_link_options(-dead_strip)
40-
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
41-
add_link_options(-static-libgcc)
42-
endif()
43-
elseif(NOT WIN32)
44-
add_compile_options(-fdata-sections -ffunction-sections)
45-
add_link_options(-Wl,--gc-sections)
29+
if(PROFILING AND NOT MSVC)
30+
target_compile_options(samseg_warnings INTERFACE -pg)
31+
target_link_options(samseg_warnings INTERFACE -pg)
4632
endif()
4733

48-
if(PROFILING)
49-
add_compile_options(-pg)
50-
add_link_options(-pg)
34+
# Binary size optimization flags, applied per-target via INTERFACE library
35+
add_library(samseg_linkopt INTERFACE)
36+
if(APPLE)
37+
target_link_options(samseg_linkopt INTERFACE -Wl,-dead_strip)
38+
elseif(NOT WIN32)
39+
target_compile_options(samseg_linkopt INTERFACE -fdata-sections -ffunction-sections)
40+
target_link_options(samseg_linkopt INTERFACE -Wl,--gc-sections)
5141
endif()
5242

5343
add_subdirectory(gems)

gems/CMakeLists.txt

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
if(NOT ITK_FOUND)
2-
find_package(ITK 5.4.5 QUIET)
3-
endif()
1+
# ITK: try local install first, fetch from source if unavailable.
2+
# FIND_PACKAGE_ARGS is not used here because ITK 5.4.x requires
3+
# include(${ITK_USE_FILE}) for factory registration, which is only
4+
# set by find_package — not by FetchContent's add_subdirectory path.
5+
find_package(ITK 5.4.5 QUIET)
46
if(NOT ITK_FOUND)
57
message(STATUS "ITK not found, fetching from source (v5.4.5)...")
68
FetchContent_Declare(ITK
@@ -13,15 +15,14 @@ if(NOT ITK_FOUND)
1315
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
1416
FetchContent_MakeAvailable(ITK)
1517
set(ITK_DIR "${itk_BINARY_DIR}" CACHE PATH "" FORCE)
18+
find_package(ITK REQUIRED)
1619
endif()
1720

18-
# Per ITK Software Guide §2.5
19-
find_package(ITK REQUIRED)
21+
# UseITK.cmake handles factory registration and required compiler flags.
22+
# Still required with ITK 5.4.x which does not export namespace targets.
2023
include(${ITK_USE_FILE})
2124

22-
if(NOT ZLIB_FOUND)
23-
find_package(ZLIB QUIET)
24-
endif()
25+
find_package(ZLIB QUIET)
2526
if(NOT ZLIB_FOUND)
2627
# ITK exports its bundled zlib-ng as a target named "zlib"
2728
if(TARGET zlib)
@@ -98,18 +99,10 @@ function(configure_gems_target target)
9899
${ITK_INCLUDE_DIRS}
99100
)
100101
target_link_libraries(${target}
101-
PRIVATE samseg_warnings ${ZLIB_LIBRARIES}
102+
PRIVATE samseg_warnings samseg_linkopt ${ZLIB_LIBRARIES}
102103
PUBLIC ${ITK_LIBRARIES}
103104
)
104105

105-
if(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64|AMD64|i[3-6]86)")
106-
if(MSVC)
107-
target_compile_options(${target} PRIVATE /arch:SSE2)
108-
else()
109-
target_compile_options(${target} PRIVATE -msse2 -mfpmath=sse)
110-
endif()
111-
endif()
112-
113106
if(DEFINED ENV{GEMS_DEBUG_CXXFLAG})
114107
message(WARNING "additional gems debug cxxflags: $ENV{GEMS_DEBUG_CXXFLAG}")
115108
target_compile_options(${target} PRIVATE $ENV{GEMS_DEBUG_CXXFLAG})

samseg/gems/_cxx/CMakeLists.txt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
set(PYBIND11_FINDPYTHON ON)
2+
3+
FetchContent_Declare(pybind11
4+
GIT_REPOSITORY https://github.com/pybind/pybind11.git
5+
GIT_TAG v2.13.6
6+
GIT_SHALLOW TRUE
7+
FIND_PACKAGE_ARGS QUIET
8+
)
9+
FetchContent_MakeAvailable(pybind11)
10+
111
pybind11_add_module(gemsbindings
212
module.cxx
313
pyKvlCalculator.cxx
@@ -9,13 +19,15 @@ pybind11_add_module(gemsbindings
919
pyKvlAffineRegistration.cxx
1020
)
1121

12-
target_include_directories(gemsbindings PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${ITK_INCLUDE_DIRS})
13-
target_link_libraries(gemsbindings PRIVATE samseg_warnings kvlGEMSCommon ${ITK_LIBRARIES})
22+
target_include_directories(gemsbindings PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
23+
target_link_libraries(gemsbindings PRIVATE samseg_warnings samseg_linkopt kvlGEMSCommon)
1424

1525
# pybind11 debug builds produce objects too large for the linker with -g
16-
target_compile_options(gemsbindings PRIVATE
17-
$<$<CONFIG:Debug>:-g0>
18-
$<$<CONFIG:RelWithDebInfo>:-g0>
19-
)
26+
if(NOT MSVC)
27+
target_compile_options(gemsbindings PRIVATE
28+
$<$<CONFIG:Debug>:-g0>
29+
$<$<CONFIG:RelWithDebInfo>:-g0>
30+
)
31+
endif()
2032

2133
install(TARGETS gemsbindings LIBRARY DESTINATION samseg/gems)

0 commit comments

Comments
 (0)