CMakeLists.txt 12.4 KB
Newer Older
1
# This is where we actually wrap the modules that have requested it.
2
include(vtkPythonWrapping)
3

4
get_property(VTK_PYTHON_MODULES GLOBAL PROPERTY VTK_PYTHON_WRAPPED)
5

6
7
# Get the include directories for the module and all its dependencies.
macro(vtk_include_recurse module)
8
9
  _vtk_module_config_recurse("${module}_PYTHON" ${module})
  include_directories(${${module}_PYTHON_INCLUDE_DIRS})
10
11
12
13
14
endmacro()

# Loop through all modules that should be wrapped, and wrap them.
foreach(module ${VTK_PYTHON_MODULES})
  vtk_module_load(${module})
15
  vtk_module_headers_load(${module})
16
17
18
19
20
  vtk_include_recurse(${module})
  vtk_add_python_wrapping(${module})
endforeach()

# Now to build the vtkPython executable, configure the .py files etc.
VTK Developers's avatar
VTK Developers committed
21
get_property(VTK_PYTHON_MODULES GLOBAL PROPERTY VTK_PYTHON_WRAPPED)
22

23
vtk_module_load(vtkWrappingPythonCore)
24
25
26
27
vtk_module_load(vtkPython)
include_directories(${CMAKE_CURRENT_BINARY_DIR}
  ${VTK_SOURCE_DIR}/Utilities
  ${vtkPython_INCLUDE_DIRS}
28
  ${vtkWrappingPythonCore_INCLUDE_DIRS})
29
# Needed for the vtkpython executable.
VTK Developers's avatar
VTK Developers committed
30
if(VTK_USE_CARBON)
31
  find_program(VTK_APPLE_RESOURCE Rez "${OSX_DEVELOPER_ROOT}/usr/bin")
VTK Developers's avatar
VTK Developers committed
32
endif()
33

VTK Developers's avatar
VTK Developers committed
34
35
if(UNIX)
  find_library(PYTHON_UTIL_LIBRARY
36
    NAMES util
37
    DOC "Utility library needed for vtkpython")
VTK Developers's avatar
VTK Developers committed
38
39
  mark_as_advanced(PYTHON_UTIL_LIBRARY)
endif()
40

41
42
# Generate the header which initializes Python modules when BUILD_SHARED_LIBS is
# OFF. The py_module_dependencies will be set to the libraries we should link
43
44
45
46
47
# against when we use the vtkpythonmodules.h file.
vtk_write_python_modules_header_for_wrapped_modules(
  "${CMAKE_CURRENT_BINARY_DIR}/vtkpythonmodules.h"
  py_module_dependencies)

48
49
50
# Create the VTK/Python  executable
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/vtkPythonAppInitConfigure.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/vtkPythonAppInitConfigure.h)
Alexander Neundorf's avatar
 
Alexander Neundorf committed
51

VTK Developers's avatar
VTK Developers committed
52
add_executable(vtkpython vtkPythonAppInit.cxx vtkpython.rc)
Alexander Neundorf's avatar
 
Alexander Neundorf committed
53

54
55
unset(VTKPYTHON_LINK_FLAGS)
unset(VTKPYTHON_LINK_LIBS)
Alexander Neundorf's avatar
 
Alexander Neundorf committed
56

VTK Developers's avatar
VTK Developers committed
57
58
59
60
61
62
63
if(CMAKE_SYSTEM_NAME MATCHES "AIX")
  get_filename_component(CMAKE_PYTHON_LIB_PREFIX "${PYTHON_LIBRARY}" PATH)
  find_file(CMAKE_PYTHON_LIBRARY_EXPORT python.exp "${CMAKE_PYTHON_LIB_PREFIX}")
  if(CMAKE_PYTHON_LIBRARY_EXPORT)
    set(VTKPYTHON_LINK_FLAGS "-Wl,-bE:${CMAKE_PYTHON_LIBRARY_EXPORT}")
  endif()
endif()
Alexander Neundorf's avatar
 
Alexander Neundorf committed
64

VTK Developers's avatar
VTK Developers committed
65
66
if(APPLE)
  if(VTK_USE_CARBON AND VTK_APPLE_RESOURCE)
67
68
    get_target_property(
      VTKPYTHON_RUNTIME_DIR vtkpython RUNTIME_OUTPUT_DIRECTORY)
VTK Developers's avatar
VTK Developers committed
69
    add_custom_command(
Alexander Neundorf's avatar
 
Alexander Neundorf committed
70
      TARGET vtkpython POST_BUILD
71
      COMMAND ${VTK_APPLE_RESOURCE} Carbon.r -o ${VTKPYTHON_RUNTIME_DIR}/vtkpython
Alexander Neundorf's avatar
 
Alexander Neundorf committed
72
      )
VTK Developers's avatar
VTK Developers committed
73
  endif()
Alexander Neundorf's avatar
 
Alexander Neundorf committed
74

75
76
77
78
79
80
  if(VTK_USE_CARBON)
    # MacOS python has a --enable-toolbox-glue option that is on by default,
    # which adds the "-u _PyMac_Error" linker flag to force inclusion of some
    # Carbon extension functions for Python.  It is irrelevant for Cocoa VTK.
    set(VTKPYTHON_LINK_FLAGS "-u _PyMac_Error")
  endif()
VTK Developers's avatar
VTK Developers committed
81
endif()
82

83
# Link against all the kit wrappers.
84
list(APPEND VTKPYTHON_LINK_LIBS vtkWrappingPythonCore)
Ken Martin's avatar
Ken Martin committed
85

VTK Developers's avatar
VTK Developers committed
86
87
88
89
90
91
92
set(vtkpython_pyc_depends
  vtkCommonPython
  vtkFilteringPython
  vtkIOPython
  vtkGraphicsPython
  vtkImagingPython
  )
93

VTK Developers's avatar
VTK Developers committed
94
if(PYTHON_UTIL_LIBRARY)
95
  list(APPEND VTKPYTHON_LINK_LIBS ${PYTHON_UTIL_LIBRARY})
VTK Developers's avatar
VTK Developers committed
96
endif()
97

98
set(VTKPYTHON_LINK_LIBS ${VTKPYTHON_LINK_LIBS} ${py_module_dependencies})
Alexander Neundorf's avatar
 
Alexander Neundorf committed
99

100
# Link to rt to prevent undefined symbol 'fdatasync'
101
102
103
104
105
106
if(CMAKE_SYSTEM MATCHES "SunOS.*" AND NOT CMAKE_COMPILER_IS_GNUCXX)
  find_library(VTK_SUNCC_RT_LIBRARY rt)
  if(VTK_SUNCC_RT_LIBRARY)
    set(VTKPYTHON_LINK_LIBS ${VTKPYTHON_LINK_LIBS} ${VTK_SUNCC_RT_LIBRARY})
  endif()
endif()
107

108
109
110
111
112
# If we are wrapping Tcl then the Tk libraries are available and should be used
# in the Python wrapping too.
if(VTK_WRAP_TCL)
  list(APPEND VTKPYTHON_LINK_LIBS ${VTK_TK_LIBRARIES})
endif()
113

114
115
116
if(HAVE_PTHREAD_H AND CMAKE_USE_PTHREADS)
  list(APPEND VTKPYTHON_LINK_LIBS ${CMAKE_THREAD_LIBS_INIT})
endif()
Alexander Neundorf's avatar
 
Alexander Neundorf committed
117

118
119
120
121
# FIXME: Is this needed here?
if(VTK_USE_FFMPEG_ENCODER)
  list(APPEND VTKPYTHON_LINK_LIBS ${FFMPEG_BASIC_LIBRARIES})
endif()
122

123
target_link_libraries(vtkpython ${VTKPYTHON_LINK_LIBS})
Alexander Neundorf's avatar
 
Alexander Neundorf committed
124

125
unset(PVTKPYTHON_EXECUTABLE)
126
# Create the pvtkpython Python wrapper executable with MPI support.
127
128
129
130
131
132
if(TARGET vtkParallelMPI)
  find_package(MPI REQUIRED)
  include_directories(${MPI_C_INCLUDE_PATH})
  set(MPI_LIBRARIES ${MPI_C_LIBRARIES})
  if(MPI_CXX_LIBRARIES)
    set(MPI_LIBRARIES ${MPI_LIBRARIES} ${MPI_CXX_LIBRARIES})
133
  endif()
134

Alexander Neundorf's avatar
 
Alexander Neundorf committed
135
  # Needed for mpich 2
136
137
138
139
140
141
142
143
144
145
146
147
  add_definitions("-DMPICH_IGNORE_CXX_SEEK")

  set(PVTKPYTHON_EXECUTABLE pvtkpython)
  add_executable(pvtkpython vtkParaPythonAppInit.cxx)
  target_link_libraries(pvtkpython ${VTKPYTHON_LINK_LIBS} vtkParallelMPI
    ${MPI_LIBRARIES})

  if(VTK_USE_CARBON AND VTK_APPLE_RESOURCE)
    add_custom_command(TARGET pvtkpython POST_BUILD
      COMMAND ${VTK_APPLE_RESOURCE} Carbon.r -o ${VTK_EXECUTABLE_DIR}/pvtkpython)
  endif()
endif()
148

149
150
151
152
153
154
155
156
# Handle out-of-source builds correctly.
#
#  1. Create a list of Python files to be installed/copied.
#  2. Copy them to VTK_BINARY_DIR if it is different from VTK_SOURCE_DIR.
#  3. Use Python's compileall to compile the copied files.
#
# *** Step 1 has to be done carefully to avoid missing out files ***

157
158
159
160
161
162
163
164
165
if(PYTHON_EXECUTABLE)
  # Make the necessary directories.
  file(MAKE_DIRECTORY
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/gtk
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/qt4
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/tk
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/util
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/wx
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/test)
166

167
168
169
# Now create a list of Python files.

# Wrapping/Python/vtk/*.py
170
171
172
173
  unset(VTK_PYTHON_FILES)
  unset(VTK_PYTHON_IMPORT_ALL)
  foreach(module ${VTK_PYTHON_MODULES})
    set(VTK_PYTHON_IMPORT_ALL "${VTK_PYTHON_IMPORT_ALL}from ${module} import *\n")
174
    configure_file(vtk/module.py.in vtk/${module}.py @ONLY)
175
  endforeach()
176
  configure_file(vtk/__init__.py.in vtk/__init__.py @ONLY)
177
178

# vtk.util package
179
  list(APPEND VTK_PYTHON_FILES
180
181
182
183
184
185
    vtk/util/__init__
    vtk/util/vtkMethodParser
    vtk/util/misc
    vtk/util/vtkConstants
    vtk/util/vtkImageExportToArray
    vtk/util/vtkImageImportFromArray
186
    vtk/util/vtkVariant
187
    vtk/util/colors
188
    vtk/util/numpy_support
189
190
191
    )

# vtk.test package
192
  list(APPEND VTK_PYTHON_FILES
193
194
195
    vtk/test/BlackBox
    vtk/test/Testing
    vtk/test/__init__
196
197
    )

198
# Tk related files
199
  list(APPEND VTK_PYTHON_FILES
200
201
202
203
204
205
206
207
    vtk/tk/__init__
    vtk/tk/vtkLoadPythonTkWidgets
    vtk/tk/vtkTkImageViewerWidget
    vtk/tk/vtkTkRenderWidget
    vtk/tk/vtkTkRenderWindowInteractor
    vtk/tk/vtkTkPhotoImage
    )

208
# PyQt v4 related files
209
  list(APPEND VTK_PYTHON_FILES
210
211
212
213
    vtk/qt4/__init__
    vtk/qt4/QVTKRenderWindowInteractor
    )

214
# wxPython related files
215
  list(APPEND VTK_PYTHON_FILES
216
217
218
219
220
221
    vtk/wx/__init__
    vtk/wx/wxVTKRenderWindow
    vtk/wx/wxVTKRenderWindowInteractor
    )

# pyGTK related files
222
  list(APPEND VTK_PYTHON_FILES
223
224
225
    vtk/gtk/GtkVTKRenderWindow
    vtk/gtk/__init__
    vtk/gtk/GtkVTKRenderWindowInteractor
226
227
    vtk/gtk/GtkGLExtVTKRenderWindow
    vtk/gtk/GtkGLExtVTKRenderWindowInteractor
228
229
230
231
232
    )

# Done listing of files.

# Now copy these files if necessary.
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  unset(VTK_PYTHON_SOURCE_FILES)
  unset(VTK_PYTHON_OUTPUT_FILES)
  foreach(file ${VTK_PYTHON_FILES})
    set(src "${CMAKE_CURRENT_BINARY_DIR}/${file}.py")
    set(VTK_PYTHON_SOURCE_FILES ${VTK_PYTHON_SOURCE_FILES} ${src})
  endforeach()

  if(NOT "${VTK_BINARY_DIR}" MATCHES "^${VTK_SOURCE_DIR}$")
    foreach(file ${VTK_PYTHON_FILES})
      set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}.py")
      set(tgt "${CMAKE_CURRENT_BINARY_DIR}/${file}.py")
      add_custom_command(DEPENDS ${src}
        COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
        OUTPUT ${tgt}
        COMMENT "source copy")
    endforeach()
  endif()
250

251
  # Byte compile the Python files.
252
253
  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/compile_all_vtk.py.in
    ${CMAKE_CURRENT_BINARY_DIR}/compile_all_vtk.py
254
    @ONLY)
255
  add_custom_command(
Alexander Neundorf's avatar
 
Alexander Neundorf committed
256
    COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/compile_all_vtk.py
257
    DEPENDS ${VTK_PYTHON_SOURCE_FILES} ${CMAKE_CURRENT_BINARY_DIR}/compile_all_vtk.py
258
    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vtk_compile_complete"
259
    )
260

261
262
263
  # Create a target on which custom commands can depend.  When this
  # is up-to-date, it is safe to run VTK python code.  Therefore
  # this should also depend on the vtkpython executable.
264
265
266
267
268
  add_custom_target(vtkpython_pyc ALL echo "..."
    DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/vtk_compile_complete")
  add_dependencies(vtkpython_pyc
    vtkpython
    ${vtkpython_pyc_depends})
269

270
  # If no runtime is to be installed then do not install python modules.
271
272
273
  if(VTK_INSTALL_NO_RUNTIME)
    set(VTK_INSTALL_NO_PYTHON 1)
  endif()
274

275
  # If set (generally by projects that include VTK), we install the
276
277
278
  # vtk{*}Python libraries using CMake rather than setup.py. Which implies that
  # they will be put will all other libraries. This is done in
  # KitCommonPythonWrapBlock.cmake
279
280
  if(VTK_INSTALL_PYTHON_USING_CMAKE)
    set(VTK_INSTALL_NO_PYTHON 1)
281
    # Install python module directory (*.py and *.pyd)
282
283
284
285
    install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/vtk"
      DESTINATION "${VTK_INSTALL_RUNTIME_DIR}/Python" COMPONENT RuntimeLibraries
      USE_SOURCE_PERMISSIONS)
  endif()
286

287
  # Add a rule to use python distutils to install the python wrappers.
288
289
  if(NOT VTK_INSTALL_NO_PYTHON)
    set(DOLLAR "$")
290

291
    # Create default python setup arguments if they are not set.
292
293
    if(NOT DEFINED VTK_PYTHON_SETUP_ARGS)
      set(VTK_PYTHON_SETUP_ARGS "--prefix=\"${DOLLAR}{CMAKE_INSTALL_PREFIX}\""
294
        CACHE STRING "Arguments passed to \"python setup.py install ...\" during installation.")
295
296
      mark_as_advanced(VTK_PYTHON_SETUP_ARGS)
    endif()
297

298
    # Change dollar sign to DOLLARSIGN
299
    string(REGEX REPLACE "\\${DOLLAR}" "DOLLARSIGN"
300
      PYTHON_SETUP_ARGS "${VTK_PYTHON_SETUP_ARGS}")
301
302

    # Get the install path for python modules
303
304
    execute_process(COMMAND "${PYTHON_EXECUTABLE}"
        "setup_install_paths.py" "purelib" ${PYTHON_SETUP_ARGS}
305
      WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
306
307
308
      OUTPUT_VARIABLE PYTHON_MODULE_INSTALL_DIR)

    # Get the install path for binary python modules
309
310
    execute_process(COMMAND "${PYTHON_EXECUTABLE}"
        "setup_install_paths.py" "platlib" ${PYTHON_SETUP_ARGS}
311
      WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
312
313
314
      OUTPUT_VARIABLE PYTHON_MODULE_PLATFORM_INSTALL_DIR)

    # Change DOLLARSIGN to dollar sign
315
    string(REGEX REPLACE "DOLLARSIGN" "${DOLLAR}"
316
      PYTHON_MODULE_INSTALL_DIR "${PYTHON_MODULE_INSTALL_DIR}")
317
    string(REGEX REPLACE "DOLLARSIGN" "${DOLLAR}"
318
319
      PYTHON_MODULE_PLATFORM_INSTALL_DIR
      "${PYTHON_MODULE_PLATFORM_INSTALL_DIR}")
320

321
    # Replace backslashes with forward slashes
322
323
    if(WIN32)
      string(REGEX REPLACE "\\\\" "/" PYTHON_MODULE_INSTALL_DIR
324
        "${PYTHON_MODULE_INSTALL_DIR}")
325
      string(REGEX REPLACE "\\\\" "/" PYTHON_MODULE_PLATFORM_INSTALL_DIR
326
        "${PYTHON_MODULE_PLATFORM_INSTALL_DIR}")
327
    endif()
328

329
    set(VTK_PYTHON_MODULE_INSTALL_DIR "${PYTHON_MODULE_INSTALL_DIR}"
330
331
      CACHE INTERNAL "Install directory for Python .py and .pyc files")

332
    set(VTK_PYTHON_MODULE_PLATFORM_INSTALL_DIR
333
      "${PYTHON_MODULE_PLATFORM_INSTALL_DIR}"
334
335
      CACHE INTERNAL "Install directory for Python binary modules")

336
337
338
    # If there are multiple configurations then add a BUILD_TYPE=...
    # argument to the python setup.py call.  The build type to use is set
    # in the CMake variable BUILD_TYPE while running the install script.
339
340
341
342
343
    if(CMAKE_CONFIGURATION_TYPES)
      set(VTK_PYTHON_SETUP_BUILD_TYPE "BUILD_TYPE=${DOLLAR}{BUILD_TYPE}")
    else()
      set(VTK_PYTHON_SETUP_BUILD_TYPE)
    endif()
344
345

    # Configure the post-install script to run python on setup.py.
346
347
    configure_file(${CMAKE_CURRENT_SOURCE_DIR}/PythonInstall.cmake.in
      ${CMAKE_CURRENT_BINARY_DIR}/PythonInstall.cmake
348
      @ONLY)
349

350
351
352
353
    install(SCRIPT ${CMAKE_CURRENT_BINARY_DIR}/PythonInstall.cmake
      COMPONENT RuntimeLibraries)
  endif()
endif()
354

355
# Create the setup.py file.
356
if(CMAKE_CONFIGURATION_TYPES)
357
358
  # The build has multiple configuration types.  If CMAKE_BUILD_TYPE
  # is set use it as the default BUILD_TYPE for setup.py to install.
359
360
361
362
363
364
365
  set(VTK_PYTHON_HAS_CONFIG_TYPES 1)
  if(CMAKE_BUILD_TYPE)
    set(VTK_PYTHON_BUILD_TYPE "\"${CMAKE_BUILD_TYPE}\"")
  else()
    set(VTK_PYTHON_BUILD_TYPE "[]")
  endif()
else()
366
367
  # The build has one configuration type.  The build type does not
  # affect installation.
368
369
370
  set(VTK_PYTHON_HAS_CONFIG_TYPES 0)
  set(VTK_PYTHON_BUILD_TYPE "[]")
endif()
371

372
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/setup.py.in
373
  ${CMAKE_CURRENT_BINARY_DIR}/setup.py @ONLY)
374
375
376
377
378

if(NOT VTK_INSTALL_NO_RUNTIME AND NOT VTK_INSTALL_NO_PYTHON)
  install(TARGETS vtkpython ${VTKPYTHON_EXECUTABLE}
    DESTINATION ${VTK_INSTALL_RUNTIME_DIR})
endif()