make mass-build GPLv2+
[mass-build.git] / build_system.py
1 # mass-build - Easily Build Software Involving a Large Amount of Source Repositories
2 # Copyright (C) 2012 Ralf Jung <post@ralfj.de>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 import os, shutil, subprocess
19
20 '''A build system must have a "build" method with parameters "reconfigure" and "waitAfterConfig".'''
21
22 # Compile, build, and install cmake projects:
23 class CMake:
24         def __init__(self, sourceFolder, buildFolder, config):
25                 self.sourceFolder = os.path.abspath(sourceFolder)
26                 self.buildFolder = os.path.abspath(buildFolder)
27                 self.config = config
28         
29         def setEnv(self, name, val):
30                 '''Set the given environment variable, return old value'''
31                 oldVal = os.getenv(name)
32                 os.putenv(name, val)
33                 return oldVal
34         
35         def prependDirToEnv(self, name, dir, default):
36                 '''Prepends the given directory to the environment variable. If the variable is empty, dir isprepended to the default.
37                    Returns the old value.'''
38                 oldVal = os.getenv(name)
39                 oldPaths = default if oldVal is None else oldVal
40                 os.putenv(name, dir+':'+oldPaths)
41                 return oldVal
42         
43         def restoreEnv(self, name, oldVal):
44                 '''Restore environment variable to previous value'''
45                 if oldVal is None:
46                         os.unsetenv(name)
47                 else:
48                         os.putenv(name, oldVal)
49         
50         def build(self, reconfigure, waitAfterConfig):
51                 # Make sure we have a build directory
52                 if not os.path.exists(self.buildFolder): os.makedirs(self.buildFolder)
53                 os.chdir(self.buildFolder)
54                 # In case of reconfiguration, delete cache file if it exists
55                 cacheFile = 'CMakeCache.txt'
56                 if os.path.exists(cacheFile) and reconfigure: os.remove(cacheFile)
57                 # Run cmake, in the proper environment, then restore old environment
58                 oldPKGConfigPath = self.setEnv('PKG_CONFIG_PATH', os.path.join(self.config['installDir'], 'lib', 'pkgconfig'))
59                 oldCMakePrefixPath = self.setEnv('CMAKE_PREFIX_PATH', self.config['installDir'])
60                 oldXDGDataDirs = self.prependDirToEnv('XDG_DATA_DIRS', os.path.join(self.config['installDir'], 'share'), '/usr/share')
61                 oldXDGConfigDirs = self.prependDirToEnv('XDG_CONFIG_DIRS', os.path.join(self.config['installDir'], 'etc', 'xdg'), '/etc/xdg')
62                 subprocess.check_call(['cmake', self.sourceFolder, '-DCMAKE_BUILD_TYPE='+self.config['buildType'],
63                         '-DCMAKE_INSTALL_PREFIX='+self.config['installDir']]+self.config.get('cmakeParameters', []))
64                 self.restoreEnv('PKG_CONFIG_PATH', oldPKGConfigPath)
65                 self.restoreEnv('CMAKE_PREFIX_PATH', oldCMakePrefixPath)
66                 self.restoreEnv('XDG_DATA_DIRS', oldXDGDataDirs)
67                 self.restoreEnv('XDG_CONFIG_DIRS', oldXDGConfigDirs)
68                 # if asked to do so, wait
69                 if waitAfterConfig:
70                         raw_input('Configuration done. Hit "Enter" to build the project. ')
71                 # run compilation
72                 subprocess.check_call(self.config.get('buildCmdPrefix', []) + ['make', '-j'+str(self.config['jobs'])])
73                 # run installation
74                 subprocess.check_call(self.config.get('installCmdPrefix', []) + ['make', 'install'])
75
76 # if auto-debuild is available, provide a wrapper for it
77 try:
78         import auto_debuild
79         class AutoDebuild:
80                 def __init__(self, sourceFolder, buildFolder, config, vcs):
81                         self.sourceFolder = os.path.abspath(sourceFolder)
82                         self.buildFolder = os.path.abspath(buildFolder)
83                         self.debFolder = os.path.abspath(config['debDir'])
84                         self.config = config
85                         self.vcs = vcs
86
87                 def build(self, reconfigure, waitAfterConfig): # reconfigure is ignored (we always do a reconfiguration)
88                         # create auto-debuild configuration
89                         versionName = self.config['versionName'] if 'versionName' in self.config else self.vcs.version()
90                         autoDebuildConfig = {
91                                 'sourceName': self.config['name'],
92                                 'buildSystem': self.config['buildSystem'],
93                                 'debDir': self.debFolder,
94                                 'buildDir': self.buildFolder,
95                                 'name': self.config['debName'],
96                                 'email': self.config['debEMail'],
97                                 'parallelJobs': self.config['jobs'],
98                                 'version': versionName,
99                                 'waitAfterConfig': waitAfterConfig,
100                         }
101                         if autoDebuildConfig['version'] is None:
102                                 raise Exception("VCS did not provide us with a proper version number, please fix this")
103                         # copy some more optional configuration
104                         for option in ('dbgPackage', 'section', 'withPython2', 'binarySkipFiles', 'binaryInstallFiles',
105                                         'buildDepends', 'binaryDepends', 'binaryRecommends', 'binaryProvides', 'binaryConflicts', 'binaryBreaks',
106                                         'binaryReplaces', 'binaryBreaksReplaces',
107                                         'alternatives', 'cmakeParameters', 'automakeParameters'):
108                                 if option in self.config:
109                                         autoDebuildConfig[option] = self.config[option]
110                         # create Debian files
111                         os.chdir(self.sourceFolder)
112                         if os.path.isdir('debian'): # clean previous build attempts
113                                 shutil.rmtree('debian')
114                         files = auto_debuild.createDebianFiles(autoDebuildConfig)
115                         # build package(s)
116                         auto_debuild.buildDebianPackage(autoDebuildConfig)
117                         # install package(s)
118                         if self.config.get('debInstall', True):
119                                 subprocess.check_call(['sudo', 'dpkg', '--install'] + files)
120
121 except ImportError:
122         #print "auto_debuild not found, disabling auto-debuild system"
123         pass