[setup] init LD_LIBRARY_PATH in cx_Freeze

As it cannot be modified later in python.
This commit is contained in:
Fabien Castan 2018-08-08 16:26:07 +00:00
parent c073b766ae
commit 5bf62502f7
3 changed files with 52 additions and 11 deletions

View file

@ -27,11 +27,13 @@ def setupEnvironment():
- Meshroom/ - Meshroom/
- aliceVision/ - aliceVision/
- bin/ # runtime bundled binaries (exe + libs) - bin/ # runtime bundled binaries (windows: exe + libs, unix: executables)
- lib/ # runtime bundled libraries (unix: libs)
- share/ # resource files - share/ # resource files
- COPYING.md # AliceVision COPYING file - aliceVision/
- cameraSensors.db # sensor database - COPYING.md # AliceVision COPYING file
- vlfeat_K80L3.tree # voctree file - cameraSensors.db # sensor database
- vlfeat_K80L3.tree # voctree file
- lib/ # Python lib folder - lib/ # Python lib folder
- qtPlugins/ - qtPlugins/
Meshroom # main executable Meshroom # main executable
@ -58,10 +60,10 @@ def setupEnvironment():
paths[index:index] = val paths[index:index] = val
os.environ[var] = os.pathsep.join(paths) os.environ[var] = os.pathsep.join(paths)
# detect if this is a frozen environment based on executable name # sys.frozen is initialized by cx_Freeze
isStandalone = "python" not in os.path.basename(sys.executable).lower() isFrozen = getattr(sys, "frozen", False)
# setup root directory (override possible by setting "MESHROOM_INSTALL_DIR" environment variable) # setup root directory (override possible by setting "MESHROOM_INSTALL_DIR" environment variable)
rootDir = os.path.dirname(sys.executable) if isStandalone else os.environ.get("MESHROOM_INSTALL_DIR", None) rootDir = os.path.dirname(sys.executable) if isFrozen else os.environ.get("MESHROOM_INSTALL_DIR", None)
if rootDir: if rootDir:
os.environ["MESHROOM_INSTALL_DIR"] = rootDir os.environ["MESHROOM_INSTALL_DIR"] = rootDir
@ -69,17 +71,13 @@ def setupEnvironment():
aliceVisionDir = os.path.join(rootDir, "aliceVision") aliceVisionDir = os.path.join(rootDir, "aliceVision")
# default directories # default directories
aliceVisionBinDir = os.path.join(aliceVisionDir, "bin") aliceVisionBinDir = os.path.join(aliceVisionDir, "bin")
aliceVisionLibDirs = [os.path.join(aliceVisionDir, "lib64"), os.path.join(aliceVisionDir, "lib")] # Unix
aliceVisionShareDir = os.path.join(aliceVisionDir, "share", "aliceVision") aliceVisionShareDir = os.path.join(aliceVisionDir, "share", "aliceVision")
qtPluginsDir = os.path.join(rootDir, "qtPlugins") qtPluginsDir = os.path.join(rootDir, "qtPlugins")
sensorDBPath = os.path.join(aliceVisionShareDir, "cameraSensors.db") sensorDBPath = os.path.join(aliceVisionShareDir, "cameraSensors.db")
voctreePath = os.path.join(aliceVisionShareDir, "vlfeat_K80L3.tree") voctreePath = os.path.join(aliceVisionShareDir, "vlfeat_K80L3.tree")
# Unix: "lib" contains shared libraries that needs to be in LD_LIBRARY_PATH
libDir = os.path.join(rootDir, "lib")
env = { env = {
'PATH': aliceVisionBinDir, 'PATH': aliceVisionBinDir,
'LD_LIBRARY_PATH': [libDir] + aliceVisionLibDirs, # Unix
'QT_PLUGIN_PATH': [qtPluginsDir], 'QT_PLUGIN_PATH': [qtPluginsDir],
'QML2_IMPORT_PATH': [os.path.join(qtPluginsDir, "qml")] 'QML2_IMPORT_PATH': [os.path.join(qtPluginsDir, "qml")]
} }

View file

@ -31,6 +31,9 @@ class PlatformExecutable(Executable):
targetName += PlatformExecutable.exeExtensions[platform.system()] targetName += PlatformExecutable.exeExtensions[platform.system()]
# get icon for platform if defined # get icon for platform if defined
icon = icons.get(platform.system(), None) if icons else None icon = icons.get(platform.system(), None) if icons else None
if platform.system() in (self.Linux, self.Darwin):
currentDir = os.path.dirname(os.path.abspath(__file__))
initScript = os.path.join(currentDir, "setupInitScriptUnix.py")
super(PlatformExecutable, self).__init__(script, initScript, base, targetName, icon, shortcutName, super(PlatformExecutable, self).__init__(script, initScript, base, targetName, icon, shortcutName,
shortcutDir, copyright, trademarks) shortcutDir, copyright, trademarks)

40
setupInitScriptUnix.py Executable file
View file

@ -0,0 +1,40 @@
#------------------------------------------------------------------------------
# ConsoleSetLibPath.py
# Initialization script for cx_Freeze which manipulates the path so that the
# directory in which the executable is found is searched for extensions but
# no other directory is searched. The environment variable LD_LIBRARY_PATH is
# manipulated first, however, to ensure that shared libraries found in the
# target directory are found. This requires a restart of the executable because
# the environment variable LD_LIBRARY_PATH is only checked at startup.
#------------------------------------------------------------------------------
import os
import sys
import zipimport
FILE_NAME = sys.executable
DIR_NAME = os.path.dirname(sys.executable)
paths = os.environ.get("LD_LIBRARY_PATH", "").split(os.pathsep)
if DIR_NAME not in paths:
paths.insert(0, DIR_NAME)
paths.insert(0, os.path.join(DIR_NAME, "lib"))
paths.insert(0, os.path.join(DIR_NAME, "aliceVision", "lib"))
paths.insert(0, os.path.join(DIR_NAME, "aliceVision", "lib64"))
os.environ["LD_LIBRARY_PATH"] = os.pathsep.join(paths)
os.execv(sys.executable, sys.argv)
sys.frozen = True
sys.path = sys.path[:4]
def run():
m = __import__("__main__")
importer = zipimport.zipimporter(os.path.dirname(os.__file__))
name, ext = os.path.splitext(os.path.basename(os.path.normcase(FILE_NAME)))
moduleName = "%s__main__" % name
code = importer.get_code(moduleName)
exec(code, m.__dict__)