#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Package locally built Perl, preserving separate architecture-dependent cores.""" import argparse import hashlib import json import os from pathlib import Path import shutil import subprocess import tempfile ROOT = Path(__file__).resolve().parents[2] VERSION = '5.44.0' MACHO_MAGIC = {b'\xcf\xfa\xed\xfe', b'\xfe\xed\xfa\xcf', b'\xca\xfe\xba\xbe', b'\xbe\xba\xfe\xca'} def native(path): with path.open('rb') as stream: return stream.read(4) in MACHO_MAGIC def stage(runtime, prefixes): manifest_path = runtime / 'manifest.json' manifest = json.loads(manifest_path.read_text()) if set(prefixes) != set(manifest['architectures']): raise SystemExit('Perl builds must cover every declared runtime architecture') helpers = {p: arches for p, arches in manifest.get('nativeHelpers', {}).items() if not p.startswith('libexec/perl/') and p != 'bin/perl'} target = runtime / 'libexec/perl' if target.exists(): shutil.rmtree(target) for arch, prefix in prefixes.items(): actual = subprocess.check_output([str(prefix / 'bin/perl'), '-e', 'print $^V'], text=True) if actual != 'v' + VERSION: raise SystemExit('Unexpected Perl version: ' + actual) installation = target / arch (installation / 'bin').mkdir(parents=True) shutil.copyfile(prefix / 'bin/perl', installation / 'bin/perl') (installation / 'bin/perl').chmod(0o755) # Core modules and their data/XS binaries, without SDK headers, static # development archives, manuals, command-line maintenance tools or CPAN # download caches. Original module notices remain in their source text. def ignored(folder, names): return [name for name in names if name == 'CORE' or name == 'pods' or name.endswith(('.pod', '.a', '.h')) or name == '.packlist'] shutil.copytree(prefix / 'lib', installation / 'lib', ignore=ignored) for path in installation.rglob('*'): if path.is_symlink(): raise SystemExit('Unexpected symlink in Perl runtime: ' + str(path)) if path.is_file() and native(path): slices = subprocess.check_output(['/usr/bin/lipo', '-archs', str(path)], text=True).split() if arch not in slices: raise SystemExit('Incorrect Perl architecture: ' + str(path)) helpers[path.relative_to(runtime).as_posix()] = [arch] env = dict(os.environ) env.setdefault('DEVELOPER_DIR', '/Applications/Xcode.app/Contents/Developer') with tempfile.TemporaryDirectory(prefix='vcc-perl-launcher-') as folder: slices = [] for arch in prefixes: binary = Path(folder) / arch subprocess.run(['xcrun', 'clang', '-O2', '-arch', arch, '-mmacosx-version-min=14.0', str(ROOT / 'tools/toolchain/perl-launcher.c'), '-o', str(binary)], env=env, check=True) slices.append(binary) launcher = runtime / 'bin/perl' if len(slices) == 1: shutil.copyfile(slices[0], launcher) else: subprocess.run(['/usr/bin/lipo', '-create', *map(str, slices), '-output', str(launcher)], check=True) launcher.chmod(0o755) helpers['bin/perl'] = list(prefixes) # Upstream script's /usr/bin/perl shebang would bypass our bundled runtime. # Preserve the script unchanged behind a relocatable wrapper instead. shutil.copyfile(ROOT / 'third_party/cmoc/src/install-coco-boot-loader', runtime / 'libexec/install-coco-boot-loader.pl') wrapper = runtime / 'bin/install-coco-boot-loader' wrapper.write_text('#!/bin/sh\n# SPDX-License-Identifier: GPL-3.0-or-later\n' 'set -eu\nruntime=$(CDPATH= cd -- "$(/usr/bin/dirname -- "$0")/.." && pwd -P)\n' 'exec "$runtime/bin/perl" "$runtime/libexec/install-coco-boot-loader.pl" "$@"\n') wrapper.chmod(0o755) notices = runtime / 'LICENSES' for source, name in [('README', 'PERL-README.txt'), ('Copying', 'PERL-COPYING.txt'), ('Artistic', 'PERL-ARTISTIC.txt'), ('cpan/Compress-Raw-Bzip2/bzip2-src/LICENSE', 'PERL-BZIP2-LICENSE.txt')]: shutil.copyfile(ROOT / 'third_party/perl' / source, notices / name) manifest['nativeHelpers'] = dict(sorted(helpers.items())) manifest['perl'] = {'version': VERSION, 'license': 'GPL-1.0-or-later OR Artistic-1.0-Perl', 'profile': 'interpreter-and-core-modules', 'architectures': list(prefixes), 'sourceManifestSHA256': hashlib.sha256((ROOT / 'third_party/manifests/perl.json').read_bytes()).hexdigest()} manifest_path.write_text(json.dumps(manifest, indent=2) + '\n') for arch in prefixes: subprocess.run(['/usr/bin/arch', '-' + arch, str(runtime / 'bin/perl'), '-MGetopt::Long', '-MFile::Spec', '-MDigest::SHA', '-e', 'use strict; use warnings; print "Bundled Perl $^V OK\\n";'], check=True) print('PASS: staged independent Perl/core-module runtime for ' + ', '.join(prefixes)) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('runtime', type=Path) parser.add_argument('--prefix', action='append', required=True, help='ARCH=installed-prefix (repeat per architecture)') args = parser.parse_args() prefixes = {} for item in args.prefix: arch, separator, value = item.partition('=') if not separator or arch not in ('arm64', 'x86_64') or arch in prefixes: parser.error('Expected one ARCH=prefix per architecture') prefixes[arch] = Path(value).resolve() stage(args.runtime.resolve(), prefixes) if __name__ == '__main__': main()