3 # Copyright (C) 2012-2018 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.
19 from __future__ import print_function
34 TEMPORARY_DIRECTORY = '/var/tmp'
36 class Error(Exception):
37 def __init__(self, value):
46 Store for Tree objects which re-uses already-created objects
47 and checks for requests for different versions of the same thing.
53 def get(self, name, specifier, target, required_by=None):
55 if t.name == name and t.specifier == specifier and t.target == target:
57 elif t.name == name and t.specifier != specifier:
58 a = specifier if specifier is not None else "[Any]"
59 if required_by is not None:
60 a += ' by %s' % required_by
61 b = t.specifier if t.specifier is not None else "[Any]"
62 if t.required_by is not None:
63 b += ' by %s' % t.required_by
64 raise Error('conflicting versions of %s required (%s versus %s)' % (name, a, b))
66 nt = Tree(name, specifier, target, required_by)
84 def __init__(self, key, default=None):
88 def offer(self, key, value):
92 class BoolOption(object):
93 def __init__(self, key):
97 def offer(self, key, value):
99 self.value = (value == 'yes' or value == '1' or value == 'true')
103 self.options = [ Option('mxe_prefix'),
104 Option('git_prefix'),
105 Option('osx_environment_prefix'),
106 Option('osx_sdk_prefix'),
108 BoolOption('docker_sudo'),
109 Option('flatpak_state_dir'),
110 Option('parallel', 4) ]
113 f = open('%s/.config/cdist' % os.path.expanduser('~'), 'r')
119 if len(l) > 0 and l[0] == '#':
122 s = l.strip().split()
124 for k in self.options:
130 for o in self.options:
131 if o.key == k and o.value is not None:
136 for o in self.options:
139 raise Error('Required setting %s not found' % k)
143 for o in self.options:
147 if self.get('docker_sudo'):
159 if not globals.quiet:
160 print('\x1b[33m* %s\x1b[0m' % m)
162 def escape_spaces(s):
163 return s.replace(' ', '\\ ')
166 """Escape a host:filename string for use with an scp command"""
168 assert(len(s) == 1 or len(s) == 2)
170 return '%s:"\'%s\'"' % (s[0], s[1])
172 return '\"%s\"' % s[0]
175 return '\"%s\"' % n.substr(' ', '\\ ')
178 log('copy %s -> %s' % (scp_escape(a), scp_escape(b)))
179 if b.startswith('s3://'):
180 command('s3cmd -P -r put "%s" "%s"' % (a, b))
182 command('scp -r %s %s' % (scp_escape(a), scp_escape(b)))
185 log('copy %s -> %s' % (scp_escape(a), scp_escape(b)))
186 if b.startswith('s3://'):
187 command('s3cmd -P put "%s" "%s"' % (a, b))
193 temp_path = os.path.join(os.path.dirname(path), ".tmp." + os.path.basename(path))
194 command('scp %s %s' % (scp_escape(a), scp_escape(host + ":" + temp_path)))
195 command('ssh %s -- mv "%s" "%s"' % (host, escape_spaces(temp_path), escape_spaces(path)))
197 command('scp %s %s' % (scp_escape(a), scp_escape(b)))
201 Make directories either locally or on a remote host; remotely if
202 d includes a colon, otherwise locally.
204 if d.startswith('s3://'):
205 # No need to create folders on S3
208 if d.find(':') == -1:
216 command('ssh %s -- mkdir -p %s' % (s[0], s[1]))
224 shutil.rmtree(a, ignore_errors=True)
230 raise Error('command %s failed' % c)
232 def command_and_read(c):
234 p = subprocess.Popen(c.split(), stdout=subprocess.PIPE)
235 f = os.fdopen(os.dup(p.stdout.fileno()))
238 def read_wscript_variable(directory, variable):
239 f = open('%s/wscript' % directory, 'r')
246 if len(s) == 3 and s[0] == variable:
253 def set_version_in_wscript(version):
254 f = open('wscript', 'rw')
255 o = open('wscript.tmp', 'w')
262 if len(s) == 3 and s[0] == "VERSION":
263 print("VERSION = '%s'" % version, file=o)
265 print(l, file=o, end="")
269 os.rename('wscript.tmp', 'wscript')
271 def append_version_to_changelog(version):
273 f = open('ChangeLog', 'r')
275 log('Could not open ChangeLog')
281 f = open('ChangeLog', 'w')
282 now = datetime.datetime.now()
283 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))
286 def append_version_to_debian_changelog(version):
287 if not os.path.exists('debian'):
288 log('Could not find debian directory')
291 command('dch -b -v %s-1 "New upstream release."' % version)
293 def devel_to_git(git_commit, filename):
294 if git_commit is not None:
295 filename = filename.replace('devel', '-%s' % git_commit)
298 def argument_options(args):
300 if args.option is not None:
301 for o in args.option:
304 raise Error("Bad option `%s'" % o)
315 def __init__(self, tree):
318 self.cwd = os.getcwd()
319 os.chdir('%s/src/%s' % (self.tree.target.directory, self.tree.name))
320 def __exit__(self, type, value, traceback):
328 def __init__(self, s):
331 if s.startswith("'"):
336 if s.endswith('devel'):
340 if s.endswith('pre'):
344 self.major = int(p[0])
345 self.minor = int(p[1])
347 self.micro = int(p[2])
352 def from_git_tag(cls, tag):
353 bits = tag.split('-')
355 if len(bits) > 1 and int(bits[1]) > 0:
359 def bump_minor(self):
363 def bump_micro(self):
369 def to_release(self):
373 s = '%d.%d.%d' % (self.major, self.minor, self.micro)
383 class Target(object):
385 Class representing the target that we are building for. This is exposed to cscripts,
386 though not all of it is guaranteed 'API'. cscripts may expect:
388 platform: platform string (e.g. 'windows', 'linux', 'osx')
389 parallel: number of parallel jobs to run
390 directory: directory to work in
391 variables: dict of environment variables
392 debug: True to build a debug version, otherwise False
393 ccache: True to use ccache, False to not
394 set(a, b): set the value of variable 'a' to 'b'
395 unset(a): unset the value of variable 'a'
396 command(c): run the command 'c' in the build environment
400 def __init__(self, platform, directory=None):
402 platform -- platform string (e.g. 'windows', 'linux', 'osx')
403 directory -- directory to work in; if None we will use a temporary directory
404 Temporary directories will be removed after use; specified directories will not.
406 self.platform = platform
407 self.parallel = int(config.get('parallel'))
409 # Environment variables that we will use when we call cscripts
413 # True to build our dependencies ourselves; False if this is taken care
414 # of in some other way
415 self.build_dependencies = True
417 if directory is None:
418 self.directory = tempfile.mkdtemp('', 'tmp', TEMPORARY_DIRECTORY)
420 self.set('CCACHE_BASEDIR', os.path.realpath(self.directory))
421 self.set('CCACHE_NOHASHDIR', '')
423 self.directory = directory
430 def package(self, project, checkout, output_dir, options):
431 tree = globals.trees.get(project, checkout, self)
432 if self.build_dependencies:
433 tree.build_dependencies(options)
435 if len(inspect.getargspec(tree.cscript['package']).args) == 3:
436 packages = tree.call('package', tree.version, options)
438 log("Deprecated cscript package() method with no options parameter")
439 packages = tree.call('package', tree.version)
441 if isinstance(packages, (str, unicode)):
442 copyfile(packages, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, packages))))
445 copyfile(p, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, p))))
447 def build(self, project, checkout, options):
448 tree = globals.trees.get(project, checkout, self)
449 if self.build_dependencies:
450 tree.build_dependencies(options)
453 def test(self, tree, test, options):
454 """test is the test case to run, or None"""
455 if self.build_dependencies:
456 tree.build_dependencies(options)
458 return tree.call('test', test)
461 self.variables[a] = b
464 del(self.variables[a])
467 return self.variables[a]
469 def append(self, k, v, s):
470 if (not k in self.variables) or len(self.variables[k]) == 0:
471 self.variables[k] = '"%s"' % v
473 e = self.variables[k]
474 if e[0] == '"' and e[-1] == '"':
475 self.variables[k] = '"%s%s%s"' % (e[1:-1], s, v)
477 self.variables[k] = '"%s%s%s"' % (e, s, v)
479 def append_with_space(self, k, v):
480 return self.append(k, v, ' ')
482 def append_with_colon(self, k, v):
483 return self.append(k, v, ':')
485 def variables_string(self, escaped_quotes=False):
487 for k, v in self.variables.items():
489 v = v.replace('"', '\\"')
490 e += '%s=%s ' % (k, v)
495 rmtree(self.directory)
509 class DockerTarget(Target):
510 def __init__(self, platform, directory, version):
511 super(DockerTarget, self).__init__(platform, directory)
512 self.version = version
514 self.privileged = False
517 opts = '-v %s:%s ' % (self.directory, self.directory)
518 for m in self.mounts:
519 opts += '-v %s:%s ' % (m, m)
521 opts += '--privileged=true '
523 opts += "-e CCACHE_DIR=/ccache --volumes-from ccache-%s" % self.image
524 self.container = command_and_read('%s run -u %s %s -itd %s /bin/bash' % (config.docker(), getpass.getuser(), opts, self.image)).read().strip()
526 def command(self, cmd):
527 dir = os.path.join(self.directory, os.path.relpath(os.getcwd(), self.directory))
528 command('%s exec -u %s -t %s /bin/bash -c \'export %s; cd %s; %s\'' % (config.docker(), getpass.getuser(), self.container, self.variables_string(), dir, cmd))
531 super(DockerTarget, self).cleanup()
532 command('%s kill %s' % (config.docker(), self.container))
535 self.mounts.append(m)
538 class FlatpakTarget(Target):
539 def __init__(self, project, checkout):
540 super(FlatpakTarget, self).__init__('flatpak')
541 self.build_dependencies = False
542 self.project = project
543 self.checkout = checkout
548 def command(self, cmd):
551 def checkout_dependencies(self):
552 tree = globals.trees.get(self.project, self.checkout, self)
553 return tree.checkout_dependencies()
558 def flatpak_builder(self):
559 b = 'flatpak-builder'
560 if config.has('flatpak_state_dir'):
561 b += ' --state-dir=%s' % config.get('flatpak_state_dir')
565 class WindowsTarget(DockerTarget):
567 This target exposes the following additional API:
569 version: Windows version ('xp' or None)
570 bits: bitness of Windows (32 or 64)
571 name: name of our target e.g. x86_64-w64-mingw32.shared
572 environment_prefix: path to Windows environment for the appropriate target (libraries and some tools)
573 tool_path: path to 32- and 64-bit tools
575 def __init__(self, version, bits, directory=None):
576 super(WindowsTarget, self).__init__('windows', directory, version)
579 self.tool_path = '%s/usr/bin' % config.get('mxe_prefix')
581 self.name = 'i686-w64-mingw32.shared'
583 self.name = 'x86_64-w64-mingw32.shared'
584 self.environment_prefix = '%s/usr/%s' % (config.get('mxe_prefix'), self.name)
586 self.set('PKG_CONFIG_LIBDIR', '%s/lib/pkgconfig' % self.environment_prefix)
587 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/bin/pkgconfig' % (self.directory, self.directory))
588 self.set('PATH', '%s/bin:%s:%s' % (self.environment_prefix, self.tool_path, os.environ['PATH']))
589 self.set('CC', '%s-gcc' % self.name)
590 self.set('CXX', '%s-g++' % self.name)
591 self.set('LD', '%s-ld' % self.name)
592 self.set('RANLIB', '%s-ranlib' % self.name)
593 self.set('WINRC', '%s-windres' % self.name)
594 cxx = '-I%s/include -I%s/include' % (self.environment_prefix, self.directory)
595 link = '-L%s/lib -L%s/lib' % (self.environment_prefix, self.directory)
596 self.set('CXXFLAGS', '"%s"' % cxx)
597 self.set('CPPFLAGS', '')
598 self.set('LINKFLAGS', '"%s"' % link)
599 self.set('LDFLAGS', '"%s"' % link)
601 self.image = 'windows'
604 def library_prefix(self):
605 log('Deprecated property library_prefix: use environment_prefix')
606 return self.environment_prefix
609 def windows_prefix(self):
610 log('Deprecated property windows_prefix: use environment_prefix')
611 return self.environment_prefix
614 def mingw_prefixes(self):
615 log('Deprecated property mingw_prefixes: use environment_prefix')
616 return [self.environment_prefix]
619 def mingw_path(self):
620 log('Deprecated property mingw_path: use tool_path')
621 return self.tool_path
624 def mingw_name(self):
625 log('Deprecated property mingw_name: use name')
629 class LinuxTarget(DockerTarget):
631 Build for Linux in a docker container.
632 This target exposes the following additional API:
634 distro: distribution ('debian', 'ubuntu', 'centos' or 'fedora')
635 version: distribution version (e.g. '12.04', '8', '6.5')
636 bits: bitness of the distribution (32 or 64)
637 detail: None or 'appimage' if we are building for appimage
640 def __init__(self, distro, version, bits, directory=None):
641 super(LinuxTarget, self).__init__('linux', directory, version)
646 self.set('CXXFLAGS', '-I%s/include' % self.directory)
647 self.set('CPPFLAGS', '')
648 self.set('LINKFLAGS', '-L%s/lib' % self.directory)
649 self.set('PKG_CONFIG_PATH',
650 '%s/lib/pkgconfig:%s/lib64/pkgconfig:/usr/local/lib64/pkgconfig:/usr/local/lib/pkgconfig' % (self.directory, self.directory))
651 self.set('PATH', '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin')
653 if self.version is None:
654 self.image = '%s-%s' % (self.distro, self.bits)
656 self.image = '%s-%s-%s' % (self.distro, self.version, self.bits)
659 super(LinuxTarget, self).setup()
661 self.set('CC', '"ccache gcc"')
662 self.set('CXX', '"ccache g++"')
664 def test(self, tree, test, options):
665 self.append_with_colon('PATH', '%s/bin' % self.directory)
666 self.append_with_colon('LD_LIBRARY_PATH', '%s/lib' % self.directory)
667 super(LinuxTarget, self).test(tree, test, options)
670 class AppImageTarget(LinuxTarget):
671 def __init__(self, work):
672 super(AppImageTarget, self).__init__('ubuntu', '14.04', 64, work)
673 self.detail = 'appimage'
674 self.privileged = True
677 class OSXTarget(Target):
678 def __init__(self, directory=None):
679 super(OSXTarget, self).__init__('osx', directory)
680 self.sdk = config.get('osx_sdk')
681 self.sdk_prefix = config.get('osx_sdk_prefix')
682 self.environment_prefix = config.get('osx_environment_prefix')
684 def command(self, c):
685 command('%s %s' % (self.variables_string(False), c))
688 class OSXSingleTarget(OSXTarget):
689 def __init__(self, bits, directory=None):
690 super(OSXSingleTarget, self).__init__(directory)
698 flags = '-isysroot %s/MacOSX%s.sdk -arch %s' % (self.sdk_prefix, self.sdk, arch)
699 enviro = '%s/%d' % (config.get('osx_environment_prefix'), bits)
701 # Environment variables
702 self.set('CFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, enviro, flags))
703 self.set('CPPFLAGS', '')
704 self.set('CXXFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, enviro, flags))
705 self.set('LDFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, enviro, flags))
706 self.set('LINKFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, enviro, flags))
707 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/lib/pkgconfig:/usr/lib/pkgconfig' % (self.directory, enviro))
708 self.set('PATH', '$PATH:/usr/bin:/sbin:/usr/local/bin:%s/bin' % enviro)
709 self.set('MACOSX_DEPLOYMENT_TARGET', config.get('osx_sdk'))
711 def package(self, project, checkout, output_dir, options):
712 raise Error('cannot package non-universal OS X versions')
714 @Target.ccache.setter
716 Target.ccache.fset(self, v)
718 self.set('CC', '"ccache gcc"')
719 self.set('CXX', '"ccache g++"')
722 class OSXUniversalTarget(OSXTarget):
723 def __init__(self, directory=None):
724 super(OSXUniversalTarget, self).__init__(directory)
726 def package(self, project, checkout, output_dir, options):
729 target = OSXSingleTarget(b, os.path.join(self.directory, '%d' % b))
730 target.ccache = self.ccache
731 tree = globals.trees.get(project, checkout, target)
732 tree.build_dependencies(options)
735 tree = globals.trees.get(project, checkout, self)
736 with TreeDirectory(tree):
737 if len(inspect.getargspec(tree.cscript['package']).args) == 3:
738 packages = tree.call('package', tree.version, options)
740 log("Deprecated cscript package() method with no options parameter")
741 packages = tree.call('package', tree.version)
743 copyfile(p, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, p))))
745 class SourceTarget(Target):
746 """Build a source .tar.bz2"""
748 super(SourceTarget, self).__init__('source')
750 def command(self, c):
751 log('host -> %s' % c)
752 command('%s %s' % (self.variables_string(), c))
755 rmtree(self.directory)
757 def package(self, project, checkout, output_dir, options):
758 tree = globals.trees.get(project, checkout, self)
759 with TreeDirectory(tree):
760 name = read_wscript_variable(os.getcwd(), 'APPNAME')
761 command('./waf dist')
762 p = os.path.abspath('%s-%s.tar.bz2' % (name, tree.version))
763 copyfile(p, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, p))))
765 # @param s Target string:
767 # or ubuntu-version-{32,64}
768 # or debian-version-{32,64}
769 # or centos-version-{32,64}
770 # or fedora-version-{32,64}
771 # or mageia-version-{32,64}
776 # @param debug True to build with debugging symbols (where possible)
777 def target_factory(args):
780 if s.startswith('windows-'):
783 target = WindowsTarget(None, int(x[1]), args.work)
785 target = WindowsTarget(x[1], int(x[2]), args.work)
787 raise Error("Bad Windows target name `%s'")
788 elif s.startswith('ubuntu-') or s.startswith('debian-') or s.startswith('centos-') or s.startswith('fedora-') or s.startswith('mageia-'):
791 raise Error("Bad Linux target name `%s'; must be something like ubuntu-16.04-32 (i.e. distro-version-bits)" % s)
792 target = LinuxTarget(p[0], p[1], int(p[2]), args.work)
793 elif s.startswith('arch-'):
796 raise Error("Bad Arch target name `%s'; must be arch-32 or arch-64")
797 target = LinuxTarget(p[0], None, int(p[1]), args.work)
798 elif s == 'raspbian':
799 target = LinuxTarget(s, None, None, args.work)
800 elif s.startswith('osx-'):
801 target = OSXSingleTarget(int(s.split('-')[1]), args.work)
803 if globals.command == 'build':
804 target = OSXSingleTarget(64, args.work)
806 target = OSXUniversalTarget(args.work)
808 target = SourceTarget()
810 target = FlatpakTarget(args.project, args.checkout)
811 elif s == 'appimage':
812 target = AppImageTarget(args.work)
815 raise Error("Bad target `%s'" % s)
817 target.debug = args.debug
818 target.ccache = args.ccache
820 if args.environment is not None:
821 for e in args.environment:
822 target.set(e, os.environ[e])
824 if args.mount is not None:
837 """Description of a tree, which is a checkout of a project,
838 possibly built. This class is never exposed to cscripts.
840 name -- name of git repository (without the .git)
841 specifier -- git tag or revision to use
842 target -- target object that we are using
843 version -- version from the wscript (if one is present)
844 git_commit -- git revision that is actually being used
845 built -- true if the tree has been built yet in this run
846 required_by -- name of the tree that requires this one
849 def __init__(self, name, specifier, target, required_by):
851 self.specifier = specifier
854 self.git_commit = None
856 self.required_by = required_by
864 redirect = '>/dev/null'
865 command('git clone %s %s/%s.git %s/src/%s' % (flags, config.get('git_prefix'), self.name, target.directory, self.name))
866 os.chdir('%s/src/%s' % (target.directory, self.name))
868 spec = self.specifier
872 command('git checkout %s %s %s' % (flags, spec, redirect))
873 self.git_commit = command_and_read('git rev-parse --short=7 HEAD').readline().strip()
874 command('git submodule init --quiet')
875 command('git submodule update --quiet')
877 proj = '%s/src/%s' % (target.directory, self.name)
880 exec(open('%s/cscript' % proj).read(), self.cscript)
882 if os.path.exists('%s/wscript' % proj):
883 v = read_wscript_variable(proj, "VERSION");
886 self.version = Version(v)
888 tag = subprocess.Popen(shlex.split('git -C %s describe --tags' % proj), stdout=subprocess.PIPE).communicate()[0][1:]
889 self.version = Version.from_git_tag(tag)
893 def call(self, function, *args):
894 with TreeDirectory(self):
895 return self.cscript[function](self.target, *args)
897 def add_defaults(self, options):
898 """Add the defaults from this into a dict options"""
899 if 'option_defaults' in self.cscript:
900 for k, v in self.cscript['option_defaults']().items():
904 def dependencies(self, options):
905 if not 'dependencies' in self.cscript:
908 if len(inspect.getargspec(self.cscript['dependencies']).args) == 2:
909 deps = self.call('dependencies', options)
911 log("Deprecated cscript dependencies() method with no options parameter")
912 deps = self.call('dependencies')
915 dep = globals.trees.get(d[0], d[1], self.target, self.name)
917 # Start with the options passed in
918 dep_options = copy.copy(options)
919 # Add things specified by the parent
921 for k, v in d[2].items():
922 if not k in dep_options:
924 # Then fill in the dependency's defaults
925 dep.add_defaults(dep_options)
927 for i in dep.dependencies(dep_options):
929 yield (dep, dep_options)
931 def checkout_dependencies(self, options={}):
932 for i in self.dependencies(options):
935 def build_dependencies(self, options):
936 for i in self.dependencies(options):
939 def build(self, options):
943 variables = copy.copy(self.target.variables)
945 # Start with the options passed in
946 options = copy.copy(options)
947 # Fill in the defaults
948 self.add_defaults(options)
950 if not globals.dry_run:
951 if len(inspect.getargspec(self.cscript['build']).args) == 2:
952 self.call('build', options)
956 self.target.variables = variables
960 # Command-line parser
966 "build": "build project",
967 "package": "package and build project",
968 "release": "release a project using its next version number (changing wscript and tagging)",
969 "pot": "build the project's .pot files",
970 "changelog": "generate a simple HTML changelog",
971 "manual": "build the project's manual",
972 "doxygen": "build the project's Doxygen documentation",
973 "latest": "print out the latest version",
974 "test": "run the project's unit tests",
975 "shell": "build the project then start a shell",
976 "checkout": "check out the project",
977 "revision": "print the head git revision number"
980 one_of = "Command is one of:\n"
982 for k, v in commands.items():
983 one_of += "\t%s\t%s\n" % (k, v)
986 parser = argparse.ArgumentParser()
987 parser.add_argument('command', help=summary)
988 parser.add_argument('-p', '--project', help='project name')
989 parser.add_argument('--minor', help='minor version number bump', action='store_true')
990 parser.add_argument('--micro', help='micro version number bump', action='store_true')
991 parser.add_argument('--latest-major', help='major version to return with latest', type=int)
992 parser.add_argument('--latest-minor', help='minor version to return with latest', type=int)
993 parser.add_argument('-c', '--checkout', help='string to pass to git for checkout')
994 parser.add_argument('-o', '--output', help='output directory', default='.')
995 parser.add_argument('-q', '--quiet', help='be quiet', action='store_true')
996 parser.add_argument('-t', '--target', help='target')
997 parser.add_argument('-k', '--keep', help='keep working tree', action='store_true')
998 parser.add_argument('--debug', help='build with debugging symbols where possible', action='store_true')
999 parser.add_argument('-w', '--work', help='override default work directory')
1000 parser.add_argument('-g', '--git-prefix', help='override configured git prefix')
1001 parser.add_argument('--test', help="name of test to run (with `test'), defaults to all")
1002 parser.add_argument('-n', '--dry-run', help='run the process without building anything', action='store_true')
1003 parser.add_argument('-e', '--environment', help='pass the value of the named environment variable into the build', action='append')
1004 parser.add_argument('-m', '--mount', help='mount a given directory in the build environment', action='append')
1005 parser.add_argument('--no-version-commit', help="use just tags for versioning, don't modify wscript, ChangeLog etc.", action='store_true')
1006 parser.add_argument('--option', help='set an option for the build (use --option key:value)', action='append')
1007 parser.add_argument('--ccache', help='use ccache', action='store_true')
1008 args = parser.parse_args()
1010 # Override configured stuff
1011 if args.git_prefix is not None:
1012 config.set('git_prefix', args.git_prefix)
1014 if args.output.find(':') == -1:
1015 # This isn't of the form host:path so make it absolute
1016 args.output = os.path.abspath(args.output) + '/'
1018 if args.output[-1] != ':' and args.output[-1] != '/':
1021 # Now, args.output is 'host:', 'host:path/' or 'path/'
1023 if args.work is not None:
1024 args.work = os.path.abspath(args.work)
1026 if args.project is None and args.command != 'shell':
1027 raise Error('you must specify -p or --project')
1029 globals.quiet = args.quiet
1030 globals.command = args.command
1031 globals.dry_run = args.dry_run
1033 if not globals.command in commands:
1034 e = 'command must be one of:\n' + one_of
1035 raise Error('command must be one of:\n%s' % one_of)
1037 if globals.command == 'build':
1038 if args.target is None:
1039 raise Error('you must specify -t or --target')
1041 target = target_factory(args)
1042 target.build(args.project, args.checkout, argument_options(args))
1046 elif globals.command == 'package':
1047 if args.target is None:
1048 raise Error('you must specify -t or --target')
1050 target = target_factory(args)
1052 if target.platform == 'linux' and target.detail != "appimage":
1053 if target.distro != 'arch':
1054 output_dir = os.path.join(args.output, '%s-%s-%d' % (target.distro, target.version, target.bits))
1056 output_dir = os.path.join(args.output, '%s-%d' % (target.distro, target.bits))
1058 output_dir = args.output
1060 makedirs(output_dir)
1062 # Start with the options passed on the command line
1063 options = copy.copy(argument_options(args))
1064 # Fill in the defaults
1065 tree = globals.trees.get(args.project, args.checkout, target)
1066 tree.add_defaults(options)
1068 target.package(args.project, args.checkout, output_dir, options)
1073 elif globals.command == 'release':
1074 if args.minor is False and args.micro is False:
1075 raise Error('you must specify --minor or --micro')
1077 target = SourceTarget()
1078 tree = globals.trees.get(args.project, args.checkout, target)
1080 version = tree.version
1081 version.to_release()
1083 version.bump_minor()
1085 version.bump_micro()
1087 with TreeDirectory(tree):
1088 if not args.no_version_commit:
1089 set_version_in_wscript(version)
1090 append_version_to_changelog(version)
1091 append_version_to_debian_changelog(version)
1092 command('git commit -a -m "Bump version"')
1094 command('git tag -m "v%s" v%s' % (version, version))
1096 if not args.no_version_commit:
1098 set_version_in_wscript(version)
1099 command('git commit -a -m "Bump version"')
1102 command('git push --tags')
1106 elif globals.command == 'pot':
1107 target = SourceTarget()
1108 tree = globals.trees.get(args.project, args.checkout, target)
1110 pots = tree.call('make_pot')
1112 copyfile(p, '%s%s' % (args.output, os.path.basename(p)))
1116 elif globals.command == 'changelog':
1117 target = SourceTarget()
1118 tree = globals.trees.get(args.project, args.checkout, target)
1120 with TreeDirectory(tree):
1121 text = open('ChangeLog', 'r')
1123 html = tempfile.NamedTemporaryFile()
1134 if len(l) > 0 and l[0] == "\t":
1136 if len(s) == 4 and s[1] == "Version" and s[3] == "released.":
1139 if last is not None and len(changes) > 0:
1140 print("<h2>Changes between version %s and %s</h2>" % (s[2], last), file=html)
1141 print("<ul>", file=html)
1143 print("<li>%s" % c, file=html)
1144 print("</ul>", file=html)
1154 changes.append(c[2:])
1156 changes[-1] += " " + c
1158 copyfile(html.file, '%schangelog.html' % args.output)
1162 elif globals.command == 'manual':
1163 target = SourceTarget()
1164 tree = globals.trees.get(args.project, args.checkout, target)
1166 outs = tree.call('make_manual')
1168 if os.path.isfile(o):
1169 copyfile(o, '%s%s' % (args.output, os.path.basename(o)))
1171 copytree(o, '%s%s' % (args.output, os.path.basename(o)))
1175 elif globals.command == 'doxygen':
1176 target = SourceTarget()
1177 tree = globals.trees.get(args.project, args.checkout, target)
1179 dirs = tree.call('make_doxygen')
1180 if hasattr(dirs, 'strip') or (not hasattr(dirs, '__getitem__') and not hasattr(dirs, '__iter__')):
1184 copytree(d, args.output)
1188 elif globals.command == 'latest':
1189 target = SourceTarget()
1190 tree = globals.trees.get(args.project, args.checkout, target)
1192 with TreeDirectory(tree):
1193 f = command_and_read('git log --tags --simplify-by-decoration --pretty="%d"')
1195 while latest is None:
1197 m = re.compile(".*\((.*)\).*").match(t)
1199 tags = m.group(1).split(', ')
1204 if len(t) > 0 and t[0] == 'v':
1206 if (args.latest_major is None or v.major == args.latest_major) and (args.latest_minor is None or v.minor == args.latest_minor):
1212 elif globals.command == 'test':
1213 if args.target is None:
1214 raise Error('you must specify -t or --target')
1218 target = target_factory(args)
1219 tree = globals.trees.get(args.project, args.checkout, target)
1220 with TreeDirectory(tree):
1221 target.test(tree, args.test, argument_options(args))
1223 if target is not None and not args.keep:
1227 if target is not None and not args.keep:
1230 elif globals.command == 'shell':
1231 if args.target is None:
1232 raise Error('you must specify -t or --target')
1234 target = target_factory(args)
1235 target.command('bash')
1237 elif globals.command == 'revision':
1239 target = SourceTarget()
1240 tree = globals.trees.get(args.project, args.checkout, target)
1241 with TreeDirectory(tree):
1242 print(command_and_read('git rev-parse HEAD').readline().strip()[:7])
1245 elif globals.command == 'checkout':
1247 if args.output is None:
1248 raise Error('you must specify -o or --output')
1250 target = SourceTarget()
1251 tree = globals.trees.get(args.project, args.checkout, target)
1252 with TreeDirectory(tree):
1253 shutil.copytree('.', args.output)
1257 raise Error('invalid command %s' % globals.command)
1262 print('cdist: %s' % str(e), file=sys.stderr)