|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { execSync } from 'node:child_process'; |
| 5 | +import { logger } from '@rock-js/tools'; |
| 6 | + |
| 7 | +interface SliceConfig { |
| 8 | + target: string; |
| 9 | + /** Additional targets for fat binaries */ |
| 10 | + additionalTargets?: string[]; |
| 11 | +} |
| 12 | + |
| 13 | +const SLICE_CONFIGS: Record<string, SliceConfig> = { |
| 14 | + 'ios-arm64': { |
| 15 | + target: 'arm64-apple-ios15.0', |
| 16 | + }, |
| 17 | + 'ios-arm64_x86_64-simulator': { |
| 18 | + target: 'arm64-apple-ios15.0-simulator', |
| 19 | + additionalTargets: ['x86_64-apple-ios15.0-simulator'], |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * Creates an empty static library for the given target. |
| 25 | + */ |
| 26 | +function createEmptyStaticLib(target: string): string { |
| 27 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'framework-strip-')); |
| 28 | + const tempObj = path.join(tempDir, 'empty.o'); |
| 29 | + const tempLib = path.join(tempDir, 'empty.a'); |
| 30 | + |
| 31 | + // Create empty object file |
| 32 | + execSync( |
| 33 | + `echo "" | xcrun clang -x c -c - -o "${tempObj}" -target ${target}`, |
| 34 | + { |
| 35 | + stdio: 'pipe', |
| 36 | + } |
| 37 | + ); |
| 38 | + |
| 39 | + // Create static library from object file |
| 40 | + execSync(`xcrun ar rcs "${tempLib}" "${tempObj}"`, { |
| 41 | + stdio: 'pipe', |
| 42 | + }); |
| 43 | + |
| 44 | + // Cleanup object file |
| 45 | + fs.unlinkSync(tempObj); |
| 46 | + |
| 47 | + return tempLib; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Creates a fat static library combining multiple architectures. |
| 52 | + */ |
| 53 | +function createFatStaticLib(targets: string[]): string { |
| 54 | + const libs = targets.map((target) => createEmptyStaticLib(target)); |
| 55 | + |
| 56 | + // Create output in a separate temp directory |
| 57 | + const outputDir = fs.mkdtempSync( |
| 58 | + path.join(os.tmpdir(), 'framework-strip-fat-') |
| 59 | + ); |
| 60 | + const outputLib = path.join(outputDir, 'fat.a'); |
| 61 | + |
| 62 | + execSync( |
| 63 | + `xcrun lipo -create ${libs.map((l) => `"${l}"`).join(' ')} -output "${outputLib}"`, |
| 64 | + { |
| 65 | + stdio: 'pipe', |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + // Cleanup individual libs and their directories |
| 70 | + libs.forEach((lib) => { |
| 71 | + fs.unlinkSync(lib); |
| 72 | + fs.rmdirSync(path.dirname(lib)); |
| 73 | + }); |
| 74 | + |
| 75 | + return outputLib; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Strips the binary from an xcframework, keeping only Swift module interfaces. |
| 80 | + * This creates an "interface-only" framework where consumers can import the module |
| 81 | + * but the actual symbols must come from another framework (e.g., BrownfieldLib). |
| 82 | + * |
| 83 | + * @param xcframeworkPath - Path to the .xcframework directory |
| 84 | + */ |
| 85 | +export async function stripFrameworkBinary( |
| 86 | + xcframeworkPath: string |
| 87 | +): Promise<void> { |
| 88 | + if (!fs.existsSync(xcframeworkPath)) { |
| 89 | + throw new Error(`XCFramework not found at: ${xcframeworkPath}`); |
| 90 | + } |
| 91 | + |
| 92 | + const frameworkName = path.basename(xcframeworkPath, '.xcframework'); |
| 93 | + |
| 94 | + logger.info( |
| 95 | + `Stripping binary from ${frameworkName}.xcframework (interface-only)...` |
| 96 | + ); |
| 97 | + |
| 98 | + const slices = fs.readdirSync(xcframeworkPath).filter((entry) => { |
| 99 | + const fullPath = path.join(xcframeworkPath, entry); |
| 100 | + return fs.statSync(fullPath).isDirectory() && entry.startsWith('ios-'); |
| 101 | + }); |
| 102 | + |
| 103 | + for (const sliceName of slices) { |
| 104 | + const frameworkDir = path.join( |
| 105 | + xcframeworkPath, |
| 106 | + sliceName, |
| 107 | + `${frameworkName}.framework` |
| 108 | + ); |
| 109 | + const binaryPath = path.join(frameworkDir, frameworkName); |
| 110 | + |
| 111 | + if (!fs.existsSync(binaryPath)) { |
| 112 | + logger.warn(`No binary found at ${binaryPath}, skipping`); |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + const config = SLICE_CONFIGS[sliceName]; |
| 117 | + if (!config) { |
| 118 | + logger.warn(`Unknown slice type: ${sliceName}, skipping`); |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + const originalSize = fs.statSync(binaryPath).size; |
| 123 | + |
| 124 | + let emptyLib: string; |
| 125 | + if (config.additionalTargets) { |
| 126 | + // Create fat library for multiple architectures |
| 127 | + emptyLib = createFatStaticLib([ |
| 128 | + config.target, |
| 129 | + ...config.additionalTargets, |
| 130 | + ]); |
| 131 | + } else { |
| 132 | + // Create single-arch library |
| 133 | + emptyLib = createEmptyStaticLib(config.target); |
| 134 | + } |
| 135 | + |
| 136 | + // Replace original binary with empty stub |
| 137 | + fs.copyFileSync(emptyLib, binaryPath); |
| 138 | + fs.unlinkSync(emptyLib); |
| 139 | + // Clean up temp directory |
| 140 | + fs.rmdirSync(path.dirname(emptyLib)); |
| 141 | + |
| 142 | + const newSize = fs.statSync(binaryPath).size; |
| 143 | + logger.debug( |
| 144 | + ` ${sliceName}: ${formatBytes(originalSize)} -> ${formatBytes(newSize)}` |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + logger.success(`${frameworkName}.xcframework is now interface-only`); |
| 149 | +} |
| 150 | + |
| 151 | +function formatBytes(bytes: number): string { |
| 152 | + if (bytes < 1024) return `${bytes} B`; |
| 153 | + if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; |
| 154 | + return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; |
| 155 | +} |
0 commit comments