Navigation Menu

Skip to content

Commit

Permalink
Merge topic 'static-builds-with-vtkpython'
Browse files Browse the repository at this point in the history
5a148ce Correct possible ODR violations in the AutoInit generated code
545c682 Allow for static builds of vtk to usable by python
0a655fc vtkpython: set the program name based on the path to the executable
440d6bc vtkPythonInterpreter: build a prefix for static builds

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: Brad King <brad.king@kitware.com>
Acked-by: Ben Boeckel <ben.boeckel@kitware.com>
Merge-request: !5696
  • Loading branch information
Robert Maynard authored and kwrobot committed Jul 8, 2019
2 parents 152728e + 5a148ce commit 41f9f50
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 22 deletions.
24 changes: 21 additions & 3 deletions CMake/vtkModuleWrapPython.cmake
Expand Up @@ -275,8 +275,9 @@ function (_vtk_module_wrap_python_library name)
"${_vtk_python_depend_module_package}.${_vtk_python_depend_library_name}Python")
endforeach ()


string(APPEND _vtk_python_module_contents
"from .${name} import *\n")
"from ${_vtk_python_import_prefix}${name} import *\n")

file(GENERATE
OUTPUT "${_vtk_python_module_file}"
Expand Down Expand Up @@ -313,13 +314,17 @@ function (_vtk_module_wrap_python_library name)
set(_vtk_python_wrap_target "VTKCompileTools::WrapPythonInit")
endif ()

if(_vtk_python_BUILD_STATIC)
set(additonal_options "${_vtk_python_import_prefix}")
endif()
add_custom_command(
OUTPUT "${_vtk_python_init_output}"
"${_vtk_python_init_impl_output}"
COMMAND "${_vtk_python_wrap_target}"
"${_vtk_python_init_data_file}"
"${_vtk_python_init_output}"
"${_vtk_python_init_impl_output}"
"${additonal_options}"
COMMENT "Generating the Python module initialization sources for ${name}"
DEPENDS
"${_vtk_python_init_data_file}"
Expand Down Expand Up @@ -561,6 +566,19 @@ function (vtk_module_wrap_python)
endif ()
string(REPLACE "." "/" _vtk_python_package_path "${_vtk_python_PYTHON_PACKAGE}")


if(_vtk_python_BUILD_STATIC)
# When doing static builds we want the statically initialized built-ins to be
# used. It is unclear in the Python-C API how to construct `namespace.module`
# so instead at the C++ level we import "namespace_module" during startup
# and than the python modules moving those imports into the correct python
# module.
string(REPLACE "." "_" _vtk_python_import_prefix "${_vtk_python_PYTHON_PACKAGE}_")
else()
# We are building dynamic libraries therefore the prefix is simply '.'
set(_vtk_python_import_prefix ".")
endif()

_vtk_module_check_destinations(_vtk_python_
MODULE_DESTINATION
STATIC_MODULE_DESTINATION
Expand Down Expand Up @@ -666,9 +684,9 @@ function (vtk_module_wrap_python)

string(APPEND _vtk_python_all_modules_include_content
"#if PY_VERSION_HEX < 0x03000000
#define PY_IMPORT(module) PyImport_AppendInittab(\"${_vtk_python_PYTHON_PACKAGE}.\" #module, init ## module)
#define PY_IMPORT(module) PyImport_AppendInittab(\"${_vtk_python_import_prefix}\" #module, init ## module)
#else
#define PY_IMPORT(module) PyImport_AppendInittab(\"${_vtk_python_PYTHON_PACKAGE}.\" #module, PyInit_ ## module)
#define PY_IMPORT(module) PyImport_AppendInittab(\"${_vtk_python_import_prefix}\" #module, PyInit_ ## module)
#endif
static void ${_vtk_python_TARGET_NAME}_load() {\n")
Expand Down
2 changes: 1 addition & 1 deletion CMake/vtkObjectFactory.cxx.in
Expand Up @@ -40,7 +40,7 @@ void @_vtk_object_factory_library_name@ObjectFactory::PrintSelf(ostream &os, vtk
}

// Registration of object factories.
static unsigned int @_vtk_object_factory_library_name@Count;
static unsigned int @_vtk_object_factory_library_name@Count = 0;

@_vtk_object_factory_configure_EXPORT_MACRO@ void @_vtk_object_factory_library_name@_AutoInit_Construct()
{
Expand Down
28 changes: 14 additions & 14 deletions Common/Core/vtkAutoInit.h
Expand Up @@ -23,13 +23,13 @@
#define VTK_AUTOINIT(M) VTK_AUTOINIT0(M,M##_AUTOINIT)
#define VTK_AUTOINIT0(M,T) VTK_AUTOINIT1(M,T)
#define VTK_AUTOINIT1(M, T) \
/* Declare every <mod>_AutoInit_Construct function. */ \
VTK_AUTOINIT_DECLARE_##T static struct M##_AutoInit { \
/* Call every <mod>_AutoInit_Construct during initialization. */ \
M##_AutoInit() { \
VTK_AUTOINIT_CONSTRUCT_##T \
} \
} M##_AutoInit_Instance;
/* Declare every <mod>_AutoInit_Construct function. */ \
VTK_AUTOINIT_DECLARE_##T namespace { \
static struct M##_AutoInit { \
/* Call every <mod>_AutoInit_Construct during initialization. */ \
M##_AutoInit() { VTK_AUTOINIT_CONSTRUCT_##T } \
} M##_AutoInit_Instance; \
}

#define VTK_AUTOINIT_DECLARE_0()
#define VTK_AUTOINIT_DECLARE_1(t1) VTK_AUTOINIT_DECLARE_0() VTK_AUTOINIT_DECLARE(t1)
Expand Down Expand Up @@ -70,13 +70,13 @@
//
// The above snippet if included in the global scope will ensure the object
// factories for vtkRenderingOpenGL2 are correctly registered and unregistered.
#define VTK_MODULE_INIT(M) \
VTK_AUTOINIT_DECLARE(M) \
static struct M##_ModuleInit { \
/* Call <mod>_AutoInit_Construct during initialization. */ \
M##_ModuleInit() { VTK_AUTOINIT_CONSTRUCT(M) } \
} M##_ModuleInit_Instance;

#define VTK_MODULE_INIT(M) \
VTK_AUTOINIT_DECLARE(M) namespace { \
static struct M##_ModuleInit { \
/* Call <mod>_AutoInit_Construct during initialization. */ \
M##_ModuleInit() { VTK_AUTOINIT_CONSTRUCT(M) } \
} M##_ModuleInit_Instance; \
}

#endif
// VTK-HeaderTest-Exclude: vtkAutoInit.h
13 changes: 13 additions & 0 deletions Utilities/PythonInterpreter/CMakeLists.txt
Expand Up @@ -5,6 +5,19 @@ set(classes
set(headers
vtkPythonStdStreamCaptureHelper.h)

if (NOT BUILD_SHARED_LIBS)
set(relative_prefix "")
set(bindir "${_vtk_build_RUNTIME_DESTINATION}")
while (bindir)
get_filename_component(bindir "${bindir}" DIRECTORY)
string(APPEND relative_prefix "../")
endwhile ()
string(APPEND relative_prefix "${_vtk_build_LIBRARY_DESTINATION}")
set_property(SOURCE vtkPythonInteractiveInterpreter.cxx APPEND
PROPERTY
COMPILE_DEFINITIONS "vtk_static_relative_prefix=\"${relative_prefix}\"")
endif ()

vtk_module_add_module(VTK::PythonInterpreter
CLASSES ${classes}
HEADERS ${headers})
4 changes: 4 additions & 0 deletions Utilities/PythonInterpreter/vtkPythonInterpreter.cxx
Expand Up @@ -743,6 +743,10 @@ void vtkPythonInterpreter::SetupVTKPythonPaths()
// if in an App bundle, the `sitepackages` dir is <app_root>/Contents/Python
,
"Contents/Python"
#endif
#if defined(vtk_static_relative_prefix)
,
vtk_static_relative_prefix
#endif
};

Expand Down
3 changes: 2 additions & 1 deletion Wrapping/Python/CMakeLists.txt
Expand Up @@ -11,7 +11,8 @@ target_link_libraries(vtkpython
VTK::WrappingPythonCore
VTK::PythonInterpreter
VTK::Python
VTK::vtkpythonmodules)
VTK::vtkpythonmodules
VTK::vtksys)
add_executable(VTK::vtkpython ALIAS vtkpython)
if (VTK_INSTALL_PYTHON_EXES)
install(
Expand Down
9 changes: 9 additions & 0 deletions Wrapping/Python/vtkPythonAppInit.cxx
Expand Up @@ -27,6 +27,7 @@
#include "vtkPythonInterpreter.h"
#include "vtkVersion.h"
#include "vtkpythonmodules.h"
#include <vtksys/SystemTools.hxx>
#include <sys/stat.h>

#include <string>
Expand Down Expand Up @@ -75,6 +76,14 @@ static void AtExitCallback()

int main(int argc, char **argv)
{
std::string fullpath;
std::string error;

if (vtksys::SystemTools::FindProgramPath(argv[0], fullpath, error))
{
vtkPythonInterpreter::SetProgramName(fullpath.c_str());
}

#ifdef VTK_COMPILED_USING_MPI
VTKMPICleanup.Initialize(&argc, &argv);
Py_AtExit(::AtExitCallback);
Expand Down
21 changes: 18 additions & 3 deletions Wrapping/Tools/vtkWrapPythonInit.c
Expand Up @@ -43,6 +43,7 @@ static void CreateInitFile(const char *libName, FILE *fout)
/* warning this code is also in getclasses.cxx under pcmaker */
/* this routine creates the init file */
static void CreateImplFile(const char *libName,
const char *importName,
int numDepends, char **depends,
int numFiles, char **files,
FILE *fout)
Expand Down Expand Up @@ -93,7 +94,7 @@ static void CreateImplFile(const char *libName,
fprintf(fout,"#else\n");
fprintf(fout," PyObject *m = Py_InitModule(\"%s\",\n"
" Py%s_Methods);\n",
libName, libName);
importName, libName);
fprintf(fout,"#endif\n\n");

fprintf(fout," PyObject *d = PyModule_GetDict(m);\n");
Expand Down Expand Up @@ -144,14 +145,15 @@ int main(int argc,char *argv[])
int numFiles = 0;
int numDepends = 0;
char libName[250];
char importName[250];
char tmpVal[250];
char *files[4000];
char *depends[400];
int doDepends = 0;

if (argc < 4)
{
fprintf(stderr,"Usage: %s input_file init_file impl_file\n",argv[0]);
fprintf(stderr,"Usage: %s input_file init_file impl_file [optional prefix]\n",argv[0]);
return 1;
}

Expand Down Expand Up @@ -202,6 +204,19 @@ int main(int argc,char *argv[])
return 1;
}

if (argc == 5)
{
size_t prefix_len = strlen(argv[4]);
size_t lib_len = strlen(libName);
memcpy(importName, argv[4], prefix_len);
memcpy(importName+prefix_len, libName, lib_len);
importName[prefix_len+lib_len] = '\0';
}
else
{
strcpy(importName, libName);
}

/* extra functions, types, etc. for the CommonCore module */
if (strcmp(libName, "vtkCommonCorePython") == 0 ||
strcmp(libName, "vtkCommonKitPython") == 0)
Expand All @@ -211,7 +226,7 @@ int main(int argc,char *argv[])
}

CreateInitFile(libName, fout_init);
CreateImplFile(libName, numDepends, depends, numFiles, files, fout_impl);
CreateImplFile(libName, importName, numDepends, depends, numFiles, files, fout_impl);
fclose(fout_init);
fclose(fout_impl);

Expand Down

0 comments on commit 41f9f50

Please sign in to comment.