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