properly disable debug information if not debug package is used
[auto-debuild.git] / auto_debuild.py
1 #!/usr/bin/python
2 import os, stat, time, subprocess, sys
3 from collections import OrderedDict
4
5 class RulesFile:
6         def __init__(self):
7                 self.env = []
8                 self.dh = []
9                 self.rules = OrderedDict()
10         
11         def write(self, f):
12                 print >>f, "#!/usr/bin/make -f"
13                 print >>f, ""
14                 print >>f, "%:"
15                 print >>f, '\t'+' '.join(self.env)+' dh $@',' '.join(self.dh)
16                 for rule in self.rules:
17                         print >>f, ""
18                         print >>f, "override_dh_"+rule+":"
19                         for line in self.rules[rule]:
20                                 print >>f, "\t"+line
21
22 def getArchitecture():
23         p = subprocess.Popen(['dpkg-architecture', '-qDEB_BUILD_ARCH'], stdout=subprocess.PIPE)
24         res = p.communicate()[0] # get only stdout
25         if p.returncode != 0: raise Exception("Querying dpkg for the architecture failed")
26         return res[0:len(res)-1] # chop of the \n at the end
27
28 # build-system specific part of rules file
29 def cmakeRules(config):
30         r = RulesFile()
31         r.dh += ["--buildsystem=cmake", "--builddirectory=build.dir"] # dh parameters: "build" is not a good idea, as that's also the name of a target...
32         r.rules['auto_configure'] = [
33                 'mkdir -p build.dir',
34                 "cd build.dir && cmake .. -DCMAKE_INSTALL_PREFIX=/usr "+' '.join(config.get('cmakeParameters', []))
35         ]
36         r.rules['auto_clean'] = ['rm -f build.dir/CMakeCache.txt'] # clean old cmake cache
37         return r
38
39 def writeDebList(list):
40         return ', '.join(list)
41
42 def createDebianFiles(config):
43         sourceName = config['sourceName']
44         binaryName = config.get('binaryName', sourceName+'-local')
45         name = config.get('name', os.getlogin())
46         email = config.get('email', os.getlogin()+'@'+os.uname()[1])
47         debDir = os.path.expanduser(config['debDir'])
48         buildSystem = config['buildSystem']
49         version = config['version']
50         dbgPackage = config.get('dbgPackage', False)
51         packageArchitecture = config.get('architecture', 'any')
52         # we return the list of files generated
53         arch = getArchitecture()
54         files = []
55         # source format file
56         if not os.path.exists('debian/source'): os.mkdir('debian/source')
57         with open('debian/source/format', 'w') as f:
58                 print >>f, "3.0 (native)"
59         # compat file
60         with open('debian/compat', 'w') as f:
61                 print >>f, "9"
62         # copyright file
63         with open('debian/copyright', 'w') as f:
64                 print >>f, "Auto-generated by auto-debuild, not suited for distribution"
65         # changelog file
66         with open('debian/changelog', 'w') as f:
67                 print >>f, sourceName,"("+version+")","UNRELEASED; urgency=low"
68                 print >>f, ""
69                 print >>f, "  * Auto-generated by auto-debuild"
70                 print >>f, ""
71                 print >>f, " --",name,"<"+email+">  "+time.strftime('%a, %d %b %Y %H:%M:%S %z')
72         # control file
73         with open('debian/control', 'w') as f:
74                 print >>f, "Source:",sourceName
75                 print >>f, "Section:",config.get('section', 'misc')
76                 print >>f, "Priority: extra"
77                 print >>f, "Maintainer: %s <%s>" % (name, email)
78                 print >>f, "Build-Depends:",writeDebList(["debhelper (>= 9)"] + config.get('buildDepends', []))
79                 print >>f, "Standards-Version: 3.9.3"
80                 print >>f, ""
81                 print >>f, "Package:",binaryName
82                 print >>f, "Architecture:",packageArchitecture
83                 print >>f, "Depends:",writeDebList(["${shlibs:Depends}", "${misc:Depends}"] + config.get('binaryDepends', []))
84                 print >>f, "Provides:",writeDebList(config.get('binaryProvides', [sourceName]))
85                 print >>f, "Description:",sourceName,"(auto-debuild)"
86                 print >>f, " Package auto-generated by auto-debuild."
87                 files.append(os.path.join(debDir, "%s_%s_%s.deb" % (binaryName, version, arch)))
88                 if dbgPackage:
89                         print >>f, ""
90                         print >>f, "Package:",binaryName+"-dbg"
91                         print >>f, "Architecture:",packageArchitecture
92                         print >>f, "Depends:",writeDebList(["${misc:Depends}", binaryName+" (= ${binary:Version})"])
93                         print >>f, "Description:",sourceName,"debug smbols (auto-debuild)"
94                         print >>f, " Package containing debug symbols for "+sourceName+", auto-generated by auto-debuild."
95                         files.append(os.path.join(debDir, "%s-dbg_%s_%s.deb" % (binaryName, version, arch)))
96         # rules file: build system specific
97         with open('debian/rules', 'w') as f:
98                 # get rule file for build system: may only touch auto_config and auto_clean rules and the dh options
99                 if buildSystem == 'cmake':
100                         r = cmakeRules(config)
101                 else:
102                         raise Exception("Invalid build system "+buildSystem)
103                 # global rules
104                 r.env += ["DEB_BUILD_OPTIONS='parallel=2'"]
105                 if not dbgPackage: r.env += ["DEB_CFLAGS_APPEND='-g0'", "DEB_CXXFLAGS_APPEND='-g0'"] # disable debug information
106                 r.dh += ['--parallel']
107                 r.rules['builddeb'] = ['dh_builddeb --destdir='+debDir] # passing this gobally to dh results in weird problems (like stuff being installed there, and not in the package...)
108                 r.rules['auto_test'] = []
109                 # for debug packages
110                 if dbgPackage:
111                         r.rules['strip'] = ['dh_strip --dbg-package='+binaryName+"-dbg"]
112                         r.rules['auto_install'] = ['dh_auto_install --destdir=debian/'+binaryName]
113                         r.rules['installdocs'] = ['dh_installdocs --link-doc='+binaryName]
114                 # dump it to a file
115                 r.write(f)
116         mode = os.stat('debian/rules').st_mode
117         os.chmod('debian/rules', mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
118         # return list of files affected
119         return files
120
121 def buildDebianPackage():
122         subprocess.check_call(['dpkg-checkbuilddeps'])
123         subprocess.check_call(['debian/rules', 'clean'])
124         subprocess.check_call(['debian/rules', 'build'])
125         subprocess.check_call(['fakeroot', 'debian/rules', 'binary'])
126         subprocess.check_call(['debian/rules', 'clean'])
127
128 # if we are called directly as script
129 if __name__ == "__main__":
130         # generate debian files
131         import imp
132         config = imp.load_source('config', 'debian/auto-debuild.conf')
133         os.remove('debian/auto-debuild.confc')
134         files = createDebianFiles(config.__dict__)
135         # check if a file is overwritten
136         for file in files:
137                 if os.path.exists(file):
138                         if raw_input("Do you want to overwrite %s (y/N)? " % file).lower() != "y":
139                                 sys.exit(1)
140         # run compilation
141         buildDebianPackage()
142         # install files
143         subprocess.check_call(['sudo', 'dpkg', '--install'] + files)