make the prefix options optional; fix SVN for the case that the parent folder does...
[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                 subprocess.check_call(['cmake', self.sourceFolder, '-DCMAKE_BUILD_TYPE='+self.config['buildType'],
22                         '-DCMAKE_INSTALL_PREFIX='+self.config['installDir']]+self.config.get('cmakeParameters', []))
23                 os.unsetenv('PKG_CONFIG_PATH')
24                 # if asked to do so, wait
25                 if waitAfterConfig:
26                         raw_input('Configuration done. Hit "Enter" to build the project. ')
27                 # run compilation
28                 subprocess.check_call(self.config.get('buildCmdPrefix', []) + ['make', '-j'+str(self.config['jobs'])])
29                 # run installation
30                 subprocess.check_call(self.config.get('installCmdPrefix', []) + ['make', 'install'])
31
32 # if auto-debuild is available, provide a wrapper for it
33 try:
34         import auto_debuild
35         class AutoDebuild:
36                 def __init__(self, sourceFolder, buildFolder, config, vcs):
37                         self.sourceFolder = os.path.abspath(sourceFolder)
38                         self.buildFolder = os.path.abspath(buildFolder)
39                         self.debFolder = os.path.abspath(config['debDir'])
40                         self.config = config
41                         self.vcs = vcs
42
43                 def build(self, reconfigure, waitAfterConfig): # reconfigure is ignored (we always do a reconfiguration)
44                         # create auto-debuild configuration
45                         versionName = self.config['versionName'] if 'versionName' in self.config else self.vcs.version()
46                         autoDebuildConfig = {
47                                 'sourceName': self.config['name'],
48                                 'buildSystem': self.config['buildSystem'],
49                                 'debDir': self.debFolder,
50                                 'buildDir': self.buildFolder,
51                                 'name': self.config['debName'],
52                                 'email': self.config['debEMail'],
53                                 'parallelJobs': self.config['jobs'],
54                                 'version': versionName,
55                                 'waitAfterConfig': waitAfterConfig,
56                         }
57                         if autoDebuildConfig['version'] is None:
58                                 raise Exception("VCS did not provide us with a proper version number, please fix this")
59                         # copy some more optional configuration
60                         for option in ('dbgPackage', 'section', 'withPython2', 'binarySkipFiles', 'binaryInstallFiles',
61                                         'buildDepends', 'binaryDepends', 'binaryRecommends', 'binaryProvides', 'binaryConflicts', 'binaryBreaks',
62                                         'binaryReplaces', 'binaryBreaksReplaces',
63                                         'alternatives', 'cmakeParameters', 'automakeParameters'):
64                                 if option in self.config:
65                                         autoDebuildConfig[option] = self.config[option]
66                         # create Debian files
67                         os.chdir(self.sourceFolder)
68                         if os.path.isdir('debian'): # clean previous build attempts
69                                 shutil.rmtree('debian')
70                         files = auto_debuild.createDebianFiles(autoDebuildConfig)
71                         # build package(s)
72                         auto_debuild.buildDebianPackage(autoDebuildConfig)
73                         # install package(s)
74                         if self.config.get('debInstall', True):
75                                 subprocess.check_call(['sudo', 'dpkg', '--install'] + files)
76
77 except ImportError:
78         #print "auto_debuild not found, disabling auto-debuild system"
79         pass