|
| 1 | +// Copyright (c) 2025 SQLite Cloud, Inc. |
| 2 | +// Licensed under the Elastic License 2.0 (see LICENSE.md). |
| 3 | + |
| 4 | +import 'dart:io'; |
| 5 | + |
| 6 | +import 'package:code_assets/code_assets.dart'; |
| 7 | +import 'package:hooks/hooks.dart'; |
| 8 | +import 'package:path/path.dart' as p; |
| 9 | + |
| 10 | +void main(List<String> args) async { |
| 11 | + await build(args, (input, output) async { |
| 12 | + if (!input.config.buildCodeAssets) return; |
| 13 | + |
| 14 | + final codeConfig = input.config.code; |
| 15 | + final os = codeConfig.targetOS; |
| 16 | + final arch = codeConfig.targetArchitecture; |
| 17 | + |
| 18 | + final binaryPath = _resolveBinaryPath(os, arch, codeConfig); |
| 19 | + if (binaryPath == null) { |
| 20 | + throw UnsupportedError( |
| 21 | + 'sqlite_sync does not support $os $arch.', |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + final nativeLibDir = p.join( |
| 26 | + input.packageRoot.toFilePath(), |
| 27 | + 'native_libraries', |
| 28 | + ); |
| 29 | + final file = File(p.join(nativeLibDir, binaryPath)); |
| 30 | + if (!file.existsSync()) { |
| 31 | + throw StateError( |
| 32 | + 'Pre-built binary not found: ${file.path}. ' |
| 33 | + 'Run the CI pipeline to populate native_libraries/.', |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + output.dependencies.add(file.uri); |
| 38 | + |
| 39 | + final assetFile = await _prepareAssetFile( |
| 40 | + input: input, |
| 41 | + os: os, |
| 42 | + arch: arch, |
| 43 | + config: codeConfig, |
| 44 | + file: file, |
| 45 | + ); |
| 46 | + |
| 47 | + output.assets.code.add( |
| 48 | + CodeAsset( |
| 49 | + package: input.packageName, |
| 50 | + name: 'src/native/sqlite_sync_extension.dart', |
| 51 | + linkMode: DynamicLoadingBundled(), |
| 52 | + file: assetFile.uri, |
| 53 | + ), |
| 54 | + ); |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +Future<File> _prepareAssetFile({ |
| 59 | + required BuildInput input, |
| 60 | + required OS os, |
| 61 | + required Architecture arch, |
| 62 | + required CodeConfig config, |
| 63 | + required File file, |
| 64 | +}) async { |
| 65 | + if (os != OS.iOS || config.iOS.targetSdk == IOSSdk.iPhoneOS) { |
| 66 | + return file; |
| 67 | + } |
| 68 | + |
| 69 | + final thinArch = switch (arch) { |
| 70 | + Architecture.arm64 => 'arm64', |
| 71 | + Architecture.x64 => 'x86_64', |
| 72 | + _ => null, |
| 73 | + }; |
| 74 | + if (thinArch == null) { |
| 75 | + return file; |
| 76 | + } |
| 77 | + |
| 78 | + final outputName = 'cloudsync_ios_sim_$thinArch.dylib'; |
| 79 | + final outputFile = File.fromUri(input.outputDirectory.resolve(outputName)); |
| 80 | + await outputFile.parent.create(recursive: true); |
| 81 | + |
| 82 | + final result = await Process.run('/usr/bin/lipo', [ |
| 83 | + file.path, |
| 84 | + '-thin', |
| 85 | + thinArch, |
| 86 | + '-output', |
| 87 | + outputFile.path, |
| 88 | + ]); |
| 89 | + if (result.exitCode != 0) { |
| 90 | + throw StateError( |
| 91 | + 'Failed to thin sqlite_sync iOS simulator binary for $thinArch: ' |
| 92 | + '${result.stderr}', |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + return outputFile; |
| 97 | +} |
| 98 | + |
| 99 | +String? _resolveBinaryPath(OS os, Architecture arch, CodeConfig config) { |
| 100 | + if (os == OS.android) { |
| 101 | + return switch (arch) { |
| 102 | + Architecture.arm64 => 'android/cloudsync_android_arm64.so', |
| 103 | + Architecture.arm => 'android/cloudsync_android_arm.so', |
| 104 | + Architecture.x64 => 'android/cloudsync_android_x64.so', |
| 105 | + _ => null, |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + if (os == OS.iOS) { |
| 110 | + final sdk = config.iOS.targetSdk; |
| 111 | + if (sdk == IOSSdk.iPhoneOS) { |
| 112 | + return 'ios/cloudsync_ios_arm64.dylib'; |
| 113 | + } |
| 114 | + // Simulator: fat binary (arm64 + x64) |
| 115 | + return 'ios-sim/cloudsync_ios-sim.dylib'; |
| 116 | + } |
| 117 | + |
| 118 | + if (os == OS.macOS) { |
| 119 | + return switch (arch) { |
| 120 | + Architecture.arm64 => 'mac/cloudsync_mac_arm64.dylib', |
| 121 | + Architecture.x64 => 'mac/cloudsync_mac_x64.dylib', |
| 122 | + _ => null, |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + if (os == OS.linux) { |
| 127 | + return switch (arch) { |
| 128 | + Architecture.x64 => 'linux/cloudsync_linux_x64.so', |
| 129 | + Architecture.arm64 => 'linux/cloudsync_linux_arm64.so', |
| 130 | + _ => null, |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + if (os == OS.windows) { |
| 135 | + return switch (arch) { |
| 136 | + Architecture.x64 => 'windows/cloudsync_windows_x64.dll', |
| 137 | + _ => null, |
| 138 | + }; |
| 139 | + } |
| 140 | + |
| 141 | + return null; |
| 142 | +} |
0 commit comments