3 # Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 class Error(Exception):
32 def __init__(self, value):
35 return '\x1b[31m%s\x1b[0m' % repr(self.value)
45 self.keys = ['linux_dir_in_chroot',
46 'linux_chroot_prefix',
47 'windows_environment_prefix',
52 'osx_environment_prefix',
59 f = open('%s/.config/cdist' % os.path.expanduser('~'), 'r')
65 if len(l) > 0 and l[0] == '#':
80 raise Error('Required setting %s not found' % k)
90 print '\x1b[33m* %s\x1b[0m' % m
93 log('copy %s -> %s' % (a, b))
97 log('copy %s -> %s' % (a, b))
106 shutil.rmtree(a, ignore_errors=True)
108 def command(c, can_fail=False):
111 if (r >> 8) and not can_fail:
112 raise Error('command %s failed' % c)
114 def command_and_read(c):
116 p = subprocess.Popen(c.split(), stdout=subprocess.PIPE)
117 f = os.fdopen(os.dup(p.stdout.fileno()))
120 def read_wscript_variable(directory, variable):
121 f = open('%s/wscript' % directory, 'r')
128 if len(s) == 3 and s[0] == variable:
140 def __init__(self, s):
143 if s.startswith("'"):
148 if s.endswith('devel'):
152 if s.endswith('pre'):
156 self.major = int(p[0])
157 self.minor = int(p[1])
159 self.micro = int(p[2])
163 def bump_minor(self):
167 def bump_micro(self):
173 def to_release(self):
177 s = '%d.%02d.%d' % (self.major, self.minor, self.micro)
187 class Target(object):
188 def __init__(self, platform, parallel):
189 self.platform = platform
190 self.parallel = parallel
191 # Environment variables that we will use when we call cscripts
195 def build_dependencies(self, project):
197 if 'dependencies' in project.cscript:
198 print project.cscript['dependencies'](self)
199 for d in project.cscript['dependencies'](self):
200 log('Building dependency %s %s of %s' % (d[0], d[1], project.name))
201 dep = Project(d[0], '.', d[1])
203 self.build_dependencies(dep)
205 # Make the options to pass in from the option_defaults of the thing
206 # we are building and any options specified by the parent.
208 if 'option_defaults' in dep.cscript:
209 options = dep.cscript['option_defaults']()
211 for k, v in d[2].iteritems():
214 self.build(dep, options)
218 def build(self, project, options=None):
219 variables = copy.copy(self.variables)
220 print 'Target %s builds %s with %s' % (self.platform, project.name, self.variables)
221 if len(inspect.getargspec(project.cscript['build']).args) == 2:
222 project.cscript['build'](self, options)
224 project.cscript['build'](self)
225 self.variables = variables
227 def package(self, project):
228 project.checkout(self)
229 self.build_dependencies(project)
231 return project.cscript['package'](self, project.version)
233 def test(self, project):
234 project.checkout(self)
235 self.build_dependencies(project)
237 project.cscript['test'](self)
240 print "Target set %s=%s" % (a, b)
241 self.variables[a] = b
244 del(self.variables[a])
247 return self.variables[a]
249 def append_with_space(self, k, v):
250 if not k in self.variables:
251 self.variables[k] = v
253 self.variables[k] = '%s %s' % (self.variables[k], v)
255 def variables_string(self, escaped_quotes=False):
257 for k, v in self.variables.iteritems():
259 v = v.replace('"', '\\"')
260 e += '%s=%s ' % (k, v)
270 class WindowsTarget(Target):
271 # @param directory directory to work in; if None, we will use a temporary directory
272 def __init__(self, bits, directory=None):
273 super(WindowsTarget, self).__init__('windows', 2)
275 if directory is None:
276 self.directory = tempfile.mkdtemp()
279 self.directory = directory
282 self.windows_prefix = '%s/%d' % (config.get('windows_environment_prefix'), self.bits)
283 if not os.path.exists(self.windows_prefix):
284 raise Error('windows prefix %s does not exist' % self.windows_prefix)
287 self.mingw_name = 'i686'
289 self.mingw_name = 'x86_64'
291 mingw_path = '%s/%d/bin' % (config.get('mingw_prefix'), self.bits)
292 self.mingw_prefixes = ['/%s/%d' % (config.get('mingw_prefix'), self.bits), '%s/%d/%s-w64-mingw32' % (config.get('mingw_prefix'), bits, self.mingw_name)]
294 self.set('PKG_CONFIG_LIBDIR', '%s/lib/pkgconfig' % self.windows_prefix)
295 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/bin/pkgconfig' % (self.work_dir_cscript(), self.work_dir_cscript()))
296 self.set('PATH', '%s/bin:%s:%s' % (self.windows_prefix, mingw_path, os.environ['PATH']))
297 self.set('CC', '%s-w64-mingw32-gcc' % self.mingw_name)
298 self.set('CXX', '%s-w64-mingw32-g++' % self.mingw_name)
299 self.set('LD', '%s-w64-mingw32-ld' % self.mingw_name)
300 self.set('RANLIB', '%s-w64-mingw32-ranlib' % self.mingw_name)
301 self.set('WINRC', '%s-w64-mingw32-windres' % self.mingw_name)
302 cxx = '-I%s/include -I%s/include' % (self.windows_prefix, self.work_dir_cscript())
303 link = '-L%s/lib -L%s/lib' % (self.windows_prefix, self.work_dir_cscript())
304 for p in self.mingw_prefixes:
305 cxx += ' -I%s/include' % p
306 link += ' -L%s/lib' % p
307 self.set('CXXFLAGS', '"%s"' % cxx)
308 self.set('LINKFLAGS', '"%s"' % link)
310 def work_dir_cdist(self):
311 return '%s/%d' % (self.directory, self.bits)
313 def work_dir_cscript(self):
314 return '%s/%d' % (self.directory, self.bits)
316 def command(self, c):
317 log('host -> %s' % c)
318 command('%s %s' % (self.variables_string(), c))
322 rmtree(self.directory)
328 class LinuxTarget(Target):
329 def __init__(self, distro, version, bits, directory=None):
330 "directory -- directory to work in; if None, we will use the configured linux_dir_in_chroot"
331 super(LinuxTarget, self).__init__('linux', 2)
333 self.version = version
335 self.chroot = '%s-%s-%d' % (self.distro, self.version, self.bits)
336 if directory is None:
337 self.dir_in_chroot = config.get('linux_dir_in_chroot')
339 self.dir_in_chroot = directory
341 for g in glob.glob('%s/*' % self.work_dir_cdist()):
344 self.set('CXXFLAGS', '-I%s/include' % self.work_dir_cscript())
345 self.set('LINKFLAGS', '-L%s/lib' % self.work_dir_cscript())
346 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:/usr/local/lib/pkgconfig' % self.work_dir_cscript())
347 self.set('PATH', '%s:/usr/local/bin' % (os.environ['PATH']))
349 def work_dir_cdist(self):
350 return '%s/%s%s' % (config.get('linux_chroot_prefix'), self.chroot, self.dir_in_chroot)
352 def work_dir_cscript(self):
353 return self.dir_in_chroot
355 def command(self, c):
356 # Work out the cwd for the chrooted command
358 prefix = '%s/%s' % (config.get('linux_chroot_prefix'), self.chroot)
359 assert(cwd.startswith(prefix))
360 cwd = cwd[len(prefix):]
362 log('schroot [%s] -> %s' % (cwd, c))
363 command('%s schroot -c %s -d %s -p -- %s' % (self.variables_string(), self.chroot, cwd, c))
366 for g in glob.glob('%s/*' % self.work_dir_cdist()):
373 class OSXTarget(Target):
374 def __init__(self, directory=None):
375 "directory -- directory to work in; if None, we will use the configured osx_dir_in_host"
376 super(OSXTarget, self).__init__('osx', 4)
378 if directory is None:
379 self.dir_in_host = config.get('osx_dir_in_host')
381 self.dir_in_host = directory
383 for g in glob.glob('%s/*' % self.dir_in_host):
386 def command(self, c):
387 command('%s %s' % (self.variables_string(False), c))
390 class OSXSingleTarget(OSXTarget):
391 def __init__(self, bits, directory=None):
392 super(OSXSingleTarget, self).__init__(directory)
400 flags = '-isysroot %s/MacOSX%s.sdk -arch %s' % (config.get('osx_sdk_prefix'), config.get('osx_sdk'), arch)
401 enviro = '%s/%d' % (config.get('osx_environment_prefix'), bits)
403 # Environment variables
404 self.set('CFLAGS', '"-I%s/include -I%s/include %s"' % (self.work_dir_cscript(), enviro, flags))
405 self.set('CXXFLAGS', '"-I%s/include -I%s/include %s"' % (self.work_dir_cscript(), enviro, flags))
406 self.set('LDFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.work_dir_cscript(), enviro, flags))
407 self.set('LINKFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.work_dir_cscript(), enviro, flags))
408 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/lib/pkgconfig:/usr/lib/pkgconfig' % (self.work_dir_cscript(), enviro))
409 self.set('PATH', '$PATH:/usr/bin:/sbin:/usr/local/bin:%s/bin' % enviro)
410 self.set('MACOSX_DEPLOYMENT_TARGET', config.get('osx_sdk'))
412 def work_dir_cdist(self):
413 return self.work_dir_cscript()
415 def work_dir_cscript(self):
416 return '%s/%d' % (self.dir_in_host, self.bits)
418 def package(self, project):
419 raise Error('cannot package non-universal OS X versions')
422 class OSXUniversalTarget(OSXTarget):
423 def __init__(self, directory=None):
424 super(OSXUniversalTarget, self).__init__(directory)
426 self.parts.append(OSXSingleTarget(32, directory))
427 self.parts.append(OSXSingleTarget(64, directory))
429 def work_dir_cscript(self):
430 return self.dir_in_host
432 def package(self, project):
435 p.build_dependencies(project)
438 return project.cscript['package'](self, project.version)
445 class SourceTarget(Target):
447 super(SourceTarget, self).__init__('source', 2)
448 self.directory = tempfile.mkdtemp()
450 def work_dir_cdist(self):
451 return self.directory
453 def work_dir_cscript(self):
454 return self.directory
456 def command(self, c):
457 log('host -> %s' % c)
458 command('%s %s' % (self.variables_string(), c))
461 rmtree(self.directory)
463 def package(self, project):
464 project.checkout(self)
465 name = read_wscript_variable(os.getcwd(), 'APPNAME')
466 command('./waf dist')
467 return os.path.abspath('%s-%s.tar.bz2' % (name, project.version))
470 # @param s Target string:
472 # or ubuntu-version-{32,64}
473 # or debian-version-{32,64}
474 # or centos-version-{32,64}
477 # @param debug True to build with debugging symbols (where possible)
478 def target_factory(s, debug, work):
480 if s.startswith('windows-'):
481 target = WindowsTarget(int(s.split('-')[1]), work)
482 elif s.startswith('ubuntu-') or s.startswith('debian-') or s.startswith('centos-'):
485 print >>sys.stderr,"Bad Linux target name `%s'; must be something like ubuntu-12.04-32 (i.e. distro-version-bits)" % s
487 target = LinuxTarget(p[0], p[1], int(p[2]), work)
488 elif s.startswith('osx-'):
489 target = OSXSingleTarget(int(s.split('-')[1]), work)
491 if args.command == 'build':
492 target = OSXSingleTarget(64, work)
494 target = OSXUniversalTarget(work)
496 target = SourceTarget()
498 if target is not None:
508 class Project(object):
509 def __init__(self, name, directory, specifier=None):
511 self.directory = directory
513 self.specifier = specifier
514 if self.specifier is None:
515 self.specifier = 'master'
517 def checkout(self, target):
522 redirect = '>/dev/null'
523 command('git clone %s %s/%s.git %s/src/%s' % (flags, config.get('git_prefix'), self.name, target.work_dir_cdist(), self.name))
524 os.chdir('%s/src/%s' % (target.work_dir_cdist(), self.name))
525 command('git checkout %s %s %s' % (flags, self.specifier, redirect))
526 command('git submodule init')
527 command('git submodule update')
528 os.chdir(self.directory)
530 proj = '%s/src/%s/%s' % (target.work_dir_cdist(), self.name, self.directory)
532 self.read_cscript('%s/cscript' % proj)
534 if os.path.exists('%s/wscript' % proj):
535 v = read_wscript_variable(proj, "VERSION");
537 self.version = Version(v)
539 def read_cscript(self, s):
541 execfile(s, self.cscript)
543 def set_version_in_wscript(version):
544 f = open('wscript', 'rw')
545 o = open('wscript.tmp', 'w')
552 if len(s) == 3 and s[0] == "VERSION":
553 print "Writing %s" % version
554 print >>o,"VERSION = '%s'" % version
560 os.rename('wscript.tmp', 'wscript')
562 def append_version_to_changelog(version):
564 f = open('ChangeLog', 'r')
566 log('Could not open ChangeLog')
572 f = open('ChangeLog', 'w')
573 now = datetime.datetime.now()
574 f.write('%d-%02d-%02d Carl Hetherington <cth@carlh.net>\n\n\t* Version %s released.\n\n' % (now.year, now.month, now.day, version))
577 def append_version_to_debian_changelog(version):
578 if not os.path.exists('debian'):
579 log('Could not find debian directory')
582 command('dch -b -v %s-1 "New upstream release."' % version)
585 # Command-line parser
588 parser = argparse.ArgumentParser()
589 parser.add_argument('command')
590 parser.add_argument('-p', '--project', help='project name')
591 parser.add_argument('-d', '--directory', help='directory within project repo', default='.')
592 parser.add_argument('--minor', help='minor version number bump', action='store_true')
593 parser.add_argument('--micro', help='micro version number bump', action='store_true')
594 parser.add_argument('-c', '--checkout', help='string to pass to git for checkout')
595 parser.add_argument('-o', '--output', help='output directory', default='.')
596 parser.add_argument('-q', '--quiet', help='be quiet', action='store_true')
597 parser.add_argument('-t', '--target', help='target')
598 parser.add_argument('-k', '--keep', help='keep working tree', action='store_true')
599 parser.add_argument('--debug', help='build with debugging symbols where possible', action='store_true')
600 parser.add_argument('-w', '--work', help='override default work directory')
601 args = parser.parse_args()
603 args.output = os.path.abspath(args.output)
604 if args.work is not None:
605 args.work = os.path.abspath(args.work)
607 if args.project is None and args.command != 'shell':
608 raise Error('you must specify -p or --project')
610 project = Project(args.project, args.directory, args.checkout)
612 if args.command == 'build':
613 if args.target is None:
614 raise Error('you must specify -t or --target')
616 target = target_factory(args.target, args.debug, args.work)
617 project.checkout(target)
618 target.build_dependencies(project)
619 target.build(project)
623 elif args.command == 'package':
624 if args.target is None:
625 raise Error('you must specify -t or --target')
627 target = target_factory(args.target, args.debug, args.work)
629 packages = target.package(project)
630 if hasattr(packages, 'strip') or (not hasattr(packages, '__getitem__') and not hasattr(packages, '__iter__')):
631 packages = [packages]
633 if target.platform == 'linux':
634 out = '%s/%s-%s-%d' % (args.output, target.distro, target.version, target.bits)
640 copyfile(p, '%s/%s' % (out, os.path.basename(p)))
643 copyfile(p, '%s/%s' % (args.output, os.path.basename(p)))
648 elif args.command == 'release':
649 if args.minor is False and args.micro is False:
650 raise Error('you must specify --minor or --micro')
652 target = SourceTarget()
653 project.checkout(target)
655 version = project.version
662 set_version_in_wscript(version)
663 append_version_to_changelog(version)
664 append_version_to_debian_changelog(version)
666 command('git commit -a -m "Bump version"')
667 command('git tag -m "v%s" v%s' % (version, version))
670 set_version_in_wscript(version)
671 command('git commit -a -m "Bump version"')
673 command('git push --tags')
677 elif args.command == 'pot':
678 target = SourceTarget()
679 project.checkout(target)
681 pots = project.cscript['make_pot'](target)
683 copyfile(p, '%s/%s' % (args.output, os.path.basename(p)))
687 elif args.command == 'changelog':
688 target = SourceTarget()
689 project.checkout(target)
691 text = open('ChangeLog', 'r')
692 html = open('%s/changelog.html' % args.output, 'w')
703 if len(l) > 0 and l[0] == "\t":
705 if len(s) == 4 and s[1] == "Version" and s[3] == "released.":
708 if last is not None and len(changes) > 0:
709 print >>html,"<h2>Changes between version %s and %s</h2>" % (s[2], last)
712 print >>html,"<li>%s" % c
723 changes.append(c[2:])
725 changes[-1] += " " + c
729 elif args.command == 'manual':
730 target = SourceTarget()
731 project.checkout(target)
733 outs = project.cscript['make_manual'](target)
735 if os.path.isfile(o):
736 copyfile(o, '%s/%s' % (args.output, os.path.basename(o)))
738 copytree(o, '%s/%s' % (args.output, os.path.basename(o)))
742 elif args.command == 'doxygen':
743 target = SourceTarget()
744 project.checkout(target)
746 dirs = project.cscript['make_doxygen'](target)
747 if hasattr(dirs, 'strip') or (not hasattr(dirs, '__getitem__') and not hasattr(dirs, '__iter__')):
751 copytree(d, '%s/%s' % (args.output, 'doc'))
755 elif args.command == 'latest':
756 target = SourceTarget()
757 project.checkout(target)
759 f = command_and_read('git log --tags --simplify-by-decoration --pretty="%d"')
761 m = re.compile(".*\((.*)\).*").match(t)
764 tags = m.group(1).split(', ')
769 if len(t) > 0 and t[0] == 'v':
775 elif args.command == 'test':
776 if args.target is None:
777 raise Error('you must specify -t or --target')
781 target = target_factory(args.target, args.debug, args.work)
784 if target is not None:
788 if target is not None:
791 elif args.command == 'shell':
792 if args.target is None:
793 raise Error('you must specify -t or --target')
795 target = target_factory(args.target, args.debug, args.work)
796 target.command('bash')
799 raise Error('invalid command %s' % args.command)