5634f9dd1be6e10ce87ae56274f40686d2c234f7
[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, 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         
16         def configure(self, force=False):
17                 if not os.path.exists(self.buildFolder): os.makedirs(self.buildFolder)
18                 os.chdir(self.buildFolder)
19                 # check if we actually need to work
20                 cacheFile = 'CMakeCache.txt'
21                 if os.path.exists(cacheFile) and os.path.exists('Makefile') and not force: return
22                 # yes we do! make sure we start clean, and then go ahead
23                 if os.path.exists(cacheFile): os.remove(cacheFile)
24                 os.putenv('PKG_CONFIG_PATH', os.path.join(self.installDir, 'lib', 'pkgconfig')) # I found no way to do this within cmake
25                 subprocess.check_call(['cmake', self.sourceFolder, '-DCMAKE_BUILD_TYPE='+self.buildType, '-DCMAKE_INSTALL_PREFIX='+self.installDir])
26                 os.unsetenv('PKG_CONFIG_PATH')
27         
28         def build(self):
29                 os.chdir(self.buildFolder)
30                 subprocess.check_call(self.buildCmdPrefix + ['make', '-j'+str(self.jobs)])
31         
32         def install(self):
33                 os.chdir(self.buildFolder)
34                 subprocess.check_call(self.installCmdPrefix + ['make', 'install'])
35
36 # if auto-debuild is available, provide a wrapper for it
37 try:
38         import auto_debuild
39         class AutoDebuild:
40                 def __init__(self, sourceFolder, module, vcs, config):
41                         self.sourceFolder = os.path.abspath(sourceFolder)
42                         self.autoDebuildConfig = {
43                                 'sourceName': module['name'],
44                                 'debDir': os.path.abspath(config['debDir']),
45                                 'buildSystem': module.get('build-system', 'cmake'),
46                         }
47                         self.vcs = vcs
48                         self.configured = False # make sure configure is called before build/install
49
50                 def configure(self, force=False): # force is ignored
51                         self.autoDebuildConfig['version'] = self.vcs.version() # by now, data has been fetched, so this is possible
52                         os.chdir(self.sourceFolder)
53                         print self.sourceFolder,self.autoDebuildConfig
54                         self.files = auto_debuild.createDebianFiles(self.autoDebuildConfig)
55                         self.configured = True
56
57                 def build(self):
58                         if not self.configured: self.configure()
59                         os.chdir(self.sourceFolder)
60                         auto_debuild.buildDebianPackage(self.autoDebuildConfig)
61
62                 def install(self):
63                         if not self.configured: self.configure()
64                         os.chdir(self.sourceFolder)
65                         subprocess.check_call(['sudo', 'dpkg', '--install'] + self.files)
66 except ImportError:
67         print "auto_debuild not found, disabling auto-debuild system"
68         pass