+# some utility functions
+def safeCall(args):
+ res = ""
+ for arg in args:
+ assert arg.find("'") < 0 # ' is not supported
+ if len(res): res += " "
+ res += "'"+arg+"'"
+ return res
+
+# abstract representation of rules file
+class RulesFile:
+ def __init__(self):
+ self.env = {}
+ self.dh = []
+ self.rules = OrderedDict()
+
+ def env2str(self):
+ res = ""
+ for name in self.env: # we rely on the name being sane (i.e., no special characters)
+ val = self.env[name]
+ assert val.find("'") < 0 # ' is not supported
+ if len(res): res += " "
+ res += name+"='"+val+"'"
+ return res
+
+ def write(self, f):
+ print >>f, "#!/usr/bin/make -f"
+ print >>f, ""
+ print >>f, "%:"
+ print >>f, '\t'+self.env2str()+' dh $@ '+safeCall(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):
+ buildDir = config.get('buildDir', 'build.dir') # "build" is not a good idea, as that's also the name of a target...
+ srcDir = os.getcwd()
+ r = RulesFile()
+ r.dh += ["--buildsystem=cmake", "--builddirectory="+buildDir] # dh parameters
+ r.rules['auto_configure'] = [
+ safeCall(["mkdir", "-p", buildDir]),
+ safeCall(["cd", buildDir]) + " && " +
+ safeCall(["cmake", srcDir, "-DCMAKE_INSTALL_PREFIX=/usr"] + config.get('cmakeParameters', []))
+ ]
+ r.rules['auto_clean'] = [safeCall(['rm', '-f', os.path.join(buildDir, 'CMakeCache.txt')])] # clean old cmake cache
+ return r
+
+def automakeRules(config):
+ r = RulesFile()
+ r.dh += ["--buildsystem=autoconf"]
+ r.rules['auto_configure'] = [
+ 'BUILD_TYPE=$$(dpkg-architecture -qDEB_BUILD_GNU_TYPE) && ' + # doing the expansion beforehand ensures that we cancel if it fails
+ 'MULTIARCH=$$(dpkg-architecture -qDEB_BUILD_MULTIARCH) && '+
+ safeCall(['./configure', '--build=$$BUILD_TYPE',
+ '--prefix=/usr', '--includedir=/usr/include', '--mandir=/usr/share/man', '--infodir=/usr/share/info',
+ '--libdir=/usr/lib/$$MULTIARCH', '--libexecdir=/usr/lib/$$MULTIARCH',
+ '--sysconfdir=/etc', '--localstatedir=/var'] +
+ 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)