forward installFiles option; debian folder is no longer persistent
[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, 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.cmakeParameters = module.get('cmakeParameters', [])
16         
17         def configure(self, force=False):
18                 if not os.path.exists(self.buildFolder): os.makedirs(self.buildFolder)
19                 os.chdir(self.buildFolder)
20                 # check if we actually need to work
21                 cacheFile = 'CMakeCache.txt'
22                 if os.path.exists(cacheFile) and os.path.exists('Makefile') and not force: return
23                 # yes we do! make sure we start clean, and then go ahead
24                 if os.path.exists(cacheFile): os.remove(cacheFile)
25                 os.putenv('PKG_CONFIG_PATH', os.path.join(self.installDir, 'lib', 'pkgconfig')) # I found no way to do this within cmake
26                 subprocess.check_call(['cmake', self.sourceFolder, '-DCMAKE_BUILD_TYPE='+self.buildType,
27                         '-DCMAKE_INSTALL_PREFIX='+self.installDir]+self.cmakeParameters)
28                 os.unsetenv('PKG_CONFIG_PATH')
29         
30         def build(self):
31                 os.chdir(self.buildFolder)
32                 subprocess.check_call(self.buildCmdPrefix + ['make', '-j'+str(self.jobs)])
33         
34         def install(self):
35                 os.chdir(self.buildFolder)
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                         buildSystem = module.get('build-system', 'cmake')
45                         self.autoDebuildConfig = {
46                                 'sourceName': module['name'],
47                                 'debDir': os.path.abspath(config['debDir']),
48                                 'buildSystem': buildSystem,
49                                 'buildDir': os.path.abspath(buildFolder),
50                                 'name': config['debName'],
51                                 'email': config['debEMail'],
52                         }
53                         self.copyOption(config, 'jobs', 'parallelJobs')
54                         self.copyOption(module, 'dbgPackage')
55                         self.copyOption(module, 'skipFiles', 'binarySkipFiles')
56                         self.copyOption(module, 'installFiles', 'binaryInstall')
57                         self.copyOption(module, 'buildDepends')
58                         self.copyOption(module, 'binaryDepends')
59                         self.copyOption(module, 'binaryRecommends')
60                         if buildSystem == 'cmake':
61                                 self.copyOption(module, 'cmakeParameters')
62                         self.vcs = vcs
63                         self.configured = False # make sure configure is called before build/install
64
65                 def copyOption(self, src, name, dstName = None):
66                         if dstName is None: dstName = name # per default, stick with original name
67                         if name in src:
68                                 self.autoDebuildConfig[dstName] = src[name]
69
70                 def configure(self, force=False): # force is ignored
71                         self.autoDebuildConfig['version'] = self.vcs.version() # by now, data has been fetched, so this is possible
72                         if self.autoDebuildConfig['version'] is None:
73                                 raise Exception("VCS did not provide us with a proper version, please fix this")
74                         os.chdir(self.sourceFolder)
75                         #print self.autoDebuildConfig
76                         self.files = auto_debuild.createDebianFiles(self.autoDebuildConfig)
77                         self.configured = True
78
79                 def build(self):
80                         if not self.configured: self.configure()
81                         os.chdir(self.sourceFolder)
82                         auto_debuild.buildDebianPackage(self.autoDebuildConfig)
83
84                 def install(self):
85                         if not self.configured: self.configure()
86                         os.chdir(self.sourceFolder)
87                         subprocess.check_call(['sudo', 'dpkg', '--install'] + self.files)
88 except ImportError:
89         print "auto_debuild not found, disabling auto-debuild system"
90         pass