#!/usr/bin/env python """mklibdeps.py""" import os import re import string import sys pkgdir = {} depfiles = {} libdeps = {} targets = {} arch = "" pri = "" pub = "" def initialize (): global arch, pri, pub arch = os.environ["SRT_ARCH"] arch = arch[0:6] pri = os.environ["SRT_PRIVATE_CONTEXT"] pub = os.environ["SRT_PUBLIC_CONTEXT"] if arch == "CYGWIN": pipe = os.popen("cygpath -w " + pri) pri = pipe.readline() pri = pri[0:-1] err = pipe.close() pipe = os.popen("cygpath -w " + pub) pub = pipe.readline() pub = pub[0:-1] err = pipe.close() def main(): scan_for_libdeps() read_libdeps() write_file() def scan_for_libdeps(): global depfiles global pkgdir global arch global pri global pub global targets deplist = [] pkglist = [] filelist = [] filename= "" pkg = "" filelist = [] if os.path.exists(pub): filelist = os.listdir(pub) for filename in filelist: fullname = os.path.join(pub, filename) if arch == "CYGWIN": fullname = resolve_symlink(fullname) if os.path.isdir(fullname): if os.path.exists(os.path.join(fullname, "GNUmakefile")): pkgdir[filename] = fullname filelist = [] if os.path.exists(pri): filelist = os.listdir(pri) for filename in filelist: fullname = os.path.join(pri, filename) if arch == "CYGWIN": fullname = resolve_symlink(fullname) if os.path.isdir(fullname): if os.path.exists(os.path.join(fullname, "GNUmakefile")): pkgdir[filename] = fullname pkglist = pkgdir.keys() pkglist.sort() for pkg in pkglist: targets[pkg] = 1 deplist = [] filelist = os.listdir(pkgdir[pkg]) for filename in filelist: if filename == "LIBDEPS": deplist.append(filename) elif filename[0:8] == "LIBDEPS_": deplist.append(filename) depfiles[pkg] = deplist def read_libdeps(): global pkgdir global depfiles global libdeps global targets pkglist = [] pkg = "" filename = "" file = "" line = "" pos = -1 depdict = {} deplib = "" liblist = [] library = "" pkglist = depfiles.keys() pkglist.sort() for pkg in pkglist: for filename in depfiles[pkg]: depdict = {} file = open(os.path.join(pkgdir[pkg], filename), "r") for line in file.readlines(): pos = string.find(line, "#") if pos != -1: line = line[:pos] line = string.strip(line) for deplib in string.split(line): depdict[deplib] = 1 targets[deplib] = 1 file.close() liblist = depdict.keys() liblist.sort() library = "" if filename == "LIBDEPS": library = pkg elif filename[0:8] == "LIBDEPS_": library = filename[8:] libdeps[library] = liblist targets[library] = 1 def write_file(): global targets global libdeps print ".PHONY: pkgs" print print ".PHONY:", targlist = targets.keys() targlist.sort() for targ in targlist: print targ, print print print "pkgs :", for targ in targlist: print targ, print print for targ in targlist: print targ, print ":" print '\t@echo "$@"' print liblist = libdeps.keys() liblist.sort() for library in liblist: print library + " :", for dep in libdeps[library]: print dep, print print print "%.mk :" print print "% :" print '\t@echo "WARNING: unknown library ($@)!" 1>&2' print def resolve_symlink(fullname): oldname = "" count = 0 while (count < 10) and (fullname != oldname): oldname = fullname fullname = follow_symlink(fullname) count = count + 1 return fullname def follow_symlink(fullname): global arch if os.path.isfile(fullname): file = open(fullname, "rb") tmp = file.read(10) if tmp == "!": tmp = file.read() (head, tail) = os.path.split(fullname) pipe = os.popen("cygpath -w " + os.path.join(head, tmp[:-1])) fullname = pipe.readline() fullname = fullname[0:-1] err = pipe.close() err = file.close() return fullname # (tmphead, tmptail) = os.path.split(tmp[:-1]) # pipe = os.popen("sh -c 'cd " + string.replace(head, "\\", "\\\\") + "; cd " + string.replace(tmphead, "\\", "\\\\") + "; pwd'") # tmp = pipe.readline() # err = pipe.close() # fullname = os.path.join(tmp[:-1], tmptail) # pipe = os.popen("cygpath -w " + fullname) # fullname = pipe.readline() # fullname = fullname[0:-1] # err = pipe.close() if __name__ == "__main__": # We are being run as a script. initialize () main () else: # We are being loaded as a module. initialize () pass