"""macOS release implementation shared by packaging and smoke tools.""" from pathlib import Path import shutil import sys import tempfile from common.build import build_application from common.files import file_sha256 from common.process import ROOT, run MACH_O = {b'\xfe\xed\xfa\xce', b'\xce\xfa\xed\xfe', b'\xfe\xed\xfa\xcf', b'\xcf\xfa\xed\xfe', b'\xca\xfe\xba\xbe', b'\xbe\xba\xfe\xca', b'\xca\xfe\xba\xbf', b'\xbf\xba\xfe\xca'} BUNDLES = {'.framework', '.appex', '.xpc', '.app'} def require_macos(): if sys.platform != 'darwin': raise RuntimeError('This operation requires macOS.') def sign_app(app, *, environment, root=ROOT): root = Path(root).resolve() app = Path(app).resolve() contents = app / 'Contents' if not contents.is_dir(): raise RuntimeError('Missing macOS application bundle.') paths = sorted(contents.rglob('*')) if any(path.is_symlink() and not path.resolve().is_relative_to(app) for path in paths): raise RuntimeError('Application symlink escapes the bundle.') engine = contents / 'Frameworks/FlutterMacOS.framework/Versions/A/FlutterMacOS' if not engine.is_file(): raise RuntimeError('Missing Flutter macOS engine.') commands = run(['/usr/bin/otool', '-l', engine], cwd=root, env=environment, capture_output=True, text=True, timeout=60).stdout if 'path /usr/local/lib/. (offset' in commands: run(['/usr/bin/install_name_tool', '-delete_rpath', '/usr/local/lib/.', engine], cwd=root, env=environment) identity = environment.get('CUTDEX_CODESIGN_IDENTITY') or '-' arguments = ['codesign', '--force', '--sign', identity] arguments += ['--timestamp=none'] if identity == '-' else ['--timestamp', '--options', 'runtime'] for path in paths: if path.is_file() and not path.is_symlink(): with path.open('rb') as stream: native = stream.read(4) in MACH_O if native: run([*arguments, '--preserve-metadata=entitlements', path], cwd=root, env=environment) bundles = [path for path in paths if path.is_dir() and not path.is_symlink() and path.suffix in BUNDLES] for bundle in sorted(bundles, key=lambda path: (len(path.parts), str(path)), reverse=True): run([*arguments, '--preserve-metadata=entitlements', bundle], cwd=root, env=environment) run([*arguments, '--entitlements', root / 'macos/Runner/Release.entitlements', app], cwd=root, env=environment) def create_dmg(app, output, *, environment, root=ROOT): root = Path(root).resolve() app, output = Path(app).resolve(), Path(output).absolute() icon = app / 'Contents/Resources/AppIcon.icns' if not (app / 'Contents/Info.plist').is_file() or not icon.is_file(): raise RuntimeError('Missing macOS application or application icon.') if output.suffix != '.dmg': raise RuntimeError('Output must have a .dmg extension.') if output.resolve().is_relative_to(app): raise RuntimeError('DMG output must be outside the application bundle.') output.parent.mkdir(parents=True, exist_ok=True) stage = Path(tempfile.mkdtemp(prefix='cutdex-dmg-', dir=environment.get('TMPDIR'))).resolve() source, mount, image = stage / 'source', stage / 'mounted', stage / 'uncompressed.dmg' mounted = False try: source.mkdir() mount.mkdir() run(['ditto', app, source / app.name], cwd=root, env=environment) (source / 'Applications').symlink_to('/Applications', target_is_directory=True) run(['ditto', icon, source / '.VolumeIcon.icns'], cwd=root, env=environment) run(['hdiutil', 'create', '-quiet', '-format', 'UDRW', '-volname', 'Cutdex', '-srcfolder', source, image], cwd=root, env=environment) run(['hdiutil', 'attach', '-quiet', '-readwrite', '-nobrowse', '-mountpoint', mount, image], cwd=root, env=environment) mounted = True run(['xcrun', 'SetFile', '-a', 'C', mount], cwd=root, env=environment) run(['hdiutil', 'detach', '-quiet', mount], cwd=root, env=environment) mounted = False run(['hdiutil', 'convert', '-quiet', image, '-format', 'UDZO', '-o', output, '-ov'], cwd=root, env=environment) run(['hdiutil', 'verify', '-quiet', output], cwd=root, env=environment) run(['swift', root / 'tool/macos/set_macos_file_icon.swift', icon, output], cwd=root, env=environment) finally: if mounted: detached = run(['hdiutil', 'detach', '-quiet', mount], cwd=root, env=environment, check=False, capture_output=True) mounted = detached.returncode != 0 if mounted: print(f'DMG is still mounted; temporary directory retained: {stage}', file=sys.stderr) else: shutil.rmtree(stage) return output def _archive(app, output, environment, root): output.unlink(missing_ok=True) run(['ditto', '-c', '-k', '--sequesterRsrc', '--keepParent', app, output], cwd=root, env=environment) def _notarize(path, profile, environment, root): run(['xcrun', 'notarytool', 'submit', path, '--keychain-profile', profile, '--wait'], cwd=root, env=environment) run(['xcrun', 'stapler', 'staple', path], cwd=root, env=environment) def build_release(runtime, output, *, environment, root=ROOT): root, runtime, output = Path(root).resolve(), Path(runtime).resolve(), Path(output).resolve() app = (root / 'build/macos/Build/Products/Release-production/cutdex.app').resolve() if not app.is_relative_to(root): raise RuntimeError('Build product must remain inside the project.') if not runtime.is_dir() or runtime.is_relative_to(app): raise RuntimeError('Provide a processing directory outside the application bundle.') if output.is_relative_to(app) or output.is_relative_to(runtime): raise RuntimeError('Output must be outside the application and processing directories.') profile = environment.get('CUTDEX_NOTARY_PROFILE') if profile and (environment.get('CUTDEX_CODESIGN_IDENTITY') or '-') == '-': raise RuntimeError('CUTDEX_NOTARY_PROFILE requires a Developer ID signing identity.') feed, key = environment.get('CUTDEX_UPDATE_FEED_URL'), environment.get('CUTDEX_UPDATE_PUBLIC_KEY') if bool(feed) != bool(key): raise RuntimeError('Set both CUTDEX_UPDATE_FEED_URL and CUTDEX_UPDATE_PUBLIC_KEY.') python = sys.executable run([python, root / 'tool/macos/check_macos_release.py', '--runtime', runtime], cwd=root, env=environment) run(['/bin/sh', root / 'tool/macos/check_macos_build_target.sh', app / 'Contents/MacOS/cutdex'], cwd=root, env=environment) embedded = app / 'Contents/Resources/processing' if not embedded.parent.resolve().is_relative_to(app): raise RuntimeError('Embedded runtime escapes the application bundle.') # A stale bundle may contain links. Never recursively delete their targets. if embedded.is_symlink(): embedded.unlink() elif embedded.exists(): if not embedded.resolve().is_relative_to(app): raise RuntimeError('Embedded runtime escapes the application bundle.') shutil.rmtree(embedded) build_application('macos', environment=environment, root=root) if not embedded.parent.resolve().is_relative_to(app): raise RuntimeError('Embedded runtime escapes the application bundle.') embedded.parent.mkdir(parents=True, exist_ok=True) output.mkdir(parents=True, exist_ok=True) run(['ditto', runtime, embedded], cwd=root, env=environment) if feed: run([python, root / 'tool/macos/macos_updates.py', 'configure', '--app', app, '--feed', feed, '--public-key', key], cwd=root, env=environment) sign_app(app, environment=environment, root=root) run([python, root / 'tool/macos/check_macos_release.py', '--app', app, '--report', output / 'package-check.json'], cwd=root, env=environment) archive, dmg = output / 'cutdex-macos14-arm64.zip', output / 'cutdex-macos14-arm64.dmg' _archive(app, archive, environment, root) if profile: run(['xcrun', 'notarytool', 'submit', archive, '--keychain-profile', profile, '--wait'], cwd=root, env=environment) run(['xcrun', 'stapler', 'staple', app], cwd=root, env=environment) _archive(app, archive, environment, root) create_dmg(app, dmg, environment=environment, root=root) if profile: _notarize(dmg, profile, environment, root) run(['xcrun', 'stapler', 'validate', dmg], cwd=root, env=environment) return app, archive, dmg def print_artifact(label, path): print(f'{label}: {path}') print(f'{file_sha256(path)} {path}')