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