rename some options for a more consistent naming; fix auto-debuild build system
[mass-build.git] / build_system.py
1 import os, subprocess
2
3 '''A build system has three methods: "configure" (with optionak "force" parameter), "build" and "install"'''
4
5 # Compile, build, and install cmake projects:
6 class CMake:
7         def __init__(self, sourceFolder, buildFolder, module, config):
8                 self.sourceFolder = os.path.abspath(sourceFolder)
9                 self.buildFolder = os.path.abspath(buildFolder)
10                 self.installDir = config['installDir']
11                 self.buildType = config['buildType']
12                 self.jobs = config['jobs']
13                 self.buildCmdPrefix = config['buildCmdPrefix']
14                 self.installCmdPrefix = config['installCmdPrefix']
15                 self.module = module
16         
17         def build(self, reconfigure=False):
18                 # Make sure we have a build directory
19                 if not os.path.exists(self.buildFolder): os.makedirs(self.buildFolder)
20                 os.chdir(self.buildFolder)
21                 # In case of reconfiguration, delete cache file if it exists
22                 cacheFile = 'CMakeCache.txt'
23                 if os.path.exists(cacheFile) and reconfigure: os.remove(cacheFile)
24                 # Run cmake
25                 os.putenv('PKG_CONFIG_PATH', os.path.join(self.installDir, 'lib', 'pkgconfig')) # I found no way to do this within cmake
26                 subprocess.check_call(['cmake', self.sourceFolder, '-DCMAKE_BUILD_TYPE='+self.buildType,
27                         '-DCMAKE_INSTALL_PREFIX='+self.installDir]+self.module.get('cmakeParameters', []))
28                 os.unsetenv('PKG_CONFIG_PATH')
29                 # run compilation
30                 subprocess.check_call(self.buildCmdPrefix + ['make', '-j'+str(self.jobs)])
31                 # run installation
32                 subprocess.check_call(self.installCmdPrefix + ['make', 'install'])
33
34 # if auto-debuild is available, provide a wrapper for it
35 try:
36         import auto_debuild
37         class AutoDebuild:
38                 def __init__(self, sourceFolder, buildFolder, module, vcs, config):
39                         self.sourceFolder = os.path.abspath(sourceFolder)
40                         self.buildFolder = os.path.abspath(buildFolder)
41                         self.jobs = config['jobs']
42                         self.debDir = os.path.abspath(config['debDir'])
43                         self.debName = config['debName']
44                         self.debEMail = config['debEMail']
45                         self.module = module
46                         self.vcs = vcs
47
48                 def build(self, reconfigure=False): # reconfigure is ignored (we always do a reconfiguration)
49                         # create auto-debuild configuration
50                         autoDebuildConfig = {
51                                 'sourceName': self.module['name'],
52                                 'buildSystem': self.module['buildSystem'],
53                                 'debDir': self.debDir,
54                                 'buildDir': self.buildFolder,
55                                 'name': self.debName,
56                                 'email': self.debEMail,
57                                 'parallelJobs': self.jobs,
58                                 'version': self.vcs.version()
59                         }
60                         if autoDebuildConfig['version'] is None:
61                                 raise Exception("VCS did not provide us with a proper version number, please fix this")
62                         # copy some more optional configuration
63                         for option in ('dbgPackage', 'binarySkipFiles', 'binaryInstallFiles', 'buildDepends', 'binaryDepends', 'binaryRecommends',
64                                         'binaryProvides', 'cmakeParameters', 'automakeParameters'):
65                                 if option in self.module:
66                                         autoDebuildConfig[option] = self.module[option]
67                         # create Debian files
68                         os.chdir(self.sourceFolder)
69                         files = auto_debuild.createDebianFiles(autoDebuildConfig)
70                         # build package(s)
71                         auto_debuild.buildDebianPackage(autoDebuildConfig)
72                         # install package(s)
73                         subprocess.check_call(['sudo', 'dpkg', '--install'] + files)
74
75 except ImportError:
76         print "auto_debuild not found, disabling auto-debuild system"
77         pass