+import os, stat, time, subprocess, sys
+from collections import OrderedDict
+
+# abstract representation of rules file
+class RulesFile:
+ def __init__(self):
+ self.env = []
+ self.dh = []
+ self.rules = OrderedDict()
+
+ def write(self, f):
+ print >>f, "#!/usr/bin/make -f"
+ print >>f, ""
+ print >>f, "%:"
+ print >>f, '\t'+' '.join(self.env)+' dh $@',' '.join(self.dh)
+ for rule in self.rules:
+ print >>f, ""
+ print >>f, "override_dh_"+rule+":"
+ for line in self.rules[rule]:
+ print >>f, "\t"+line
+
+# build-system specific part of rules file
+def cmakeRules(config):
+ r = RulesFile()
+ r.dh += ["--buildsystem=cmake", "--builddirectory=build.dir"] # dh parameters: "build" is not a good idea, as that's also the name of a target...
+ r.rules['auto_configure'] = [
+ 'mkdir -p build.dir',
+ "cd build.dir && cmake .. -DCMAKE_INSTALL_PREFIX=/usr "+' '.join(config.get('cmakeParameters', []))
+ ]
+ r.rules['auto_clean'] = ['rm -f build.dir/CMakeCache.txt'] # clean old cmake cache
+ return r
+
+def automakeRules(config):
+ r = RulesFile()
+ r.dh += ["--buildsystem=autoconf"]
+ r.rules['auto_configure'] = [
+ '@dpkg-architecture -qDEB_BUILD_GNU_TYPE > /dev/null', # make sure this command runs successfully (and hope it does so again)
+ '@dpkg-architecture -qDEB_BUILD_MULTIARCH > /dev/null', # make sure this command runs successfully (and hope it does so again)
+ './configure --build=$$(dpkg-architecture -qDEB_BUILD_GNU_TYPE) ' +
+ '--prefix=/usr --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info ' +
+ '--libdir=/usr/lib/$$(dpkg-architecture -qDEB_BUILD_MULTIARCH) '+
+ '--libexecdir=/usr/lib/$$(dpkg-architecture -qDEB_BUILD_MULTIARCH) '+
+ '--sysconfdir=/etc --localstatedir=/var ' +
+ ' '.join(config.get('automakeParameters', []))
+ ]
+ r.rules['auto_clean'] = ['rm -f config.status'] # do not re-use old configuration
+ return r
+
+# utility functions
+def commandInBuildEnv(config, command):
+ schroot = config.get('schroot')
+ if schroot is not None: command = ['schroot', '-c', schroot, '--'] + command
+ return command
+
+def getArchitecture(config):
+ cmd = commandInBuildEnv(config, ['dpkg-architecture', '-qDEB_BUILD_ARCH'])
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
+ res = p.communicate()[0] # get only stdout
+ if p.returncode != 0: raise Exception("Querying dpkg for the architecture failed")
+ return res[0:len(res)-1] # chop of the \n at the end