+import os, stat, time, subprocess, sys
+from collections import OrderedDict
+
+class RulesFile:
+ def __init__(self):
+ self.buildOptions = ''
+ self.dh = []
+ self.rules = OrderedDict()
+
+ def write(self, f):
+ print >>f, "#!/usr/bin/make -f"
+ print >>f, ""
+ print >>f, "%:"
+ print >>f, '\tDEB_BUILD_OPTIONS="'+self.buildOptions+'" 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
+
+def getArchitecture():
+ p = subprocess.Popen(['dpkg-architecture', '-qDEB_BUILD_ARCH'], 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
+
+# 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 .. "+' '.join(['-DCMAKE_INSTALL_PREFIX=/usr', '-DCMAKE_BUILD_TYPE=Release'] + config.get('cmakeParameters', []))
+ ]
+ r.rules['auto_clean'] = ['rm -f build.dir/CMakeCache.txt'] # clean old cmake cache
+ return r