Skip to content

Instantly share code, notes, and snippets.

@renefritze
Created May 18, 2012 10:54
Show Gist options
  • Save renefritze/2724665 to your computer and use it in GitHub Desktop.
Save renefritze/2724665 to your computer and use it in GitHub Desktop.
Link order test
#!/usr/bin/env python
import sys
import os
import subprocess
try:
threads = int(os.environ['LPT'])
except:
threads = 1
libs = []
rest = []
for arg in sys.argv[1:]:
try:
if arg[0:2] == '-l':
libs.append( arg )
else:
rest.append( arg )
except Exception,e:
print(e)
import itertools
perms = [p for p in itertools.permutations(libs)]
procs = []
for i in range(len(perms)/threads + 1):
for p in range(threads):
if (i*threads + p) >= len(perms):
break
perm = perms[i*threads + p]
call = list(rest)
call.extend(perm)
procs.append(subprocess.Popen(call))
while True:
rets = [p.poll() for p in procs]
if not None in rets:
idx = rets.index(0)
if idx != -1:
perm = perms[i*threads + idx]
print ' '.join(rest) , ' '.join(perm)
print 'old: ', ' '.join(libs)
print 'new: ', ' '.join(perm)
sys.exit(0)
break
@atcl
Copy link

atcl commented May 18, 2012

About: Empircal Link Order Determination
Usage: ./lpt.py g++ -omyprog my.o compiled.o code.o -L/libs -llib1 -llib2 -L/morelibs
Output: A working link command as well as a comparision of input order of -l's and working order of -l's
Extra: As this permutation approach can take quite while (dependeing on the number of libs to link) lpt checks for the environment variable LPT and starts with as many threads.
License: zlib/libpng

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment