3 # Copyright (C) 2012-2019 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
33 import multiprocessing
35 TEMPORARY_DIRECTORY = '/var/tmp'
37 class Error(Exception):
38 def __init__(self, value):
47 Store for Tree objects which re-uses already-created objects
48 and checks for requests for different versions of the same thing.
54 def get(self, name, specifier, target, required_by=None):
56 if t.name == name and t.specifier == specifier and t.target == target:
58 elif t.name == name and t.specifier != specifier:
59 a = specifier if specifier is not None else "[Any]"
60 if required_by is not None:
61 a += ' by %s' % required_by
62 b = t.specifier if t.specifier is not None else "[Any]"
63 if t.required_by is not None:
64 b += ' by %s' % t.required_by
65 raise Error('conflicting versions of %s required (%s versus %s)' % (name, a, b))
67 nt = Tree(name, specifier, target, required_by)
85 def __init__(self, key, default=None):
89 def offer(self, key, value):
93 class BoolOption(object):
94 def __init__(self, key):
98 def offer(self, key, value):
100 self.value = (value == 'yes' or value == '1' or value == 'true')
104 self.options = [ Option('mxe_prefix'),
105 Option('git_prefix'),
106 Option('osx_environment_prefix'),
107 Option('osx_sdk_prefix'),
110 Option('apple_password'),
111 BoolOption('docker_sudo'),
112 BoolOption('docker_no_user'),
113 Option('docker_hub_repository'),
114 Option('flatpak_state_dir'),
115 Option('parallel', multiprocessing.cpu_count()) ]
118 f = open('%s/.config/cdist' % os.path.expanduser('~'), 'r')
124 if len(l) > 0 and l[0] == '#':
127 s = l.strip().split()
129 for k in self.options:
135 for o in self.options:
136 if o.key == k and o.value is not None:
141 for o in self.options:
144 raise Error('Required setting %s not found' % k)
148 for o in self.options:
152 if self.get('docker_sudo'):
164 if not globals.quiet:
165 print('\x1b[33m* %s\x1b[0m' % m)
167 def escape_spaces(s):
168 return s.replace(' ', '\\ ')
171 """Escape a host:filename string for use with an scp command"""
173 assert(len(s) == 1 or len(s) == 2)
175 return '%s:"\'%s\'"' % (s[0], s[1])
177 return '\"%s\"' % s[0]
180 return '\"%s\"' % n.substr(' ', '\\ ')
183 log('copy %s -> %s' % (scp_escape(a), scp_escape(b)))
184 if b.startswith('s3://'):
185 command('s3cmd -P -r put "%s" "%s"' % (a, b))
187 command('scp -r %s %s' % (scp_escape(a), scp_escape(b)))
190 log('copy %s -> %s' % (scp_escape(a), scp_escape(b)))
191 if b.startswith('s3://'):
192 command('s3cmd -P put "%s" "%s"' % (a, b))
198 temp_path = os.path.join(os.path.dirname(path), ".tmp." + os.path.basename(path))
199 command('scp %s %s' % (scp_escape(a), scp_escape(host + ":" + temp_path)))
200 command('ssh %s -- mv "%s" "%s"' % (host, escape_spaces(temp_path), escape_spaces(path)))
202 command('scp %s %s' % (scp_escape(a), scp_escape(b)))
206 Make directories either locally or on a remote host; remotely if
207 d includes a colon, otherwise locally.
209 if d.startswith('s3://'):
210 # No need to create folders on S3
213 if d.find(':') == -1:
221 command('ssh %s -- mkdir -p %s' % (s[0], s[1]))
229 shutil.rmtree(a, ignore_errors=True)
235 raise Error('command %s failed' % c)
237 def command_and_read(c):
239 p = subprocess.Popen(c.split(), stdout=subprocess.PIPE)
240 f = os.fdopen(os.dup(p.stdout.fileno()))
243 def read_wscript_variable(directory, variable):
244 f = open('%s/wscript' % directory, 'r')
251 if len(s) == 3 and s[0] == variable:
258 def set_version_in_wscript(version):
259 f = open('wscript', 'rw')
260 o = open('wscript.tmp', 'w')
267 if len(s) == 3 and s[0] == "VERSION":
268 print("VERSION = '%s'" % version, file=o)
270 print(l, file=o, end="")
274 os.rename('wscript.tmp', 'wscript')
276 def append_version_to_changelog(version):
278 f = open('ChangeLog', 'r')
280 log('Could not open ChangeLog')
286 f = open('ChangeLog', 'w')
287 now = datetime.datetime.now()
288 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))
291 def append_version_to_debian_changelog(version):
292 if not os.path.exists('debian'):
293 log('Could not find debian directory')
296 command('dch -b -v %s-1 "New upstream release."' % version)
298 def devel_to_git(git_commit, filename):
299 if git_commit is not None:
300 filename = filename.replace('devel', '-%s' % git_commit)
303 def argument_options(args):
305 if args.option is not None:
306 for o in args.option:
309 raise Error("Bad option `%s'" % o)
320 def __init__(self, tree):
323 self.cwd = os.getcwd()
324 os.chdir('%s/src/%s' % (self.tree.target.directory, self.tree.name))
325 def __exit__(self, type, value, traceback):
333 def __init__(self, s):
336 if s.startswith("'"):
341 if s.endswith('devel'):
345 if s.endswith('pre'):
349 self.major = int(p[0])
350 self.minor = int(p[1])
352 self.micro = int(p[2])
357 def from_git_tag(cls, tag):
358 bits = tag.split('-')
360 if len(bits) > 1 and int(bits[1]) > 0:
364 def bump_minor(self):
368 def bump_micro(self):
374 def to_release(self):
378 s = '%d.%d.%d' % (self.major, self.minor, self.micro)
388 class Target(object):
390 Class representing the target that we are building for. This is exposed to cscripts,
391 though not all of it is guaranteed 'API'. cscripts may expect:
393 platform: platform string (e.g. 'windows', 'linux', 'osx')
394 parallel: number of parallel jobs to run
395 directory: directory to work in
396 variables: dict of environment variables
397 debug: True to build a debug version, otherwise False
398 ccache: True to use ccache, False to not
399 set(a, b): set the value of variable 'a' to 'b'
400 unset(a): unset the value of variable 'a'
401 command(c): run the command 'c' in the build environment
405 def __init__(self, platform, directory=None):
407 platform -- platform string (e.g. 'windows', 'linux', 'osx')
408 directory -- directory to work in; if None we will use a temporary directory
409 Temporary directories will be removed after use; specified directories will not.
411 self.platform = platform
412 self.parallel = int(config.get('parallel'))
414 # Environment variables that we will use when we call cscripts
418 # True to build our dependencies ourselves; False if this is taken care
419 # of in some other way
420 self.build_dependencies = True
422 if directory is None:
423 self.directory = tempfile.mkdtemp('', 'tmp', TEMPORARY_DIRECTORY)
425 self.set('CCACHE_BASEDIR', os.path.realpath(self.directory))
426 self.set('CCACHE_NOHASHDIR', '')
428 self.directory = directory
435 def package(self, project, checkout, output_dir, options):
436 tree = globals.trees.get(project, checkout, self)
437 if self.build_dependencies:
438 tree.build_dependencies(options)
440 if len(inspect.getargspec(tree.cscript['package']).args) == 3:
441 packages = tree.call('package', tree.version, options)
443 log("Deprecated cscript package() method with no options parameter")
444 packages = tree.call('package', tree.version)
446 if isinstance(packages, (str, unicode)):
447 copyfile(packages, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, packages))))
450 copyfile(p, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, p))))
452 def build(self, project, checkout, options):
453 tree = globals.trees.get(project, checkout, self)
454 if self.build_dependencies:
455 tree.build_dependencies(options)
458 def test(self, tree, test, options):
459 """test is the test case to run, or None"""
460 if self.build_dependencies:
461 tree.build_dependencies(options)
463 return tree.call('test', test)
466 self.variables[a] = b
469 del(self.variables[a])
472 return self.variables[a]
474 def append(self, k, v, s):
475 if (not k in self.variables) or len(self.variables[k]) == 0:
476 self.variables[k] = '"%s"' % v
478 e = self.variables[k]
479 if e[0] == '"' and e[-1] == '"':
480 self.variables[k] = '"%s%s%s"' % (e[1:-1], s, v)
482 self.variables[k] = '"%s%s%s"' % (e, s, v)
484 def append_with_space(self, k, v):
485 return self.append(k, v, ' ')
487 def append_with_colon(self, k, v):
488 return self.append(k, v, ':')
490 def variables_string(self, escaped_quotes=False):
492 for k, v in self.variables.items():
494 v = v.replace('"', '\\"')
495 e += '%s=%s ' % (k, v)
500 rmtree(self.directory)
514 class DockerTarget(Target):
515 def __init__(self, platform, directory, version):
516 super(DockerTarget, self).__init__(platform, directory)
517 self.version = version
519 self.privileged = False
522 if config.get('docker_no_user'):
524 return '-u %s' % getpass.getuser()
527 opts = '-v %s:%s ' % (self.directory, self.directory)
528 for m in self.mounts:
529 opts += '-v %s:%s ' % (m, m)
531 opts += '--privileged=true '
533 opts += "-e CCACHE_DIR=/ccache --volumes-from ccache-%s" % self.image
536 if config.has('docker_hub_repository'):
537 tag = '%s:%s' % (config.get('docker_hub_repository'), tag)
539 self.container = command_and_read('%s run %s %s -itd %s /bin/bash' % (config.docker(), self._user_tag(), opts, tag)).read().strip()
541 def command(self, cmd):
542 dir = os.path.join(self.directory, os.path.relpath(os.getcwd(), self.directory))
543 command('%s exec %s -t %s /bin/bash -c \'export %s; cd %s; %s\'' % (config.docker(), self._user_tag(), self.container, self.variables_string(), dir, cmd))
546 super(DockerTarget, self).cleanup()
547 command('%s kill %s' % (config.docker(), self.container))
550 self.mounts.append(m)
553 class FlatpakTarget(Target):
554 def __init__(self, project, checkout):
555 super(FlatpakTarget, self).__init__('flatpak')
556 self.build_dependencies = False
557 self.project = project
558 self.checkout = checkout
563 def command(self, cmd):
566 def checkout_dependencies(self):
567 tree = globals.trees.get(self.project, self.checkout, self)
568 return tree.checkout_dependencies()
573 def flatpak_builder(self):
574 b = 'flatpak-builder'
575 if config.has('flatpak_state_dir'):
576 b += ' --state-dir=%s' % config.get('flatpak_state_dir')
580 class WindowsTarget(DockerTarget):
582 This target exposes the following additional API:
584 version: Windows version ('xp' or None)
585 bits: bitness of Windows (32 or 64)
586 name: name of our target e.g. x86_64-w64-mingw32.shared
587 environment_prefix: path to Windows environment for the appropriate target (libraries and some tools)
588 tool_path: path to 32- and 64-bit tools
590 def __init__(self, version, bits, directory=None):
591 super(WindowsTarget, self).__init__('windows', directory, version)
594 self.tool_path = '%s/usr/bin' % config.get('mxe_prefix')
596 self.name = 'i686-w64-mingw32.shared'
598 self.name = 'x86_64-w64-mingw32.shared'
599 self.environment_prefix = '%s/usr/%s' % (config.get('mxe_prefix'), self.name)
601 self.set('PKG_CONFIG_LIBDIR', '%s/lib/pkgconfig' % self.environment_prefix)
602 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/bin/pkgconfig' % (self.directory, self.directory))
603 self.set('PATH', '%s/bin:%s:%s' % (self.environment_prefix, self.tool_path, os.environ['PATH']))
604 self.set('CC', '%s-gcc' % self.name)
605 self.set('CXX', '%s-g++' % self.name)
606 self.set('LD', '%s-ld' % self.name)
607 self.set('RANLIB', '%s-ranlib' % self.name)
608 self.set('WINRC', '%s-windres' % self.name)
609 cxx = '-I%s/include -I%s/include' % (self.environment_prefix, self.directory)
610 link = '-L%s/lib -L%s/lib' % (self.environment_prefix, self.directory)
611 self.set('CXXFLAGS', '"%s"' % cxx)
612 self.set('CPPFLAGS', '')
613 self.set('LINKFLAGS', '"%s"' % link)
614 self.set('LDFLAGS', '"%s"' % link)
616 self.image = 'windows'
619 def library_prefix(self):
620 log('Deprecated property library_prefix: use environment_prefix')
621 return self.environment_prefix
624 def windows_prefix(self):
625 log('Deprecated property windows_prefix: use environment_prefix')
626 return self.environment_prefix
629 def mingw_prefixes(self):
630 log('Deprecated property mingw_prefixes: use environment_prefix')
631 return [self.environment_prefix]
634 def mingw_path(self):
635 log('Deprecated property mingw_path: use tool_path')
636 return self.tool_path
639 def mingw_name(self):
640 log('Deprecated property mingw_name: use name')
644 class LinuxTarget(DockerTarget):
646 Build for Linux in a docker container.
647 This target exposes the following additional API:
649 distro: distribution ('debian', 'ubuntu', 'centos' or 'fedora')
650 version: distribution version (e.g. '12.04', '8', '6.5')
651 bits: bitness of the distribution (32 or 64)
652 detail: None or 'appimage' if we are building for appimage
655 def __init__(self, distro, version, bits, directory=None):
656 super(LinuxTarget, self).__init__('linux', directory, version)
661 self.set('CXXFLAGS', '-I%s/include' % self.directory)
662 self.set('CPPFLAGS', '')
663 self.set('LINKFLAGS', '-L%s/lib' % self.directory)
664 self.set('PKG_CONFIG_PATH',
665 '%s/lib/pkgconfig:%s/lib64/pkgconfig:/usr/local/lib64/pkgconfig:/usr/local/lib/pkgconfig' % (self.directory, self.directory))
666 self.set('PATH', '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin')
668 if self.version is None:
669 self.image = '%s-%s' % (self.distro, self.bits)
671 self.image = '%s-%s-%s' % (self.distro, self.version, self.bits)
674 super(LinuxTarget, self).setup()
676 self.set('CC', '"ccache gcc"')
677 self.set('CXX', '"ccache g++"')
679 def test(self, tree, test, options):
680 self.append_with_colon('PATH', '%s/bin' % self.directory)
681 self.append_with_colon('LD_LIBRARY_PATH', '%s/lib' % self.directory)
682 super(LinuxTarget, self).test(tree, test, options)
685 class AppImageTarget(LinuxTarget):
686 def __init__(self, work):
687 super(AppImageTarget, self).__init__('ubuntu', '16.04', 64, work)
688 self.detail = 'appimage'
689 self.privileged = True
692 class OSXTarget(Target):
693 def __init__(self, directory=None):
694 super(OSXTarget, self).__init__('osx', directory)
695 self.sdk = config.get('osx_sdk')
696 self.sdk_prefix = config.get('osx_sdk_prefix')
697 self.environment_prefix = config.get('osx_environment_prefix')
698 self.apple_id = config.get('apple_id')
699 self.apple_password = config.get('apple_password')
701 def command(self, c):
702 command('%s %s' % (self.variables_string(False), c))
705 class OSXSingleTarget(OSXTarget):
706 def __init__(self, bits, directory=None):
707 super(OSXSingleTarget, self).__init__(directory)
715 flags = '-isysroot %s/MacOSX%s.sdk -arch %s' % (self.sdk_prefix, self.sdk, arch)
716 enviro = '%s/%d' % (config.get('osx_environment_prefix'), bits)
718 # Environment variables
719 self.set('CFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, enviro, flags))
720 self.set('CPPFLAGS', '')
721 self.set('CXXFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, enviro, flags))
722 self.set('LDFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, enviro, flags))
723 self.set('LINKFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, enviro, flags))
724 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/lib/pkgconfig:/usr/lib/pkgconfig' % (self.directory, enviro))
725 self.set('PATH', '$PATH:/usr/bin:/sbin:/usr/local/bin:%s/bin' % enviro)
726 self.set('MACOSX_DEPLOYMENT_TARGET', config.get('osx_sdk'))
727 self.set('CCACHE_BASEDIR', self.directory)
729 @Target.ccache.setter
731 Target.ccache.fset(self, v)
733 self.set('CC', '"ccache gcc"')
734 self.set('CXX', '"ccache g++"')
737 class OSXUniversalTarget(OSXTarget):
738 def __init__(self, directory=None):
739 super(OSXUniversalTarget, self).__init__(directory)
742 def package(self, project, checkout, output_dir, options):
745 target = OSXSingleTarget(b, os.path.join(self.directory, '%d' % b))
746 target.ccache = self.ccache
747 tree = globals.trees.get(project, checkout, target)
748 tree.build_dependencies(options)
751 tree = globals.trees.get(project, checkout, self)
752 with TreeDirectory(tree):
753 if len(inspect.getargspec(tree.cscript['package']).args) == 3:
754 packages = tree.call('package', tree.version, options)
756 log("Deprecated cscript package() method with no options parameter")
757 packages = tree.call('package', tree.version)
759 copyfile(p, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, p))))
761 class SourceTarget(Target):
762 """Build a source .tar.bz2"""
764 super(SourceTarget, self).__init__('source')
766 def command(self, c):
767 log('host -> %s' % c)
768 command('%s %s' % (self.variables_string(), c))
771 rmtree(self.directory)
773 def package(self, project, checkout, output_dir, options):
774 tree = globals.trees.get(project, checkout, self)
775 with TreeDirectory(tree):
776 name = read_wscript_variable(os.getcwd(), 'APPNAME')
777 command('./waf dist')
778 p = os.path.abspath('%s-%s.tar.bz2' % (name, tree.version))
779 copyfile(p, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, p))))
781 # @param s Target string:
783 # or ubuntu-version-{32,64}
784 # or debian-version-{32,64}
785 # or centos-version-{32,64}
786 # or fedora-version-{32,64}
787 # or mageia-version-{32,64}
792 # @param debug True to build with debugging symbols (where possible)
793 def target_factory(args):
796 if s.startswith('windows-'):
799 target = WindowsTarget(None, int(x[1]), args.work)
801 target = WindowsTarget(x[1], int(x[2]), args.work)
803 raise Error("Bad Windows target name `%s'")
804 elif s.startswith('ubuntu-') or s.startswith('debian-') or s.startswith('centos-') or s.startswith('fedora-') or s.startswith('mageia-'):
807 raise Error("Bad Linux target name `%s'; must be something like ubuntu-16.04-32 (i.e. distro-version-bits)" % s)
808 target = LinuxTarget(p[0], p[1], int(p[2]), args.work)
809 elif s.startswith('arch-'):
812 raise Error("Bad Arch target name `%s'; must be arch-32 or arch-64")
813 target = LinuxTarget(p[0], None, int(p[1]), args.work)
814 elif s == 'raspbian':
815 target = LinuxTarget(s, None, None, args.work)
816 elif s.startswith('osx-'):
817 target = OSXSingleTarget(int(s.split('-')[1]), args.work)
819 if globals.command == 'build':
820 target = OSXSingleTarget(64, args.work)
822 target = OSXUniversalTarget(args.work)
824 target = SourceTarget()
826 target = FlatpakTarget(args.project, args.checkout)
827 elif s == 'appimage':
828 target = AppImageTarget(args.work)
831 raise Error("Bad target `%s'" % s)
833 target.debug = args.debug
834 target.ccache = args.ccache
836 if args.environment is not None:
837 for e in args.environment:
838 target.set(e, os.environ[e])
840 if args.mount is not None:
853 """Description of a tree, which is a checkout of a project,
854 possibly built. This class is never exposed to cscripts.
856 name -- name of git repository (without the .git)
857 specifier -- git tag or revision to use
858 target -- target object that we are using
859 version -- version from the wscript (if one is present)
860 git_commit -- git revision that is actually being used
861 built -- true if the tree has been built yet in this run
862 required_by -- name of the tree that requires this one
865 def __init__(self, name, specifier, target, required_by):
867 self.specifier = specifier
870 self.git_commit = None
872 self.required_by = required_by
880 redirect = '>/dev/null'
881 command('git clone %s %s/%s.git %s/src/%s' % (flags, config.get('git_prefix'), self.name, target.directory, self.name))
882 os.chdir('%s/src/%s' % (target.directory, self.name))
884 spec = self.specifier
888 command('git checkout %s %s %s' % (flags, spec, redirect))
889 self.git_commit = command_and_read('git rev-parse --short=7 HEAD').readline().strip()
890 command('git submodule init --quiet')
891 command('git submodule update --quiet')
893 proj = '%s/src/%s' % (target.directory, self.name)
896 exec(open('%s/cscript' % proj).read(), self.cscript)
898 if os.path.exists('%s/wscript' % proj):
899 v = read_wscript_variable(proj, "VERSION");
902 self.version = Version(v)
904 tag = subprocess.Popen(shlex.split('git -C %s describe --tags' % proj), stdout=subprocess.PIPE).communicate()[0][1:]
905 self.version = Version.from_git_tag(tag)
909 def call(self, function, *args):
910 with TreeDirectory(self):
911 return self.cscript[function](self.target, *args)
913 def add_defaults(self, options):
914 """Add the defaults from this into a dict options"""
915 if 'option_defaults' in self.cscript:
916 for k, v in self.cscript['option_defaults']().items():
920 def dependencies(self, options):
921 if not 'dependencies' in self.cscript:
924 if len(inspect.getargspec(self.cscript['dependencies']).args) == 2:
925 deps = self.call('dependencies', options)
927 log("Deprecated cscript dependencies() method with no options parameter")
928 deps = self.call('dependencies')
931 dep = globals.trees.get(d[0], d[1], self.target, self.name)
933 # Start with the options passed in
934 dep_options = copy.copy(options)
935 # Add things specified by the parent
937 for k, v in d[2].items():
938 if not k in dep_options:
940 # Then fill in the dependency's defaults
941 dep.add_defaults(dep_options)
943 for i in dep.dependencies(dep_options):
945 yield (dep, dep_options)
947 def checkout_dependencies(self, options={}):
948 for i in self.dependencies(options):
951 def build_dependencies(self, options):
952 for i in self.dependencies(options):
955 def build(self, options):
959 variables = copy.copy(self.target.variables)
961 # Start with the options passed in
962 options = copy.copy(options)
963 # Fill in the defaults
964 self.add_defaults(options)
966 if not globals.dry_run:
967 if len(inspect.getargspec(self.cscript['build']).args) == 2:
968 self.call('build', options)
972 self.target.variables = variables
976 # Command-line parser
982 "build": "build project",
983 "package": "package and build project",
984 "release": "release a project using its next version number (changing wscript and tagging)",
985 "pot": "build the project's .pot files",
986 "changelog": "generate a simple HTML changelog",
987 "manual": "build the project's manual",
988 "doxygen": "build the project's Doxygen documentation",
989 "latest": "print out the latest version",
990 "test": "run the project's unit tests",
991 "shell": "build the project then start a shell",
992 "checkout": "check out the project",
993 "revision": "print the head git revision number"
996 one_of = "Command is one of:\n"
998 for k, v in commands.items():
999 one_of += "\t%s\t%s\n" % (k, v)
1002 parser = argparse.ArgumentParser()
1003 parser.add_argument('command', help=summary)
1004 parser.add_argument('-p', '--project', help='project name')
1005 parser.add_argument('--minor', help='minor version number bump', action='store_true')
1006 parser.add_argument('--micro', help='micro version number bump', action='store_true')
1007 parser.add_argument('--latest-major', help='major version to return with latest', type=int)
1008 parser.add_argument('--latest-minor', help='minor version to return with latest', type=int)
1009 parser.add_argument('-c', '--checkout', help='string to pass to git for checkout')
1010 parser.add_argument('-o', '--output', help='output directory', default='.')
1011 parser.add_argument('-q', '--quiet', help='be quiet', action='store_true')
1012 parser.add_argument('-t', '--target', help='target')
1013 parser.add_argument('-k', '--keep', help='keep working tree', action='store_true')
1014 parser.add_argument('--debug', help='build with debugging symbols where possible', action='store_true')
1015 parser.add_argument('-w', '--work', help='override default work directory')
1016 parser.add_argument('-g', '--git-prefix', help='override configured git prefix')
1017 parser.add_argument('--test', help="name of test to run (with `test'), defaults to all")
1018 parser.add_argument('-n', '--dry-run', help='run the process without building anything', action='store_true')
1019 parser.add_argument('-e', '--environment', help='pass the value of the named environment variable into the build', action='append')
1020 parser.add_argument('-m', '--mount', help='mount a given directory in the build environment', action='append')
1021 parser.add_argument('--no-version-commit', help="use just tags for versioning, don't modify wscript, ChangeLog etc.", action='store_true')
1022 parser.add_argument('--option', help='set an option for the build (use --option key:value)', action='append')
1023 parser.add_argument('--ccache', help='use ccache', action='store_true')
1024 args = parser.parse_args()
1026 # Override configured stuff
1027 if args.git_prefix is not None:
1028 config.set('git_prefix', args.git_prefix)
1030 if args.output.find(':') == -1:
1031 # This isn't of the form host:path so make it absolute
1032 args.output = os.path.abspath(args.output) + '/'
1034 if args.output[-1] != ':' and args.output[-1] != '/':
1037 # Now, args.output is 'host:', 'host:path/' or 'path/'
1039 if args.work is not None:
1040 args.work = os.path.abspath(args.work)
1042 if args.project is None and args.command != 'shell':
1043 raise Error('you must specify -p or --project')
1045 globals.quiet = args.quiet
1046 globals.command = args.command
1047 globals.dry_run = args.dry_run
1049 if not globals.command in commands:
1050 e = 'command must be one of:\n' + one_of
1051 raise Error('command must be one of:\n%s' % one_of)
1053 if globals.command == 'build':
1054 if args.target is None:
1055 raise Error('you must specify -t or --target')
1057 target = target_factory(args)
1058 target.build(args.project, args.checkout, argument_options(args))
1062 elif globals.command == 'package':
1063 if args.target is None:
1064 raise Error('you must specify -t or --target')
1068 target = target_factory(args)
1070 if target.platform == 'linux' and target.detail != "appimage":
1071 if target.distro != 'arch':
1072 output_dir = os.path.join(args.output, '%s-%s-%d' % (target.distro, target.version, target.bits))
1074 output_dir = os.path.join(args.output, '%s-%d' % (target.distro, target.bits))
1076 output_dir = args.output
1078 makedirs(output_dir)
1080 # Start with the options passed on the command line
1081 options = copy.copy(argument_options(args))
1082 # Fill in the defaults
1083 tree = globals.trees.get(args.project, args.checkout, target)
1084 tree.add_defaults(options)
1085 target.package(args.project, args.checkout, output_dir, options)
1087 if target is not None and not args.keep:
1091 if target is not None and not args.keep:
1094 elif globals.command == 'release':
1095 if args.minor is False and args.micro is False:
1096 raise Error('you must specify --minor or --micro')
1098 target = SourceTarget()
1099 tree = globals.trees.get(args.project, args.checkout, target)
1101 version = tree.version
1102 version.to_release()
1104 version.bump_minor()
1106 version.bump_micro()
1108 with TreeDirectory(tree):
1109 if not args.no_version_commit:
1110 set_version_in_wscript(version)
1111 append_version_to_changelog(version)
1112 append_version_to_debian_changelog(version)
1113 command('git commit -a -m "Bump version"')
1115 command('git tag -m "v%s" v%s' % (version, version))
1117 if not args.no_version_commit:
1119 set_version_in_wscript(version)
1120 command('git commit -a -m "Bump version"')
1123 command('git push --tags')
1127 elif globals.command == 'pot':
1128 target = SourceTarget()
1129 tree = globals.trees.get(args.project, args.checkout, target)
1131 pots = tree.call('make_pot')
1133 copyfile(p, '%s%s' % (args.output, os.path.basename(p)))
1137 elif globals.command == 'changelog':
1138 target = SourceTarget()
1139 tree = globals.trees.get(args.project, args.checkout, target)
1141 with TreeDirectory(tree):
1142 text = open('ChangeLog', 'r')
1144 html = tempfile.NamedTemporaryFile()
1155 if len(l) > 0 and l[0] == "\t":
1157 if len(s) == 4 and s[1] == "Version" and s[3] == "released.":
1160 if last is not None and len(changes) > 0:
1161 print("<h2>Changes between version %s and %s</h2>" % (s[2], last), file=html)
1162 print("<ul>", file=html)
1164 print("<li>%s" % c, file=html)
1165 print("</ul>", file=html)
1175 changes.append(c[2:])
1177 changes[-1] += " " + c
1179 copyfile(html.file, '%schangelog.html' % args.output)
1183 elif globals.command == 'manual':
1184 target = SourceTarget()
1185 tree = globals.trees.get(args.project, args.checkout, target)
1187 outs = tree.call('make_manual')
1189 if os.path.isfile(o):
1190 copyfile(o, '%s%s' % (args.output, os.path.basename(o)))
1192 copytree(o, '%s%s' % (args.output, os.path.basename(o)))
1196 elif globals.command == 'doxygen':
1197 target = SourceTarget()
1198 tree = globals.trees.get(args.project, args.checkout, target)
1200 dirs = tree.call('make_doxygen')
1201 if hasattr(dirs, 'strip') or (not hasattr(dirs, '__getitem__') and not hasattr(dirs, '__iter__')):
1205 copytree(d, args.output)
1209 elif globals.command == 'latest':
1210 target = SourceTarget()
1211 tree = globals.trees.get(args.project, args.checkout, target)
1213 with TreeDirectory(tree):
1214 f = command_and_read('git log --tags --simplify-by-decoration --pretty="%d"')
1216 while latest is None:
1218 m = re.compile(".*\((.*)\).*").match(t)
1220 tags = m.group(1).split(', ')
1225 if len(t) > 0 and t[0] == 'v':
1227 if (args.latest_major is None or v.major == args.latest_major) and (args.latest_minor is None or v.minor == args.latest_minor):
1233 elif globals.command == 'test':
1234 if args.target is None:
1235 raise Error('you must specify -t or --target')
1239 target = target_factory(args)
1240 tree = globals.trees.get(args.project, args.checkout, target)
1241 with TreeDirectory(tree):
1242 target.test(tree, args.test, argument_options(args))
1244 if target is not None and not args.keep:
1248 if target is not None and not args.keep:
1251 elif globals.command == 'shell':
1252 if args.target is None:
1253 raise Error('you must specify -t or --target')
1255 target = target_factory(args)
1256 target.command('bash')
1258 elif globals.command == 'revision':
1260 target = SourceTarget()
1261 tree = globals.trees.get(args.project, args.checkout, target)
1262 with TreeDirectory(tree):
1263 print(command_and_read('git rev-parse HEAD').readline().strip()[:7])
1266 elif globals.command == 'checkout':
1268 if args.output is None:
1269 raise Error('you must specify -o or --output')
1271 target = SourceTarget()
1272 tree = globals.trees.get(args.project, args.checkout, target)
1273 with TreeDirectory(tree):
1274 shutil.copytree('.', args.output)
1278 raise Error('invalid command %s' % globals.command)
1283 print('cdist: %s' % str(e), file=sys.stderr)