Skip to content

Commit 965cb2e

Browse files
Rafael Palomarjcfr
authored andcommitted
COMP: Fix build of python wrapping as a standalone project
This commit adds a copy of the CMake module vtkMacroKitPythonWrap based of Slicer/Slicer@78a71cd9a
1 parent ffc4d9b commit 965cb2e

File tree

1 file changed

+293
-0
lines changed

1 file changed

+293
-0
lines changed

CMake/vtkMacroKitPythonWrap.cmake

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
################################################################################
2+
#
3+
# Program: 3D Slicer
4+
#
5+
# Copyright (c) Kitware Inc.
6+
#
7+
# See COPYRIGHT.txt
8+
# or http://www.slicer.org/copyright/copyright.txt for details.
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc.
17+
# and was partially funded by NIH grant 3P41RR013218-12S1
18+
#
19+
################################################################################
20+
21+
# Based on VTK/CMake/KitCommonWrapBlock.cmake
22+
23+
# Add <dep> as a dependency of <module_name> and recurse on <dep>'s dependencies.
24+
# Appends dependencies and their include directories to lists:
25+
# - _<module_name>_wrap_depends
26+
# - _<module_name>_wrap_include_dirs
27+
# Ignores VTK dependencies.
28+
macro(_get_dependencies_recurse module_name dep)
29+
string(REGEX REPLACE "(.+)PythonD\$" "\\1" _dep_base ${dep})
30+
if(${_dep_base}_WRAP_HIERARCHY_FILE)
31+
list(APPEND _${module_name}_wrap_depends ${_dep_base})
32+
endif()
33+
34+
set(_wrap_include_dirs ${${_dep_base}_INCLUDE_DIRS})
35+
if(_wrap_include_dirs)
36+
list(APPEND _${module_name}_wrap_include_dirs ${_wrap_include_dirs})
37+
endif()
38+
39+
list(FIND ${_dep_base}_WRAP_DEPENDS "${_dep_base}" _index)
40+
if(NOT _index EQUAL -1)
41+
message(FATAL_ERROR "${_dep_base} can NOT depends on itself [${_dep_base}_WRAP_DEPENDS: ${${_dep_base}_WRAP_DEPENDS}]")
42+
endif()
43+
44+
foreach(_dep ${${_dep_base}_WRAP_DEPENDS})
45+
list(FIND VTK_LIBRARIES "${_dep}" _index)
46+
if(_index EQUAL -1)
47+
_get_dependencies_recurse(${module_name} "${_dep}")
48+
endif()
49+
endforeach()
50+
endmacro()
51+
52+
#!
53+
#! vtkMacroKitPythonWrap(
54+
#!
55+
#! # Single value arguments
56+
#! KIT_NAME <name>
57+
#! KIT_INSTALL_BIN_DIR <dir>
58+
#! KIT_INSTALL_LIB_DIR <dir>
59+
#! [KIT_MODULE_INSTALL_BIN_DIR <dir>]
60+
#! [KIT_MODULE_INSTALL_LIB_DIR <dir>]
61+
#!
62+
#! # Multi-value arguments
63+
#! KIT_SRCS <src> [<src> [...]]
64+
#! [MY_KIT_PYTHON_EXTRA_SRCS <src> [<src> ...]]
65+
#! [KIT_WRAP_HEADERS <header> [<header ...]]
66+
#! [KIT_PYTHON_LIBRARIES <lib> [<lib> ...]]
67+
#!
68+
#! )
69+
#!
70+
#! Variables that Change Behavior:
71+
#!
72+
#! Slicer_VTK_WRAP_HIERARCHY_DIR:
73+
#!
74+
#! Directory where to output the hierarchy files
75+
#! Default is ${Slicer_BINARY_DIR}
76+
#!
77+
#! Slicer_VTK_WRAP_MODULE_INSTALL_COMPONENT_IDENTIFIER:
78+
#!
79+
#! Install component associated with "Python" module. This does not include
80+
#! the "PythonD" libraries.
81+
#! Default is "RuntimeLibraries" which is the same as the component associated
82+
#! with the "PythonD" lilbraries.
83+
#!
84+
#! Slicer_VTK_WRAP_HIERARCHY_TARGETS_PROPERTY_NAME:
85+
#!
86+
#! Name of the global property associated with the list of all wrapped library
87+
#! names.
88+
#! Default is "SLICER_WRAP_HIERARCHY_TARGETS"
89+
#!
90+
macro(vtkMacroKitPythonWrap)
91+
set(options)
92+
set(oneValueArgs KIT_NAME KIT_INSTALL_BIN_DIR KIT_INSTALL_LIB_DIR KIT_MODULE_INSTALL_BIN_DIR KIT_MODULE_INSTALL_LIB_DIR)
93+
set(multiValueArgs KIT_SRCS KIT_PYTHON_EXTRA_SRCS KIT_WRAP_HEADERS KIT_PYTHON_LIBRARIES)
94+
cmake_parse_arguments(MY "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
95+
96+
# Sanity checks
97+
set(expected_defined_vars
98+
VTK_CMAKE_DIR VTK_WRAP_PYTHON BUILD_SHARED_LIBS VTK_LIBRARIES)
99+
foreach(var ${expected_defined_vars})
100+
if(NOT DEFINED ${var})
101+
message(FATAL_ERROR "error: ${var} CMake variable is not defined !")
102+
endif()
103+
endforeach()
104+
105+
set(expected_nonempty_vars KIT_NAME KIT_INSTALL_BIN_DIR KIT_INSTALL_LIB_DIR)
106+
foreach(var ${expected_nonempty_vars})
107+
if("${MY_${var}}" STREQUAL "")
108+
message(FATAL_ERROR "error: ${var} CMake variable is empty !")
109+
endif()
110+
endforeach()
111+
112+
# Default arguments
113+
if("${MY_KIT_MODULE_INSTALL_BIN_DIR}" STREQUAL "")
114+
set(MY_KIT_MODULE_INSTALL_BIN_DIR ${MY_KIT_INSTALL_BIN_DIR})
115+
endif()
116+
if("${MY_KIT_MODULE_INSTALL_LIB_DIR}" STREQUAL "")
117+
set(MY_KIT_MODULE_INSTALL_LIB_DIR ${MY_KIT_INSTALL_LIB_DIR})
118+
endif()
119+
# Default global variables
120+
if(NOT DEFINED Slicer_VTK_WRAP_HIERARCHY_DIR)
121+
# set(Slicer_VTK_WRAP_HIERARCHY_DIR ${Slicer_BINARY_DIR})
122+
set(Slicer_VTK_WRAP_HIERARCHY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
123+
endif()
124+
if(NOT DEFINED Slicer_VTK_WRAP_MODULE_INSTALL_COMPONENT_IDENTIFIER)
125+
set(Slicer_VTK_WRAP_MODULE_INSTALL_COMPONENT_IDENTIFIER "RuntimeLibraries")
126+
endif()
127+
if(NOT DEFINED Slicer_VTK_WRAP_HIERARCHY_TARGETS_PROPERTY_NAME)
128+
set(Slicer_VTK_WRAP_HIERARCHY_TARGETS_PROPERTY_NAME "SLICER_WRAP_HIERARCHY_TARGETS")
129+
endif()
130+
131+
if(VTK_WRAP_PYTHON AND BUILD_SHARED_LIBS)
132+
133+
# Tell vtkWrapPython.cmake to set VTK_PYTHON_LIBRARIES for us.
134+
set(VTK_WRAP_PYTHON_FIND_LIBS 1)
135+
include(${VTK_CMAKE_DIR}/vtkWrapPython.cmake)
136+
137+
set(TMP_WRAP_FILES ${MY_KIT_SRCS} ${MY_KIT_WRAP_HEADERS})
138+
set(_wrap_hierarchy_stamp_file)
139+
140+
# Create list of wrapping dependencies for generating the hierarchy file.
141+
set(_kit_wrap_depends)
142+
set(_kit_wrap_include_dirs ${VTK_INCLUDE_DIRS})
143+
144+
# Add kit include dirs
145+
list(APPEND _kit_wrap_include_dirs ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
146+
if(DEFINED Slicer_Base_INCLUDE_DIRS)
147+
list(APPEND _kit_wrap_include_dirs ${Slicer_Base_INCLUDE_DIRS})
148+
endif()
149+
set(_kit_include_dirs ${${MY_KIT_NAME}_INCLUDE_DIRS})
150+
if(_kit_include_dirs)
151+
list(APPEND _kit_wrap_include_dirs ${_kit_include_dirs})
152+
endif()
153+
154+
# Add VTK dependencies
155+
foreach(_dep ${VTK_LIBRARIES})
156+
list(APPEND _kit_wrap_depends ${_dep})
157+
endforeach()
158+
159+
# Recursively add dependencies and get their include directories
160+
foreach(_dep ${MY_KIT_PYTHON_LIBRARIES})
161+
set(_${MY_KIT_NAME}_wrap_depends)
162+
set(_${MY_KIT_NAME}_wrap_include_dirs)
163+
_get_dependencies_recurse("${MY_KIT_NAME}" "${_dep}")
164+
list(APPEND _kit_wrap_depends ${_${MY_KIT_NAME}_wrap_depends})
165+
list(APPEND _kit_wrap_include_dirs ${_${MY_KIT_NAME}_wrap_include_dirs})
166+
endforeach()
167+
168+
if(_kit_wrap_depends)
169+
list(REMOVE_DUPLICATES _kit_wrap_depends)
170+
endif()
171+
if(_kit_wrap_include_dirs)
172+
list(REMOVE_DUPLICATES _kit_wrap_include_dirs)
173+
endif()
174+
175+
# Update list of include directories for wrapper tool command lines
176+
list(APPEND VTK_WRAP_INCLUDE_DIRS ${_kit_wrap_include_dirs})
177+
178+
# Generate hierarchy files for VTK8 and later
179+
if(NOT ${VTK_VERSION_MAJOR} VERSION_LESS 8)
180+
include(${VTK_CMAKE_DIR}/vtkWrapHierarchy.cmake)
181+
182+
# Set variables for this and future runs of vtk_wrap_hierarchy:
183+
# - <module_name>_WRAP_DEPENDS
184+
# - <module_name>_WRAP_HIERARCHY_FILE
185+
set(${MY_KIT_NAME}_WRAP_DEPENDS "${_kit_wrap_depends}" CACHE INTERNAL "${MY_KIT_NAME} wrapping dependencies" FORCE)
186+
set(_wrap_hierarchy_file "${Slicer_VTK_WRAP_HIERARCHY_DIR}/${MY_KIT_NAME}Hierarchy.txt")
187+
if(${VTK_VERSION_MAJOR}.${VTK_VERSION_MINOR} VERSION_LESS "8.2")
188+
set(_wrap_hierarchy_stamp_file ${CMAKE_CURRENT_BINARY_DIR}/${MY_KIT_NAME}Hierarchy.stamp.txt)
189+
endif()
190+
set(${MY_KIT_NAME}_WRAP_HIERARCHY_FILE "${_wrap_hierarchy_file}" CACHE INTERNAL "${MY_KIT_NAME} wrap hierarchy file" FORCE)
191+
192+
set_property(GLOBAL APPEND PROPERTY ${Slicer_VTK_WRAP_HIERARCHY_TARGETS_PROPERTY_NAME} ${MY_KIT_NAME})
193+
194+
# Set variables for vtk_wrap_python3:
195+
# - KIT_HIERARCHY_FILE
196+
set(KIT_HIERARCHY_FILE "${_wrap_hierarchy_file}")
197+
198+
# Generate hierarchy files
199+
vtk_wrap_hierarchy(${MY_KIT_NAME} ${Slicer_VTK_WRAP_HIERARCHY_DIR} "${TMP_WRAP_FILES}")
200+
endif()
201+
202+
VTK_WRAP_PYTHON3(${MY_KIT_NAME}Python KitPython_SRCS "${TMP_WRAP_FILES}")
203+
204+
include_directories("${PYTHON_INCLUDE_PATH}")
205+
206+
# Create a python module that can be loaded dynamically. It links to
207+
# the shared library containing the wrappers for this kit.
208+
add_library(${MY_KIT_NAME}PythonD ${KitPython_SRCS} ${MY_KIT_PYTHON_EXTRA_SRCS})
209+
210+
# Include the hierarchy stamp file in the main kit library to ensure
211+
# hierarchy file is created.
212+
# XXX Use target_sources if cmake_minimum_required >= 3.1
213+
get_target_property(_kit_srcs ${MY_KIT_NAME} SOURCES)
214+
if(${VTK_VERSION_MAJOR}.${VTK_VERSION_MINOR} VERSION_LESS "8.2")
215+
list(APPEND _kit_srcs ${_wrap_hierarchy_stamp_file})
216+
else()
217+
list(APPEND _kit_srcs ${_wrap_hierarchy_file})
218+
endif()
219+
set_target_properties(${MY_KIT_NAME} PROPERTIES SOURCES "${_kit_srcs}")
220+
221+
set(VTK_KIT_PYTHON_LIBRARIES)
222+
# XXX Hard-coded list of VTK kits available when building
223+
# with VTK_ENABLE_KITS set to 1
224+
set(vtk_kits
225+
vtkCommonKit
226+
vtkFiltersKit
227+
vtkImagingKit
228+
vtkRenderingKit
229+
vtkIOKit
230+
vtkOpenGLKit
231+
vtkInteractionKit
232+
vtkViewsKit
233+
vtkParallelKit
234+
vtkWrappingKit
235+
)
236+
foreach(c ${VTK_LIBRARIES} ${vtk_kits})
237+
if(${c} MATCHES "^vtk.+" AND TARGET ${c}PythonD) # exclude system libraries
238+
list(APPEND VTK_KIT_PYTHON_LIBRARIES ${c}PythonD)
239+
endif()
240+
endforeach()
241+
set(VTK_PYTHON_CORE vtkWrappingPythonCore)
242+
target_link_libraries(
243+
${MY_KIT_NAME}PythonD
244+
${MY_KIT_NAME}
245+
${VTK_PYTHON_CORE}
246+
${VTK_PYTHON_LIBRARIES}
247+
${VTK_KIT_PYTHON_LIBRARIES}
248+
${MY_KIT_PYTHON_LIBRARIES}
249+
)
250+
251+
install(TARGETS ${MY_KIT_NAME}PythonD
252+
RUNTIME DESTINATION ${MY_KIT_INSTALL_BIN_DIR} COMPONENT RuntimeLibraries
253+
LIBRARY DESTINATION ${MY_KIT_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
254+
ARCHIVE DESTINATION ${MY_KIT_INSTALL_LIB_DIR} COMPONENT Development
255+
)
256+
257+
# Add a top-level dependency on the main kit library. This is needed
258+
# to make sure no python source files are generated until the
259+
# hierarchy file is built (it is built when the kit library builds)
260+
add_dependencies(${MY_KIT_NAME}PythonD ${MY_KIT_NAME})
261+
262+
# Add dependencies that may have been generated by VTK_WRAP_PYTHON3 to
263+
# the python wrapper library. This is needed for the
264+
# pre-custom-command hack in Visual Studio 6.
265+
if(KIT_PYTHON_DEPS)
266+
add_dependencies(${MY_KIT_NAME}PythonD ${KIT_PYTHON_DEPS})
267+
endif()
268+
269+
# Create a python module that can be loaded dynamically. It links to
270+
# the shared library containing the wrappers for this kit.
271+
add_library(${MY_KIT_NAME}Python MODULE ${MY_KIT_NAME}PythonInit.cxx)
272+
target_link_libraries(${MY_KIT_NAME}Python
273+
${MY_KIT_NAME}PythonD
274+
)
275+
276+
# Python extension modules on Windows must have the extension ".pyd"
277+
# instead of ".dll" as of Python 2.5. Older python versions do support
278+
# this suffix.
279+
if(WIN32 AND NOT CYGWIN)
280+
set_target_properties(${MY_KIT_NAME}Python PROPERTIES SUFFIX ".pyd")
281+
endif()
282+
283+
# Make sure that no prefix is set on the library
284+
set_target_properties(${MY_KIT_NAME}Python PROPERTIES PREFIX "")
285+
286+
install(TARGETS ${MY_KIT_NAME}Python
287+
RUNTIME DESTINATION ${MY_KIT_MODULE_INSTALL_BIN_DIR} COMPONENT ${Slicer_VTK_WRAP_MODULE_INSTALL_COMPONENT_IDENTIFIER}
288+
LIBRARY DESTINATION ${MY_KIT_MODULE_INSTALL_LIB_DIR} COMPONENT ${Slicer_VTK_WRAP_MODULE_INSTALL_COMPONENT_IDENTIFIER}
289+
ARCHIVE DESTINATION ${MY_KIT_MODULE_INSTALL_LIB_DIR} COMPONENT Development
290+
)
291+
endif()
292+
293+
endmacro()

0 commit comments

Comments
 (0)