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