vtkPythonWrapping.cmake 10.3 KB
Newer Older
1
2
3
4
5
6
if (VTK_UNDEFINED_SYMBOLS_ALLOWED)
  set(_QUIET_LIBRARY "QUIET")
else()
  set(_QUIET_LIBRARY "REQUIRED")
endif()
find_package(PythonLibs ${_QUIET_LIBRARY})
VTK Developers's avatar
VTK Developers committed
7
include(vtkWrapPython)
8
9
include(vtkTargetLinkLibrariesWithDynamicLookup)

10
11
12
13
14
15
16
if(PYTHONINTERP_FOUND AND PYTHONLIBS_FOUND)
  set(_interp_version "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
  set(_libs_version "${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION}")
  if(NOT ${_interp_version} STREQUAL ${_libs_version})
    message(WARNING "Python library ${_libs_version} mismatches python ${_interp_version}.")
   endif()
endif()
VTK Developers's avatar
VTK Developers committed
17

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# To support wrapping of either module or kit, this function
# has two signatures:
# 1) vtk_add_python_wrapping(<module_name> <sources_var>)
# 2) vtk_add_python_wrapping("<module_name>[ <module_name>]" <sources_var> <kit_name>)
#
# Legacy code may call this function with a single argument. In that case,
# vtk_add_python_wrapping_library() is called internally to maintain backwards
# compatibility.
function(vtk_add_python_wrapping module_names)
  if(${ARGC} EQUAL 1)
    set(_legacy TRUE)
    message(AUTHOR_WARNING
      "Calling vtk_add_python_wrapping() with a single argument is deprecated.\n"
      "Replace calls like:\n"
      "    vtk_add_python_wrapping(\${module})\n"
      "with:\n"
      "    vtk_add_python_wrapping(\${module} module_srcs)\n"
      "    vtk_add_python_wrapping_library(\${module} module_srcs \${module})")
  endif()
  if("${ARGV1}" MATCHES ".+")
    set(sources_var ${ARGV1})
  endif()
  if("${ARGV2}" MATCHES ".+")
    list(REMOVE_AT ARGN 0)
    set(target ${ARGN})
  else()
    set(target ${module_names})
  endif()
VTK Developers's avatar
VTK Developers committed
46
  if(NOT VTK_WRAP_PYTHON_INIT_EXE)
47
    if(TARGET vtkWrapPythonInit)
48
49
50
51
      set (VTK_WRAP_PYTHON_INIT_EXE vtkWrapPythonInit)
    else()
      message(FATAL_ERROR "VTK must be built with Python wrapping turned on.")
    endif()
VTK Developers's avatar
VTK Developers committed
52
  endif()
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

  set(EXTRA_PYTHON_INCLUDE_DIRS)
  set(KIT_HIERARCHY_FILE)
  set(VTK_WRAP_HINTS_FILES)

  foreach(module_name IN LISTS module_names)
    list(APPEND EXTRA_PYTHON_INCLUDE_DIRS ${${module_name}_PYTHON_INCLUDE_DIRS})

    if(NOT ${module_name}_EXCLUDE_FROM_WRAP_HIERARCHY)
      list(APPEND KIT_HIERARCHY_FILE ${${module_name}_WRAP_HIERARCHY_FILE})
    endif()

    if(${module_name}_WRAP_HINTS AND EXISTS "${${module_name}_WRAP_HINTS}")
      list(APPEND VTK_WRAP_HINTS_FILES "${${module_name}_WRAP_HINTS}")
    endif()
  endforeach()

  if(VTK_WRAP_HINTS_FILES)
    set(VTK_WRAP_HINTS ${VTK_WRAP_HINTS_FILES})
  endif()

  vtk_wrap_python(${target}Python Python_SRCS "${module_names}")
  if(_legacy)
    set(_sources "${Python_SRCS}" "${extra_srcs}")
    vtk_add_python_wrapping_library(${module_names} _sources ${module_names})
  else()
    set(${sources_var} "${Python_SRCS}" "${extra_srcs}" PARENT_SCOPE)
  endif()
endfunction()

function(vtk_add_python_wrapping_library module srcs)
VTK Developers's avatar
VTK Developers committed
84
  # Need to add the Wrapping/Python to the include directory
85
  set(_python_include_dirs
86
87
    ${VTK_SOURCE_DIR}/Wrapping/Python
    ${VTK_BINARY_DIR}/Wrapping/Python
88
89
    ${VTK_SOURCE_DIR}/Utilities/Python
    ${VTK_BINARY_DIR}/Utilities/Python
VTK Developers's avatar
VTK Developers committed
90
    ${PYTHON_INCLUDE_DIRS})
91
  set(XY ${PYTHON_MAJOR_VERSION}${PYTHON_MINOR_VERSION})
VTK Developers's avatar
VTK Developers committed
92

93
  # Figure out the dependent PythonXYD libraries for the module
94
  set(extra_links)
95
96
97
98
99
100
101
102
  string(REPLACE "Kit" "" kit_basename "${module}")
  if (_${kit_basename}_is_kit)
    set(${module}_WRAP_DEPENDS)
    foreach (kit_module IN LISTS _${kit_basename}_modules)
      list(APPEND ${module}_WRAP_DEPENDS
        ${${kit_module}_WRAP_DEPENDS})
    endforeach ()
  endif ()
103
  foreach(dep IN LISTS ${module}_WRAP_DEPENDS)
104
105
106
107
108
109
110
111
112
    if (module STREQUAL dep OR kit_basename STREQUAL dep)
      continue ()
    endif ()

    if (VTK_ENABLE_KITS AND ${dep}_KIT)
      if (NOT ${dep}_KIT STREQUAL kit_basename)
        list(APPEND extra_links ${${dep}_KIT}KitPythonD)
      endif ()
    elseif (TARGET ${dep}PythonD)
113
      list(APPEND extra_links ${dep}PythonD)
114
    endif ()
VTK Developers's avatar
VTK Developers committed
115
116
  endforeach()

117
118
  vtk_add_library(${module}PythonD ${${srcs}})
  get_property(output_name TARGET ${module}PythonD PROPERTY OUTPUT_NAME)
119
  string(REPLACE "PythonD" "Python${XY}D" output_name "${output_name}")
120
  set_property(TARGET ${module}PythonD PROPERTY OUTPUT_NAME ${output_name})
121
122
  set_property(TARGET ${module}PythonD APPEND
    PROPERTY INCLUDE_DIRECTORIES ${_python_include_dirs})
123
  target_link_libraries(${module}PythonD LINK_PUBLIC
124
125
126
    vtkWrappingPythonCore ${extra_links})
  vtk_target_link_libraries_with_dynamic_lookup(
    ${module}PythonD LINK_PUBLIC ${VTK_PYTHON_LIBRARIES})
127

128
  if (MSVC)
129
    set_target_properties(${module}PythonD
130
131
132
      PROPERTIES STATIC_LIBRARY_FLAGS ${CMAKE_MODULE_LINKER_FLAGS})
  endif()

133
134
135
136
137
138
139
140
141
142
143
144
145
146
  foreach (submodule IN LISTS ARGN)
    if(${submodule}_IMPLEMENTS)
      set_property(TARGET ${module}PythonD APPEND PROPERTY COMPILE_DEFINITIONS
        "${submodule}_AUTOINIT=1(${submodule})")
    endif()
    target_link_libraries(${module}PythonD LINK_PUBLIC ${submodule})
  endforeach ()

  set(prefix ${module})
  if(_${module}_is_kit)
    set(prefix ${prefix}${VTK_KIT_SUFFIX})
  endif()
  _vtk_add_python_module(${module}Python ${prefix}PythonInit.cxx)
  target_link_libraries(${module}Python ${module}PythonD)
147
148
  set_property(TARGET ${module}Python APPEND
    PROPERTY INCLUDE_DIRECTORIES ${_python_include_dirs})
149
150
151
152
153
154
155
156
157
158
159
160
endfunction()

#------------------------------------------------------------------------------
# _vtk_add_python_module(<name> src1 src2..) is used to build modules for Python.
# A python module is the module that gets imported in Python interpretor.
# This is an internal function used by vtk_add_python_wrapping() to create the
# module for each VTK module. If BUILD_SHARED_LIBS is OFF, this simply creates
# a static library. In that case applications are expected to use
# vtk_write_python_modules_header_for_wrapped_modules() to generate a header
# that statically initializes the modules after Py_Inititalize() is called.
function(_vtk_add_python_module name)
  if (BUILD_SHARED_LIBS)
161
    add_library(${name} MODULE ${ARGN})
162
    set_property(TARGET ${name} PROPERTY PREFIX "${PYTHON_MODULE_PREFIX}")
163
164
165
166
    if (WIN32 AND NOT CYGWIN)
      # when building shared on Windows, the python module files need to be
      # named as *.pyd
      set_target_properties(${name} PROPERTIES SUFFIX ".pyd")
167
168
169
      if (PYTHON_DEBUG_LIBRARY)
        set_target_properties(${name} PROPERTIES OUTPUT_NAME_DEBUG "${name}_d")
      endif()
170
    endif()
171
    # build python module libraries under the ${VTK_BUILD_PYTHON_MODULES_DIR} dir.
172
    set_target_properties(${name}
173
      PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${VTK_BUILD_PYTHON_MODULES_DIR}/vtk)
174
175
    if (NOT VTK_INSTALL_NO_RUNTIME)
      install(TARGETS ${name}
176
177
        RUNTIME DESTINATION ${VTK_INSTALL_PYTHON_MODULES_DIR}/vtk
        LIBRARY DESTINATION ${VTK_INSTALL_PYTHON_MODULES_DIR}/vtk
178
179
180
        ARCHIVE DESTINATION ${VTK_INSTALL_ARCHIVE_DIR}
        )
    endif()
181
  else ()
182
183
184
185
    # when building statically, the module targets need to be exported since
    # others can link against them, unlike when building shared, and hence we
    # use vtk_add_library() call.
    vtk_add_library(${name} ${ARGN})
VTK Developers's avatar
VTK Developers committed
186
187
  endif()
endfunction()
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

#------------------------------------------------------------------------------
# vtk_write_python_modules_header(filename vtk-module1 vtk-module2...)
# This is similar to vtk_write_python_modules_header_for_wrapped_modules()
# execpt caller is expected to explitictly specify the modules to be initialized
# in the generated header. The header file will have non-empty content only if
# BUILD_SHARED_LIBS is OFF. When ON, the header file contains empty functions
# and hence can still be used by the client code, but it doesn't really do any
# initialization.
function(vtk_write_python_modules_header filename)
  get_filename_component(_name "${filename}" NAME_WE)
  STRING(REPLACE "." "_" _name "${_name}")
  STRING(TOUPPER ${_name} _nameUpper)

  set (EXTERN_DEFINES)
  set (INIT_CALLS)

  if (NOT BUILD_SHARED_LIBS)
    # fill in the init functions only when BUILD_SHARED_LIBS is OFF.
207
208
    set(EXTERN_DEFINES "${EXTERN_DEFINES}\n#if PY_VERSION_HEX < 0x03000000")
    set(INIT_CALLS "${INIT_CALLS}\n#if PY_VERSION_HEX < 0x03000000")
209
    foreach (module ${ARGN})
210
211
212
213
      set (EXTERN_DEFINES "${EXTERN_DEFINES}\n  extern void init${module}Python();")
      set (INIT_CALLS "${INIT_CALLS}\n
  static char name${module}[] = \"${module}Python\";
  PyImport_AppendInittab(name${module}, init${module}Python);")
214
    endforeach()
215
216
217
218
219
220
221
222
    set(EXTERN_DEFINES "${EXTERN_DEFINES}\n#else /* PY3K */")
    set(INIT_CALLS "${INIT_CALLS}\n#else /* PY3K */")
    foreach (module ${ARGN})
      set (EXTERN_DEFINES "${EXTERN_DEFINES}\n  extern PyObject *PyInit_${module}Python();")
      set (INIT_CALLS "${INIT_CALLS}\n  PyImport_AppendInittab(\"${module}Python\", PyInit_${module}Python);")
    endforeach()
    set(EXTERN_DEFINES "${EXTERN_DEFINES}\n#endif /* PY3K */\n")
    set(INIT_CALLS "${INIT_CALLS}\n#endif /* PY3K */\n")
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
  endif()

  configure_file(${VTK_CMAKE_DIR}/pythonmodules.h.in
    ${filename} @ONLY)
endfunction()

#------------------------------------------------------------------------------
# create init header for all python wrapped modules.
# this uses VTK_PYTHON_WRAPPED global property which is filled with every pyhton
# module.
# Usage: vtk_write_python_modules_header_for_wrapped_modules(
#           <filename> <out_variable>)
# out_variable is set to the list of dependencies to which the code including
# the header file should link against (using target_link_libraries()).
function(vtk_write_python_modules_header_for_wrapped_modules filename out_var)
  get_property(python_wrapped_modules GLOBAL PROPERTY VTK_PYTHON_WRAPPED)
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
  if(VTK_ENABLE_KITS)
    # Process VTK_PYTHON_WRAPPED to generate a list of Python wrapped kits and
    # modules.
    set(_kits_and_modules)
    foreach(module IN LISTS python_wrapped_modules)
      vtk_module_load("${module}")
      if(${module}_KIT)
        set(kit ${${module}_KIT})
        list(APPEND _kits_and_modules ${kit}Kit)
      else()
        list(APPEND _kits_and_modules ${module})
      endif()
    endforeach()
    list(REMOVE_DUPLICATES _kits_and_modules)
    set(python_wrapped_modules ${_kits_and_modules})
  endif()
255
256
257
  vtk_write_python_modules_header(
    "${filename}" ${python_wrapped_modules})
  set (dependencies)
258
259
260
261
  if (NOT BUILD_SHARED_LIBS)
    foreach(mod IN LISTS python_wrapped_modules)
      list(APPEND dependencies ${mod}Python)
    endforeach()
VTK Developers's avatar
VTK Developers committed
262
  endif()
263
  set (${out_var} "${dependencies}" PARENT_SCOPE)
VTK Developers's avatar
VTK Developers committed
264
endfunction()