Modernize CMake to use target-scoped constructs (minimum 3.22)#141
Modernize CMake to use target-scoped constructs (minimum 3.22)#141
Conversation
- Update cmake_minimum_required from 3.10 to 3.22 - Add LANGUAGES to project() command - Use list(APPEND ...) for CMAKE_MODULE_PATH - Replace -fPIC flags with CMAKE_POSITION_INDEPENDENT_CODE - Replace add_definitions() with target_compile_definitions() - Replace include_directories() with target_include_directories() - Replace link_libraries() with per-target target_link_libraries() - Modernize FindNETCDF.cmake and FindNIFTI.cmake with find_package_handle_standard_args - Remove deprecated include_directories/link_directories from UseLIBMINC.cmake.in Co-authored-by: gdevenyi <3001850+gdevenyi@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR modernizes the libminc build system by replacing directory-scoped CMake settings with target-scoped properties and updating several custom find modules, aligned with a new minimum CMake version (3.22).
Changes:
- Replaces directory-scoped
link_libraries()usage in tests/examples with per-targettarget_link_libraries(). - Introduces target-scoped include paths and compile definitions for core targets (e.g.,
minc2,minc_io). - Modernizes
FindNETCDF.cmake/FindNIFTI.cmaketo usefind_package_handle_standard_args()and simplifiesCMAKE_MODULE_PATHhandling.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| CMakeLists.txt | Bumps CMake minimum, switches to target-scoped include dirs/compile defs, enables PIC via CMAKE_POSITION_INDEPENDENT_CODE. |
| testdir/CMakeLists.txt | Replaces directory-wide linking with explicit per-test target linking. |
| ezminc/CMakeLists.txt | Makes minc_io include paths/compile defs target-scoped. |
| ezminc/tests/CMakeLists.txt | Switches to per-target linking for test executables. |
| ezminc/examples/CMakeLists.txt | Switches to per-target linking for example executables. |
| cmake-modules/FindNETCDF.cmake | Uses FindPackageHandleStandardArgs for standardized discovery reporting. |
| cmake-modules/FindNIFTI.cmake | Uses FindPackageHandleStandardArgs for standardized discovery reporting. |
| UseLIBMINC.cmake.in | Removes legacy include/link directory side effects and adds guidance comments. |
Comments suppressed due to low confidence (2)
CMakeLists.txt:490
- When building the static library variant (LIBMINC_BUILD_SHARED_LIBS), MINC1 support currently only adds NETCDF include/link settings to the shared target. The static target (${LIBMINC_LIBRARY_STATIC}) may fail to compile (missing netcdf headers) and/or link (missing netcdf lib) when LIBMINC_MINC1_SUPPORT is ON. Apply NETCDF include dirs and NETCDF_LIBRARY to the static target as well, and ensure the NETCDF link is attached to the static target (not the shared one) inside the UNIX/LIBMINC_BUILD_SHARED_LIBS block.
if(LIBMINC_BUILD_SHARED_LIBS)
add_library(${LIBMINC_LIBRARY_STATIC} STATIC ${minc_LIB_SRCS} ${minc_HEADERS} ${volume_io_LIB_SRCS} ${volume_io_HEADERS} )
target_include_directories(${LIBMINC_LIBRARY_STATIC} PUBLIC ${MINC_INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS})
target_compile_definitions(${LIBMINC_LIBRARY_STATIC} PUBLIC ${MINC_COMPILE_DEFINITIONS})
target_link_libraries(${LIBMINC_LIBRARY_STATIC} ${HDF5_LIBRARY} ${NIFTI_LIBRARIES} ${ZLIB_LIBRARY} ${RT_LIBRARY} m ${CMAKE_DL_LIBS} )
if(LIBMINC_MINC1_SUPPORT)
target_link_libraries(${LIBMINC_LIBRARY} ${NETCDF_LIBRARY})
endif()
UseLIBMINC.cmake.in:17
- UseLIBMINC.cmake.in no longer applies LIBMINC_INCLUDE_DIRS/LIBMINC_LIBRARY_DIRS to consumers (include_directories/link_directories were removed), but the installed package does not appear to install an EXPORT file / imported targets either. This makes the advertised “backward-compatible variable-based configuration” effectively non-functional for downstream projects that still do
include(${LIBMINC_USE_FILE})and link via ${LIBMINC_LIBRARIES}/${EZMINC_LIBRARIES}. Either restore the legacy include/link directory setup here, or complete the migration by installing/exporting targets and updating the config/use-file to rely on them.
# Usage: Downstream projects should prefer using target_link_libraries()
# with the imported LIBMINC targets. This file provides backward-compatible
# variable-based configuration.
# Legacy include/link directory variables for projects not using
# target-based dependencies directly:
# LIBMINC_INCLUDE_DIRS, LIBMINC_LIBRARY_DIRS, LIBMINC_LIBRARIES
if(HAVE_MINC1)
add_definitions( -DHAVE_MINC1=1)
endif()
if(HAVE_MINC2)
set(MINC2 "1")
add_definitions( -DMINC2=1 -DHAVE_MINC2=1)
endif()
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@gdevenyi what is this? |
|
i was working on my own changes in https://github.com/BIC-MNI/libminc/tree/modernize_cmake |
Sorry I didn't see that. I am indeed also trying to modernize the cmake because it won't build on the newest versions of cmake. I'll leave this here for you to cherry-pick any changes, but I will otherwise defer to your branch. |
|
Already fixed. |
Replaces deprecated directory-scoped CMake commands with modern target-scoped equivalents across the entire build system.
Minimum version & project
cmake_minimum_requiredfrom 3.10 to 3.22LANGUAGES C CXXtoproject()Target-scoped build properties
add_definitions()→target_compile_definitions()on library targetsinclude_directories()→target_include_directories()withPUBLICvisibility for transitive propagationlink_libraries()→ per-targettarget_link_libraries()in testdir, ezminc/tests, ezminc/examplesOther modernizations
-fPICflag injection →CMAKE_POSITION_INDEPENDENT_CODEset(CMAKE_MODULE_PATH ...)→list(APPEND CMAKE_MODULE_PATH ...)FindNETCDF.cmake/FindNIFTI.cmake: replace manual found-logic withfind_package_handle_standard_args, drop hardcoded search pathsUseLIBMINC.cmake.in: remove deprecatedinclude_directories()andlink_directories()💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.