]> Creatis software - gdcm.git/blob - gdcmPython/CMakeLists.txt
BUG: Small update but bug still subsist
[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 SUBDIRS(demo)
9
10 #-----------------------------------------------------------------------------
11 # SWIG Wrapping
12 #
13
14 # CMake 2.0 will fully support Swig wrapping so split based on cmake version:
15
16 IF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9)
17
18   FIND_PACKAGE(SWIG REQUIRED)
19   INCLUDE(${SWIG_USE_FILE})
20
21   #INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
22
23   #SET(CMAKE_SWIG_FLAGS "")
24
25   SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES CPLUSPLUS ON)
26   SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES SWIG_FLAGS "-includeall")
27   SWIG_ADD_MODULE(gdcm python
28     gdcm.i gdcm_wrap.cxx)
29   SWIG_LINK_LIBRARIES(gdcm ${PYTHON_LIBRARIES})
30   TARGET_LINK_LIBRARIES(_gdcm gdcm)
31
32 ELSE(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9)   
33
34   # As of 09/10/2003 there was no FindSWIG.cmake
35   # later we should use SWIG instead of swig
36   FIND_PROGRAM(SWIG_EXECUTABLE
37     NAMES swig-1.3 swig
38   )
39   
40   IF(NOT SWIG_EXECUTABLE)
41     MESSAGE(FATAL_ERROR "Swig could not be found wrapping is impossible")
42   ENDIF(NOT SWIG_EXECUTABLE)
43   
44   # Build our custom command :
45   ADD_CUSTOM_COMMAND(
46     OUTPUT    ${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx
47     COMMAND   ${SWIG_EXECUTABLE}
48     ARGS      -python -c++ -I${GDCM_SOURCE_DIR}/src -o 
49               ${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx 
50               ${GDCM_SOURCE_DIR}/gdcmPython/gdcm.i 
51     DEPENDS   ${GDCM_SOURCE_DIR}/gdcmPython/gdcm.i
52     COMMENT   "Generating gdcm_wrap.cxx/gdcm.py based on gdcm.i"
53   )
54   # Since the file gdcm_wrap.cxx does not exist, mark it as GENERATED:
55   SET_SOURCE_FILES_PROPERTIES(${GDCM_BINARY_DIR}/gdcmPython/gdcm_wrap.cxx GENERATED)
56
57   ADD_LIBRARY(pygdcm gdcm_wrap.cxx)
58   TARGET_LINK_LIBRARIES(pygdcm
59     ${PYTHON_LIBRARY}
60     gdcm
61   )
62   # http://www.cmake.org/pipermail/cmake/2003-August/004190.html
63   #SET_TARGET_PROPERTIES(pygdcm PROPERTIES PREFIX "_")
64
65   # This swig command will also outputs gdcm.py :
66   SET_SOURCE_FILES_PROPERTIES(${GDCM_BINARY_DIR}/gdcmPython/gdcm.py GENERATED)
67
68   # Instead of a simple copy operation let create our own gdcm.py file:
69   # First read what swig dumped:
70   FILE(READ ${GDCM_BINARY_DIR}/gdcmPython/gdcm.py SWIG_GDCM_PY)
71
72   # Then create the gdcm.py = gdcmVersion.py + gdcm.py (swig) file:
73   FILE(WRITE ${GDCM_BINARY_DIR}/bin/gdcm.py
74     "import os\n"
75     "gdcmVERSION=\"${GDCM_VERSION_FULL}\"\n"
76     "GDCM_DATA_PATH=\"${GDCM_DATA_ROOT}\"\n\n"
77     ${SWIG_GDCM_PY}
78     )
79
80   # Seems like they want instead a file called gdcmPython.py
81   # Thus add the custom command to copy it to proper location:
82   ADD_CUSTOM_COMMAND(
83     TARGET pygdcm
84     POST_BUILD
85     COMMAND ${CMAKE_COMMAND}
86     ARGS -E copy 
87       ${GDCM_BINARY_DIR}/bin/gdcm.py 
88       ${GDCM_BINARY_DIR}/bin/gdcmPython.py
89   )
90
91   # Here comes the hack to rename the library to a more 'swig' compliant scheme"
92   SET(LIB_NAME ${CMAKE_SHARED_LIBRARY_PREFIX}pygdcm${CMAKE_SHARED_LIBRARY_SUFFIX})
93   SET(EXE_DIR ${GDCM_BINARY_DIR}/Executable)
94   IF(EXECUTABLE_OUTPUT_PATH)
95     SET(EXE_DIR ${EXECUTABLE_OUTPUT_PATH})
96   ENDIF(EXECUTABLE_OUTPUT_PATH)
97   SET(LIB_DIR ${GDCM_BINARY_DIR}/Library/Shared)
98   IF(LIBRARY_OUTPUT_PATH)
99     SET(LIB_DIR ${LIBRARY_OUTPUT_PATH})
100   ENDIF(LIBRARY_OUTPUT_PATH)
101
102   IF(WIN32 OR APPLE)
103     SET(SHARED_MUST_BE_IN_EXE_DIR 1)
104   ENDIF(WIN32 OR APPLE)
105
106   IF(NOT SHARED_MUST_BE_IN_EXE_DIR)
107     SET(EXE_DIR ${LIB_DIR}) 
108   ENDIF(NOT SHARED_MUST_BE_IN_EXE_DIR)
109
110   ADD_CUSTOM_COMMAND(
111     TARGET pygdcm
112     POST_BUILD 
113     COMMAND ${CMAKE_COMMAND} ARGS -E copy
114     ${LIB_DIR}/${CMAKE_CFG_INTDIR}/${LIB_NAME}
115     ${EXE_DIR}/${CMAKE_CFG_INTDIR}/_gdcm${CMAKE_SHARED_LIBRARY_SUFFIX}
116     )
117
118   # Remove temporary (lib)pygdcm(.so .dll) library:
119   ADD_CUSTOM_COMMAND(
120     TARGET pygdcm
121     POST_BUILD
122     COMMAND ${CMAKE_COMMAND}
123     ARGS -E remove ${LIB_DIR}/${CMAKE_CFG_INTDIR}/${LIB_NAME}
124   )
125
126   # where are library stored ?
127 #  SET(GDCM_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR})
128 #  ADD_CUSTOM_COMMAND(
129 #    TARGET pygdcm
130 #    POST_BUILD
131 #    COMMAND ${CMAKE_COMMAND}
132 #    ARGS -E copy
133 #      ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/${MY_LIB_NAME}${CMAKE_SHLIB_SUFFIX}
134 #      ${EXECUTABLE_OUTPUT_PATH}/${CMAKE_CFG_INTDIR}/vtkXY.pvm
135 #    TARGET vtkXY
136 #  )
137
138 ENDIF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9)   
139
140
141 #-----------------------------------------------------------------------------
142 # VTK Wrapping
143
144 IF(GDCM_VTK)
145   INCLUDE_DIRECTORIES(
146     ${GDCM_SOURCE_DIR}/vtk
147   )
148
149   SET(vtkgdcmPython_la_SOURCES
150       ${GDCM_SOURCE_DIR}/vtk/vtkGdcmReader.cxx
151   )
152   SET_SOURCE_FILES_PROPERTIES(vtkGdcmReaderPython.cxx GENERATED)
153   
154   # Configure Python module, which is the plugin itself
155   IF (GDCM_WRAP_PYTHON)
156     VTK_WRAP_PYTHON2(vtkgdcmPython gdcmfoo
157                    ${vtkgdcmPython_la_SOURCES})
158     # The C++ files must be made into a C++ library
159     ADD_LIBRARY(vtkgdcmPython SHARED ${gdcmfoo})
160     # set the libraries to link against
161     TARGET_LINK_LIBRARIES (vtkgdcmPython
162                            #_pygdcm
163                            vtkgdcm
164                            vtkCommonPython
165                            vtkIOPython)
166   ENDIF (GDCM_WRAP_PYTHON)
167   
168   #generate a setup.py according to VTK installation 
169   #put it in subdir in order to not override old one
170   # as it is a pain...override existing file...
171   
172   GET_FILENAME_COMPONENT(VTKPATH ${VTK_DIR}/../../ ABSOLUTE)
173   CONFIGURE_FILE(
174       ${GDCM_SOURCE_DIR}/gdcmPython/setup.py.in
175       ${GDCM_BINARY_DIR}/setup.py
176   )
177   #MESSAGE(STATUS ${VTKPATH})
178 ENDIF(GDCM_VTK)
179
180 #-----------------------------------------------------------------------------
181 # Install stuff:
182
183 #Should install in path of PYTHON_LIBRARY and not the default CMAKE_INSTALL_PREFIX
184
185 GET_FILENAME_COMPONENT(PYTHON_LIB_INSTALL ${PYTHON_LIBRARY} PATH)
186 GET_FILENAME_COMPONENT(SITEPACKAGE 
187   ${PYTHON_LIB_INSTALL}/../site-packages ABSOLUTE)
188 #MESSAGE(STATUS ${SITEPACKAGE})
189 #INSTALL_FILES(/include "\\.h$")
190 #INSTALL_TARGETS(/lib/ gdcm)
191
192 # gdcm.py is a file generated by swig
193 # then from python you can do import gdcm, and it will load _gdcm.so
194 #INSTALL_TARGETS(/lib/ gdcm.py)
195 #INSTALL_PROGRAMS(/lib/ gdcm.py)
196 #INSTALL_FILES(/lib/ .py gdcm.py)
197
198 # When installing swig-generated python module one should pay attention
199 # to library naming convention, see here for more detail:
200 # http://www.swig.org/Doc1.3/Python.html#n6
201 # even with the $swig -module mylib, there is always a trailing underscore added