2 # Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 platform -- platform string (e.g. 'windows', 'linux', 'osx')
27 directory -- directory to work in; if None we will use a temporary directory
28 Temporary directories will be removed after use; specified directories will not.
30 def __init__(self, platform, directory=None):
31 self.platform = platform
32 self.parallel = int(globals.config.get('parallel'))
34 # self.directory is the working directory
36 self.directory = tempfile.mkdtemp('', 'tmp', globals.config.get('temporary_directory'))
39 self.directory = directory
42 # Environment variables that we will use when we call cscripts
46 def package(self, project, checkout):
47 tree = globals.trees.get(project, checkout, self)
49 return tree.call('package', tree.version), tree.git_commit
53 return tree.call('test')
59 del(self.variables[a])
62 return self.variables[a]
64 def append_with_space(self, k, v):
65 if (not k in self.variables) or len(self.variables[k]) == 0:
66 self.variables[k] = '"%s"' % v
69 if e[0] == '"' and e[-1] == '"':
70 self.variables[k] = '"%s %s"' % (e[1:-1], v)
72 self.variables[k] = '"%s %s"' % (e, v)
74 def variables_string(self, escaped_quotes=False):
76 for k, v in self.variables.iteritems():
78 v = v.replace('"', '\\"')
79 e += '%s=%s ' % (k, v)
84 rmtree(self.directory)
90 class WindowsTarget(Target):
91 def __init__(self, bits, directory=None):
92 super(WindowsTarget, self).__init__('windows', directory)
95 if globals.config.get('windows_environment_prefix') is None:
96 raise Error('windows prefix not configured')
97 self.windows_prefix = '%s/%d' % (globals.config.get('windows_environment_prefix'), self.bits)
98 if not os.path.exists(self.windows_prefix):
99 raise Error('windows prefix %s does not exist' % self.windows_prefix)
102 self.mingw_name = 'i686'
104 self.mingw_name = 'x86_64'
106 self.mingw_path = '%s/%d/bin' % (globals.config.get('mingw_prefix'), self.bits)
107 self.mingw_prefixes = ['/%s/%d' % (globals.config.get('mingw_prefix'), self.bits),
108 '%s/%d/%s-w64-mingw32' % (globals.config.get('mingw_prefix'), bits, self.mingw_name)]
110 self.set('PKG_CONFIG_LIBDIR', '%s/lib/pkgconfig' % self.windows_prefix)
111 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/bin/pkgconfig' % (self.directory, self.directory))
112 self.set('PATH', '%s/bin:%s:%s' % (self.windows_prefix, self.mingw_path, os.environ['PATH']))
113 self.set('CC', '%s-w64-mingw32-gcc' % self.mingw_name)
114 self.set('CXX', '%s-w64-mingw32-g++' % self.mingw_name)
115 self.set('LD', '%s-w64-mingw32-ld' % self.mingw_name)
116 self.set('RANLIB', '%s-w64-mingw32-ranlib' % self.mingw_name)
117 self.set('WINRC', '%s-w64-mingw32-windres' % self.mingw_name)
118 cxx = '-I%s/include -I%s/include' % (self.windows_prefix, self.directory)
119 link = '-L%s/lib -L%s/lib' % (self.windows_prefix, self.directory)
120 for p in self.mingw_prefixes:
121 cxx += ' -I%s/include' % p
122 link += ' -L%s/lib' % p
123 self.set('CXXFLAGS', '"%s"' % cxx)
124 self.set('CPPFLAGS', '')
125 self.set('LINKFLAGS', '"%s"' % link)
126 self.set('LDFLAGS', '"%s"' % link)
128 def command(self, c):
129 log('host -> %s' % c)
130 command('%s %s' % (self.variables_string(), c))
132 class LinuxTarget(Target):
133 """Parent for Linux targets"""
134 def __init__(self, distro, version, bits, directory=None):
135 super(LinuxTarget, self).__init__('linux', directory)
137 self.version = version
140 self.set('CXXFLAGS', '-I%s/include' % self.directory)
141 self.set('CPPFLAGS', '')
142 self.set('LINKFLAGS', '-L%s/lib' % self.directory)
143 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:/usr/local/lib/pkgconfig' % self.directory)
144 self.set('PATH', '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin')
146 class ChrootTarget(LinuxTarget):
147 """Build in a chroot"""
148 def __init__(self, distro, version, bits, directory=None):
149 super(ChrootTarget, self).__init__(distro, version, bits, directory)
150 # e.g. ubuntu-14.04-64
151 if self.version is not None and self.bits is not None:
152 self.chroot = '%s-%s-%d' % (self.distro, self.version, self.bits)
154 self.chroot = self.distro
155 # e.g. /home/carl/Environments/ubuntu-14.04-64
156 self.chroot_prefix = '%s/%s' % (globals.config.get('linux_chroot_prefix'), self.chroot)
158 def command(self, c):
159 command('%s schroot -c %s -p -- %s' % (self.variables_string(), self.chroot, c))
162 class HostTarget(LinuxTarget):
163 """Build directly on the host"""
164 def __init__(self, distro, version, bits, directory=None):
165 super(HostTarget, self).__init__(distro, version, bits, directory)
167 def command(self, c):
168 command('%s %s' % (self.variables_string(), c))
174 class OSXTarget(Target):
175 def __init__(self, directory=None):
176 super(OSXTarget, self).__init__('osx', directory)
177 self.sdk = globals.config.get('osx_sdk')
178 self.sdk_prefix = globals.config.get('osx_sdk_prefix')
180 def command(self, c):
181 command('%s %s' % (self.variables_string(False), c))
184 class OSXSingleTarget(OSXTarget):
185 def __init__(self, bits, directory=None):
186 super(OSXSingleTarget, self).__init__(directory)
194 flags = '-isysroot %s/MacOSX%s.sdk -arch %s' % (self.sdk_prefix, self.sdk, arch)
195 enviro = '%s/%d' % (globals.config.get('osx_environment_prefix'), bits)
197 # Environment variables
198 self.set('CFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, enviro, flags))
199 self.set('CPPFLAGS', '')
200 self.set('CXXFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, enviro, flags))
201 self.set('LDFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, enviro, flags))
202 self.set('LINKFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, enviro, flags))
203 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/lib/pkgconfig:/usr/lib/pkgconfig' % (self.directory, enviro))
204 self.set('PATH', '$PATH:/usr/bin:/sbin:/usr/local/bin:%s/bin' % enviro)
205 self.set('MACOSX_DEPLOYMENT_TARGET', globals.config.get('osx_sdk'))
207 def package(self, project, checkout):
208 raise Error('cannot package non-universal OS X versions')
211 class OSXUniversalTarget(OSXTarget):
212 def __init__(self, directory=None):
213 super(OSXUniversalTarget, self).__init__(directory)
215 def package(self, project, checkout):
218 target = OSXSingleTarget(b, os.path.join(self.directory, '%d' % b))
219 tree = globals.trees.get(project, checkout, target)
222 tree = globals.trees.get(project, checkout, self)
223 with TreeDirectory(tree):
224 return tree.call('package', tree.version), tree.git_commit
226 class SourceTarget(Target):
227 """Build a source .tar.bz2"""
229 super(SourceTarget, self).__init__('source')
231 def command(self, c):
232 log('host -> %s' % c)
233 command('%s %s' % (self.variables_string(), c))
236 rmtree(self.directory)
238 def package(self, project, checkout):
239 tree = globals.trees.get(project, checkout, self)
240 with TreeDirectory(tree):
241 name = read_wscript_variable(os.getcwd(), 'APPNAME')
242 command('./waf dist')
243 return os.path.abspath('%s-%s.tar.bz2' % (name, tree.version)), tree.git_commit
245 class TestTarget(Target):
247 super(TestTarget, self).__init__('test', os.path.realpath('test'))
249 # @param s Target string:
251 # or ubuntu-version-{32,64}
252 # or debian-version-{32,64}
253 # or centos-version-{32,64}
256 # @param debug True to build with debugging symbols (where possible)
257 def factory(s, debug, work):
259 if s.startswith('windows-'):
260 target = WindowsTarget(int(s.split('-')[1]), work)
261 elif s.startswith('ubuntu-') or s.startswith('debian-') or s.startswith('centos-'):
264 raise Error("Bad Linux target name `%s'; must be something like ubuntu-12.04-32 (i.e. distro-version-bits)" % s)
265 target = ChrootTarget(p[0], p[1], int(p[2]), work)
266 elif s == 'raspbian':
267 target = ChrootTarget(s, None, None, work)
270 f = open('/etc/fedora-release', 'r')
271 l = f.readline().strip().split()
272 if command_and_read('uname -m').read().strip() == 'x86_64':
276 target = HostTarget("fedora", l[2], bits, work)
277 except Exception as e:
278 raise Error("could not identify distribution for `host' target (%s)" % e)
279 elif s.startswith('osx-'):
280 target = OSXSingleTarget(int(s.split('-')[1]), work)
282 if globals.command == 'build':
283 target = OSXSingleTarget(64, work)
285 target = OSXUniversalTarget(work)
287 target = SourceTarget()
289 target = TestTarget()
292 raise Error("Bad target `%s'" % s)