]> Creatis software - gdcm.git/blobdiff - gdcmPython/CMakeLists.txt
ENH: Saving a comment for a better approach in cmake 2.2 (later)
[gdcm.git] / gdcmPython / CMakeLists.txt
index b48dc60b235155921aa6a3ba3c563f5c73132220..e1bdef88ab9147b782986b3d41dc54c1e60bc8d1 100644 (file)
@@ -12,130 +12,33 @@ INCLUDE_DIRECTORIES(
 
 # CMake 2.0 will fully support Swig wrapping so split based on cmake version:
 
-IF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9)
-
-  # 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}/gdcmPython")
-  # Looks like a bug to me in cmake 2.0.x:
-  SEPARATE_ARGUMENTS(CMAKE_SWIG_FLAGS)
-
-  FIND_PACKAGE(SWIG REQUIRED)
+# 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}/gdcmPython\"")
+#SET(CMAKE_SWIG_OUTDIR "${GDCM_BINARY_DIR}/gdcmPython")
+# Looks like a bug to me in cmake 2.0.x:
+SEPARATE_ARGUMENTS(CMAKE_SWIG_FLAGS)
+
+FIND_PACKAGE(SWIG REQUIRED)
+IF(SWIG_FOUND)
   INCLUDE(${SWIG_USE_FILE})
+ENDIF(SWIG_FOUND)
 
-  #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 recursively include all the referenced
-  # files, "including" the C++ <sstream>, <list>, <map>... 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}
-    )
+SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES CPLUSPLUS ON)
+# BUG: This is only needed with cmake <= 2.0.5
+SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES SWIG_FLAGS "")
+# END OF BUG
+SWIG_ADD_MODULE(gdcm python gdcm.i)  #gdcm_wrap.cxx, or gdcmPYTHON_wrap.cxx
+SWIG_LINK_LIBRARIES(gdcm gdcm ${PYTHON_LIBRARIES})
 
-ENDIF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9)   
+# Get rid of -Wall / -W / -pedantic if exist
+SET(CMAKE_CXX_FLAGS "")
+# I guess I need to put them back on this file:
+#SET_SOURCE_FILES_PROPERTIES(
+#  ${swig_generated_file_fullname}
+#  PROPERTIES COMPILE_FLAGS "")
 
+INSTALL_TARGETS(/lib/gdcm/ _gdcm)
 
 #-----------------------------------------------------------------------------
 # VTK Wrapping
@@ -171,6 +74,8 @@ IF(GDCM_VTK)
                            vtkgdcm
                            vtkCommonPython
                            vtkIOPython)
+    INSTALL_TARGETS(/lib/gdcm/ vtkgdcmPython)
+
   ENDIF (GDCM_WRAP_PYTHON)
 ENDIF(GDCM_VTK)
 
@@ -184,22 +89,11 @@ ENDIF(GDCM_VTK)
 #)
 
 #-----------------------------------------------------------------------------
-# Install stuff:
-
-# 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:
-
+# Python package configuration
 FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/gdcmVersion.py
     "gdcmVERSION=\"${GDCM_VERSION}\"\n"
     )
 
-FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/gdcm.pth
-    "gdcmPython\n"
-    )
-
 FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/__init__.py
     "__all__ = [\"core\",\n"
     "           \"vtk\",\n"
@@ -224,6 +118,35 @@ IF(GDCM_VTK)
     )
 ENDIF(GDCM_VTK)
 
+#-----------------------------------------------------------------------------
+# Python installation
+IF(WIN32)
+  FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/gdcmPython.pth
+       "${GDCM_BINARY_DIR}\n"
+       "${GDCM_BINARY_DIR}/bin/release\n"
+  )
+ELSE(WIN32)
+  FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/gdcmPython.pth
+       "${CMAKE_INSTALL_PREFIX}/lib/gdcm\n"
+  )
+ENDIF(WIN32)
+
+# Install a python path file in site-packages that tells
+# python where to find the gdcmPython module
+IF(UNIX)
+FIND_PATH(PYTHON_DIR "site-packages"
+   /usr/lib/python2.4
+   /usr/lib/python2.3
+   /usr/lib/python2.2
+   /usr/lib/python2.1
+) 
+STRING(REGEX REPLACE "/usr" "" PYTHON_DIR ${PYTHON_DIR})
+
+INSTALL_FILES(${PYTHON_DIR}/site-packages "gdcmPython.pth")
+INSTALL_FILES(/lib/gdcm/gdcmPython "\\.py$")
+INSTALL_FILES(/lib/gdcm/gdcmPython FILES "gdcm.py")
+ENDIF(UNIX)
+
 #-----------------------------------------------------------------------------
 # Add test from demo subdir: