Skip to content

Commit

Permalink
ENH: Allow building exe and lib of elastix and transformix together
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Dekker committed Aug 25, 2020
1 parent 4040b47 commit 3944a13
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 89 deletions.
13 changes: 11 additions & 2 deletions CMake/elastixExportTarget.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ function(elastix_export_target tgt)
type STREQUAL "SHARED_LIBRARY")
set_property(TARGET ${tgt} PROPERTY VERSION 1)
set_property(TARGET ${tgt} PROPERTY SOVERSION 1)
set_property(TARGET ${tgt} PROPERTY
OUTPUT_NAME ${tgt}-${ELASTIX_VERSION_MAJOR}.${ELASTIX_VERSION_MINOR})

if ("${tgt}" STREQUAL "elastix_lib")
set_property(TARGET ${tgt} PROPERTY
OUTPUT_NAME elastix-${ELASTIX_VERSION_MAJOR}.${ELASTIX_VERSION_MINOR})
elseif ("${tgt}" STREQUAL "transformix_lib")
set_property(TARGET ${tgt} PROPERTY
OUTPUT_NAME transformix-${ELASTIX_VERSION_MAJOR}.${ELASTIX_VERSION_MINOR})
else()
set_property(TARGET ${tgt} PROPERTY
OUTPUT_NAME ${tgt}-${ELASTIX_VERSION_MAJOR}.${ELASTIX_VERSION_MINOR})
endif()
endif()

export(TARGETS ${tgt}
Expand Down
37 changes: 16 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,27 @@ include( CTest )
#---------------------------------------------------------------------
# Generate executable, static library or dynamic library.
mark_as_advanced( ELASTIX_BUILD_EXECUTABLE )
option( ELASTIX_BUILD_EXECUTABLE "Generate executable or library?" ON )

if( NOT ELASTIX_BUILD_EXECUTABLE )
option( ELASTIX_BUILD_EXECUTABLE "Generate executable? (The library is always generated anyway.)" ON )

# The following may make smaller and quicker loading libraries,
# that hides unnecessary symbols. Available from CMake 3.0.0.
#set( CMAKE_C_VISIBILITY_PRESET hidden )
#set( CMAKE_CXX_VISIBILITY_PRESET hidden )

mark_as_advanced( BUILD_SHARED_LIBS )
option( BUILD_SHARED_LIBS "Build shared libraries?" OFF )
if( BUILD_SHARED_LIBS )
add_definitions( -D_ELASTIX_USE_SHARED_LIBRARY )
mark_as_advanced( BUILD_SHARED_LIBS )
option( BUILD_SHARED_LIBS "Build shared libraries?" OFF )
if( BUILD_SHARED_LIBS )
add_definitions( -D_ELASTIX_USE_SHARED_LIBRARY )

# We need to make sure that also the ITK is compiled with shared
# libraries on. Related to flag (ITK_)BUILD_SHARED_LIBS. todo
# We need to make sure that also the ITK is compiled with shared
# libraries on. Related to flag (ITK_)BUILD_SHARED_LIBS. todo

# In order to compile a shared library, all static sub-libraries
# need to build position independend code.
# Otherwise an error similar to the following is raised:
# relocation R_X86_64_32S against `.bss' can not be used when
# making a shared object; recompile with -fPIC
set( CMAKE_POSITION_INDEPENDENT_CODE ON )
endif()
# In order to compile a shared library, all static sub-libraries
# need to build position independend code.
# Otherwise an error similar to the following is raised:
# relocation R_X86_64_32S against `.bss' can not be used when
# making a shared object; recompile with -fPIC
set( CMAKE_POSITION_INDEPENDENT_CODE ON )
endif()

#---------------------------------------------------------------------
Expand Down Expand Up @@ -444,9 +441,7 @@ option( ELASTIX_USE_GTEST "Use GoogleTest to test Elastix implementation" OFF )
if( ELASTIX_USE_GTEST )
enable_testing()
add_subdirectory( Common/GTesting )
if( NOT ELASTIX_BUILD_EXECUTABLE )
add_subdirectory( Core/Main/GTesting )
endif()
add_subdirectory( Core/Main/GTesting )
endif()

#---------------------------------------------------------------------
Expand Down Expand Up @@ -597,8 +592,8 @@ export_library_dependencies( ${elxLIBRARY_DEPENDS_FILE} )
# The build settings file. (necessary for elastix?)
#set( ITK_BUILD_SETTINGS_FILE ${ITK_BINARY_DIR}/ITKBuildSettings.cmake )

elastix_export_target( elastix )
elastix_export_target( transformix )
elastix_export_target( elastix_lib )
elastix_export_target( transformix_lib )

foreach( LIB IN LISTS AllComponentLibs )
elastix_export_target( ${LIB} )
Expand Down
116 changes: 52 additions & 64 deletions Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,37 +117,54 @@ elastix_export_target( param )
# Define the mevis dcm tiff lib to which we should link.
set( mevisdcmtifflib mevisdcmtiff )

set( ELASTIX_TARGET_LINK_LIBRARIES
param
xoutlib
elxCommon
elxCore
${mevisdcmtifflib}
${AllComponentLibs}
${ITK_LIBRARIES}
)
if( ELASTIX_USE_OPENCL )
set( ELASTIX_TARGET_LINK_LIBRARIES ${ELASTIX_TARGET_LINK_LIBRARIES} elxOpenCL )
endif()

#---------------------------------------------------------------------
# Create the elastix executable or library.
if( ELASTIX_BUILD_EXECUTABLE )
add_executable( elastix
add_executable( elastix_exe
Main/elastix.cxx
Main/elastix.h
Kernel/elxElastixMain.cxx
Kernel/elxElastixMain.h
${InstallFilesForExecutables}
)
else()
# The library type (STATIC or SHARED) is determined by the parameter
# BUILD_SHARED_LIBS.
add_library( elastix
Main/elxParameterObject.cxx
Main/elxParameterObject.h
Main/elastixlib.cxx
Main/elastixlib.h
Kernel/elxElastixMain.cxx
Kernel/elxElastixMain.h
${InstallFilesForExecutables}
)
set_target_properties( elastix_exe PROPERTIES OUTPUT_NAME elastix )
target_compile_definitions( elastix_exe PRIVATE ELX_CMAKE_VERSION="${CMAKE_VERSION}" )
target_link_libraries( elastix_exe ${ELASTIX_TARGET_LINK_LIBRARIES} )
endif()

target_compile_definitions(elastix PRIVATE ELX_CMAKE_VERSION="${CMAKE_VERSION}")
# The library type (STATIC or SHARED) is determined by the parameter
# BUILD_SHARED_LIBS.
add_library( elastix_lib
Main/elxParameterObject.cxx
Main/elxParameterObject.h
Main/elastixlib.cxx
Main/elastixlib.h
Kernel/elxElastixMain.cxx
Kernel/elxElastixMain.h
${InstallFilesForExecutables}
)
set_target_properties( elastix_lib PROPERTIES OUTPUT_NAME elastix )
target_compile_definitions( elastix_lib PRIVATE ELX_CMAKE_VERSION="${CMAKE_VERSION}" )
target_link_libraries( elastix_lib ${ELASTIX_TARGET_LINK_LIBRARIES} )

#---------------------------------------------------------------------
# Create the transformix executable.

if( ELASTIX_BUILD_EXECUTABLE )
add_executable( transformix
add_executable( transformix_exe
Main/transformix.cxx
Main/elastix.h
Kernel/elxElastixMain.cxx
Expand All @@ -156,56 +173,27 @@ if( ELASTIX_BUILD_EXECUTABLE )
Kernel/elxTransformixMain.h
${InstallFilesForExecutables}
)
else()
# The library type (STATIC or SHARED) is determined by the parameter
# BUILD_SHARED_LIBS.
add_library( transformix
Main/elxParameterObject.cxx
Main/elxParameterObject.h
Main/transformixlib.cxx
Main/transformixlib.h
Kernel/elxElastixMain.cxx
Kernel/elxElastixMain.h
Kernel/elxTransformixMain.cxx
Kernel/elxTransformixMain.h
${InstallFilesForExecutables}
)
set( ELASTIX_LIBRARIES elastix transformix PARENT_SCOPE )
set_target_properties(transformix_exe PROPERTIES OUTPUT_NAME transformix)
target_link_libraries( transformix_exe ${ELASTIX_TARGET_LINK_LIBRARIES} )
endif()

#---------------------------------------------------------------------
# Link elastix against other libraries.

target_link_libraries( elastix
param
xoutlib
elxCommon
elxCore
${mevisdcmtifflib}
${AllComponentLibs}
${ITK_LIBRARIES}
)

if( ELASTIX_USE_OPENCL )
target_link_libraries( elastix elxOpenCL )
endif()

#---------------------------------------------------------------------
# Link transformix against other libraries.

target_link_libraries( transformix
param
xoutlib
elxCommon
elxCore
${mevisdcmtifflib}
${AllComponentLibs}
${ITK_LIBRARIES}
# The library type (STATIC or SHARED) is determined by the parameter
# BUILD_SHARED_LIBS.
add_library( transformix_lib
Main/elxParameterObject.cxx
Main/elxParameterObject.h
Main/transformixlib.cxx
Main/transformixlib.h
Kernel/elxElastixMain.cxx
Kernel/elxElastixMain.h
Kernel/elxTransformixMain.cxx
Kernel/elxTransformixMain.h
${InstallFilesForExecutables}
)
set_target_properties( transformix_lib PROPERTIES OUTPUT_NAME transformix )
target_link_libraries( transformix_lib ${ELASTIX_TARGET_LINK_LIBRARIES} )

if( ELASTIX_USE_OPENCL )
target_link_libraries( transformix elxOpenCL )
endif()
set( ELASTIX_LIBRARIES elastix transformix PARENT_SCOPE )

if( MSVC )
# NOTE: that linker /INCREMENTAL:NO flag makes it impossible to use
Expand All @@ -221,13 +209,13 @@ endif()
#---------------------------------------------------------------------
# Define the install directory for elastix and transformix.

if( NOT WIN32 )
if( ELASTIX_BUILD_EXECUTABLE AND NOT WIN32 )
# Tell the executables where to find the required .so files.
set_target_properties( elastix transformix
set_target_properties( elastix_exe transformix_exe
PROPERTIES INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib:${ITK_DIR}" )
endif()

install( TARGETS elastix transformix elxCore
install( TARGETS elastix_lib transformix_lib elxCore
ARCHIVE DESTINATION ${ELASTIX_ARCHIVE_DIR}
LIBRARY DESTINATION ${ELASTIX_LIBRARY_DIR}
RUNTIME DESTINATION ${ELASTIX_RUNTIME_DIR}
Expand Down
2 changes: 1 addition & 1 deletion Core/Main/GTesting/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ add_executable(ElastixLibGTest
target_link_libraries( ElastixLibGTest
GTest::GTest
GTest::Main
elastix
elastix_lib
${ITK_LIBRARIES}
)

Expand Down
2 changes: 1 addition & 1 deletion Testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ macro( elx_add_run_test testbasename howtocompare )
CONFIGURATIONS Release
COMMAND ${python_executable} ${pythonlandmarks}
-b ${baselineTP} -d ${output_dir} -f ${TestDataDir}/3DCT_lung_baseline_landmarks.txt -t 1.0
-p $<TARGET_FILE:transformix> )
-p $<TARGET_FILE:transformix_exe> )
set_tests_properties( ${testname}_COMPARE_LANDMARKS
PROPERTIES DEPENDS ${testname}_OUTPUT )
endif()
Expand Down

0 comments on commit 3944a13

Please sign in to comment.