Skip to content

Commit

Permalink
[setup] init LD_LIBRARY_PATH in cx_Freeze
Browse files Browse the repository at this point in the history
As it cannot be modified later in python.
  • Loading branch information
fcastan committed Aug 8, 2018
1 parent c073b76 commit 5bf6250
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
20 changes: 9 additions & 11 deletions meshroom/__init__.py
Expand Up @@ -27,11 +27,13 @@ def setupEnvironment():
- Meshroom/
- 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
- COPYING.md # AliceVision COPYING file
- cameraSensors.db # sensor database
- vlfeat_K80L3.tree # voctree file
- aliceVision/
- COPYING.md # AliceVision COPYING file
- cameraSensors.db # sensor database
- vlfeat_K80L3.tree # voctree file
- lib/ # Python lib folder
- qtPlugins/
Meshroom # main executable
Expand All @@ -58,28 +60,24 @@ def addToEnvPath(var, val, index=-1):
paths[index:index] = val
os.environ[var] = os.pathsep.join(paths)

# detect if this is a frozen environment based on executable name
isStandalone = "python" not in os.path.basename(sys.executable).lower()
# sys.frozen is initialized by cx_Freeze
isFrozen = getattr(sys, "frozen", False)
# 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:
os.environ["MESHROOM_INSTALL_DIR"] = rootDir

aliceVisionDir = os.path.join(rootDir, "aliceVision")
# default directories
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")
qtPluginsDir = os.path.join(rootDir, "qtPlugins")
sensorDBPath = os.path.join(aliceVisionShareDir, "cameraSensors.db")
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 = {
'PATH': aliceVisionBinDir,
'LD_LIBRARY_PATH': [libDir] + aliceVisionLibDirs, # Unix
'QT_PLUGIN_PATH': [qtPluginsDir],
'QML2_IMPORT_PATH': [os.path.join(qtPluginsDir, "qml")]
}
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Expand Up @@ -31,6 +31,9 @@ def __init__(self, script, initScript=None, base=None, targetName=None, icons=No
targetName += PlatformExecutable.exeExtensions[platform.system()]
# get icon for platform if defined
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,
shortcutDir, copyright, trademarks)

Expand Down
40 changes: 40 additions & 0 deletions setupInitScriptUnix.py
@@ -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__)

0 comments on commit 5bf6250

Please sign in to comment.