]> Creatis software - gdcm.git/blob - gdcmPython/CMakeLists.txt
ENH:First chunk of patch:
[gdcm.git] / gdcmPython / CMakeLists.txt
1 # gdcm Python wrapping stuff:
2
3 INCLUDE_DIRECTORIES(
4   ${GDCM_SOURCE_DIR}/src
5   ${PYTHON_INCLUDE_PATH}
6   ${GDCM_BINARY_DIR}/
7 )
8
9 #-----------------------------------------------------------------------------
10 # SWIG Wrapping
11 #
12
13 # CMake 2.0 will fully support Swig wrapping so split based on cmake version:
14
15 IF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9)
16
17   FIND_PACKAGE(SWIG REQUIRED)
18   INCLUDE(${SWIG_USE_FILE})
19
20   #INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
21   #SET(CMAKE_SWIG_FLAGS "")
22
23   #SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES PYTHON ON)
24   SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES CPLUSPLUS ON)
25   SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES SWIG_FLAGS "-includeall")
26   SWIG_ADD_MODULE(gdcm python gdcm.i)  #gdcm_wrap.cxx
27   SWIG_LINK_LIBRARIES(gdcm gdcm ${PYTHON_LIBRARIES})
28   
29   #SWIG_GET_EXTRA_OUTPUT_FILES(python foobar "/usr/" "gdcm" )
30   #MESSAGE(FATAL_ERROR ${swig_generated_file_fullname})
31   #MESSAGE(FATAL_ERROR ${swig_source_file_fullname})
32
33   # Swig creates a gdcm.py file put it in the right place, if only cmake
34   # was able to do stuff properly ...
35   ADD_CUSTOM_TARGET( CMAKE_S____ ALL)
36   ADD_CUSTOM_COMMAND(
37     TARGET CMAKE_S____
38     POST_BUILD
39     COMMAND ${CMAKE_COMMAND}
40     ARGS -E copy 
41       ${GDCM_BINARY_DIR}/gdcmPython/gdcm.py
42       ${GDCM_BINARY_DIR}/bin/gdcm.py
43   )
44
45 ELSE(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9)   
46
47   # As of 09/10/2003 there was no FindSWIG.cmake
48   # later we should use SWIG instead of swig
49   FIND_PROGRAM(SWIG_EXECUTABLE
50     NAMES swig-1.3 swig
51   )
52   
53   IF(NOT SWIG_EXECUTABLE)
54     MESSAGE(FATAL_ERROR "Swig could not be found wrapping is impossible")
55   ENDIF(NOT SWIG_EXECUTABLE)
56
57   # Build our custom command :
58   ADD_CUSTOM_COMMAND(
59     OUTPUT    ${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx
60     COMMAND   ${SWIG_EXECUTABLE}
61     ARGS      -python -c++ -I${GDCM_SOURCE_DIR}/src -o 
62               ${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx 
63               ${GDCM_SOURCE_DIR}/gdcmPython/gdcm.i 
64     DEPENDS   ${GDCM_SOURCE_DIR}/gdcmPython/gdcm.i
65     COMMENT   "Generating gdcm_wrap.cxx/gdcm.py based on gdcm.i"
66   )
67   # Since the file gdcm_wrap.cxx does not exist, mark it as GENERATED:
68   SET_SOURCE_FILES_PROPERTIES(${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx GENERATED)
69
70   ADD_LIBRARY(pygdcm gdcm_wrap.cxx)
71   TARGET_LINK_LIBRARIES(pygdcm
72     ${PYTHON_LIBRARY}
73     gdcm
74   )
75   # http://www.cmake.org/pipermail/cmake/2003-August/004190.html
76   #SET_TARGET_PROPERTIES(pygdcm PROPERTIES PREFIX "_")
77
78   # This swig command will also outputs gdcm.py :
79   SET_SOURCE_FILES_PROPERTIES(${GDCM_BINARY_DIR}/gdcmPython/gdcm.py GENERATED)
80
81   # Instead of a simple copy operation let create our own gdcm.py file:
82   # First read what swig dumped:
83   IF(EXISTS ${GDCM_BINARY_DIR}/gdcmPython/gdcm.py)
84     FILE(READ ${GDCM_BINARY_DIR}/gdcmPython/gdcm.py SWIG_GDCM_PY)
85   ENDIF(EXISTS ${GDCM_BINARY_DIR}/gdcmPython/gdcm.py)
86
87   # Then create the gdcm.py = gdcmVersion.py + gdcm.py (swig) file:
88   FILE(WRITE ${GDCM_BINARY_DIR}/bin/gdcmVersion.py
89     "import os\n"
90     "gdcmVERSION=\"${GDCM_VERSION}\"\n"
91     "GDCM_DATA_PATH=\"${GDCM_DATA_ROOT}\"\n\n"
92     ${SWIG_GDCM_PY}
93     )
94
95   # Seems like they want instead a file called gdcmPython.py
96   # Thus add the custom command to copy it to proper location:
97 #  ADD_CUSTOM_COMMAND(
98 #    TARGET pygdcm
99 #    POST_BUILD
100 #    COMMAND ${CMAKE_COMMAND}
101 #    ARGS -E copy 
102 #      ${GDCM_BINARY_DIR}/bin/gdcm.py 
103 #      ${GDCM_BINARY_DIR}/bin/gdcmPython.py
104 #  )
105
106   # Here comes the hack to rename the library to a more 'swig' compliant scheme"
107   SET(LIB_NAME ${CMAKE_SHARED_LIBRARY_PREFIX}pygdcm${CMAKE_SHARED_LIBRARY_SUFFIX})
108   SET(EXE_DIR ${GDCM_BINARY_DIR}/Executable)
109   IF(EXECUTABLE_OUTPUT_PATH)
110     SET(EXE_DIR ${EXECUTABLE_OUTPUT_PATH})
111   ENDIF(EXECUTABLE_OUTPUT_PATH)
112   SET(LIB_DIR ${GDCM_BINARY_DIR}/Library/Shared)
113   IF(LIBRARY_OUTPUT_PATH)
114     SET(LIB_DIR ${LIBRARY_OUTPUT_PATH})
115   ENDIF(LIBRARY_OUTPUT_PATH)
116
117   IF(WIN32 OR APPLE)
118     SET(SHARED_MUST_BE_IN_EXE_DIR 1)
119   ENDIF(WIN32 OR APPLE)
120
121   IF(NOT SHARED_MUST_BE_IN_EXE_DIR)
122     SET(EXE_DIR ${LIB_DIR}) 
123   ENDIF(NOT SHARED_MUST_BE_IN_EXE_DIR)
124
125   ADD_CUSTOM_COMMAND(
126     TARGET pygdcm
127     POST_BUILD 
128     COMMAND ${CMAKE_COMMAND} ARGS -E copy
129     ${LIB_DIR}/${CMAKE_CFG_INTDIR}/${LIB_NAME}
130     ${EXE_DIR}/${CMAKE_CFG_INTDIR}/_gdcm${CMAKE_SHARED_LIBRARY_SUFFIX}
131     )
132
133   # Remove temporary (lib)pygdcm(.so .dll) library:
134 #  ADD_CUSTOM_COMMAND(
135 #    TARGET pygdcm
136 #    POST_BUILD
137 #    COMMAND ${CMAKE_COMMAND}
138 #    ARGS -E remove ${LIB_DIR}/${CMAKE_CFG_INTDIR}/${LIB_NAME}
139 #  )
140
141   # where are library stored ?
142 #  SET(GDCM_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
143 #  ADD_CUSTOM_COMMAND(
144 #    TARGET pygdcm
145 #    POST_BUILD
146 #    COMMAND ${CMAKE_COMMAND}
147 #    ARGS -E copy
148 #      ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/${MY_LIB_NAME}${CMAKE_SHLIB_SUFFIX}
149 #      ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/vtkXY.pvm
150 #    TARGET vtkXY
151 #  )
152
153 ENDIF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9)   
154
155
156 #-----------------------------------------------------------------------------
157 # VTK Wrapping
158
159 IF(GDCM_VTK)
160   INCLUDE_DIRECTORIES(
161     ${GDCM_SOURCE_DIR}/vtk
162   )
163
164   SET(vtkgdcmPython_la_SOURCES
165       ${GDCM_SOURCE_DIR}/vtk/vtkGdcmReader.cxx
166   )
167   SET_SOURCE_FILES_PROPERTIES(vtkGdcmReaderPython.cxx GENERATED)
168   
169   # Configure Python module, which is the plugin itself
170   IF (GDCM_WRAP_PYTHON)
171     IF(NOT VTK_WRAP_PYTHON)
172       MESSAGE(FATAL_ERROR "Can't build gdcm python wrapping if VTK_WRAP_PYTHON is OFF" )
173     ENDIF(NOT VTK_WRAP_PYTHON)
174     VTK_WRAP_PYTHON2(vtkgdcmPython gdcmWrapSOURCES
175                    ${vtkgdcmPython_la_SOURCES})
176     # The C++ files must be made into a C++ library
177     ADD_LIBRARY(vtkgdcmPython SHARED ${gdcmWrapSOURCES})  #MODULE on apple ?
178     # set the libraries to link against
179     #SET_TARGET_PROPERTIES(${foobar} PROPERTIES PREFIX "")
180
181     #GET_TARGET_PROPERTY(swig_gdcm_fullpath "${SWIG_MODULE_gdcm_REAL_NAME}" LOCATION)
182     TARGET_LINK_LIBRARIES (vtkgdcmPython
183                            vtkgdcm
184                            vtkCommonPython
185                            vtkIOPython)
186   ENDIF (GDCM_WRAP_PYTHON)
187 ENDIF(GDCM_VTK)
188
189   # generate a setup.py according to VTK installation 
190   # put it in subdir in order to not override old one
191   
192   GET_FILENAME_COMPONENT(VTKPATH ${VTK_DIR}/../../ ABSOLUTE)
193   CONFIGURE_FILE(
194       ${GDCM_SOURCE_DIR}/gdcmPython/setup.py.in
195       ${GDCM_BINARY_DIR}/setup.py
196   )
197
198 #-----------------------------------------------------------------------------
199 # Install stuff:
200
201 # Cmake cannot handle installing since python wants stuff to be installed
202 # in /usr/lib/pythonx.x/site-package and not a a std /usr/lib
203 # Thus generate a setup.py file using distutils
204
205 # Create a gdcm.pth file:
206
207 FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/gdcm.pth
208     "gdcmPython\n"
209     )
210
211 FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/__init__.py
212     "import gdcm\n"
213     )
214
215 IF(GDCM_VTK)
216   FILE(APPEND ${GDCM_BINARY_DIR}/gdcmPython/__init__.py
217     "import os\n"
218     "if os.name == 'posix':\n"
219     "  from libvtkgdcmPython import *\n"
220     "else:\n"
221     "  from vtkgdcmPython import *\n"
222     )
223 ENDIF(GDCM_VTK)
224
225 #-----------------------------------------------------------------------------
226 # Add test from demo subdir:
227
228 SUBDIRS(demo)