From b630772afcf01e8edb5445122ac5edbea1d458fb Mon Sep 17 00:00:00 2001 From: looooo Date: Wed, 25 Dec 2019 21:34:34 +0100 Subject: [PATCH] python: add option INSTALL_TO_SITEPACKAGES If INSTALL_TO_SITEPACKAGES is enabled, the freecad-namespace package (freecad/__init__.py) is installed in the python-sitepackage-dir. Further, the library install path (CMAKE_INSTALL_LIBDIR) is used to find the FreeCAD shared library. If, for some reason, one wants to import another installed FreeCAD version it's possible to set the environment variable "PATH_TO_FREECAD_LIBDIR" to point to the wanted FreeCAD-library (FreeCAD.so / FreeCAD.dll) --- .../InitializeFreeCADBuildOptions.cmake | 3 +- src/Ext/freecad/CMakeLists.txt | 20 ++++++------- src/Ext/freecad/__init__.py | 4 --- src/Ext/freecad/__init__.py.template | 30 +++++++++++++++++++ 4 files changed, 42 insertions(+), 15 deletions(-) delete mode 100644 src/Ext/freecad/__init__.py create mode 100644 src/Ext/freecad/__init__.py.template diff --git a/cMake/FreeCAD_Helpers/InitializeFreeCADBuildOptions.cmake b/cMake/FreeCAD_Helpers/InitializeFreeCADBuildOptions.cmake index 4d42e54755..7274639ea8 100644 --- a/cMake/FreeCAD_Helpers/InitializeFreeCADBuildOptions.cmake +++ b/cMake/FreeCAD_Helpers/InitializeFreeCADBuildOptions.cmake @@ -10,8 +10,9 @@ macro(InitializeFreeCADBuildOptions) option(FREECAD_USE_EXTERNAL_KDL "Use system installed orocos-kdl instead of the bundled." OFF) option(FREECAD_USE_FREETYPE "Builds the features using FreeType libs" ON) option(FREECAD_BUILD_DEBIAN "Prepare for a build of a Debian package" OFF) - option(BUILD_WITH_CONDA "Set ON if you build freecad with conda" OFF) + option(BUILD_WITH_CONDA "Set ON if you build FreeCAD with conda" OFF) option(BUILD_DYNAMIC_LINK_PYTHON "If OFF extension-modules do not link against python-libraries" ON) + option(INSTALL_TO_SITEPACKAGES "If ON the freecad root namespace (python) is installed into python's site-packages" OFF) option(OCCT_CMAKE_FALLBACK "disable usage of occt-config files" OFF) if (WIN32 OR APPLE) option(FREECAD_USE_QT_FILEDIALOG "Use Qt's file dialog instead of the native one." OFF) diff --git a/src/Ext/freecad/CMakeLists.txt b/src/Ext/freecad/CMakeLists.txt index 4d0420aac4..aeafc4aaf8 100644 --- a/src/Ext/freecad/CMakeLists.txt +++ b/src/Ext/freecad/CMakeLists.txt @@ -3,18 +3,18 @@ EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} -c OUTPUT_VARIABLE python_libs OUTPUT_STRIP_TRAILING_WHITESPACE ) SET(PYTHON_MAIN_DIR ${python_libs}) -SET(EXT_SRCS - __init__.py -) +set(NAMESPACE_INIT "${CMAKE_BINARY_DIR}/Ext/freecad/__init__.py") +configure_file(__init__.py.template ${NAMESPACE_INIT}) -ADD_CUSTOM_TARGET(freecad_COPY_SOURCE ALL - SOURCES ${EXT_SRCS}) - -fc_copy_sources(freecad_COPY_SOURCE "${CMAKE_BINARY_DIR}/Ext/freecad" ${EXT_SRCS}) +if (INSTALL_TO_SITEPACKAGES) + SET(SITE_PACKAGE_DIR ${PYTHON_MAIN_DIR}/freecad) +else () + SET(SITE_PACKAGE_DIR Ext/freecad) +endif() INSTALL( FILES - __init__.py + ${NAMESPACE_INIT} DESTINATION - Ext/freecad -) \ No newline at end of file + ${SITE_PACKAGE_DIR} +) diff --git a/src/Ext/freecad/__init__.py b/src/Ext/freecad/__init__.py deleted file mode 100644 index b6f98eb927..0000000000 --- a/src/Ext/freecad/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -import pkgutil -__path__ = pkgutil.extend_path(__path__, __name__) - -import FreeCAD as app \ No newline at end of file diff --git a/src/Ext/freecad/__init__.py.template b/src/Ext/freecad/__init__.py.template new file mode 100644 index 0000000000..86335f6e22 --- /dev/null +++ b/src/Ext/freecad/__init__.py.template @@ -0,0 +1,30 @@ +try: + import FreeCAD as app +except ModuleNotFoundError: + # sys.path modification has not happened yet + + import sys as _sys + import os as _os + + # 1. we check if an env-variable "PATH_TO_FREECAD_LIB" is already set + try: + _path_to_freecad_libdir = _os.environ["PATH_TO_FREECAD_LIBDIR"] + except KeyError: + + # 2. we use the default freecad defined for this package + _path_to_freecad_libdir = "${CMAKE_INSTALL_LIBDIR}" + print("PATH_TO_FREECAD_LIBDIR not specified, using default \ +FreeCAD version in {}".format( "${CMAKE_INSTALL_LIBDIR}")) + + _sys.path.append(_path_to_freecad_libdir) # this is the default version + import FreeCAD as app + +# as this is a namespace-package we need to extend the path + +#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +# TO NOT OVERWRITE THIS FILE, NO OTHER MODULE IS ALLOWED TO ! +# PROVIDE A freecad/__init__.py FILE ! +#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +from pkgutil import extend_path as _extend_path +__path__ = _extend_path(__path__, __name__)