14877d41562339beda7b861dc20288aede1a4a6e
[mass-build.git] / build_system.py
1 import os, shutil, 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.waitAfterConfig = config.get('waitAfterConfig', False)
16                 self.module = module
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.module.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 module. ')
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, module, vcs, config):
43                         self.sourceFolder = os.path.abspath(sourceFolder)
44                         self.buildFolder = os.path.abspath(buildFolder)
45                         self.jobs = config['jobs']
46                         self.debDir = os.path.abspath(config['debDir'])
47                         self.debName = config['debName']
48                         self.debEMail = config['debEMail']
49                         self.waitAfterConfig = config.get('waitAfterConfig', False)
50                         self.module = module
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.module['name'],
57                                 'buildSystem': self.module['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.module:
73                                         autoDebuildConfig[option] = self.module[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