]> Creatis software - gdcm.git/blob - gdcmPython/CMakeLists.txt
ENH: minor cleanup to use siwg's outdir instead of manually copying gdcm.py
[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_DIRECTORIES(
156     ${GDCM_SOURCE_DIR}/vtk
157   )
158
159   SET(vtkgdcmPython_la_SOURCES
160       ${GDCM_SOURCE_DIR}/vtk/vtkGdcmReader.cxx
161   )
162   SET_SOURCE_FILES_PROPERTIES(vtkGdcmReaderPython.cxx GENERATED)
163   
164   # Configure Python module, which is the plugin itself
165   IF (GDCM_WRAP_PYTHON)
166     IF(NOT VTK_WRAP_PYTHON)
167       MESSAGE(FATAL_ERROR "Can't build gdcm python wrapping if VTK_WRAP_PYTHON is OFF" )
168     ENDIF(NOT VTK_WRAP_PYTHON)
169     VTK_WRAP_PYTHON2(vtkgdcmPython gdcmWrapSOURCES
170                    ${vtkgdcmPython_la_SOURCES})
171     # The C++ files must be made into a C++ library
172     ADD_LIBRARY(vtkgdcmPython SHARED ${gdcmWrapSOURCES})  #MODULE on apple ?
173     # set the libraries to link against
174     #SET_TARGET_PROPERTIES(${foobar} PROPERTIES PREFIX "")
175
176     #GET_TARGET_PROPERTY(swig_gdcm_fullpath "${SWIG_MODULE_gdcm_REAL_NAME}" LOCATION)
177     TARGET_LINK_LIBRARIES (vtkgdcmPython
178                            vtkgdcm
179                            vtkCommonPython
180                            vtkIOPython)
181   ENDIF (GDCM_WRAP_PYTHON)
182 ENDIF(GDCM_VTK)
183
184   # generate a setup.py according to VTK installation 
185   # put it in subdir in order to not override old one
186   
187   GET_FILENAME_COMPONENT(VTKPATH ${VTK_DIR}/../../ ABSOLUTE)
188   CONFIGURE_FILE(
189       ${GDCM_SOURCE_DIR}/gdcmPython/setup.py.in
190       ${GDCM_BINARY_DIR}/setup.py
191   )
192
193 #-----------------------------------------------------------------------------
194 # Install stuff:
195
196 # Cmake cannot handle installing since python wants stuff to be installed
197 # in /usr/lib/pythonx.x/site-package and not a a std /usr/lib
198 # Thus generate a setup.py file using distutils
199
200 # Create a gdcm.pth file:
201
202 FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/gdcm.pth
203     "gdcmPython\n"
204     )
205
206 FILE(WRITE ${GDCM_BINARY_DIR}/gdcmPython/__init__.py
207     "import gdcm\n"
208     )
209
210 IF(GDCM_VTK)
211   FILE(APPEND ${GDCM_BINARY_DIR}/gdcmPython/__init__.py
212     "import os\n"
213     "if os.name == 'posix':\n"
214     "  from libvtkgdcmPython import *\n"
215     "else:\n"
216     "  from vtkgdcmPython import *\n"
217     )
218 ENDIF(GDCM_VTK)
219
220 #-----------------------------------------------------------------------------
221 # Add test from demo subdir:
222
223 SUBDIRS(demo)