nixGL/nixGL
2017-09-16 14:28:57 +02:00

51 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python
import sys
import os
import subprocess
# Any rpath with this pattern is ignored
# TODO: perhaps libOpenGL.so in nix exists in other packages, we
# should extend this list
ignored = [b'mesa-noglu', b'mesa']
# These are the list of system library which are added
# TODO: extend this list for other systems
systemLibs = [b"/usr/lib", b"/lib"]
# extract command line and real path of the program
cmd = (prog, *args) = sys.argv[1:]
realProg = subprocess.check_output(["which", prog]).strip()
# extract libs deps of the program
paths = []
for line in subprocess.check_output(['ldd', realProg]).split(b'\n'):
line = line.split()
if len(line) == 4:
lib = line[2]
path = os.path.dirname(lib)
for c in ignored:
if c in path:
break
else:
paths.append(path)
# build the new environment
newenv = os.environb
oldLdLibraryPath = newenv.get(b'LD_LIBRARY_PATH', b'').split()
# The build order is IMPORTANT
# first, any LD_LIBRARY_PATH from outside world. That's the default
# behavior, LD_LIBRARY_PATH has precedance over rpath
# Then, the different directory which satisfy our rpaths
# The the system library where the openGL lib can be located
# This ensure three properties:
# a) system library (and libOpenGL.so) are used AFTER the one of nix
# b) the executible respect the LD_LIBRARY_PATH for an user viewpoint
newLibraryPath = b':'.join(oldLdLibraryPath + paths + systemLibs)
newenv.update({b'LD_LIBRARY_PATH': newLibraryPath})
os.execvpe(prog, cmd, newenv)