Skip to content

Commit 867d93c

Browse files
Cleanup VTK's Python package.
VTK's wrapped C++-Python module libraries are built under the `VTK_BUILD_PYTHON_MODULE_DIR`. That way, we don't need to add extra paths to locate those. Since those are indeed Python module files, they should indeed be under the VTK_BUILD_PYTHON_MODULE_DIR. VTK's Python package is built under the path specified by `VTK_BUILD_PYTHON_MODULE_DIR` and installed to `VTK_INSTALL_PYTHON_MODULE_DIR`. Improved defaults for VTK_BUILD_PYTHON_MODULE_DIR and VTK_INSTALL_PYTHON_MODULE_DIR. The new defaults avoid having to separately deal with python module paths for build and install directories.
1 parent 6dcef46 commit 867d93c

File tree

4 files changed

+53
-151
lines changed

4 files changed

+53
-151
lines changed

CMake/vtkModuleMacrosPython.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@ function (vtk_module_python_package name)
3838

3939
vtk_module_impl()
4040
vtk_module_export("")
41+
vtk_python_package(${name} ${ARGN})
42+
endfunction()
43+
4144

45+
#------------------------------------------------------------------------------
46+
# This is same as vtk_module_python_package except it can be used for a Python
47+
# package that's not a VTK module. This is indeed called by
48+
# `vtk_module_python_package` once the VTK module specific init/export calls are
49+
# invoked.
50+
#------------------------------------------------------------------------------
51+
function(vtk_python_package name)
4252
set(_packages)
4353
set(_no_install)
4454
set(_can_use_system)

CMake/vtkPythonWrapping.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ function(_vtk_add_python_module name)
165165
# named as *.pyd
166166
set_target_properties(${name} PROPERTIES SUFFIX ".pyd")
167167
endif()
168+
# build python module libraries under the ${VTK_BUILD_PYTHON_MODULE_DIR} dir.
169+
set_target_properties(${name}
170+
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${VTK_BUILD_PYTHON_MODULE_DIR}/vtk)
171+
if (NOT VTK_INSTALL_NO_RUNTIME)
172+
install(TARGETS ${name}
173+
RUNTIME DESTINATION ${VTK_INSTALL_PYTHON_MODULE_DIR}/vtk
174+
LIBRARY DESTINATION ${VTK_INSTALL_PYTHON_MODULE_DIR}/vtk
175+
ARCHIVE DESTINATION ${VTK_INSTALL_ARCHIVE_DIR}
176+
)
177+
endif()
168178
else ()
169179
# when building statically, the module targets need to be exported since
170180
# others can link against them, unlike when building shared, and hence we

Utilities/Python/CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,31 @@ vtk_module_impl()
2828
# Export location of python module dirs in install and build tree for every vtkpython module to use
2929
# As long as those modules depend on vtkpython, they can retrieve and use these
3030
if (NOT VTK_BUILD_PYTHON_MODULE_DIR)
31-
set(VTK_BUILD_PYTHON_MODULE_DIR
32-
"${VTK_BINARY_DIR}/Wrapping/Python"
31+
if(WIN32)
32+
set(VTK_BUILD_PYTHON_MODULE_DIR
33+
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Lib/site-packages"
3334
CACHE
3435
PATH "Directory where python modules will be built" FORCE)
36+
else()
37+
set(VTK_BUILD_PYTHON_MODULE_DIR
38+
"${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/python${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION}/site-packages"
39+
CACHE
40+
PATH "Directory where python modules will be built" FORCE)
41+
endif()
3542
endif()
3643

3744
if (NOT VTK_INSTALL_PYTHON_MODULE_DIR)
38-
set(VTK_INSTALL_PYTHON_MODULE_DIR
45+
if(WIN32)
46+
set(VTK_INSTALL_PYTHON_MODULE_DIR
47+
"${VTK_INSTALL_RUNTIME_DIR}/Lib/site-packages"
48+
CACHE
49+
PATH "Directory where python modules will be installed" FORCE)
50+
else()
51+
set(VTK_INSTALL_PYTHON_MODULE_DIR
3952
"${VTK_INSTALL_LIBRARY_DIR}/python${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION}/site-packages"
4053
CACHE
4154
PATH "Directory where python modules will be installed" FORCE)
55+
endif()
4256
endif()
4357

4458
# Export location of python module dirs in install and build tree for for

Wrapping/Python/CMakeLists.txt

Lines changed: 16 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -277,173 +277,43 @@ endif()
277277
# *** Step 1 has to be done carefully to avoid missing out files ***
278278

279279
if(PYTHON_EXECUTABLE)
280-
# Make the necessary directories.
281-
file(MAKE_DIRECTORY
282-
${CMAKE_CURRENT_BINARY_DIR}/vtk/gtk
283-
${CMAKE_CURRENT_BINARY_DIR}/vtk/qt4
284-
${CMAKE_CURRENT_BINARY_DIR}/vtk/qt
285-
${CMAKE_CURRENT_BINARY_DIR}/vtk/tk
286-
${CMAKE_CURRENT_BINARY_DIR}/vtk/util
287-
${CMAKE_CURRENT_BINARY_DIR}/vtk/wx
288-
${CMAKE_CURRENT_BINARY_DIR}/vtk/test
289-
${CMAKE_CURRENT_BINARY_DIR}/vtk/numpy_interface)
290-
291-
# Now create a list of Python files.
280+
281+
# Configure files that need to be configured, including generation of the *.py
282+
# files for each of the enabled VTK modules.
292283

293284
# Wrapping/Python/vtk/*.py
294-
unset(VTK_PYTHON_FILES)
295285
unset(VTK_PYTHON_IMPORT_ALL)
296286
foreach(module IN LISTS VTK_PYTHON_MODULES_AND_KITS)
297287
set(VTK_PYTHON_IMPORT_ALL
298288
"${VTK_PYTHON_IMPORT_ALL}from .${module} import *\n")
299-
configure_file(vtk/module.py.in vtk/${module}.py @ONLY)
300-
list(APPEND VTK_PYTHON_FILES
301-
"${CMAKE_CURRENT_BINARY_DIR}/vtk/${module}.py")
289+
configure_file(vtk/module.py.in
290+
"${VTK_BUILD_PYTHON_MODULE_DIR}/vtk/${module}.py" @ONLY)
302291
endforeach()
303-
configure_file(vtk/__init__.py.in vtk/__init__.py @ONLY)
304-
list(APPEND VTK_PYTHON_FILES
305-
"${CMAKE_CURRENT_BINARY_DIR}/vtk/__init__.py")
292+
configure_file(vtk/__init__.py.in
293+
"${VTK_BUILD_PYTHON_MODULE_DIR}/vtk/__init__.py" @ONLY)
306294

307295
# Kit module adapters
308296
foreach(kit IN LISTS vtk_kits)
309297
set(_module_kit ${kit}${VTK_KIT_SUFFIX})
310298
foreach(dep IN LISTS _${kit}_modules)
311-
configure_file(vtk/kit_module__init__.py.in vtk/${dep}.py @ONLY)
312-
list(APPEND VTK_PYTHON_FILES
313-
"${CMAKE_CURRENT_BINARY_DIR}/vtk/${dep}.py")
299+
configure_file(vtk/kit_module__init__.py.in
300+
"${VTK_BUILD_PYTHON_MODULE_DIR}/vtk/${dep}.py" @ONLY)
314301
endforeach()
315-
endforeach()
316-
unset(_module_kit)
317-
318-
# vtk.util package
319-
list(APPEND VTK_PYTHON_FILES
320-
vtk/util/__init__
321-
vtk/util/_argparse
322-
vtk/util/vtkMethodParser
323-
vtk/util/misc
324-
vtk/util/vtkConstants
325-
vtk/util/vtkImageExportToArray
326-
vtk/util/vtkImageImportFromArray
327-
vtk/util/vtkVariant
328-
vtk/util/colors
329-
vtk/util/numpy_support
330-
vtk/util/vtkAlgorithm
331-
vtk/util/keys
332-
)
333-
334-
# vtk.test package
335-
list(APPEND VTK_PYTHON_FILES
336-
vtk/test/BlackBox
337-
vtk/test/Testing
338-
vtk/test/__init__
339-
)
340-
341-
# Tk related files
342-
list(APPEND VTK_PYTHON_FILES
343-
vtk/tk/__init__
344-
vtk/tk/vtkLoadPythonTkWidgets
345-
vtk/tk/vtkTkImageViewerWidget
346-
vtk/tk/vtkTkRenderWidget
347-
vtk/tk/vtkTkRenderWindowInteractor
348-
vtk/tk/vtkTkPhotoImage
349-
)
350-
351-
# PyQt related files
352-
list(APPEND VTK_PYTHON_FILES
353-
vtk/qt/__init__
354-
vtk/qt/QVTKRenderWindowInteractor
355-
vtk/qt4/__init__
356-
vtk/qt4/QVTKRenderWindowInteractor
357-
)
358-
359-
# wxPython related files
360-
list(APPEND VTK_PYTHON_FILES
361-
vtk/wx/__init__
362-
vtk/wx/wxVTKRenderWindow
363-
vtk/wx/wxVTKRenderWindowInteractor
364-
)
365-
366-
# pyGTK related files
367-
list(APPEND VTK_PYTHON_FILES
368-
vtk/gtk/GtkVTKRenderWindow
369-
vtk/gtk/__init__
370-
vtk/gtk/GtkVTKRenderWindowInteractor
371-
vtk/gtk/GtkGLExtVTKRenderWindow
372-
vtk/gtk/GtkGLExtVTKRenderWindowInteractor
373-
)
374-
375-
# vtk.numpy_interface related files
376-
list(APPEND VTK_PYTHON_FILES
377-
vtk/numpy_interface/__init__
378-
vtk/numpy_interface/algorithms
379-
vtk/numpy_interface/dataset_adapter
380-
vtk/numpy_interface/internal_algorithms
381-
)
382-
# Done listing of files.
383-
384-
# Now copy these files if necessary.
385-
unset(VTK_PYTHON_SOURCE_FILES)
386-
unset(VTK_PYTHON_OUTPUT_FILES)
387-
foreach(file ${VTK_PYTHON_FILES})
388-
if (NOT IS_ABSOLUTE "${file}")
389-
set(src "${CMAKE_CURRENT_BINARY_DIR}/${file}.py")
390-
list(APPEND VTK_PYTHON_SOURCE_FILES "${src}")
391-
endif ()
302+
unset(_module_kit)
392303
endforeach()
393304

394-
if(NOT "${VTK_BINARY_DIR}" STREQUAL "${VTK_SOURCE_DIR}")
395-
foreach(file ${VTK_PYTHON_FILES})
396-
if (NOT IS_ABSOLUTE "${file}")
397-
set(src "${CMAKE_CURRENT_SOURCE_DIR}/${file}.py")
398-
set(tgt "${CMAKE_CURRENT_BINARY_DIR}/${file}.py")
399-
add_custom_command(DEPENDS ${src}
400-
COMMAND ${CMAKE_COMMAND} -E copy ${src} ${tgt}
401-
OUTPUT ${tgt}
402-
COMMENT "source copy")
403-
endif ()
404-
endforeach()
405-
endif()
305+
# Build all py files that form the `vtk` package.
306+
vtk_python_package(vtkpython_pyc vtk)
406307

407-
# Byte compile the Python files.
408-
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/compile_all_vtk.py.in
409-
${CMAKE_CURRENT_BINARY_DIR}/compile_all_vtk.py
410-
@ONLY)
411-
add_custom_command(
412-
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/compile_all_vtk.py
413-
DEPENDS ${VTK_PYTHON_SOURCE_FILES} ${CMAKE_CURRENT_BINARY_DIR}/compile_all_vtk.py
414-
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/vtk_compile_complete"
415-
)
416-
417-
# Create a target on which custom commands can depend. When this
418-
# is up-to-date, it is safe to run VTK python code. Therefore
419-
# this should also depend on the vtkpython executable.
420-
add_custom_target(vtkpython_pyc ALL
421-
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/vtk_compile_complete")
422308
if(TARGET vtkpython)
423-
add_dependencies(vtkpython_pyc
424-
vtkpython
425-
)
309+
add_dependencies(vtkpython vtkpython_pyc)
310+
endif()
311+
if(TARGET pvtkpython)
312+
add_dependencies(pvtkpython vtkpython_pyc)
426313
endif()
427314

428315
# If no runtime is to be installed then do not install python modules.
429316
if(NOT VTK_INSTALL_NO_RUNTIME)
430-
431-
# Install python modules
432-
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/vtk"
433-
DESTINATION ${VTK_INSTALL_PYTHON_MODULE_DIR} COMPONENT RuntimeLibraries
434-
USE_SOURCE_PERMISSIONS)
435-
436-
# Install python extension library that backs the modules
437-
if (BUILD_SHARED_LIBS AND NOT VTK_INSTALL_NO_LIBRARIES)
438-
foreach(module IN LISTS VTK_PYTHON_MODULES_AND_KITS)
439-
install(TARGETS ${module}Python
440-
RUNTIME DESTINATION ${VTK_INSTALL_RUNTIME_DIR} COMPONENT RuntimeLibraries
441-
LIBRARY DESTINATION ${VTK_INSTALL_PYTHON_MODULE_DIR}/vtk COMPONENT RuntimeLibraries
442-
ARCHIVE DESTINATION ${VTK_INSTALL_ARCHIVE_DIR} COMPONENT Development
443-
)
444-
endforeach()
445-
endif()
446-
447317
# Install the conveniently configured python interpretters
448318
if(NOT VTK_INSTALL_NO_PYTHON_EXES AND VTK_ENABLE_VTKPYTHON)
449319
# Install the vtkpython executable
@@ -456,7 +326,5 @@ if(PYTHON_EXECUTABLE)
456326
DESTINATION ${VTK_INSTALL_RUNTIME_DIR})
457327
endif()
458328
endif()
459-
460329
endif()
461-
462330
endif()

0 commit comments

Comments
 (0)