Commit b1cc9fb5 authored by Kasper Marstal's avatar Kasper Marstal
Browse files

COMP: Add automatic module dependency to CMake build system

parent 1eaf3de6
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# OSs
.DS_Store
# IDEs
.idea
cmake-build-*
.kdev4
# Source
Documentation/build
......@@ -18,88 +18,172 @@
#=========================================================================
# ---------------------------------------------------------------------
# Private helper macros
# Private macros
macro( _selxmodule_check_name MODULE )
# "No funny stuff" - Karl Hungus
if( NOT "${_name}" MATCHES "^[a-zA-Z_]*$" )
message( FATAL_ERROR "Invalid module name: ${MODULE}" )
endif()
# Check if module was found during initialization
list( FIND SUPERELASTIX_MODULES "${MODULE}" MODULE_FOUND )
if( ${MODULE_FOUND} EQUAL -1 )
message( FATAL_ERROR "Module not found: ${MODULE}")
message( FATAL_ERROR "Module not found: ${MODULE}" )
endif()
endmacro()
macro( _selxmodule_enable MODULE_NAME )
_selxmodule_check_name( ${MODULE_NAME} )
macro( _selxmodules_initialize )
set( SUPERELASTIX_MODULES )
set( SUPERELASTIX_INCLUDE_DIRS )
set( SUPERELASTIX_LIBRARIES )
set( SUPERELASTIX_TEST_SOURCE_FILES )
file( GLOB_RECURSE MODULE_CMAKE_FILES RELATIVE "${CMAKE_SOURCE_DIR}"
"${CMAKE_SOURCE_DIR}/Modules/*/Module*.cmake"
)
message( STATUS "Found the following SuperElastix modules:")
foreach( MODULE_CMAKE_FILE ${MODULE_CMAKE_FILES})
get_filename_component( MODULE ${MODULE_CMAKE_FILE} NAME_WE )
get_filename_component( ${MODULE}_PATH ${MODULE_CMAKE_FILE} PATH )
message( STATUS " ${MODULE}" )
option( "USE_${MODULE}" OFF )
set( "${MODULE}_CMAKE_FILE" ${CMAKE_SOURCE_DIR}/${MODULE_CMAKE_FILE} )
set( "${MODULE}_IS_ENABLED" FALSE )
set( ${MODULE}_SOURCE_DIR ${CMAKE_SOURCE_DIR}/${${MODULE}_PATH} )
set( ${MODULE}_BINARY_DIR ${CMAKE_BINARY_DIR}/${${MODULE}_PATH} )
# Collect header files for Visual Studio Project
# http://stackoverflow.com/questions/8316104/specify-how-cmake-creates-visual-studio-project
file( GLOB ${MODULE}_HEADER_FILES "${${MODULE}_SOURCE_DIR}/*/include/*.*")
set( ${MODULE}_INCLUDE_DIRS )
set( ${MODULE}_LIBRARY_DIRS )
set( ${MODULE}_LIBRARIES )
if( NOT ${MODULE_NAME}_ENABLED )
set( ${MODULE_NAME}_ENABLED ON )
list( APPEND SUPERELASTIX_MODULES ${MODULE} )
endforeach()
endmacro()
macro( _selxmodule_enable MODULE UPSTREAM )
_selxmodule_check_name( ${MODULE} )
message( STATUS "Enabling ${MODULE} requested by ${UPSTREAM}.")
include( ${${MODULE_NAME}_FILE} )
if( NOT ${MODULE}_IS_ENABLED )
set( ${MODULE}_IS_ENABLED TRUE )
if( ${MODULE_NAME}_INCLUDE_DIRS )
include_directories( ${${MODULE_NAME}_INCLUDE_DIRS} )
list( APPEND SUPERELASTIX_INCLUDE_DIRS ${${MODULE_NAME}_INCLUDE_DIRS} )
include( ${${MODULE}_CMAKE_FILE} )
add_library( ${MODULE} "${${MODULE}_HEADER_FILES}" "${${MODULE}_SOURCE_FILES}" )
_selxmodule_include_directories( ${MODULE} ${MODULE} )
if( NOT ${MODULE} STREQUAL ModuleCore )
_selxmodule_include_directories( ${MODULE} ModuleCore )
_selxmodule_link_libraries( ${MODULE} ModuleCore )
endif()
if( ${MODULE_NAME}_LIBRARIES )
link_directories( ${${MODULE_NAME}_LIBRARY_DIRS} )
list( APPEND SUPERELASTIX_LIBRARIES ${${MODULE_NAME}_LIBRARIES} )
if( ${MODULE}_MODULE_DEPENDENCIES )
_selxmodule_include_directories( ${MODULE} ${${MODULE}_MODULE_DEPENDENCIES} )
_selxmodule_link_libraries( ${MODULE} ${${MODULE}_MODULE_DEPENDENCIES} )
endif()
if( ${MODULE_NAME}_TESTS )
list( APPEND SUPERELASTIX_TESTS ${${MODULE_NAME}_TESTS} )
if( SUPERELASTIX_BUILD_TESTING AND ${MODULE}_TEST_SOURCE_FILES )
list( APPEND SUPERELASTIX_TEST_SOURCE_FILES ${${MODULE}_TEST_SOURCE_FILES} )
endif()
# SUPERELASTIX_INCLUDE_DIRS and SUPERELASTIX_LIBRARIES are convenience
# variables that should only be used when absolutely necessary, e.g. when
# compiling tests from a central place in Testing/Unit/CMakeLists.txt
if( ${MODULE}_INCLUDE_DIRS )
list( APPEND SUPERELASTIX_INCLUDE_DIRS ${${MODULE}_INCLUDE_DIRS} )
endif()
if( ${MODULE}_LIBRARIES )
list( APPEND SUPERELASTIX_LIBRARIES ${${MODULE}_LIBRARIES} )
endif()
if( ${MODULE}_MODULE_DEPENDENCIES )
_selxmodule_enable_dependencies( ${MODULE}_MODULE_DEPENDENCIES ${MODULE} )
# TODO: Resolve cylic dependency graph. SuperElastix compiles only because
# CMake is not aware of dependencies and because of the specific order in
# which modules are compiled
# add_dependencies( ${MODULE} ${${MODULE}_MODULE_DEPENDENCIES} )
endif()
# TODO: Add support for indicating dependencies between modules and recursive enabling of these dependencies
message( STATUS "${MODULE} enabled." )
else()
message( STATUS "${MODULE} already enabled." )
endif()
endmacro()
macro( _selxmodule_disable MODULE_NAME )
# TODO: selxmodule_disable
macro( _selxmodule_enable_dependencies MODULES UPSTREAM )
foreach( MODULE ${${MODULES}} )
_selxmodule_enable( ${MODULE} ${UPSTREAM} )
endforeach()
endmacro()
macro( _selxmodules_initialize )
set( SUPERELASTIX_MODULES )
set( SUPERELASTIX_INCLUDE_DIRS )
set( SUPERELASTIX_LIBRARIES )
set( SUPERELASTIX_TESTS )
macro( _selxmodule_disable MODULE )
set( ${MODULE}_IS_ENABLED FALSE )
list( FILTER SUPERELASTIX_INCLUDE_DIRS MATCHES EXCLUDE> REGEX "(.*)${MODULE}(.*)" )
list( FILTER SUPERELASTIX_LIBRARY_DIRS MATCHES EXCLUDE> REGEX "(.*)${MODULE}(.*)" )
list( FILTER SUPERELASTIX_LIBRARIES MATCHES EXCLUDE> REGEX "(.*)${MODULE}(.*)" )
list( FILTER SUPERELASTIX_TEST_SOURCE_FILES MATCHES EXCLUDE> REGEX "(.*)${MODULE}(.*)" )
endmacro()
file( GLOB_RECURSE MODULE_FILES RELATIVE "${CMAKE_SOURCE_DIR}"
"${CMAKE_SOURCE_DIR}/Modules/*/Module*.cmake"
)
macro( _selxmodule_include_directories TARGET DEPENDENCIES )
foreach( DEPENDENCY ${DEPENDENCIES} )
target_include_directories( ${TARGET} PUBLIC ${${DEPENDENCY}_INCLUDE_DIRS} )
endforeach()
endmacro()
message( STATUS "Found the following SuperElastix modules:")
foreach( MODULE_FILE ${MODULE_FILES})
get_filename_component( MODULE_NAME ${MODULE_FILE} NAME_WE )
get_filename_component( ${MODULE_NAME}_PATH ${MODULE_FILE} PATH )
message( STATUS " ${MODULE_NAME}" )
macro( _selxmodule_link_libraries TARGET DEPENDENCIES )
foreach( DEPENDENCY ${DEPENDENCIES} )
target_include_directories( ${TARGET} PUBLIC ${${DEPENDENCY}_INCLUDE_DIRS} )
target_link_libraries( ${TARGET} ${DEPENDENCY} )
endforeach()
endmacro()
option( "USE_${MODULE_NAME}" OFF )
set( "${MODULE_NAME}_FILE" ${CMAKE_SOURCE_DIR}/${MODULE_FILE} )
set( "${MODULE_NAME}_ENABLED" OFF )
set( ${MODULE_NAME}_SOURCE_DIR ${CMAKE_SOURCE_DIR}/${${MODULE_NAME}_PATH} )
set( ${MODULE_NAME}_BINARY_DIR ${CMAKE_BINARY_DIR}/${${MODULE_NAME}_PATH} )
# ---------------------------------------------------------------------
# Public macros
set( ${MODULE_NAME}_INCLUDE_DIRS )
set( ${MODULE_NAME}_LIBRARY_DIRS )
set( ${MODULE_NAME}_LIBRARIES )
macro( enable_module MODULE )
_selxmodule_enable( ${MODULE} "user" )
endmacro()
list( APPEND SUPERELASTIX_MODULES ${MODULE_NAME} )
macro( enable_modules MODULES )
foreach( MODULE ${${MODULES}} )
enable_module( ${MODULE} )
endforeach()
endmacro()
_selxmodules_initialize()
macro( disable_module MODULE )
_selxmodule_disable( ${MODULE} )
endmacro()
macro( disable_modules MODULES )
foreach( MODULE ${${MODULES}} )
disable_module( ${MODULE} "user" )
endforeach()
endmacro()
# ---------------------------------------------------------------------
# Public interface
macro( selxmodule_enable MODULE )
_selxmodule_enable( ${MODULE} )
endmacro()
# Build core
_selxmodules_initialize()
_selxmodule_enable( ModuleCore "SuperElastix" )
# Enable user-selected modules
# TODO: Loop over USE
# Disable user-selected modules
# TODO:
......@@ -19,6 +19,8 @@
cmake_minimum_required( VERSION 3.0.2 )
cmake_policy( SET CMP0064 OLD )
# Explicitly add INCREMENTAL linking option to command lines.
# http://www.cmake.org/pipermail/cmake/2010-February/035174.html
#set( MSVC_INCREMENTAL_DEFAULT ON )
......@@ -89,17 +91,27 @@ include_directories( ${Boost_INCLUDE_DIRS} )
# ---------------------------------------------------------------------
# Build SuperElastix
# For now we just enable all modules
include( selxModules )
selxmodule_enable( ModuleCore )
selxmodule_enable( ModuleExamples )
selxmodule_enable( ModuleSinksAndSources )
selxmodule_enable( ModuleItkSmoothingRecursiveGaussianImageFilter )
selxmodule_enable( ModuleItkImageRegistrationMethodv4 )
selxmodule_enable( ModuleItkSyNImageRegistrationMethod )
selxmodule_enable( ModuleElastix )
selxmodule_enable( ModuleController )
# TODO: Build tests depending on enabled modules
mark_as_advanced( SUPERELASTIX_EXPRESS_BUILD )
option( SUPERELASTIX_EXPRESS_BUILD "" OFF )
set( SUPERELASTIX_DEFAULT_MODULES
ModuleElastix
ModuleExamples
ModuleItkSmoothingRecursiveGaussianImageFilter
ModuleItkImageRegistrationMethodv4
ModuleItkSyNImageRegistrationMethod
ModuleController
)
if( ${SUPERELASTIX_EXPRESS_BUILD} )
disable_modules( ${SUPERELASTIX_DEFAULT_MODULES} )
else()
enable_modules( ${SUPERELASTIX_DEFAULT_MODULES} )
endif()
# TODO: Functionality to disable default modules again
# ---------------------------------------------------------------------
# Testing
......
......@@ -19,30 +19,17 @@
set( MODULE ModuleController )
# Export include files
set( ${MODULE}_INCLUDE_DIRS
${${MODULE}_SOURCE_DIR}/include
)
# Collect header files for Visual Studio Project
file(GLOB ${MODULE}_HEADER_FILES "${${MODULE}_SOURCE_DIR}/include/*.*")
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxRegistrationController.cxx
)
# Export libraries
set( ${MODULE}_LIBRARIES
${MODULE}
)
# Export tests
set( ${MODULE}_TESTS
set( ${MODULE}_TEST_SOURCE_FILES
)
# Module source files
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxRegistrationController.cxx
)
# Compile library
add_library( ${MODULE} STATIC "${${MODULE}_SOURCE_FILES}" ${${MODULE}_HEADER_FILES})
target_link_libraries( ${MODULE} ${SUPERELASTIX_LIBRARIES} )
......@@ -37,38 +37,25 @@ if( NOT EXISTS ${ELASTIX_USE_FILE} )
message(FATAL_ERROR "Could not find UseElastix.cmake. Point ELASTIX_DIR to folder containing UseElastix.cmake or use SuperBuild.")
endif()
# Export include files
# TODO: Add include and link directories manually to avoid elastix polluting CMake environment
include( ${ELASTIX_USE_FILE} )
# Export include files
set( ${MODULE}_INCLUDE_DIRS
${${MODULE}_SOURCE_DIR}/include
)
# Collect header files for Visual Studio Project
file(GLOB ${MODULE}_HEADER_FILES "${${MODULE}_SOURCE_DIR}/include/*.*")
# Export libraries
set( ${MODULE}_LIBRARIES
${MODULE}
elastix
transformix
)
# Export tests
set( ${MODULE}_TESTS
${${MODULE}_SOURCE_DIR}/test/selxElastixComponentTest.cxx
)
# Module source files
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxElastixComponent.cxx
${${MODULE}_SOURCE_DIR}/src/selxMonolithicElastix.cxx
${${MODULE}_SOURCE_DIR}/src/selxMonolithicTransformix.cxx
)
# Compile library
)
add_library( ${MODULE} STATIC ${${MODULE}_SOURCE_FILES} ${${MODULE}_HEADER_FILES})
set( ${MODULE}_TEST_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/test/selxElastixComponentTest.cxx
)
target_link_libraries( ${MODULE} ${ELASTIX_LIBRARIES} )
set( ${MODULE}_LIBRARIES
${MODULE}
elastix
transformix
)
......@@ -19,20 +19,10 @@
set( MODULE ModuleExamples )
# Export include files
set( ${MODULE}_INCLUDE_DIRS
${${MODULE}_SOURCE_DIR}/include
)
# Collect header files for Visual Studio Project
file(GLOB ${MODULE}_HEADER_FILES "${${MODULE}_SOURCE_DIR}/include/*.*")
# Export libraries
set( ${MODULE}_LIBRARIES
${MODULE}
)
# Module source files
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxExample3rdPartyCode.cxx
${${MODULE}_SOURCE_DIR}/src/selxExample4thPartyCode.cxx
......@@ -46,8 +36,6 @@ set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxMetricComponent1.cxx
)
# Compile library
add_library( ${MODULE} STATIC "${${MODULE}_SOURCE_FILES}" ${${MODULE}_HEADER_FILES})
target_link_libraries( ${MODULE} ${SUPERELASTIX_LIBRARIES} )
set( ${MODULE}_LIBRARIES
${MODULE}
)
......@@ -19,31 +19,20 @@
set( MODULE ModuleItkSmoothingRecursiveGaussianImageFilter )
# Export include files
set( ${MODULE}_INCLUDE_DIRS
${${MODULE}_SOURCE_DIR}/include
)
# Collect header files for Visual Studio Project
file(GLOB ${MODULE}_HEADER_FILES "${${MODULE}_SOURCE_DIR}/include/*.*")
# Export libraries
set( ${MODULE}_LIBRARIES
${MODULE}
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxItkSmoothingRecursiveGaussianImageFilterComponent.cxx
)
# Export tests
set( ${MODULE}_TESTS
set( ${MODULE}_TEST_SOURCE_FILES
selxitkImageFilterTest.cxx
)
# Module source files
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxItkSmoothingRecursiveGaussianImageFilterComponent.cxx
set( ${MODULE}_LIBRARIES
${MODULE}
)
# Compile library
add_library( ${MODULE} STATIC "${${MODULE}_SOURCE_FILES}" ${${MODULE}_HEADER_FILES})
target_link_libraries( ${MODULE} ${SUPERELASTIX_LIBRARIES} )
......@@ -19,24 +19,10 @@
set( MODULE ModuleSinksAndSources )
# Export include files
set( ${MODULE}_INCLUDE_DIRS
${${MODULE}_SOURCE_DIR}/include
)
# Collect header files for Visual Studio Project
file(GLOB ${MODULE}_HEADER_FILES "${${MODULE}_SOURCE_DIR}/include/*.*")
# Export libraries
set( ${MODULE}_LIBRARIES
${MODULE}
)
# Export tests
set( ${MODULE}_TESTS
)
# Module source files
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxItkImageSource.cxx
${${MODULE}_SOURCE_DIR}/src/selxItkImageSink.cxx
......@@ -45,12 +31,15 @@ set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxDisplacementFieldItkImageFilterSink.cxx
${${MODULE}_SOURCE_DIR}/src/selxItkMeshSource.cxx
${${MODULE}_SOURCE_DIR}/src/selxItkMeshSink.cxx
)
# Compile library
set( ${MODULE}_LIBRARIES
${MODULE}
)
add_library( ${MODULE} STATIC "${${MODULE}_SOURCE_FILES}" ${${MODULE}_HEADER_FILES})
set( ${MODULE}_TEST_SOURCE_FILES )
target_link_libraries( ${MODULE} ${SUPERELASTIX_LIBRARIES} )
set( ${MODULE}_MODULE_DEPENDENCIES
ModuleController
)
......@@ -19,32 +19,19 @@
set( MODULE ModuleItkImageRegistrationMethodv4 )
# Export include files
set( ${MODULE}_INCLUDE_DIRS
${${MODULE}_SOURCE_DIR}/include
)
# Collect header files for Visual Studio Project
file(GLOB ${MODULE}_HEADER_FILES "${${MODULE}_SOURCE_DIR}/include/*.*")
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxItkImageRegistrationMethodv4Component.cxx
)
# Export libraries
set( ${MODULE}_LIBRARIES
${MODULE}
)
# Export tests
set( ${MODULE}_TESTS
set( ${MODULE}_TEST_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/test/selxRegistrationItkv4Test.cxx
${${MODULE}_SOURCE_DIR}/test/selxWBIRDemoTest.cxx
)
# Module source files
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxItkImageRegistrationMethodv4Component.cxx
)
# Compile library
add_library( ${MODULE} STATIC ${${MODULE}_SOURCE_FILES} ${${MODULE}_HEADER_FILES})
target_link_libraries( ${MODULE} ${SUPERELASTIX_LIBRARIES} )
......@@ -19,31 +19,18 @@
set( MODULE ModuleItkSyNImageRegistrationMethod )
# Export include files
set( ${MODULE}_INCLUDE_DIRS
${${MODULE}_SOURCE_DIR}/include
)
# Collect header files for Visual Studio Project
file(GLOB ${MODULE}_HEADER_FILES "${${MODULE}_SOURCE_DIR}/include/*.*")
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxItkSyNImageRegistrationMethodComponent.cxx
)
# Export libraries
set( ${MODULE}_LIBRARIES
${MODULE}
)
# Export tests
set( ${MODULE}_TESTS
set( ${MODULE}_TEST_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/test/selxSyNRegistrationItkv4Test.cxx
)
# Module source files
set( ${MODULE}_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/src/selxItkSyNImageRegistrationMethodComponent.cxx
)
# Compile library
add_library( ${MODULE} STATIC ${${MODULE}_SOURCE_FILES} ${${MODULE}_HEADER_FILES})
target_link_libraries( ${MODULE} ${SUPERELASTIX_LIBRARIES} )
......@@ -19,7 +19,6 @@
set( MODULE ModuleCore )
# Export include files
set( ${MODULE}_INCLUDE_DIRS
${${MODULE}_SOURCE_DIR}/Common/include
${${MODULE}_SOURCE_DIR}/Blueprints/include
......@@ -29,19 +28,21 @@ set( ${MODULE}_INCLUDE_DIRS
${${MODULE}_SOURCE_DIR}/Filter/include
${${MODULE}_SOURCE_DIR}/FileIO/include
${${MODULE}_SOURCE_DIR}/ComponentRegistration/include
)
# Collect header files for Visual Studio Project
# http://stackoverflow.com/questions/8316104/specify-how-cmake-creates-visual-studio-project
file(GLOB ${MODULE}_HEADER_FILES "${${MODULE}_SOURCE_DIR}/*/include/*.*")
)
# Export libraries
set( ${MODULE}_LIBRARIES
${MODULE}
set( ${MODULE