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 TEMPORARY_DIRECTORY = '/tmp'
39 class Error(Exception):
40 def __init__(self, value):
52 def __init__(self, key, default=None):
56 def offer(self, key, value):
60 class BoolOption(object):
61 def __init__(self, key):
65 def offer(self, key, value):
67 self.value = (value == 'yes' or value == '1' or value == 'true')
71 self.options = [ Option('linux_chroot_prefix'),
72 Option('windows_environment_prefix'),
73 Option('mingw_prefix'),
75 Option('osx_build_host'),
76 Option('osx_environment_prefix'),
77 Option('osx_sdk_prefix'),
79 Option('parallel', 4) ]
82 f = open('%s/.config/cdist' % os.path.expanduser('~'), 'r')
88 if len(l) > 0 and l[0] == '#':
93 for k in self.options:
99 for o in self.options:
103 raise Error('Required setting %s not found' % k)
112 if not globals.quiet:
113 print '\x1b[33m* %s\x1b[0m' % m
116 log('copy %s -> %s' % (a, b))
117 shutil.copytree(a, b)
120 log('copy %s -> %s' % (a, b))
121 shutil.copyfile(a, b)
129 shutil.rmtree(a, ignore_errors=True)
131 def command(c, can_fail=False):
134 if (r >> 8) and not can_fail:
135 raise Error('command %s failed' % c)
137 def command_and_read(c):
139 p = subprocess.Popen(c.split(), stdout=subprocess.PIPE)
140 f = os.fdopen(os.dup(p.stdout.fileno()))
143 def read_wscript_variable(directory, variable):
144 f = open('%s/wscript' % directory, 'r')
151 if len(s) == 3 and s[0] == variable:
163 def __init__(self, s):
166 if s.startswith("'"):
171 if s.endswith('devel'):
175 if s.endswith('pre'):
179 self.major = int(p[0])
180 self.minor = int(p[1])
182 self.micro = int(p[2])
186 def bump_minor(self):
190 def bump_micro(self):
196 def to_release(self):
200 s = '%d.%d.%d' % (self.major, self.minor, self.micro)
210 class Target(object):
211 # @param directory directory to work in; if None we will use a temporary directory
212 # Temporary directories will be removed after use; specified directories will not
213 def __init__(self, platform, directory=None):
214 self.platform = platform
215 self.parallel = int(config.get('parallel'))
217 if directory is None:
218 self.directory = tempfile.mkdtemp('', 'tmp', TEMPORARY_DIRECTORY)
221 self.directory = directory
224 # Environment variables that we will use when we call cscripts
228 def build_dependencies(self, project):
230 if 'dependencies' in project.cscript:
231 for d in project.cscript['dependencies'](self):
232 log('Building dependency %s %s of %s' % (d[0], d[1], project.name))
233 dep = Project(d[0], '.', d[1])
235 self.build_dependencies(dep)
237 # Make the options to pass in from the option_defaults of the thing
238 # we are building and any options specified by the parent.
240 if 'option_defaults' in dep.cscript:
241 options = dep.cscript['option_defaults']()
243 for k, v in d[2].iteritems():
246 self.build(dep, options)
250 def build(self, project, options=None):
251 variables = copy.copy(self.variables)
252 if len(inspect.getargspec(project.cscript['build']).args) == 2:
253 project.cscript['build'](self, options)
255 project.cscript['build'](self)
256 self.variables = variables
258 def package(self, project):
259 project.checkout(self)
260 self.build_dependencies(project)
262 return project.cscript['package'](self, project.version)
264 def test(self, project):
265 project.checkout(self)
266 self.build_dependencies(project)
268 project.cscript['test'](self)
271 self.variables[a] = b
274 del(self.variables[a])
277 return self.variables[a]
279 def append_with_space(self, k, v):
280 if not k in self.variables:
281 self.variables[k] = v
283 self.variables[k] = '%s %s' % (self.variables[k], v)
285 def variables_string(self, escaped_quotes=False):
287 for k, v in self.variables.iteritems():
289 v = v.replace('"', '\\"')
290 e += '%s=%s ' % (k, v)
295 rmtree(self.directory)
301 class WindowsTarget(Target):
302 def __init__(self, bits, directory=None):
303 super(WindowsTarget, self).__init__('windows', directory)
306 self.windows_prefix = '%s/%d' % (config.get('windows_environment_prefix'), self.bits)
307 if not os.path.exists(self.windows_prefix):
308 raise Error('windows prefix %s does not exist' % self.windows_prefix)
311 self.mingw_name = 'i686'
313 self.mingw_name = 'x86_64'
315 mingw_path = '%s/%d/bin' % (config.get('mingw_prefix'), self.bits)
316 self.mingw_prefixes = ['/%s/%d' % (config.get('mingw_prefix'), self.bits), '%s/%d/%s-w64-mingw32' % (config.get('mingw_prefix'), bits, self.mingw_name)]
318 self.set('PKG_CONFIG_LIBDIR', '%s/lib/pkgconfig' % self.windows_prefix)
319 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/bin/pkgconfig' % (self.directory, self.directory))
320 self.set('PATH', '%s/bin:%s:%s' % (self.windows_prefix, mingw_path, os.environ['PATH']))
321 self.set('CC', '%s-w64-mingw32-gcc' % self.mingw_name)
322 self.set('CXX', '%s-w64-mingw32-g++' % self.mingw_name)
323 self.set('LD', '%s-w64-mingw32-ld' % self.mingw_name)
324 self.set('RANLIB', '%s-w64-mingw32-ranlib' % self.mingw_name)
325 self.set('WINRC', '%s-w64-mingw32-windres' % self.mingw_name)
326 cxx = '-I%s/include -I%s/include' % (self.windows_prefix, self.directory)
327 link = '-L%s/lib -L%s/lib' % (self.windows_prefix, self.directory)
328 for p in self.mingw_prefixes:
329 cxx += ' -I%s/include' % p
330 link += ' -L%s/lib' % p
331 self.set('CXXFLAGS', '"%s"' % cxx)
332 self.set('CPPFLAGS', '')
333 self.set('LINKFLAGS', '"%s"' % link)
335 def command(self, c):
336 log('host -> %s' % c)
337 command('%s %s' % (self.variables_string(), c))
343 class LinuxTarget(Target):
344 def __init__(self, distro, version, bits, directory=None):
345 super(LinuxTarget, self).__init__('linux', directory)
347 self.version = version
349 # e.g. ubuntu-14.04-64
350 self.chroot = '%s-%s-%d' % (self.distro, self.version, self.bits)
351 # e.g. /home/carl/Environments/ubuntu-14.04-64
352 self.chroot_prefix = '%s/%s' % (config.get('linux_chroot_prefix'), self.chroot)
354 self.set('CXXFLAGS', '-I%s/include' % self.directory)
355 self.set('CPPFLAGS', '')
356 self.set('LINKFLAGS', '-L%s/lib' % self.directory)
357 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:/usr/local/lib/pkgconfig' % self.directory)
358 self.set('PATH', '%s:/usr/local/bin' % (os.environ['PATH']))
360 def command(self, c):
361 command('%s schroot -c %s -p -- %s' % (self.variables_string(), self.chroot, c))
367 class OSXTarget(Target):
368 def __init__(self, directory=None):
369 super(OSXTarget, self).__init__('osx', directory)
371 def command(self, c):
372 command('%s %s' % (self.variables_string(False), c))
375 class OSXSingleTarget(OSXTarget):
376 def __init__(self, bits, directory=None):
377 super(OSXSingleTarget, self).__init__(directory)
385 flags = '-isysroot %s/MacOSX%s.sdk -arch %s' % (config.get('osx_sdk_prefix'), config.get('osx_sdk'), arch)
386 enviro = '%s/%d' % (config.get('osx_environment_prefix'), bits)
388 # Environment variables
389 self.set('CFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, enviro, flags))
390 self.set('CPPFLAGS', '')
391 self.set('CXXFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, enviro, flags))
392 self.set('LDFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, enviro, flags))
393 self.set('LINKFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, enviro, flags))
394 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/lib/pkgconfig:/usr/lib/pkgconfig' % (self.directory, enviro))
395 self.set('PATH', '$PATH:/usr/bin:/sbin:/usr/local/bin:%s/bin' % enviro)
396 self.set('MACOSX_DEPLOYMENT_TARGET', config.get('osx_sdk'))
398 def package(self, project):
399 raise Error('cannot package non-universal OS X versions')
402 class OSXUniversalTarget(OSXTarget):
403 def __init__(self, directory=None):
404 super(OSXUniversalTarget, self).__init__(directory)
406 self.parts.append(OSXSingleTarget(32, os.path.join(self.directory, '32')))
407 self.parts.append(OSXSingleTarget(64, os.path.join(self.directory, '64')))
409 def package(self, project):
412 p.build_dependencies(project)
415 return project.cscript['package'](self, project.version)
422 class SourceTarget(Target):
424 super(SourceTarget, self).__init__('source')
426 def command(self, c):
427 log('host -> %s' % c)
428 command('%s %s' % (self.variables_string(), c))
431 rmtree(self.directory)
433 def package(self, project):
434 project.checkout(self)
435 name = read_wscript_variable(os.getcwd(), 'APPNAME')
436 command('./waf dist')
437 return os.path.abspath('%s-%s.tar.bz2' % (name, project.version))
440 # @param s Target string:
442 # or ubuntu-version-{32,64}
443 # or debian-version-{32,64}
444 # or centos-version-{32,64}
447 # @param debug True to build with debugging symbols (where possible)
448 def target_factory(s, debug, work):
450 if s.startswith('windows-'):
451 target = WindowsTarget(int(s.split('-')[1]), work)
452 elif s.startswith('ubuntu-') or s.startswith('debian-') or s.startswith('centos-'):
455 print >>sys.stderr,"Bad Linux target name `%s'; must be something like ubuntu-12.04-32 (i.e. distro-version-bits)" % s
457 target = LinuxTarget(p[0], p[1], int(p[2]), work)
458 elif s.startswith('osx-'):
459 target = OSXSingleTarget(int(s.split('-')[1]), work)
461 if globals.command == 'build':
462 target = OSXSingleTarget(64, work)
464 target = OSXUniversalTarget(work)
466 target = SourceTarget()
468 if target is not None:
478 class Project(object):
479 def __init__(self, name, directory, specifier=None):
481 self.directory = directory
483 self.specifier = specifier
484 self.git_commit = None
485 if self.specifier is None:
486 self.specifier = 'master'
488 def checkout(self, target):
493 redirect = '>/dev/null'
494 command('git clone %s %s/%s.git %s/src/%s' % (flags, config.get('git_prefix'), self.name, target.directory, self.name))
495 os.chdir('%s/src/%s' % (target.directory, self.name))
496 command('git checkout %s %s %s' % (flags, self.specifier, redirect))
497 self.git_commit = command_and_read('git rev-parse --short=7 HEAD').readline().strip()
498 command('git submodule init --quiet')
499 command('git submodule update --quiet')
500 os.chdir(self.directory)
502 proj = '%s/src/%s/%s' % (target.directory, self.name, self.directory)
504 self.read_cscript('%s/cscript' % proj)
506 if os.path.exists('%s/wscript' % proj):
507 v = read_wscript_variable(proj, "VERSION");
509 self.version = Version(v)
511 def read_cscript(self, s):
513 execfile(s, self.cscript)
515 def set_version_in_wscript(version):
516 f = open('wscript', 'rw')
517 o = open('wscript.tmp', 'w')
524 if len(s) == 3 and s[0] == "VERSION":
525 print "Writing %s" % version
526 print >>o,"VERSION = '%s'" % version
532 os.rename('wscript.tmp', 'wscript')
534 def append_version_to_changelog(version):
536 f = open('ChangeLog', 'r')
538 log('Could not open ChangeLog')
544 f = open('ChangeLog', 'w')
545 now = datetime.datetime.now()
546 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))
549 def append_version_to_debian_changelog(version):
550 if not os.path.exists('debian'):
551 log('Could not find debian directory')
554 command('dch -b -v %s-1 "New upstream release."' % version)
556 def devel_to_git(project, filename):
557 if project.git_commit is not None:
558 filename = filename.replace('devel', '-%s' % project.git_commit)
563 # Command-line parser
567 parser = argparse.ArgumentParser()
568 parser.add_argument('command')
569 parser.add_argument('-p', '--project', help='project name')
570 parser.add_argument('-d', '--directory', help='directory within project repo', default='.')
571 parser.add_argument('--minor', help='minor version number bump', action='store_true')
572 parser.add_argument('--micro', help='micro version number bump', action='store_true')
573 parser.add_argument('--major', help='major version to return with latest', type=int)
574 parser.add_argument('-c', '--checkout', help='string to pass to git for checkout')
575 parser.add_argument('-o', '--output', help='output directory', default='.')
576 parser.add_argument('-q', '--quiet', help='be quiet', action='store_true')
577 parser.add_argument('-t', '--target', help='target')
578 parser.add_argument('-k', '--keep', help='keep working tree', action='store_true')
579 parser.add_argument('--debug', help='build with debugging symbols where possible', action='store_true')
580 parser.add_argument('-w', '--work', help='override default work directory')
581 args = parser.parse_args()
583 args.output = os.path.abspath(args.output)
584 if args.work is not None:
585 args.work = os.path.abspath(args.work)
587 if args.project is None and args.command != 'shell':
588 raise Error('you must specify -p or --project')
590 globals.quiet = args.quiet
591 globals.command = args.command
593 project = Project(args.project, args.directory, args.checkout)
595 commands = ['build', 'package', 'release', 'pot', 'changelog', 'manual', 'doxygen', 'latest', 'test', 'shell', 'revision']
596 if globals.command not in commands:
597 e = 'command must be one of: '
602 if globals.command == 'build':
603 if args.target is None:
604 raise Error('you must specify -t or --target')
606 target = target_factory(args.target, args.debug, args.work)
607 project.checkout(target)
608 target.build_dependencies(project)
609 target.build(project)
613 elif globals.command == 'package':
614 if args.target is None:
615 raise Error('you must specify -t or --target')
617 target = target_factory(args.target, args.debug, args.work)
619 packages = target.package(project)
620 if hasattr(packages, 'strip') or (not hasattr(packages, '__getitem__') and not hasattr(packages, '__iter__')):
621 packages = [packages]
623 if target.platform == 'linux':
624 out = '%s/%s-%s-%d' % (args.output, target.distro, target.version, target.bits)
630 copyfile(p, '%s/%s' % (out, os.path.basename(devel_to_git(project, p))))
633 copyfile(p, '%s/%s' % (args.output, os.path.basename(devel_to_git(project, p))))
638 elif globals.command == 'release':
639 if args.minor is False and args.micro is False:
640 raise Error('you must specify --minor or --micro')
642 target = SourceTarget()
643 project.checkout(target)
645 version = project.version
652 set_version_in_wscript(version)
653 append_version_to_changelog(version)
654 append_version_to_debian_changelog(version)
656 command('git commit -a -m "Bump version"')
657 command('git tag -m "v%s" v%s' % (version, version))
660 set_version_in_wscript(version)
661 command('git commit -a -m "Bump version"')
663 command('git push --tags')
667 elif globals.command == 'pot':
668 target = SourceTarget()
669 project.checkout(target)
671 pots = project.cscript['make_pot'](target)
673 copyfile(p, '%s/%s' % (args.output, os.path.basename(p)))
677 elif globals.command == 'changelog':
678 target = SourceTarget()
679 project.checkout(target)
681 text = open('ChangeLog', 'r')
682 html = open('%s/changelog.html' % args.output, 'w')
693 if len(l) > 0 and l[0] == "\t":
695 if len(s) == 4 and s[1] == "Version" and s[3] == "released.":
698 if last is not None and len(changes) > 0:
699 print >>html,"<h2>Changes between version %s and %s</h2>" % (s[2], last)
702 print >>html,"<li>%s" % c
713 changes.append(c[2:])
715 changes[-1] += " " + c
719 elif globals.command == 'manual':
720 target = SourceTarget()
721 project.checkout(target)
723 outs = project.cscript['make_manual'](target)
725 if os.path.isfile(o):
726 copyfile(o, '%s/%s' % (args.output, os.path.basename(o)))
728 copytree(o, '%s/%s' % (args.output, os.path.basename(o)))
732 elif globals.command == 'doxygen':
733 target = SourceTarget()
734 project.checkout(target)
736 dirs = project.cscript['make_doxygen'](target)
737 if hasattr(dirs, 'strip') or (not hasattr(dirs, '__getitem__') and not hasattr(dirs, '__iter__')):
741 copytree(d, '%s/%s' % (args.output, 'doc'))
745 elif globals.command == 'latest':
746 target = SourceTarget()
747 project.checkout(target)
749 f = command_and_read('git log --tags --simplify-by-decoration --pretty="%d"')
751 while latest is None:
753 m = re.compile(".*\((.*)\).*").match(t)
755 tags = m.group(1).split(', ')
760 if len(t) > 0 and t[0] == 'v':
762 if args.major is None or v.major == args.major:
768 elif globals.command == 'test':
769 if args.target is None:
770 raise Error('you must specify -t or --target')
774 target = target_factory(args.target, args.debug, args.work)
777 if target is not None:
781 if target is not None:
784 elif globals.command == 'shell':
785 if args.target is None:
786 raise Error('you must specify -t or --target')
788 target = target_factory(args.target, args.debug, args.work)
789 target.command('bash')
791 elif globals.command == 'revision':
793 target = SourceTarget()
794 project.checkout(target)
795 print command_and_read('git rev-parse HEAD').readline().strip()[:7]
799 raise Error('invalid command %s' % globals.command)
804 print >>sys.stderr,'cdist: %s' % str(e)