3 # Copyright (C) 2012-2021 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
27 import multiprocessing
29 from pathlib import Path
39 class Error(Exception):
40 def __init__(self, value):
49 Store for Tree objects which re-uses already-created objects
50 and checks for requests for different versions of the same thing.
56 def get(self, name, specifier, target, required_by=None):
58 if t.name == name and t.specifier == specifier and t.target == target:
60 elif t.name == name and t.specifier != specifier:
61 a = specifier if specifier is not None else "[Any]"
62 if required_by is not None:
63 a += ' by %s' % required_by
64 b = t.specifier if t.specifier is not None else "[Any]"
65 if t.required_by is not None:
66 b += ' by %s' % t.required_by
67 raise Error('conflicting versions of %s required (%s versus %s)' % (name, a, b))
69 nt = Tree(name, specifier, target, required_by)
73 def add_built(self, name, specifier, target):
74 self.trees.append(Tree(name, specifier, target, None, built=True))
91 def __init__(self, key, default=None):
95 def offer(self, key, value):
99 class BoolOption(object):
100 def __init__(self, key):
104 def offer(self, key, value):
106 self.value = (value == 'yes' or value == '1' or value == 'true')
110 self.options = [ Option('mxe_prefix'),
111 Option('git_prefix'),
112 Option('git_reference'),
113 Option('osx_environment_prefix'),
114 Option('osx_sdk_prefix'),
116 Option('osx_intel_deployment'),
117 Option('osx_arm_deployment'),
118 Option('osx_keychain_file'),
119 Option('osx_keychain_password'),
121 Option('apple_password'),
122 BoolOption('docker_sudo'),
123 BoolOption('docker_no_user'),
124 Option('docker_hub_repository'),
125 Option('flatpak_state_dir'),
126 Option('parallel', multiprocessing.cpu_count()),
127 Option('temp', '/var/tmp')]
129 config_dir = '%s/.config' % os.path.expanduser('~')
130 if not os.path.exists(config_dir):
132 config_file = '%s/cdist' % config_dir
133 if not os.path.exists(config_file):
134 f = open(config_file, 'w')
135 for o in self.options:
136 print('# %s ' % o.key, file=f)
138 print('Template config file written to %s; please edit and try again.' % config_file, file=sys.stderr)
142 f = open('%s/.config/cdist' % os.path.expanduser('~'), 'r')
148 if len(l) > 0 and l[0] == '#':
151 s = l.strip().split()
153 for k in self.options:
159 for o in self.options:
160 if o.key == k and o.value is not None:
165 for o in self.options:
168 raise Error('Required setting %s not found' % k)
172 for o in self.options:
176 if self.get('docker_sudo'):
188 if not globals.quiet:
189 print('\x1b[33m* %s\x1b[0m' % m)
193 print('\x1b[35m* %s\x1b[0m' % m)
195 def escape_spaces(s):
196 return s.replace(' ', '\\ ')
199 """Escape a host:filename string for use with an scp command"""
201 assert(len(s) == 1 or len(s) == 2)
203 return '%s:"\'%s\'"' % (s[0], s[1])
205 return '\"%s\"' % s[0]
208 return '\"%s\"' % n.substr(' ', '\\ ')
211 log_normal('copy %s -> %s' % (scp_escape(a), scp_escape(b)))
212 if b.startswith('s3://'):
213 command('s3cmd -P -r put "%s" "%s"' % (a, b))
215 command('scp -r %s %s' % (scp_escape(a), scp_escape(b)))
218 log_normal('copy %s -> %s with cwd %s' % (scp_escape(a), scp_escape(b), os.getcwd()))
219 if b.startswith('s3://'):
220 command('s3cmd -P put "%s" "%s"' % (a, b))
226 temp_path = os.path.join(os.path.dirname(path), ".tmp." + os.path.basename(path))
227 command('scp %s %s' % (scp_escape(a), scp_escape(host + ":" + temp_path)))
228 command('ssh %s -- mv "%s" "%s"' % (host, escape_spaces(temp_path), escape_spaces(path)))
230 command('scp %s %s' % (scp_escape(a), scp_escape(b)))
234 Make directories either locally or on a remote host; remotely if
235 d includes a colon, otherwise locally.
237 if d.startswith('s3://'):
238 # No need to create folders on S3
241 if d.find(':') == -1:
249 command('ssh %s -- mkdir -p %s' % (s[0], s[1]))
252 log_normal('remove %s' % a)
256 log_normal('remove %s' % a)
257 shutil.rmtree(a, ignore_errors=True)
262 r = subprocess.run(c, shell=True)
263 if r.returncode != 0:
264 raise Error('command %s failed (%d)' % (c, r.returncode))
265 except Exception as e:
266 raise Error('command %s failed (%s)' % (c, e))
268 def command_and_read(c):
270 p = subprocess.Popen(c.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
271 (out, err) = p.communicate()
272 if p.returncode != 0:
273 raise Error('command %s failed (%s)' % (c, err))
274 return str(out, 'utf-8').splitlines()
276 def read_wscript_variable(directory, variable):
277 f = open('%s/wscript' % directory, 'r')
284 if len(s) == 3 and s[0] == variable:
292 def devel_to_git(git_commit, filename):
293 if git_commit is not None:
294 filename = filename.replace('devel', '-%s' % git_commit)
298 def get_command_line_options(args):
299 """Get the options specified by --option on the command line"""
301 if args.option is not None:
302 for o in args.option:
305 raise Error("Bad option `%s'" % o)
307 options[b[0]] = False
316 def __init__(self, tree):
319 self.cwd = os.getcwd()
320 os.chdir('%s/src/%s' % (self.tree.target.directory, self.tree.name))
321 def __exit__(self, type, value, traceback):
329 def __init__(self, s):
332 if s.startswith("'"):
337 if s.endswith('devel'):
341 if s.endswith('pre'):
345 self.major = int(p[0])
346 self.minor = int(p[1])
348 self.micro = int(p[2])
353 def from_git_tag(cls, tag):
354 bits = tag.split('-')
356 if len(bits) > 1 and int(bits[1]) > 0:
360 def bump_minor(self):
364 def bump_micro(self):
370 def to_release(self):
374 s = '%d.%d.%d' % (self.major, self.minor, self.micro)
384 class Target(object):
386 Class representing the target that we are building for. This is exposed to cscripts,
387 though not all of it is guaranteed 'API'. cscripts may expect:
389 platform: platform string (e.g. 'windows', 'linux', 'osx')
390 parallel: number of parallel jobs to run
391 directory: directory to work in
392 variables: dict of environment variables
393 debug: True to build a debug version, otherwise False
394 ccache: True to use ccache, False to not
395 set(a, b): set the value of variable 'a' to 'b'
396 unset(a): unset the value of variable 'a'
397 command(c): run the command 'c' in the build environment
401 def __init__(self, platform, directory=None):
403 platform -- platform string (e.g. 'windows', 'linux', 'osx')
404 directory -- directory to work in; if None we will use a temporary directory
405 Temporary directories will be removed after use; specified directories will not.
407 self.platform = platform
408 self.parallel = int(config.get('parallel'))
410 # Environment variables that we will use when we call cscripts
414 # True to build our dependencies ourselves; False if this is taken care
415 # of in some other way
416 self.build_dependencies = True
418 if directory is None:
419 self.directory = tempfile.mkdtemp('', 'tmp', config.get('temp'))
421 self.set('CCACHE_BASEDIR', os.path.realpath(self.directory))
422 self.set('CCACHE_NOHASHDIR', '')
424 self.directory = os.path.realpath(directory)
431 def _cscript_package(self, tree, options):
433 Call package() in the cscript and return what it returns, except that
434 anything not in a list will be put into one.
436 if len(inspect.getfullargspec(tree.cscript['package']).args) == 3:
437 packages = tree.call('package', tree.version, options)
439 log_normal("Deprecated cscript package() method with no options parameter")
440 packages = tree.call('package', tree.version)
442 return packages if isinstance(packages, list) else [packages]
444 def _copy_packages(self, tree, packages, output_dir):
446 copyfile(p, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, p))))
448 def package(self, project, checkout, output_dir, options, notarize):
449 tree = self.build(project, checkout, options)
450 tree.add_defaults(options)
451 p = self._cscript_package(tree, options)
452 self._copy_packages(tree, p, output_dir)
454 def build(self, project, checkout, options):
455 tree = globals.trees.get(project, checkout, self)
456 if self.build_dependencies:
457 tree.build_dependencies(options)
461 def test(self, project, checkout, target, test, options):
462 """test is the test case to run, or None"""
463 tree = globals.trees.get(project, checkout, target)
465 tree.add_defaults(options)
466 with TreeDirectory(tree):
467 if len(inspect.getfullargspec(tree.cscript['test']).args) == 3:
468 return tree.call('test', options, test)
470 log_normal('Deprecated cscript test() method with no options parameter')
471 return tree.call('test', test)
474 self.variables[a] = b
477 del(self.variables[a])
480 return self.variables[a]
482 def append(self, k, v, s):
483 if (not k in self.variables) or len(self.variables[k]) == 0:
484 self.variables[k] = '"%s"' % v
486 e = self.variables[k]
487 if e[0] == '"' and e[-1] == '"':
488 self.variables[k] = '"%s%s%s"' % (e[1:-1], s, v)
490 self.variables[k] = '"%s%s%s"' % (e, s, v)
492 def append_with_space(self, k, v):
493 return self.append(k, v, ' ')
495 def append_with_colon(self, k, v):
496 return self.append(k, v, ':')
498 def variables_string(self, escaped_quotes=False):
500 for k, v in self.variables.items():
502 v = v.replace('"', '\\"')
503 e += '%s=%s ' % (k, v)
508 rmtree(self.directory)
522 class DockerTarget(Target):
523 def __init__(self, platform, directory):
524 super(DockerTarget, self).__init__(platform, directory)
526 self.privileged = False
529 if config.get('docker_no_user'):
531 return '-u %s' % getpass.getuser()
533 def _mount_option(self, d):
534 return '-v %s:%s ' % (os.path.realpath(d), os.path.realpath(d))
537 opts = self._mount_option(self.directory)
538 for m in self.mounts:
539 opts += self._mount_option(m)
540 if config.has('git_reference'):
541 opts += self._mount_option(config.get('git_reference'))
543 opts += '--privileged=true '
545 opts += "-e CCACHE_DIR=/ccache/%s-%d --mount source=ccache,target=/ccache" % (self.image, os.getuid())
548 if config.has('docker_hub_repository'):
549 tag = '%s:%s' % (config.get('docker_hub_repository'), tag)
551 self.container = command_and_read('%s run %s %s -itd %s /bin/bash' % (config.docker(), self._user_tag(), opts, tag))[0].strip()
553 def command(self, cmd):
554 dir = os.path.join(self.directory, os.path.relpath(os.getcwd(), self.directory))
555 interactive_flag = '-i ' if sys.stdin.isatty() else ''
556 command('%s exec %s %s -t %s /bin/bash -c \'export %s; cd %s; %s\'' % (config.docker(), self._user_tag(), interactive_flag, self.container, self.variables_string(), dir, cmd))
559 super(DockerTarget, self).cleanup()
560 command('%s kill %s' % (config.docker(), self.container))
563 self.mounts.append(m)
566 class FlatpakTarget(Target):
567 def __init__(self, project, checkout):
568 super(FlatpakTarget, self).__init__('flatpak')
569 self.build_dependencies = False
570 self.project = project
571 self.checkout = checkout
576 def command(self, cmd):
579 def checkout_dependencies(self):
580 tree = globals.trees.get(self.project, self.checkout, self)
581 return tree.checkout_dependencies()
586 def flatpak_builder(self):
587 b = 'flatpak-builder'
588 if config.has('flatpak_state_dir'):
589 b += ' --state-dir=%s' % config.get('flatpak_state_dir')
593 class WindowsDockerTarget(DockerTarget):
595 This target exposes the following additional API:
597 version: Windows version ('xp' or None)
598 bits: bitness of Windows (32 or 64)
599 name: name of our target e.g. x86_64-w64-mingw32.shared
600 environment_prefix: path to Windows environment for the appropriate target (libraries and some tools)
601 tool_path: path to 32- and 64-bit tools
603 def __init__(self, windows_version, bits, directory, environment_version):
604 super(WindowsDockerTarget, self).__init__('windows', directory)
605 self.version = windows_version
608 self.tool_path = '%s/usr/bin' % config.get('mxe_prefix')
610 self.name = 'i686-w64-mingw32.shared'
612 self.name = 'x86_64-w64-mingw32.shared'
613 self.environment_prefix = '%s/usr/%s' % (config.get('mxe_prefix'), self.name)
615 self.set('PKG_CONFIG_LIBDIR', '%s/lib/pkgconfig' % self.environment_prefix)
616 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/bin/pkgconfig' % (self.directory, self.directory))
617 self.set('PATH', '%s/bin:%s:%s' % (self.environment_prefix, self.tool_path, os.environ['PATH']))
618 self.set('LD', '%s-ld' % self.name)
619 self.set('RANLIB', '%s-ranlib' % self.name)
620 self.set('WINRC', '%s-windres' % self.name)
621 cxx = '-I%s/include -I%s/include' % (self.environment_prefix, self.directory)
622 link = '-L%s/lib -L%s/lib' % (self.environment_prefix, self.directory)
623 self.set('CXXFLAGS', '"%s"' % cxx)
624 self.set('CPPFLAGS', '')
625 self.set('LINKFLAGS', '"%s"' % link)
626 self.set('LDFLAGS', '"%s"' % link)
628 self.image = 'windows'
629 if environment_version is not None:
630 self.image += '_%s' % environment_version
635 self.set('CC', '"ccache %s-gcc"' % self.name)
636 self.set('CXX', '"ccache %s-g++"' % self.name)
638 self.set('CC', '%s-gcc' % self.name)
639 self.set('CXX', '%s-g++' % self.name)
642 def library_prefix(self):
643 log_normal('Deprecated property library_prefix: use environment_prefix')
644 return self.environment_prefix
647 def windows_prefix(self):
648 log_normal('Deprecated property windows_prefix: use environment_prefix')
649 return self.environment_prefix
652 def mingw_prefixes(self):
653 log_normal('Deprecated property mingw_prefixes: use environment_prefix')
654 return [self.environment_prefix]
657 def mingw_path(self):
658 log_normal('Deprecated property mingw_path: use tool_path')
659 return self.tool_path
662 def mingw_name(self):
663 log_normal('Deprecated property mingw_name: use name')
667 class WindowsNativeTarget(Target):
669 This target exposes the following additional API:
671 version: Windows version ('xp' or None)
672 bits: bitness of Windows (32 or 64)
673 name: name of our target e.g. x86_64-w64-mingw32.shared
674 environment_prefix: path to Windows environment for the appropriate target (libraries and some tools)
676 def __init__(self, directory):
677 super().__init__('windows', directory)
681 self.environment_prefix = config.get('windows_native_environmnet_prefix')
683 self.set('PATH', '%s/bin:%s' % (self.environment_prefix, os.environ['PATH']))
685 def command(self, cmd):
689 class LinuxTarget(DockerTarget):
691 Build for Linux in a docker container.
692 This target exposes the following additional API:
694 distro: distribution ('debian', 'ubuntu', 'centos' or 'fedora')
695 version: distribution version (e.g. '12.04', '8', '6.5')
696 bits: bitness of the distribution (32 or 64)
697 detail: None or 'appimage' if we are building for appimage
700 def __init__(self, distro, version, bits, directory=None):
701 super(LinuxTarget, self).__init__('linux', directory)
703 self.version = version
707 self.set('CXXFLAGS', '-I%s/include' % self.directory)
708 self.set('CPPFLAGS', '')
709 self.set('LINKFLAGS', '-L%s/lib' % self.directory)
710 self.set('PKG_CONFIG_PATH',
711 '%s/lib/pkgconfig:%s/lib64/pkgconfig:/usr/local/lib64/pkgconfig:/usr/local/lib/pkgconfig' % (self.directory, self.directory))
712 self.set('PATH', '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin')
714 if self.version is None:
715 self.image = '%s-%s' % (self.distro, self.bits)
717 self.image = '%s-%s-%s' % (self.distro, self.version, self.bits)
720 super(LinuxTarget, self).setup()
722 self.set('CC', '"ccache gcc"')
723 self.set('CXX', '"ccache g++"')
725 def test(self, project, checkout, target, test, options):
726 self.append_with_colon('PATH', '%s/bin' % self.directory)
727 self.append_with_colon('LD_LIBRARY_PATH', '%s/lib' % self.directory)
728 super(LinuxTarget, self).test(project, checkout, target, test, options)
731 class AppImageTarget(LinuxTarget):
732 def __init__(self, work):
733 super(AppImageTarget, self).__init__('ubuntu', '18.04', 64, work)
734 self.detail = 'appimage'
735 self.privileged = True
738 def notarize_dmg(dmg, bundle_id):
740 ['xcrun', 'altool', '--notarize-app', '-t', 'osx', '-f', dmg, '--primary-bundle-id', bundle_id, '-u', config.get('apple_id'), '-p', config.get('apple_password'), '--output-format', 'xml'],
744 def string_after(process, key):
745 lines = p.stdout.decode('utf-8').splitlines()
746 for i in range(0, len(lines)):
747 if lines[i].find(key) != -1:
748 return lines[i+1].strip().replace('<string>', '').replace('</string>', '')
750 request_uuid = string_after(p, "RequestUUID")
751 if request_uuid is None:
752 print("Response: %s" % p)
753 raise Error('No RequestUUID found in response from Apple')
755 for i in range(0, 30):
756 print('%s: checking up on %s' % (datetime.datetime.now(), request_uuid))
757 p = subprocess.run(['xcrun', 'altool', '--notarization-info', request_uuid, '-u', config.get('apple_id'), '-p', config.get('apple_password'), '--output-format', 'xml'], capture_output=True)
758 status = string_after(p, 'Status')
759 print('%s: got status %s' % (datetime.datetime.now(), status))
760 if status == 'invalid':
761 raise Error("Notarization failed")
762 elif status == 'success':
763 subprocess.run(['xcrun', 'stapler', 'staple', dmg])
765 elif status != "in progress":
766 print("Could not understand xcrun response")
770 raise Error("Notarization timed out")
773 class OSXTarget(Target):
774 def __init__(self, directory=None):
775 super(OSXTarget, self).__init__('osx', directory)
776 self.sdk_prefix = config.get('osx_sdk_prefix')
777 self.environment_prefix = config.get('osx_environment_prefix')
778 self.apple_id = config.get('apple_id')
779 self.apple_password = config.get('apple_password')
780 self.osx_keychain_file = config.get('osx_keychain_file')
781 self.osx_keychain_password = config.get('osx_keychain_password')
783 def command(self, c):
784 command('%s %s' % (self.variables_string(False), c))
786 def unlock_keychain(self):
787 self.command('security unlock-keychain -p %s %s' % (self.osx_keychain_password, self.osx_keychain_file))
789 def _cscript_package_and_notarize(self, tree, options, notarize):
791 Call package() in the cscript and notarize the .dmgs that are returned, if notarize == True
793 p = self._cscript_package(tree, options)
795 if not isinstance(x, tuple):
796 raise Error('macOS packages must be returned from cscript as tuples of (dmg-filename, bundle-id)')
798 notarize_dmg(x[0], x[1])
800 with f as f.open(dmg + '.id', 'w'):
802 return [x[0] for x in p]
805 class OSXSingleTarget(OSXTarget):
806 def __init__(self, arch, sdk, deployment, directory=None):
807 super(OSXSingleTarget, self).__init__(directory)
810 self.deployment = deployment
812 flags = '-isysroot %s/MacOSX%s.sdk -arch %s' % (self.sdk_prefix, sdk, arch)
813 host_enviro = '%s/x86_64' % config.get('osx_environment_prefix')
814 target_enviro = '%s/%s' % (config.get('osx_environment_prefix'), arch)
816 self.bin = '%s/bin' % target_enviro
818 # Environment variables
819 self.set('CFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, target_enviro, flags))
820 self.set('CPPFLAGS', '')
821 self.set('CXXFLAGS', '"-I%s/include -I%s/include %s"' % (self.directory, target_enviro, flags))
822 self.set('LDFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, target_enviro, flags))
823 self.set('LINKFLAGS', '"-L%s/lib -L%s/lib %s"' % (self.directory, target_enviro, flags))
824 self.set('PKG_CONFIG_PATH', '%s/lib/pkgconfig:%s/lib/pkgconfig:/usr/lib/pkgconfig' % (self.directory, target_enviro))
825 self.set('PATH', '$PATH:/usr/bin:/sbin:/usr/local/bin:%s/bin' % host_enviro)
826 self.set('MACOSX_DEPLOYMENT_TARGET', self.deployment)
827 self.set('CCACHE_BASEDIR', self.directory)
829 @Target.ccache.setter
831 Target.ccache.fset(self, v)
833 self.set('CC', '"ccache gcc"')
834 self.set('CXX', '"ccache g++"')
836 def package(self, project, checkout, output_dir, options, notarize):
837 tree = self.build(project, checkout, options)
838 tree.add_defaults(options)
839 self.unlock_keychain()
840 p = self._cscript_package_and_notarize(tree, options, notarize)
841 self._copy_packages(tree, p, output_dir)
844 class OSXUniversalTarget(OSXTarget):
845 def __init__(self, directory=None):
846 super(OSXUniversalTarget, self).__init__(directory)
847 self.sdk = config.get('osx_sdk')
849 def package(self, project, checkout, output_dir, options, notarize):
850 for arch, deployment in (('x86_64', config.get('osx_intel_deployment')), ('arm64', config.get('osx_arm_deployment'))):
851 target = OSXSingleTarget(arch, self.sdk, deployment, os.path.join(self.directory, arch))
852 target.ccache = self.ccache
853 tree = globals.trees.get(project, checkout, target)
854 tree.build_dependencies(options)
857 self.unlock_keychain()
858 tree = globals.trees.get(project, checkout, self)
859 with TreeDirectory(tree):
860 for p in self._cscript_package_and_notarize(tree, options, notarize):
861 copyfile(p, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, p))))
863 class SourceTarget(Target):
864 """Build a source .tar.bz2"""
866 super(SourceTarget, self).__init__('source')
868 def command(self, c):
869 log_normal('host -> %s' % c)
870 command('%s %s' % (self.variables_string(), c))
873 rmtree(self.directory)
875 def package(self, project, checkout, output_dir, options, notarize):
876 tree = globals.trees.get(project, checkout, self)
877 with TreeDirectory(tree):
878 name = read_wscript_variable(os.getcwd(), 'APPNAME')
879 command('./waf dist')
880 p = os.path.abspath('%s-%s.tar.bz2' % (name, tree.version))
881 copyfile(p, os.path.join(output_dir, os.path.basename(devel_to_git(tree.git_commit, p))))
883 # @param s Target string:
885 # or ubuntu-version-{32,64}
886 # or debian-version-{32,64}
887 # or centos-version-{32,64}
888 # or fedora-version-{32,64}
889 # or mageia-version-{32,64}
894 # @param debug True to build with debugging symbols (where possible)
895 def target_factory(args):
898 if s.startswith('windows-'):
900 if platform.system() == "Windows":
901 target = WindowsNativeTarget(args.work)
904 target = WindowsDockerTarget(None, int(x[1]), args.work, args.environment_version)
906 target = WindowsDockerTarget(x[1], int(x[2]), args.work, args.environment_version)
908 raise Error("Bad Windows target name `%s'")
909 elif s.startswith('ubuntu-') or s.startswith('debian-') or s.startswith('centos-') or s.startswith('fedora-') or s.startswith('mageia-'):
912 raise Error("Bad Linux target name `%s'; must be something like ubuntu-16.04-32 (i.e. distro-version-bits)" % s)
913 target = LinuxTarget(p[0], p[1], int(p[2]), args.work)
914 elif s.startswith('arch-'):
917 raise Error("Bad Arch target name `%s'; must be arch-32 or arch-64")
918 target = LinuxTarget(p[0], None, int(p[1]), args.work)
919 elif s == 'raspbian':
920 target = LinuxTarget(s, None, None, args.work)
922 target = OSXUniversalTarget(args.work)
923 elif s == 'osx-intel':
924 target = OSXSingleTarget('x86_64', config.get('osx_sdk'), config.get('osx_intel_deployment'), args.work)
926 target = SourceTarget()
928 target = FlatpakTarget(args.project, args.checkout)
929 elif s == 'appimage':
930 target = AppImageTarget(args.work)
933 raise Error("Bad target `%s'" % s)
935 target.debug = args.debug
936 target.ccache = args.ccache
938 if args.environment is not None:
939 for e in args.environment:
940 target.set(e, os.environ[e])
942 if args.mount is not None:
955 """Description of a tree, which is a checkout of a project,
956 possibly built. This class is never exposed to cscripts.
958 name -- name of git repository (without the .git)
959 specifier -- git tag or revision to use
960 target -- target object that we are using
961 version -- version from the wscript (if one is present)
962 git_commit -- git revision that is actually being used
963 built -- true if the tree has been built yet in this run
964 required_by -- name of the tree that requires this one
967 def __init__(self, name, specifier, target, required_by, built=False):
969 self.specifier = specifier
972 self.git_commit = None
974 self.required_by = required_by
977 proj = '%s/src/%s' % (target.directory, self.name)
984 redirect = '>/dev/null'
985 if config.has('git_reference'):
986 ref = '--reference-if-able %s/%s.git' % (config.get('git_reference'), self.name)
989 command('git clone %s %s %s/%s.git %s/src/%s' % (flags, ref, config.get('git_prefix'), self.name, target.directory, self.name))
990 os.chdir('%s/src/%s' % (target.directory, self.name))
992 spec = self.specifier
996 command('git checkout %s %s %s' % (flags, spec, redirect))
997 self.git_commit = command_and_read('git rev-parse --short=7 HEAD')[0].strip()
1000 exec(open('%s/cscript' % proj).read(), self.cscript)
1003 # cscript can include submodules = False to stop submodules being fetched
1004 if (not 'submodules' in self.cscript or self.cscript['submodules'] == True) and os.path.exists('.gitmodules'):
1005 command('git submodule --quiet init')
1006 paths = command_and_read('git config --file .gitmodules --get-regexp path')
1007 urls = command_and_read('git config --file .gitmodules --get-regexp url')
1008 for path, url in zip(paths, urls):
1010 if config.has('git_reference'):
1011 url = url.split(' ')[1]
1012 ref_path = os.path.join(config.get('git_reference'), os.path.basename(url))
1013 if os.path.exists(ref_path):
1014 ref = '--reference %s' % ref_path
1015 path = path.split(' ')[1]
1016 command('git submodule --quiet update %s %s' % (ref, path))
1018 if os.path.exists('%s/wscript' % proj):
1019 v = read_wscript_variable(proj, "VERSION");
1022 self.version = Version(v)
1025 tag = command_and_read('git -C %s describe --tags' % proj)[0][1:]
1026 self.version = Version.from_git_tag(tag)
1028 # We'll leave version as None if we can't read it; maybe this is a bad idea
1029 # Should probably just install git on the Windows VM
1034 def call(self, function, *args):
1035 with TreeDirectory(self):
1036 return self.cscript[function](self.target, *args)
1038 def add_defaults(self, options):
1039 """Add the defaults from self into a dict options"""
1040 if 'option_defaults' in self.cscript:
1041 from_cscript = self.cscript['option_defaults']
1042 if isinstance(from_cscript, dict):
1043 defaults_dict = from_cscript
1045 log_normal("Deprecated cscript option_defaults method; replace with a dict")
1046 defaults_dict = from_cscript()
1047 for k, v in defaults_dict.items():
1048 if not k in options:
1051 def dependencies(self, options):
1053 yield details of the dependencies of this tree. Each dependency is returned
1054 as a tuple of (tree, options, parent_tree). The 'options' parameter are the options that
1055 we want to force for 'self'.
1057 if not 'dependencies' in self.cscript:
1060 if len(inspect.getfullargspec(self.cscript['dependencies']).args) == 2:
1061 self_options = copy.copy(options)
1062 self.add_defaults(self_options)
1063 deps = self.call('dependencies', self_options)
1065 log_normal("Deprecated cscript dependencies() method with no options parameter")
1066 deps = self.call('dependencies')
1068 # Loop over our immediate dependencies
1070 dep = globals.trees.get(d[0], d[1], self.target, self.name)
1072 # deps only get their options from the parent's cscript
1073 dep_options = d[2] if len(d) > 2 else {}
1074 for i in dep.dependencies(dep_options):
1076 yield (dep, dep_options, self)
1078 def checkout_dependencies(self, options={}):
1079 for i in self.dependencies(options):
1082 def build_dependencies(self, options):
1084 Called on the 'main' project tree (-p on the command line) to build all dependencies.
1085 'options' will be the ones from the command line.
1087 for i in self.dependencies(options):
1090 def build(self, options):
1094 log_verbose("Building %s %s %s with %s" % (self.name, self.specifier, self.version, options))
1096 variables = copy.copy(self.target.variables)
1098 options = copy.copy(options)
1099 self.add_defaults(options)
1101 if not globals.dry_run:
1102 if len(inspect.getfullargspec(self.cscript['build']).args) == 2:
1103 self.call('build', options)
1107 self.target.variables = variables
1112 # Command-line parser
1118 "build": "build project",
1119 "package": "build and package the project",
1120 "release": "release a project using its next version number (adding a tag)",
1121 "pot": "build the project's .pot files",
1122 "manual": "build the project's manual",
1123 "doxygen": "build the project's Doxygen documentation",
1124 "latest": "print out the latest version",
1125 "test": "build the project and run its unit tests",
1126 "shell": "start a shell in the project''s work directory",
1127 "checkout": "check out the project",
1128 "revision": "print the head git revision number",
1129 "dependencies" : "print details of the project's dependencies as a .dot file"
1134 for k, v in commands.items():
1135 one_of += "\t%s%s\n" % (k.ljust(20), v)
1138 parser = argparse.ArgumentParser()
1139 parser.add_argument('-p', '--project', help='project name')
1140 parser.add_argument('-c', '--checkout', help='string to pass to git for checkout')
1141 parser.add_argument('-o', '--output', help='output directory', default='.')
1142 parser.add_argument('-q', '--quiet', help='be quiet', action='store_true')
1143 parser.add_argument('-t', '--target', help='target', action='append')
1144 parser.add_argument('--environment-version', help='version of environment to use')
1145 parser.add_argument('-k', '--keep', help='keep working tree', action='store_true')
1146 parser.add_argument('--debug', help='build with debugging symbols where possible', action='store_true')
1147 parser.add_argument('-w', '--work', help='override default work directory')
1148 parser.add_argument('-g', '--git-prefix', help='override configured git prefix')
1149 parser.add_argument('-n', '--dry-run', help='run the process without building anything', action='store_true')
1150 parser.add_argument('-e', '--environment', help='pass the value of the named environment variable into the build', action='append')
1151 parser.add_argument('-m', '--mount', help='mount a given directory in the build environment', action='append')
1152 parser.add_argument('--option', help='set an option for the build (use --option key:value)', action='append')
1153 parser.add_argument('--ccache', help='use ccache', action='store_true')
1154 parser.add_argument('--verbose', help='be verbose', action='store_true')
1156 subparsers = parser.add_subparsers(help='command to run', dest='command')
1157 parser_build = subparsers.add_parser("build", help="build project")
1158 parser_package = subparsers.add_parser("package", help="build and package project")
1159 parser_package.add_argument('--no-notarize', help='do not notarize .dmg packages', action='store_true')
1160 parser_release = subparsers.add_parser("release", help="release a project using its next version number (adding a tag)")
1161 parser_release.add_argument('--minor', help='minor version number bump', action='store_true')
1162 parser_release.add_argument('--micro', help='micro version number bump', action='store_true')
1163 parser_pot = subparsers.add_parser("pot", help="build the project's .pot files")
1164 parser_manual = subparsers.add_parser("manual", help="build the project's manual")
1165 parser_doxygen = subparsers.add_parser("doxygen", help="build the project's Doxygen documentation")
1166 parser_latest = subparsers.add_parser("latest", help="print out the latest version")
1167 parser_latest.add_argument('--major', help='major version to return', type=int)
1168 parser_latest.add_argument('--minor', help='minor version to return', type=int)
1169 parser_test = subparsers.add_parser("test", help="build the project and run its unit tests")
1170 parser_test.add_argument('--no-implicit-build', help='do not build first', action='store_true')
1171 parser_test.add_argument('--test', help="name of test to run, defaults to all")
1172 parser_shell = subparsers.add_parser("shell", help="build the project then start a shell")
1173 parser_checkout = subparsers.add_parser("checkout", help="check out the project")
1174 parser_revision = subparsers.add_parser("revision", help="print the head git revision number")
1175 parser_dependencies = subparsers.add_parser("dependencies", help="print details of the project's dependencies as a .dot file")
1176 parser_notarize = subparsers.add_parser("notarize", help="notarize .dmgs in a directory using *.dmg.id files")
1177 parser_notarize.add_argument('--dmgs', help='directory containing *.dmg and *.dmg.id')
1180 args = parser.parse_args()
1182 # Check for incorrect multiple parameters
1183 if args.target is not None:
1184 if len(args.target) > 1:
1185 parser.error('multiple -t options specified')
1188 args.target = args.target[0]
1190 # Override configured stuff
1191 if args.git_prefix is not None:
1192 config.set('git_prefix', args.git_prefix)
1194 if args.output.find(':') == -1:
1195 # This isn't of the form host:path so make it absolute
1196 args.output = os.path.abspath(args.output) + '/'
1198 if args.output[-1] != ':' and args.output[-1] != '/':
1201 # Now, args.output is 'host:', 'host:path/' or 'path/'
1203 if args.work is not None:
1204 args.work = os.path.abspath(args.work)
1205 if not os.path.exists(args.work):
1206 os.makedirs(args.work)
1208 if args.project is None and args.command != 'shell':
1209 raise Error('you must specify -p or --project')
1211 globals.quiet = args.quiet
1212 globals.verbose = args.verbose
1213 globals.dry_run = args.dry_run
1215 if args.command == 'build':
1216 if args.target is None:
1217 raise Error('you must specify -t or --target')
1219 target = target_factory(args)
1220 target.build(args.project, args.checkout, get_command_line_options(args))
1224 elif args.command == 'package':
1225 if args.target is None:
1226 raise Error('you must specify -t or --target')
1230 target = target_factory(args)
1232 if target.platform == 'linux' and target.detail != "appimage":
1233 if target.distro != 'arch':
1234 output_dir = os.path.join(args.output, '%s-%s-%d' % (target.distro, target.version, target.bits))
1236 output_dir = os.path.join(args.output, '%s-%d' % (target.distro, target.bits))
1238 output_dir = args.output
1240 makedirs(output_dir)
1241 target.package(args.project, args.checkout, output_dir, get_command_line_options(args), not args.no_notarize)
1243 if target is not None and not args.keep:
1247 if target is not None and not args.keep:
1250 elif args.command == 'release':
1251 if args.minor is False and args.micro is False:
1252 raise Error('you must specify --minor or --micro')
1254 target = SourceTarget()
1255 tree = globals.trees.get(args.project, args.checkout, target)
1257 version = tree.version
1258 version.to_release()
1260 version.bump_minor()
1262 version.bump_micro()
1264 with TreeDirectory(tree):
1265 command('git tag -m "v%s" v%s' % (version, version))
1266 command('git push --tags')
1270 elif args.command == 'pot':
1271 target = SourceTarget()
1272 tree = globals.trees.get(args.project, args.checkout, target)
1274 pots = tree.call('make_pot')
1276 copyfile(p, '%s%s' % (args.output, os.path.basename(p)))
1280 elif args.command == 'manual':
1281 target = SourceTarget()
1282 tree = globals.trees.get(args.project, args.checkout, target)
1284 outs = tree.call('make_manual')
1286 if os.path.isfile(o):
1287 copyfile(o, '%s%s' % (args.output, os.path.basename(o)))
1289 copytree(o, '%s%s' % (args.output, os.path.basename(o)))
1293 elif args.command == 'doxygen':
1294 target = SourceTarget()
1295 tree = globals.trees.get(args.project, args.checkout, target)
1297 dirs = tree.call('make_doxygen')
1298 if hasattr(dirs, 'strip') or (not hasattr(dirs, '__getitem__') and not hasattr(dirs, '__iter__')):
1302 copytree(d, args.output)
1306 elif args.command == 'latest':
1307 target = SourceTarget()
1308 tree = globals.trees.get(args.project, args.checkout, target)
1310 with TreeDirectory(tree):
1311 f = command_and_read('git log --tags --simplify-by-decoration --pretty="%d"')
1314 while latest is None:
1317 m = re.compile(".*\((.*)\).*").match(t)
1319 tags = m.group(1).split(', ')
1324 if len(t) > 0 and t[0] == 'v':
1326 if (args.major is None or v.major == args.major) and (args.minor is None or v.minor == args.minor):
1332 elif args.command == 'test':
1333 if args.target is None:
1334 raise Error('you must specify -t or --target')
1338 target = target_factory(args)
1339 options = get_command_line_options(args)
1340 if args.no_implicit_build:
1341 globals.trees.add_built(args.project, args.checkout, target)
1343 target.build(args.project, args.checkout, options)
1344 target.test(args.project, args.checkout, target, args.test, options)
1346 if target is not None and not args.keep:
1349 elif args.command == 'shell':
1350 if args.target is None:
1351 raise Error('you must specify -t or --target')
1353 target = target_factory(args)
1354 target.command('bash')
1356 elif args.command == 'revision':
1358 target = SourceTarget()
1359 tree = globals.trees.get(args.project, args.checkout, target)
1360 with TreeDirectory(tree):
1361 print(command_and_read('git rev-parse HEAD')[0].strip()[:7])
1364 elif args.command == 'checkout':
1366 if args.output is None:
1367 raise Error('you must specify -o or --output')
1369 target = SourceTarget()
1370 tree = globals.trees.get(args.project, args.checkout, target)
1371 with TreeDirectory(tree):
1372 shutil.copytree('.', args.output)
1375 elif args.command == 'dependencies':
1376 if args.target is None:
1377 raise Error('you must specify -t or --target')
1378 if args.checkout is None:
1379 raise Error('you must specify -c or --checkout')
1381 target = target_factory(args)
1382 tree = globals.trees.get(args.project, args.checkout, target)
1383 print("strict digraph {")
1384 for d in list(tree.dependencies({})):
1385 print("%s -> %s;" % (d[2].name.replace("-", "-"), d[0].name.replace("-", "_")))
1388 elif args.command == 'notarize':
1389 if args.dmgs is None:
1390 raise Error('you must specify ---dmgs')
1391 if args.no_notarize:
1392 raise Error('it makes no sense to pass --no-notarize with the notarize command')
1394 for dmg in Path(args.dmgs).iter():
1397 with open(dmg + '.id') as f:
1398 id = f.getline().strip()
1400 raise Error('could not find ID file for %s' % dmg)
1401 notarize_dmg(dmg, id)
1406 print('cdist: %s' % str(e), file=sys.stderr)