9802fc4b6c528edb23013878668896d4c1b816fd
[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', 'section', 'binarySkipFiles', 'binaryInstallFiles',
64                                         'buildDepends', 'binaryDepends', 'binaryRecommends', 'binaryProvides',
65                                         'cmakeParameters', 'automakeParameters'):
66                                 if option in self.module:
67                                         autoDebuildConfig[option] = self.module[option]
68                         # create Debian files
69                         os.chdir(self.sourceFolder)
70                         files = auto_debuild.createDebianFiles(autoDebuildConfig)
71                         # build package(s)
72                         auto_debuild.buildDebianPackage(autoDebuildConfig)
73                         # install package(s)
74                         subprocess.check_call(['sudo', 'dpkg', '--install'] + files)
75
76 except ImportError:
77         print "auto_debuild not found, disabling auto-debuild system"
78         pass