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)
This commit is contained in:
looooo
2019-12-25 21:34:34 +01:00
committed by Yorik van Havre
parent fa44c7ae2f
commit b630772afc
4 changed files with 42 additions and 15 deletions

View File

@@ -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)

View File

@@ -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
)
${SITE_PACKAGE_DIR}
)

View File

@@ -1,4 +0,0 @@
import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)
import FreeCAD as app

View File

@@ -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__)