Skip to content

Commit

Permalink
BUG: Do not link against specific Python version on OS X
Browse files Browse the repository at this point in the history
Added new CMake macro sitk_target_link_libraries_with_dynamic_lookup,
to help resolve symbols at load time and not link time. This macro is
used to link loadable module against their language libraries to
improve compatibility.

This will help packaging on homebrew, as homebrew is starting to
enforce the policy that libraries should not be linked to a specific
python.

Thanks to: Michka Popoff <michkapopoff@gmail.com>

Change-Id: Idc02ecb7a567a0e8c022097ff827f51a6af2e80d
  • Loading branch information
blowekamp committed Feb 9, 2016
1 parent 83396f9 commit 8fe8a78
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 9 deletions.
21 changes: 21 additions & 0 deletions CMake/sitkExtras.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

#
# Link a library to a target such that the symbols are resolved at
# run-time not link-time. This should be used when compiling a
# loadable module when the symbols should be resolve from the run-time
# environment where the module is loaded, and not a specific system
# library.
#
# Specifically, for OSX it uses undefined dynamic_lookup. This is
# simular to using "-shared" on Linux where undefined symbols are
# ignored.
#
# http://blog.tim-smith.us/2015/09/python-extension-modules-os-x/
#
macro( sitk_target_link_libraries_with_dynamic_lookup target )
if ( ${CMAKE_SYSTEM_NAME} MATCHES "Darwin" )
set_target_properties( ${target} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup" )
else()
target_link_libraries ( ${target} ${ARGN} )
endif()
endmacro()
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ find_package(ITK REQUIRED )
#we require certain packages be turned on in ITK
include(sitkCheckForITKModuleDependencies)

include(sitkExtras)

if(ITK_FOUND)

# NOTE: We are purposely not calling UseITK yet. However, we must make
Expand Down
2 changes: 1 addition & 1 deletion Wrapping/CSharp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if(DEFINED SimpleITK_VERSION_POST)
${CMAKE_CURRENT_SOURCE_DIR}/CSharp.i
${CMAKE_CURRENT_SOURCE_DIR}/CSharpTypemapHelper.i )
swig_add_module(SimpleITKCSharpNative csharp SimpleITK.i)
swig_link_libraries(SimpleITKCSharpNative ${SimpleITK_LIBRARIES})
target_link_libraries(${SWIG_MODULE_SimpleITKCSharpNative_TARGET_NAME} ${SimpleITK_LIBRARIES})
set_target_properties(${SWIG_MODULE_SimpleITKCSharpNative_TARGET_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CSHARP_BINARY_DIRECTORY})
if ( UNIX )
set_target_properties(${SWIG_MODULE_SimpleITKCSharpNative_TARGET_NAME} PROPERTIES PREFIX "lib")
Expand Down
2 changes: 1 addition & 1 deletion Wrapping/Java/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ set(CMAKE_SWIG_OUTDIR ${JAVA_SOURCE_DIRECTORY}/org/itk/simple/)
set(CMAKE_SWIG_FLAGS -I${CMAKE_CURRENT_SOURCE_DIR} -package "org.itk.simple" ${CMAKE_SWIG_GLOBAL_FLAGS})
set(SWIG_MODULE_SimpleITKJava_EXTRA_DEPS ${SWIG_EXTRA_DEPS} ${CMAKE_CURRENT_SOURCE_DIR}/Java.i ${CMAKE_CURRENT_SOURCE_DIR}/JavaDoc.i)
SWIG_add_module ( SimpleITKJava java SimpleITK.i )
SWIG_link_libraries(SimpleITKJava ${SimpleITK_LIBRARIES})
target_link_libraries( ${SWIG_MODULE_SimpleITKJava_TARGET_NAME} ${SimpleITK_LIBRARIES})
set_source_files_properties(${swig_generated_file_fullname} PROPERTIES COMPILE_FLAGS "-w")
sitk_strip_target( ${SWIG_MODULE_SimpleITKJava_TARGET_NAME} )

Expand Down
8 changes: 5 additions & 3 deletions Wrapping/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ set(SWIG_MODULE_SimpleITK_EXTRA_DEPS ${SWIG_EXTRA_DEPS}
SWIG_add_module ( SimpleITK python
SimpleITK.i
sitkPyCommand.cxx )
SWIG_link_libraries ( SimpleITK ${SimpleITK_LIBRARIES} ${PYTHON_LIBRARIES})
set(SWIG_MODULE_SimpleITKPython_TARGET_NAME "${SWIG_MODULE_SimpleITK_TARGET_NAME}")
target_link_libraries ( ${SWIG_MODULE_SimpleITKPython_TARGET_NAME} ${SimpleITK_LIBRARIES} )
sitk_target_link_libraries_with_dynamic_lookup( ${SWIG_MODULE_SimpleITKPython_TARGET_NAME} ${PYTHON_LIBRARIES} )
set_source_files_properties(${swig_generated_file_fullname} PROPERTIES COMPILE_FLAGS "-w")
sitk_strip_target( ${SWIG_MODULE_SimpleITK_TARGET_NAME} )
sitk_strip_target( ${SWIG_MODULE_SimpleITKPython_TARGET_NAME} )


set(SWIG_MODULE_SimpleITKPython_TARGET_NAME "${SWIG_MODULE_SimpleITK_TARGET_NAME}")

# Installation
set( SIMPLEITK_PYTHON_PACKAGE_DIR "${SimpleITK_BINARY_DIR}/Wrapping/Python" )
Expand Down
4 changes: 2 additions & 2 deletions Wrapping/R/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ set(CMAKE_SWIG_FLAGS ${CMAKE_SWIG_GLOBAL_FLAGS})
set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR})
set(SWIG_MODULE_SimpleITK_EXTRA_DEPS ${SWIG_EXTRA_DEPS} ${CMAKE_CURRENT_SOURCE_DIR}/R.i )
SWIG_add_module ( SimpleITK r SimpleITK.i sitkRCommand.cxx )
SWIG_link_libraries ( SimpleITK ${SimpleITK_LIBRARIES} )
target_link_libraries ( ${SWIG_MODULE_SimpleITK_TARGET_NAME} ${SimpleITK_LIBRARIES} )

# on some platforms the r libraries are not required at link time...
if(R_LIBRARIES)
SWIG_link_libraries ( SimpleITK ${R_LIBRARIES} )
sitk_target_link_libraries_with_dynamic_lookup ( ${SWIG_MODULE_SimpleITK_TARGET_NAME} ${R_LIBRARIES} )
endif()

set_source_files_properties(${swig_generated_file_fullname} PROPERTIES COMPILE_FLAGS "-w")
Expand Down
3 changes: 2 additions & 1 deletion Wrapping/Ruby/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(SWIG_MODULE_simpleitk_EXTRA_DEPS ${SWIG_EXTRA_DEPS}
${CMAKE_CURRENT_SOURCE_DIR}/Ruby.i)

SWIG_add_module( simpleitk ruby SimpleITK.i )
SWIG_link_libraries( simpleitk ${SimpleITK_LIBRARIES} ${RUBY_LIBRARY})
target_link_libraries( ${SWIG_MODULE_simpleitk_TARGET_NAME} ${SimpleITK_LIBRARIES} )
sitk_target_link_libraries_with_dynamic_lookup( ${SWIG_MODULE_simpleitk_TARGET_NAME} ${RUBY_LIBRARY} )
set_source_files_properties( ${swig_generated_file_fullname} PROPERTIES COMPILE_FLAGS "-w")
sitk_strip_target( ${SWIG_MODULE_simpleitk_TARGET_NAME} )
3 changes: 2 additions & 1 deletion Wrapping/Tcl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ set(CMAKE_SWIG_OUTDIR ${CMAKE_CURRENT_BINARY_DIR})
set(SWIG_MODULE_SimpleITKTCL_EXTRA_DEPS ${SWIG_EXTRA_DEPS} ${CMAKE_CURRENT_SOURCE_DIR}/Tcl.i )
set(SWIG_MODULE_SimpleITKTcl_EXTRA_DEPS ${SWIG_MODULE_SimpleITKTCL_EXTRA_DEPS})
# SWIG_add_module ( SimpleITKTcl tcl SimpleITK.i SimpleITKTCL_wrap.cxx )
# SWIG_link_libraries ( SimpleITKTcl ${SimpleITK_LIBRARIES} ${TCL_LIBRARY})
# target_link_libraries ( ${SWIG_MODULE_SimpleITKTcl_TARGET_NAME} ${SimpleITK_LIBRARIES} )
# sitk_target_link_libraries_with_dynamic_lookup( ${SWIG_MODULE_SimpleITKTcl_TARGET_NAME} ${TCL_LIBRARY} )
# set_source_files_properties(${swig_generated_file_fullname} PROPERTIES COMPILE_FLAGS "-w")
# add_executable ( SimpleITKTclsh ${swig_generated_file_fullname} )
# target_link_libraries ( SimpleITKTclsh ${SimpleITK_LIBRARIES} ${TCL_LIBRARY})
Expand Down

0 comments on commit 8fe8a78

Please sign in to comment.