#! /usr/bin/env python """ Make a Cygwin distribution tarball normally invoked as ./makeCygwin.py 2>&1 | tee make.log """ # !!! VERY MINIMAL ERROR CHCKING !!! import sys from os import chdir, getcwd, remove, system from os.path import abspath from time import time, asctime from distutils.spawn import spawn from distutils.dir_util import mkpath, copy_tree, remove_tree # could be made into command line switches noisy = 1 preclean = 1 #preclean = 0 postclean = 0 # timers elapsedMake = 0 elapsedConfigure = 0 elapsedInstall = 0 # Shouldn't need to touch anything after here buildDir = "obj" installDir = "inst" home = getcwd() def main(): if preclean: cleanUp(noisy) mkpath(buildDir) makeCygwin() if postclean: cleanUp(noisy) def makeCygwin(): start = time() logSeparator("MakeCygwin") tarball = prepTarBall() configure() make() install() package(tarball) elapsed = time() - start log_elapsed_time("MakeCygwin",elapsed) def configure(): global elapsedConfigure start = time() logSeparator("configure") curDir = pushDir(buildDir) cmd = [ "%s/src/configure"%(home), "--enable-shared", "--prefix=/usr", "--target=i686-pc-cygwin", "--host=i686-pc-cygwin", "--enable-haifa", "--exec-prefix=/usr", "--libdir=/usr/lib", "--libexecdir=/usr/sbin", "--sysconfdir=/etc" ] spawn(cmd, verbose=noisy) elapsedConfigure = time() - start changeDir(curDir) def make(): global elapsedMake start = time() logSeparator("make") curDir = pushDir(buildDir) cmd = [ "make", "tooldir=/usr"] spawn(cmd, verbose=noisy) elapsedMake = time() - start changeDir(curDir) def install(): global elapsedInstall start = time() logSeparator("install") curDir = pushDir(buildDir) rootDir = "%s/%s"%(home,installDir) cmd = [ 'make', "prefix=%s/usr"%(rootDir), "exec_prefix=%s/usr"%(rootDir), "bindir=%s/usr/bin"%(rootDir), "libdir=%s/usr/lib"%(rootDir), "libexecdir=%s/usr/sbin"%(rootDir), "sysconfdir=%s/etc"%(rootDir), "includedir=%s/usr/include"%(rootDir), "install"] spawn(cmd, verbose=noisy) elapsedInstall = time() - start changeDir(curDir) def tarName(): from time import time, gmtime tmp = gmtime(time()) date = "%4d%02d%02d"%(tmp[0],tmp[1],tmp[2]) return "cygwin-%s"%(date) def prepTarBall(): tarball = tarName() logSeparator("prep for creating %s"%(tarball)) remove_file(tarball+'.tar',noisy) remove_file(tarball+'.tar.gz',noisy) return tarball def removeRedundentLibs(libDir): """ Linking with these libraries will cause multiple definition link time errors as they are included in the Cygwin DLL """ linkedLibs = ['libc.a','libg.a','libm.a'] curDir = pushDir(libDir) for lib in linkedLibs: remove_file(lib,noisy) changeDir(curDir) def makeLinkedLibs(libDir): """ Linking with these libraries will cause multiple definition link time errors as they are included in the Cygwin DLL """ curDir = pushDir(libDir) # let's just use the existing symlinks :-) # spawn(['ln','-s',"libcygwin.a", "libc.a"], verbose=noisy) # spawn(['ln','-s',"libcygwin.a", "libg.a"], verbose=noisy) # spawn(['ln','-s',"libcygwin.a", "libm.a"], verbose=noisy) changeDir(curDir) def stripFiles(binDir): curDir = pushDir(binDir) # spawn(['strip','*.exe'], verbose=noisy) system("strip *.exe") changeDir(curDir) def package(archive_name): """'normalize' the directory tree and make a tarball""" from distutils.archive_util import make_archive logSeparator("Creating Tarball") rootDir = "%s/%s"%(home,installDir) # ?? I do not know why I should have to do this ?? tempDir = "%s/usr/i686-pc-cygwin"%(rootDir) copy_tree( "%s/lib"%(tempDir), "%s/usr/lib"%(rootDir), verbose=noisy ) copy_tree( "%s/include"%(tempDir), "%s/usr/include"%(rootDir), verbose=noisy ) remove_tree(tempDir, verbose=noisy ) stripFiles("%s/usr/bin"%(rootDir)) # makeLinkedLibs("%s/lib"%(installDir)) removeRedundentLibs("%s/usr/lib"%(rootDir)) tarball = make_archive(archive_name, 'gztar', root_dir=rootDir, verbose=noisy) if noisy: logSeparator("%s created successfully"%(tarball)) def remove_file(fname, verbose=0): if verbose: print "removing file %s"%(fname) try: remove(fname) except: print "ERROR: removing file %s %s"%(getcwd(),fname) pass def cleanUp(noisy=0): logSeparator("Cleaning %s"%(home)) changeDir(home) try: remove_tree(buildDir,verbose=noisy) except: pass try: remove_tree(installDir,verbose=noisy) except: pass def logSeparator(msg=""): print print "%s %s: %s"%(5*'*',msg,asctime()) print def changeDir(dir): try: chdir(dir) except: print "directory error%s/%s"%(home,dir) raise sys.exit() def pushDir(dir): curDir = abspath(getcwd()) try: chdir(dir) except: print "directory error%s/%s"%(home,dir) raise sys.exit() return curDir def log_elapsed_time(msg, elapsed): minutes = elapsed / 60 seconds = elapsed % 60 logSeparator("%s: Time used %d min %d secs"%(msg,minutes,seconds)) ########################## if __name__ == "__main__": global elapsedConfigure,elapsedMake,elapsedInstall start = time() logSeparator("Process Started") main() log_elapsed_time("configure", elapsedConfigure) log_elapsed_time("make",elapsedMake) log_elapsed_time("install",elapsedInstall) elapsed = time() - start log_elapsed_time(__name__,elapsed)