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