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