X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=gdcmPython%2FCMakeLists.txt;h=04a30511a219ad8b7e494b3c942f83e387d3bf2a;hb=8d10ccb6794a0f22234983990e2181a24625fbd8;hp=eb5c1a19999f7de5d2aa7db341b6411797612240;hpb=b6ce788973e58d2fa32d85a9c60f97e2a42c4a3a;p=gdcm.git diff --git a/gdcmPython/CMakeLists.txt b/gdcmPython/CMakeLists.txt index eb5c1a19..04a30511 100644 --- a/gdcmPython/CMakeLists.txt +++ b/gdcmPython/CMakeLists.txt @@ -1,6 +1,4 @@ -# TODO -# http://www.cmake.org/pipermail/cmake/2003-August/004185.html -# http://www.cmake.org/pipermail/cmake/2003-January/001092.html +# gdcm Python wrapping stuff: INCLUDE_DIRECTORIES( ${GDCM_SOURCE_DIR}/src @@ -8,52 +6,164 @@ INCLUDE_DIRECTORIES( ${GDCM_BINARY_DIR}/ ) -#----------------------------------------------------------------------------- -# Create the gdcmVersion.py file: -FILE(WRITE ${GDCM_BINARY_DIR}/gdcmVersion.py - "gdcmVERSION=\"${GDCM_VERSION_FULL}\"" - ) - #----------------------------------------------------------------------------- # SWIG Wrapping # -# Since the file gdcm_wrap.cxx does not exist, mark it as GENERATED: -SET_SOURCE_FILES_PROPERTIES(gdcm_wrap.cxx GENERATED) -ADD_LIBRARY(pygdcm gdcm_wrap.cxx) +# CMake 2.0 will fully support Swig wrapping so split based on cmake version: -TARGET_LINK_LIBRARIES(pygdcm - ${PYTHON_LIBRARY} - gdcm -) -# http://www.cmake.org/pipermail/cmake/2003-August/004190.html -# SET_TARGET_PROPERTIES(pygdcm PROPERTIES PREFIX "_") +IF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9) -#----------------------------------------------------------------------------- -# SWIG Wrapping -# + # We need to pass that to swig to be sure all modules are generated in the proper dir + SET(CMAKE_SWIG_FLAGS "-outdir ${GDCM_BINARY_DIR}/bin") + # Looks like a bug to me in cmake 2.0.x: + SEPARATE_ARGUMENTS(CMAKE_SWIG_FLAGS) -# As of 09/10/2003 there was no FindSWIG.cmake -# later we should use SWIG instead of swig -# Build our custom command : -ADD_CUSTOM_COMMAND( - TARGET pygdcm - COMMAND swig - ARGS -python -c++ -I${GDCM_SOURCE_DIR}/src -o - ${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx - ${GDCM_SOURCE_DIR}/gdcmPython/gdcm.i - SOURCE ${GDCM_SOURCE_DIR}/gdcmPython/gdcm.i - OUTPUTS ${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx - COMMENT "Generating gdcm_wrap.cxx/gdcm.py based on gdcm.i" -) + FIND_PACKAGE(SWIG REQUIRED) + INCLUDE(${SWIG_USE_FILE}) + + #SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES PYTHON ON) + SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES CPLUSPLUS ON) + # Setting SWIG_FLAGS to "-includeall" at the following line looks like + # a bad idea since swig tries to reculsively include all the referenced + # files, "including" the C++ , , ... Of course swig + # has no way of finding the path to those files (unless it is told by + # cmake) since they are related to the compiler installation layout. + # Anyhow, since this inclusion recursion is not necessary, just don't + # do it. + # Note: apparently the line is required in order to avoid a NOTFOUND + # as argument of swig on invocation of make. + SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES SWIG_FLAGS "") + SWIG_ADD_MODULE(gdcm python gdcm.i) #gdcm_wrap.cxx + SWIG_LINK_LIBRARIES(gdcm gdcm ${PYTHON_LIBRARIES}) + + SET(GDCM_CXX_FLAGS ${CMAKE_SWIG_FLAGS}) + # Get rid of -Wall / -W / -pedantic if exist + SET(CMAKE_CXX_FLAGS "") + # I guess I need to put them back on the this file: + SET_SOURCE_FILES_PROPERTIES(${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx + PROPERTIES COMPILE_FLAGS "") +# SET_TARGET_PROPERTIES(${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx +# PROPERTIES CMAKE_CXX_FLAGS "-foo") + +ELSE(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9) + + # As of 09/10/2003 there was no FindSWIG.cmake + # later we should use SWIG instead of swig + FIND_PROGRAM(SWIG_EXECUTABLE + NAMES swig-1.3 swig + ) + + IF(NOT SWIG_EXECUTABLE) + MESSAGE(FATAL_ERROR "Swig could not be found wrapping is impossible") + ENDIF(NOT SWIG_EXECUTABLE) + + # Build our custom command : + ADD_CUSTOM_COMMAND( + OUTPUT ${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx + COMMAND ${SWIG_EXECUTABLE} + ARGS -python -c++ -I${GDCM_SOURCE_DIR}/src -o + ${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx + ${GDCM_SOURCE_DIR}/gdcmPython/gdcm.i + DEPENDS ${GDCM_SOURCE_DIR}/gdcmPython/gdcm.i + COMMENT "Generating gdcm_wrap.cxx/gdcm.py based on gdcm.i" + ) + # Since the file gdcm_wrap.cxx does not exist, mark it as GENERATED: + SET_SOURCE_FILES_PROPERTIES(${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx GENERATED) + + ADD_LIBRARY(pygdcm gdcm_wrap.cxx) + TARGET_LINK_LIBRARIES(pygdcm + ${PYTHON_LIBRARY} + gdcm + ) + # http://www.cmake.org/pipermail/cmake/2003-August/004190.html + #SET_TARGET_PROPERTIES(pygdcm PROPERTIES PREFIX "_") + + # This swig command will also outputs gdcm.py : + SET_SOURCE_FILES_PROPERTIES(${GDCM_BINARY_DIR}/gdcmPython/gdcm.py GENERATED) + + # Instead of a simple copy operation let create our own gdcm.py file: + # First read what swig dumped: + IF(EXISTS ${GDCM_BINARY_DIR}/gdcmPython/gdcm.py) + FILE(READ ${GDCM_BINARY_DIR}/gdcmPython/gdcm.py SWIG_GDCM_PY) + ENDIF(EXISTS ${GDCM_BINARY_DIR}/gdcmPython/gdcm.py) + + # Then create the gdcm.py = gdcmVersion.py + gdcm.py (swig) file: + FILE(WRITE ${GDCM_BINARY_DIR}/bin/gdcmVersion.py + "import os\n" + "gdcmVERSION=\"${GDCM_VERSION}\"\n" + "GDCM_DATA_PATH=\"${GDCM_DATA_ROOT}\"\n\n" + ${SWIG_GDCM_PY} + ) + + # Seems like they want instead a file called gdcmPython.py + # Thus add the custom command to copy it to proper location: +# ADD_CUSTOM_COMMAND( +# TARGET pygdcm +# POST_BUILD +# COMMAND ${CMAKE_COMMAND} +# ARGS -E copy +# ${GDCM_BINARY_DIR}/bin/gdcm.py +# ${GDCM_BINARY_DIR}/bin/gdcmPython.py +# ) + + # Here comes the hack to rename the library to a more 'swig' compliant scheme" + SET(LIB_NAME ${CMAKE_SHARED_LIBRARY_PREFIX}pygdcm${CMAKE_SHARED_LIBRARY_SUFFIX}) + SET(EXE_DIR ${GDCM_BINARY_DIR}/Executable) + IF(EXECUTABLE_OUTPUT_PATH) + SET(EXE_DIR ${EXECUTABLE_OUTPUT_PATH}) + ENDIF(EXECUTABLE_OUTPUT_PATH) + SET(LIB_DIR ${GDCM_BINARY_DIR}/Library/Shared) + IF(LIBRARY_OUTPUT_PATH) + SET(LIB_DIR ${LIBRARY_OUTPUT_PATH}) + ENDIF(LIBRARY_OUTPUT_PATH) + + IF(WIN32 OR APPLE) + SET(SHARED_MUST_BE_IN_EXE_DIR 1) + ENDIF(WIN32 OR APPLE) + + IF(NOT SHARED_MUST_BE_IN_EXE_DIR) + SET(EXE_DIR ${LIB_DIR}) + ENDIF(NOT SHARED_MUST_BE_IN_EXE_DIR) + + ADD_CUSTOM_COMMAND( + TARGET pygdcm + POST_BUILD + COMMAND ${CMAKE_COMMAND} ARGS -E copy + ${LIB_DIR}/${CMAKE_CFG_INTDIR}/${LIB_NAME} + ${EXE_DIR}/${CMAKE_CFG_INTDIR}/_gdcm${CMAKE_SHARED_LIBRARY_SUFFIX} + ) + + # Remove temporary (lib)pygdcm(.so .dll) library: +# ADD_CUSTOM_COMMAND( +# TARGET pygdcm +# POST_BUILD +# COMMAND ${CMAKE_COMMAND} +# ARGS -E remove ${LIB_DIR}/${CMAKE_CFG_INTDIR}/${LIB_NAME} +# ) + + # where are library stored ? +# SET(GDCM_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}) +# ADD_CUSTOM_COMMAND( +# TARGET pygdcm +# POST_BUILD +# COMMAND ${CMAKE_COMMAND} +# ARGS -E copy +# ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/${MY_LIB_NAME}${CMAKE_SHLIB_SUFFIX} +# ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/vtkXY.pvm +# TARGET vtkXY +# ) + +ENDIF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9) -# This swig command will also outputs gdcm.py : -SET_SOURCE_FILES_PROPERTIES(gdcm.py GENERATED) #----------------------------------------------------------------------------- # VTK Wrapping # IF(GDCM_VTK) + # Include the VTK library + INCLUDE(${VTK_USE_FILE}) + INCLUDE_DIRECTORIES( ${GDCM_SOURCE_DIR}/vtk ) @@ -65,49 +175,61 @@ IF(GDCM_VTK) # Configure Python module, which is the plugin itself IF (GDCM_WRAP_PYTHON) - VTK_WRAP_PYTHON2(vtkgdcmPython gdcmfoo + IF(NOT VTK_WRAP_PYTHON) + MESSAGE(FATAL_ERROR "Can't build gdcm python wrapping if VTK_WRAP_PYTHON is OFF" ) + ENDIF(NOT VTK_WRAP_PYTHON) + VTK_WRAP_PYTHON2(vtkgdcmPython gdcmWrapSOURCES ${vtkgdcmPython_la_SOURCES}) # The C++ files must be made into a C++ library - ADD_LIBRARY(vtkgdcmPython SHARED ${gdcmfoo}) + ADD_LIBRARY(vtkgdcmPython SHARED ${gdcmWrapSOURCES}) #MODULE on apple ? # set the libraries to link against + #SET_TARGET_PROPERTIES(${foobar} PROPERTIES PREFIX "") + + #GET_TARGET_PROPERTY(swig_gdcm_fullpath "${SWIG_MODULE_gdcm_REAL_NAME}" LOCATION) TARGET_LINK_LIBRARIES (vtkgdcmPython - pygdcm vtkgdcm vtkCommonPython vtkIOPython) ENDIF (GDCM_WRAP_PYTHON) - - #generate a setup.py according to VTK installation - #put it in subdir in order to not override old one - # as it is a pain...override existing file... +ENDIF(GDCM_VTK) + + # generate a setup.py according to VTK installation + # put it in subdir in order to not override old one GET_FILENAME_COMPONENT(VTKPATH ${VTK_DIR}/../../ ABSOLUTE) CONFIGURE_FILE( ${GDCM_SOURCE_DIR}/gdcmPython/setup.py.in ${GDCM_BINARY_DIR}/setup.py ) - #MESSAGE(STATUS ${VTKPATH}) -ENDIF(GDCM_VTK) #----------------------------------------------------------------------------- # Install stuff: -#Should install in path of PYTHON_LIBRARY and not the default CMAKE_INSTALL_PREFIX - -GET_FILENAME_COMPONENT(PYTHON_LIB_INSTALL ${PYTHON_LIBRARY} PATH) -GET_FILENAME_COMPONENT(SITEPACKAGE - ${PYTHON_LIB_INSTALL}/../site-packages ABSOLUTE) -#MESSAGE(STATUS ${SITEPACKAGE}) -#INSTALL_FILES(/include "\\.h$") -#INSTALL_TARGETS(/lib/ gdcm) - -# gdcm.py is a file generated by swig -# then from python you can do import gdcm, and it will load _gdcm.so -#INSTALL_TARGETS(/lib/ gdcm.py) -#INSTALL_PROGRAMS(/lib/ gdcm.py) -#INSTALL_FILES(/lib/ .py gdcm.py) - -# When installing swig-generated python module one should pay attention -# to library naming convention, see here for more detail: -# http://www.swig.org/Doc1.3/Python.html#n6 -# even with the $swig -module mylib, there is always a trailing underscore added +# Cmake cannot handle installing since python wants stuff to be installed +# in /usr/lib/pythonx.x/site-package and not a a std /usr/lib +# Thus generate a setup.py file using distutils + +# Create a gdcm.pth file: + +FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/gdcm.pth + "gdcmPython\n" + ) + +FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/__init__.py + "import gdcm\n" + ) + +IF(GDCM_VTK) + FILE(APPEND ${GDCM_BINARY_DIR}/gdcmPython/__init__.py + "import os\n" + "if os.name == 'posix':\n" + " from libvtkgdcmPython import *\n" + "else:\n" + " from vtkgdcmPython import *\n" + ) +ENDIF(GDCM_VTK) + +#----------------------------------------------------------------------------- +# Add test from demo subdir: + +SUBDIRS(demo)