78039f19e77b649c8d876837cb66c268e206df01
[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 build(self, reconfigure=False):
18                 # Make sure we have a build directory
19                 if not os.path.exists(self.buildFolder): os.makedirs(self.buildFolder)
20                 os.chdir(self.buildFolder)
21                 # In case of reconfiguration, delete cache file if it exists
22                 cacheFile = 'CMakeCache.txt'
23                 if os.path.exists(cacheFile) and reconfigure: os.remove(cacheFile)
24                 # Run cmake
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                 # run compilation
30                 subprocess.check_call(self.buildCmdPrefix + ['make', '-j'+str(self.jobs)])
31                 # run installation
32                 subprocess.check_call(self.installCmdPrefix + ['make', 'install'])
33
34 # if auto-debuild is available, provide a wrapper for it
35 try:
36         import auto_debuild
37         class AutoDebuild:
38                 def __init__(self, sourceFolder, buildFolder, module, vcs, config):
39                         self.sourceFolder = os.path.abspath(sourceFolder)
40                         buildSystem = module.get('build-system', 'cmake')
41                         self.autoDebuildConfig = {
42                                 'sourceName': module['name'],
43                                 'debDir': os.path.abspath(config['debDir']),
44                                 'buildSystem': buildSystem,
45                                 'buildDir': os.path.abspath(buildFolder),
46                                 'name': config['debName'],
47                                 'email': config['debEMail'],
48                         }
49                         self.copyOption(config, 'jobs', 'parallelJobs')
50                         self.copyOption(module, 'dbgPackage')
51                         self.copyOption(module, 'skipFiles', 'binarySkipFiles')
52                         self.copyOption(module, 'installFiles', 'binaryInstall')
53                         self.copyOption(module, 'buildDepends')
54                         self.copyOption(module, 'binaryDepends')
55                         self.copyOption(module, 'binaryRecommends')
56                         if buildSystem == 'cmake':
57                                 self.copyOption(module, 'cmakeParameters')
58                         self.vcs = vcs
59
60                 def copyOption(self, src, name, dstName = None):
61                         if dstName is None: dstName = name # per default, stick with original name
62                         if name in src:
63                                 self.autoDebuildConfig[dstName] = src[name]
64
65                 def build(self, reconfigure=False): # reconfigure is ignored (we always do a reconfiguration)
66                         # Get us a version number
67                         self.autoDebuildConfig['version'] = self.vcs.version() # by now, data has been fetched, so this is possible
68                         if self.autoDebuildConfig['version'] is None:
69                                 raise Exception("VCS did not provide us with a proper version, please fix this")
70                         # create Debian files
71                         os.chdir(self.sourceFolder)
72                         files = auto_debuild.createDebianFiles(self.autoDebuildConfig)
73                         # build package(s)
74                         auto_debuild.buildDebianPackage(self.autoDebuildConfig)
75                         # install package(s)
76                         subprocess.check_call(['sudo', 'dpkg', '--install'] + files)
77
78 except ImportError:
79         print "auto_debuild not found, disabling auto-debuild system"
80         pass