+# a dict with some useful additional getters which can convert types and handle one-element lists like their single member
+class ConfigDict(dict):
+ def getstr(self, name, default = None):
+ if not name in self: return default
+ val = self[name]
+ if isinstance(val, list):
+ if len(val) != 1: raise Exception('%s is a list, but it should not' % name)
+ return val[0]
+ else:
+ return val
+
+ def getint(self, name, default = None):
+ return int(self.getstr(name, default))
+
+ def getbool(self, name, default = None):
+ val = self.getstr(name, default)
+ if isinstance(val, bool): return val # already a bool
+ return val.lower() in ('true', 'yes', 'on', '1')
+
+# create a safe-to-call shell command from the array
+def safeCall(*args):
+ res = ""
+ for arg in args:
+ assert arg.find("'") < 0 # ' is not supported
+ if len(res): res += " "
+ res += "'"+arg+"'"
+ return res
+
+# Load a section-less config file: maps parameter names to space-separated lists of strings (with shell quotation)
+# Lines starting with spaces are continuation lines
+def loadConfigFile(file):
+ # read config file
+ linenr = 0
+ with open(file) as file:
+ result = ConfigDict()
+ curKey = None
+ for line in file:
+ linenr += 1
+ isCont = len(line) and line[0].isspace() # remember if we were a continuation line
+ if isCont and curKey is None:
+ raise Exception("Invalid config, line %d: Starting with continuation line" % linenr)
+ line = line.strip()
+ if not len(line) or line.startswith("#"): continue # skip empty and comment lines
+ try:
+ if isCont:
+ # continuation line
+ result[curKey] += shlex.split(line)
+ else:
+ # option line
+ pos = line.index("=") # will raise exception when substring is not found
+ curKey = line[:pos].strip()
+ result[curKey] = shlex.split(line[pos+1:]) # shlex.split also strips
+ except Exception:
+ raise Exception("Invalid config, line %d: Error parsing line (quoting issue?)" % linenr)
+ # add some convencience get functions
+ return result
+
+# representation of a build system
+class BuildSystem:
+ def __init__(self, ruleMaker, buildDepends = [], binaryDepends = []):
+ self.ruleMaker = ruleMaker
+ self.buildDepends = buildDepends
+ self.binaryDepends = binaryDepends
+
+# abstract representation of rules file
+class RulesFile:
+ def __init__(self):
+ self.env = {}
+ self.dh = []
+ self.dhWith = set()
+ 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("#!/usr/bin/make -f", file=f)
+ print(file=f)
+ print(".PHONY: build", file=f) # there may be a directory called "build"
+ print(file=f)
+ print("build %:", file=f) # need to mention "build" here again explicitly so PHONY takes effect
+ # write proper dh call
+ dh = self.dh
+ if self.dhWith:
+ dh.append('--with='+','.join(self.dhWith))
+ print('\t'+self.env2str()+' dh $@ '+safeCall(*dh), file=f)
+ # write remaining rules
+ for rule in self.rules:
+ print(file=f)
+ print("override_dh_"+rule+":", file=f)
+ for line in self.rules[rule]:
+ print("\t"+line, file=f)
+
+# rule-makers
+def cmakeRules(r, config):
+ buildDir = config.getstr('buildDir', 'build')
+ srcDir = os.getcwd()
+ 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
+
+def automakeRules(r, config):
+ buildDir = config.getstr('buildDir', 'build')
+ srcDir = os.getcwd()
+ # "build" is what we are building *on*, and "host" is what we are building *for* (and GNU is weird...)
+ # also, we don't really support cross-building... ;-) (to do so, we'd have to write shell code that checks whether BUILD_GNU_TYPE
+ # and HOST_GNU_TYPE are equal, and if they are not, add a --host parameter)
+ r.dh += ["--buildsystem=autoconf", "--builddirectory="+config.getstr('buildDir')]
+ r.rules['auto_configure'] = [
+ safeCall("mkdir", "-p", buildDir),
+ safeCall("cd", buildDir) + " && " +
+ 'BUILD_TYPE=$$(dpkg-architecture -qDEB_BUILD_GNU_TYPE) && ' + # doing the expansion beforehand ensures that we cancel if it fails
+ 'MULTIARCH=$$(dpkg-architecture -qDEB_HOST_MULTIARCH) && '+
+ safeCall(srcDir+"/configure") +
+ ' --build=$$BUILD_TYPE '+ # do the escape manually here so we can use the variables (there's no user-controlled string in here anyway)
+ '--prefix=/usr --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info '+
+ '--libdir=/usr/lib/$$MULTIARCH --libexecdir=/usr/lib/$$MULTIARCH '+
+ safeCall('--docdir=/usr/share/doc/'+config['binaryName'], '--sysconfdir=/etc', '--localstatedir=/var', *config.get('automakeParameters', []))
+ ]
+ r.rules['auto_clean'] = [safeCall("rm", "-f", "--", buildDir+"/config.status")] # do not re-use old configuration
+
+def makefileRules(r, config):
+ r.dh += ["--buildsystem=makefile"]
+ r.rules['auto_configure'] = []
+
+def noneRules(r, config):
+ r.dh += ["--buildsystem=makefile"] # makefile does the least possible harm
+ r.rules['auto_configure'] = []
+ r.rules['auto_build'] = []
+ r.rules['auto_clean'] = []
+ return r
+
+# build systems
+buildSystems = {
+ 'cmake': BuildSystem(cmakeRules, ["cmake"]),
+ 'automake': BuildSystem(automakeRules),
+ 'makefile': BuildSystem(makefileRules),
+ 'none': BuildSystem(noneRules),
+}
+
+# utility functions
+def commandInBuildEnv(config, command):
+ schroot = config.getstr('schroot')
+ if schroot is not None: command = ['schroot', '-c', schroot, '--'] + command
+ return command
+
+def getArchitecture(config):
+ cmd = commandInBuildEnv(config, ['dpkg-architecture', '-qDEB_HOST_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
+
+def writeDependency(f, name, list):
+ if len(list):
+ print(name+": "+', '.join(list), file=f)
+
+def deleteDebianFolder():
+ if os.path.islink('debian'):
+ target = os.readlink('debian')
+ if os.path.exists(target):
+ shutil.rmtree(target)
+ os.remove('debian')
+ else:
+ shutil.rmtree('debian')
+
+# actual work functions