CMakeLists.txt 10.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
if (WIN32)
  if (building_vtkpythonrc)
    # XXX: With long paths, this command line can get too long because of all
    # the include directories required for vtkpython to work. The thing is that
    # CMake's RC rule in Ninja does not support response files, so the path can
    # end up way too long. Since this doesn't need those include paths, act as
    # if it is in a separate directory to avoid all the include_directories
    # used here.
    add_library(vtkpythonrc STATIC dummy.cxx vtkpython.rc)
    return()
11
12
  elseif (CMAKE_GENERATOR MATCHES "Visual Studio")
    set(extra_srcs vtkpython.rc)
13
14
  else ()
    set(building_vtkpythonrc TRUE)
15
    set(extra_links vtkpythonrc)
16
17
18
19
20
    # Make a separate directory scope for building vtkpythonrc.
    add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/vtkpythonrc")
  endif ()
endif ()

21
# This is where we actually wrap the modules that have requested it.
22
include(vtkPythonWrapping)
23

24
get_property(VTK_PYTHON_MODULES GLOBAL PROPERTY VTK_PYTHON_WRAPPED)
25

26
27
# Get the include directories for the module and all its dependencies.
macro(vtk_include_recurse module)
28
29
  _vtk_module_config_recurse("${module}_PYTHON" ${module})
  include_directories(${${module}_PYTHON_INCLUDE_DIRS})
30
31
32
33
34
endmacro()

# Loop through all modules that should be wrapped, and wrap them.
foreach(module ${VTK_PYTHON_MODULES})
  vtk_module_load(${module})
35
  vtk_module_headers_load(${module})
36
37
38
39
  vtk_include_recurse(${module})
  vtk_add_python_wrapping(${module})
endforeach()

40
41
42
43
option(VTK_ENABLE_VTKPYTHON "Enable vtkpython and pvtkpython binaries" ON)
mark_as_advanced(VTK_ENABLE_VTKPYTHON)
if(VTK_ENABLE_VTKPYTHON)

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

47
vtk_module_load(vtkWrappingPythonCore)
48
49
50
51
vtk_module_load(vtkPython)
include_directories(${CMAKE_CURRENT_BINARY_DIR}
  ${VTK_SOURCE_DIR}/Utilities
  ${vtkPython_INCLUDE_DIRS}
52
  ${vtkWrappingPythonCore_INCLUDE_DIRS})
53

VTK Developers's avatar
VTK Developers committed
54
55
if(UNIX)
  find_library(PYTHON_UTIL_LIBRARY
56
    NAMES util
57
    DOC "Utility library needed for vtkpython")
VTK Developers's avatar
VTK Developers committed
58
59
  mark_as_advanced(PYTHON_UTIL_LIBRARY)
endif()
60

61
62
# 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
63
64
65
66
67
# 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)

68
69
70
# 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
71

72
add_executable(vtkpython vtkPythonAppInit.cxx ${extra_srcs})
73
74

if (WIN32)
75
  target_link_libraries(vtkpython ${extra_links})
76
endif ()
Alexander Neundorf's avatar
   
Alexander Neundorf committed
77

78
79
unset(VTKPYTHON_LINK_FLAGS)
unset(VTKPYTHON_LINK_LIBS)
Alexander Neundorf's avatar
   
Alexander Neundorf committed
80

VTK Developers's avatar
VTK Developers committed
81
82
83
84
85
86
87
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
88

89
# Link against all the kit wrappers.
90
list(APPEND VTKPYTHON_LINK_LIBS vtkWrappingPythonCore)
Ken Martin's avatar
Ken Martin committed
91

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

96
set(VTKPYTHON_LINK_LIBS ${VTKPYTHON_LINK_LIBS} ${py_module_dependencies})
Alexander Neundorf's avatar
   
Alexander Neundorf committed
97

98
# Link to rt to prevent undefined symbol 'fdatasync'
99
100
101
102
103
104
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()
105

106
107
108
109
110
# 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()
111

112
113
114
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
115

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

121
target_link_libraries(vtkpython ${VTKPYTHON_LINK_LIBS})
Alexander Neundorf's avatar
   
Alexander Neundorf committed
122

123
unset(PVTKPYTHON_EXECUTABLE)
124
# Create the pvtkpython Python wrapper executable with MPI support.
125
126
127
128
129
130
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})
131
  endif()
132

Alexander Neundorf's avatar
   
Alexander Neundorf committed
133
  # Needed for mpich 2
134
135
136
137
138
139
140
  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})
endif()
141

142
143
endif()

144
145
146
147
148
149
150
151
# 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 ***

152
153
154
155
156
if(PYTHON_EXECUTABLE)
  # Make the necessary directories.
  file(MAKE_DIRECTORY
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/gtk
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/qt4
157
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/qt
158
159
160
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/tk
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/util
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/wx
161
162
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/test
    ${CMAKE_CURRENT_BINARY_DIR}/vtk/numpy_interface)
163

164
165
166
# Now create a list of Python files.

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

# vtk.util package
181
  list(APPEND VTK_PYTHON_FILES
182
    vtk/util/__init__
183
    vtk/util/_argparse
184
185
186
187
188
    vtk/util/vtkMethodParser
    vtk/util/misc
    vtk/util/vtkConstants
    vtk/util/vtkImageExportToArray
    vtk/util/vtkImageImportFromArray
189
    vtk/util/vtkVariant
190
    vtk/util/colors
191
    vtk/util/numpy_support
192
    vtk/util/vtkAlgorithm
193
    vtk/util/keys
194
195
196
    )

# vtk.test package
197
  list(APPEND VTK_PYTHON_FILES
198
199
200
    vtk/test/BlackBox
    vtk/test/Testing
    vtk/test/__init__
201
202
    )

203
# Tk related files
204
  list(APPEND VTK_PYTHON_FILES
205
206
207
208
209
210
211
212
    vtk/tk/__init__
    vtk/tk/vtkLoadPythonTkWidgets
    vtk/tk/vtkTkImageViewerWidget
    vtk/tk/vtkTkRenderWidget
    vtk/tk/vtkTkRenderWindowInteractor
    vtk/tk/vtkTkPhotoImage
    )

Alex Tsui's avatar
Alex Tsui committed
213
# PyQt related files
214
  list(APPEND VTK_PYTHON_FILES
215
216
    vtk/qt/__init__
    vtk/qt/QVTKRenderWindowInteractor
217
218
219
220
    vtk/qt4/__init__
    vtk/qt4/QVTKRenderWindowInteractor
    )

221
# wxPython related files
222
  list(APPEND VTK_PYTHON_FILES
223
224
225
226
227
228
    vtk/wx/__init__
    vtk/wx/wxVTKRenderWindow
    vtk/wx/wxVTKRenderWindowInteractor
    )

# pyGTK related files
229
  list(APPEND VTK_PYTHON_FILES
230
231
232
    vtk/gtk/GtkVTKRenderWindow
    vtk/gtk/__init__
    vtk/gtk/GtkVTKRenderWindowInteractor
233
234
    vtk/gtk/GtkGLExtVTKRenderWindow
    vtk/gtk/GtkGLExtVTKRenderWindowInteractor
235
236
    )

237
238
239
240
241
242
243
# vtk.numpy_interface related files
  list(APPEND VTK_PYTHON_FILES
    vtk/numpy_interface/__init__
    vtk/numpy_interface/algorithms
    vtk/numpy_interface/dataset_adapter
    vtk/numpy_interface/internal_algorithms
    )
244
245
246
# Done listing of files.

# Now copy these files if necessary.
247
248
249
  unset(VTK_PYTHON_SOURCE_FILES)
  unset(VTK_PYTHON_OUTPUT_FILES)
  foreach(file ${VTK_PYTHON_FILES})
250
251
252
253
    if (NOT IS_ABSOLUTE "${file}")
      set(src "${CMAKE_CURRENT_BINARY_DIR}/${file}.py")
      list(APPEND VTK_PYTHON_SOURCE_FILES "${src}")
    endif ()
254
255
  endforeach()

256
  if(NOT "${VTK_BINARY_DIR}" STREQUAL "${VTK_SOURCE_DIR}")
257
    foreach(file ${VTK_PYTHON_FILES})
258
259
260
261
262
263
264
265
      if (NOT IS_ABSOLUTE "${file}")
        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")
      endif ()
266
267
    endforeach()
  endif()
268

269
  # Byte compile the Python files.
270
271
  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/compile_all_vtk.py.in
    ${CMAKE_CURRENT_BINARY_DIR}/compile_all_vtk.py
272
    @ONLY)
273
  add_custom_command(
Alexander Neundorf's avatar
   
Alexander Neundorf committed
274
    COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/compile_all_vtk.py
275
    DEPENDS ${VTK_PYTHON_SOURCE_FILES} ${CMAKE_CURRENT_BINARY_DIR}/compile_all_vtk.py
276
    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vtk_compile_complete"
277
    )
278

279
280
281
  # 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.
282
  add_custom_target(vtkpython_pyc ALL
283
284
285
    DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/vtk_compile_complete")
  add_dependencies(vtkpython_pyc
    vtkpython
286
    )
287

288
  # If no runtime is to be installed then do not install python modules.
289
  if(NOT VTK_INSTALL_NO_RUNTIME)
290

291
    # Install python modules
292
    install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/vtk"
293
      DESTINATION ${VTK_INSTALL_PYTHON_MODULE_DIR} COMPONENT RuntimeLibraries
294
      USE_SOURCE_PERMISSIONS)
295

296
    # Install python extension library that backs the modules
297
298
299
300
    if (BUILD_SHARED_LIBS AND NOT VTK_INSTALL_NO_LIBRARIES)
      foreach(module ${VTK_PYTHON_MODULES})
        install(TARGETS ${module}Python
          RUNTIME DESTINATION ${VTK_INSTALL_RUNTIME_DIR} COMPONENT RuntimeLibraries
301
          LIBRARY DESTINATION ${VTK_INSTALL_PYTHON_MODULE_DIR}/vtk COMPONENT RuntimeLibraries
302
303
304
          ARCHIVE DESTINATION ${VTK_INSTALL_ARCHIVE_DIR} COMPONENT Development
          )
      endforeach()
305
    endif()
306

307
    # Install the conveniently configured python interpretters
308
    if(NOT VTK_INSTALL_NO_PYTHON_EXES AND VTK_ENABLE_VTKPYTHON)
309
310
      # Install the vtkpython executable
      install(TARGETS vtkpython
311
        DESTINATION ${VTK_INSTALL_RUNTIME_DIR})
312
313
314
315
316
317

      if(PVTKPYTHON_EXECUTABLE)
        # Install the mpi enabled vtkpython executable
        install(TARGETS pvtkpython
          DESTINATION ${VTK_INSTALL_RUNTIME_DIR})
      endif()
318
    endif()
319

320
  endif()
321

322
endif()