971cf7b17ecffaff0293b32997a998bbbba4a6c4
[mass-build.git] / build_system.py
1 import os, shutil, subprocess
2
3 '''A build system must have a "build" method with parameters "reconfigure" and "waitAfterConfig".'''
4
5 # Compile, build, and install cmake projects:
6 class CMake:
7         def __init__(self, sourceFolder, buildFolder, config):
8                 self.sourceFolder = os.path.abspath(sourceFolder)
9                 self.buildFolder = os.path.abspath(buildFolder)
10                 self.config = config
11         
12         def build(self, reconfigure, waitAfterConfig):
13                 # Make sure we have a build directory
14                 if not os.path.exists(self.buildFolder): os.makedirs(self.buildFolder)
15                 os.chdir(self.buildFolder)
16                 # In case of reconfiguration, delete cache file if it exists
17                 cacheFile = 'CMakeCache.txt'
18                 if os.path.exists(cacheFile) and reconfigure: os.remove(cacheFile)
19                 # Run cmake
20                 os.putenv('PKG_CONFIG_PATH', os.path.join(self.config['installDir'], 'lib', 'pkgconfig')) # I found no way to do this within cmake
21                 os.putenv('CMAKE_PREFIX_PATH', self.config['installDir'])
22                 subprocess.check_call(['cmake', self.sourceFolder, '-DCMAKE_BUILD_TYPE='+self.config['buildType'],
23                         '-DCMAKE_INSTALL_PREFIX='+self.config['installDir']]+self.config.get('cmakeParameters', []))
24                 os.unsetenv('PKG_CONFIG_PATH')
25                 os.unsetenv('CMAKE_PREFIX_PATH')
26                 # if asked to do so, wait
27                 if waitAfterConfig:
28                         raw_input('Configuration done. Hit "Enter" to build the project. ')
29                 # run compilation
30                 subprocess.check_call(self.config.get('buildCmdPrefix', []) + ['make', '-j'+str(self.config['jobs'])])
31                 # run installation
32                 subprocess.check_call(self.config.get('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, config, vcs):
39                         self.sourceFolder = os.path.abspath(sourceFolder)
40                         self.buildFolder = os.path.abspath(buildFolder)
41                         self.debFolder = os.path.abspath(config['debDir'])
42                         self.config = config
43                         self.vcs = vcs
44
45                 def build(self, reconfigure, waitAfterConfig): # reconfigure is ignored (we always do a reconfiguration)
46                         # create auto-debuild configuration
47                         versionName = self.config['versionName'] if 'versionName' in self.config else self.vcs.version()
48                         autoDebuildConfig = {
49                                 'sourceName': self.config['name'],
50                                 'buildSystem': self.config['buildSystem'],
51                                 'debDir': self.debFolder,
52                                 'buildDir': self.buildFolder,
53                                 'name': self.config['debName'],
54                                 'email': self.config['debEMail'],
55                                 'parallelJobs': self.config['jobs'],
56                                 'version': versionName,
57                                 'waitAfterConfig': waitAfterConfig,
58                         }
59                         if autoDebuildConfig['version'] is None:
60                                 raise Exception("VCS did not provide us with a proper version number, please fix this")
61                         # copy some more optional configuration
62                         for option in ('dbgPackage', 'section', 'withPython2', 'binarySkipFiles', 'binaryInstallFiles',
63                                         'buildDepends', 'binaryDepends', 'binaryRecommends', 'binaryProvides', 'binaryConflicts', 'binaryBreaks',
64                                         'binaryReplaces', 'binaryBreaksReplaces',
65                                         'alternatives', 'cmakeParameters', 'automakeParameters'):
66                                 if option in self.config:
67                                         autoDebuildConfig[option] = self.config[option]
68                         # create Debian files
69                         os.chdir(self.sourceFolder)
70                         if os.path.isdir('debian'): # clean previous build attempts
71                                 shutil.rmtree('debian')
72                         files = auto_debuild.createDebianFiles(autoDebuildConfig)
73                         # build package(s)
74                         auto_debuild.buildDebianPackage(autoDebuildConfig)
75                         # install package(s)
76                         if self.config.get('debInstall', True):
77                                 subprocess.check_call(['sudo', 'dpkg', '--install'] + files)
78
79 except ImportError:
80         #print "auto_debuild not found, disabling auto-debuild system"
81         pass