From afb7c504eaf1481628cd3b6e0cb0dbd89f7feb1e Mon Sep 17 00:00:00 2001 From: renaud gaudin Date: Mon, 4 Dec 2023 11:06:58 +0000 Subject: [PATCH] Fixed #667: CoreKiwix for iOS Simulator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When we introduced CoreKiwix.xcframework, we made two mistakes: - We forgot that some devs are on Apple Silicon and did not include arm64 arch for simulator - We kept the `x86_64-apple-ios` target thinking simulator is a “just” an iOS on x64 This target mistake prevented XCode from finding our lib usable with the Simulator on x64. Not including an arm64 version obviously prevented Silicon-using devs from using the Simulator. The included libs changes now from | Name | Content | | ---------------------------- | ----------------------------------------------------- | | `ios-arm64` | All iOS devices are arm64 | | `ios-x86_64-simulator` | Single x64 binary for Simulator | | `macos-arm64_x86_64` | Fat binary for macOS with both x64 and arm64 | To | Name | Content | | ---------------------------- | ----------------------------------------------------- | | `ios-arm64` | All iOS devices are arm64 | | `ios-arm64_x86_64-simulator` | Fsat binary for iOS Simulator with both x64 and arm64 | | `macos-arm64_x86_64` | Fat binary for macOS with both x64 and arm64 | - `iOSx64` Platforn renamed to `iOSx64Simulator` with its target fixed to `x86-apple-ios-simulator` (was `x86_64-apple-ios`) - Added platformn `iOSArm64Simulator` for Apple Silicon devs to run Simulator - `AppleXCFramework` dependency gets two changes: - Depends on all those platforms - `_make_macos_fat()` turned generic and called both for macOS fat binary and iOS Simulator one --- kiwixbuild/dependencies/apple_xcframework.py | 52 +++++++++++++------- kiwixbuild/platforms/ios.py | 15 ++++-- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/kiwixbuild/dependencies/apple_xcframework.py b/kiwixbuild/dependencies/apple_xcframework.py index 986ece7..b7ef11f 100644 --- a/kiwixbuild/dependencies/apple_xcframework.py +++ b/kiwixbuild/dependencies/apple_xcframework.py @@ -9,7 +9,13 @@ from .base import Dependency, NoopSource, Builder as BaseBuilder class AppleXCFramework(Dependency): name = "apple_xcframework" - subPlatformNames = ["macOS_x86_64", "macOS_arm64_static", "iOS_x86_64", "iOS_arm64"] + subPlatformNames = [ + "macOS_x86_64", + "macOS_arm64_static", + "iOS_arm64", + "iOSSimulator_x86_64", + "iOSSimulator_arm64", + ] Source = NoopSource class Builder(BaseBuilder): @@ -23,10 +29,18 @@ class AppleXCFramework(Dependency): target for target in self.all_subplatforms if target.startswith("macOS") ] + @property + def iossimulator_subplatforms(self): + return [ + target + for target in self.all_subplatforms + if target.startswith("iOSSimulator") + ] + @property def ios_subplatforms(self): return [ - target for target in self.all_subplatforms if target.startswith("iOS") + target for target in self.all_subplatforms if target.startswith("iOS_") ] @classmethod @@ -56,13 +70,6 @@ class AppleXCFramework(Dependency): static_ars = [str(f) for f in Path(lib_dir).glob("*.a")] # create merged.a from all *.a in install_dir/lib - command = [ - "libtool", - "-static", - "-o", - "merged.a", - *static_ars - ] run_command(command, lib_dir, context) # will be included in xcframework @@ -71,14 +78,14 @@ class AppleXCFramework(Dependency): return xcf_libs - def _make_macos_fat(self, context): - """create fat merged.a in fake macOS_fat install/lib with macOS archs""" - macos_libs = [] - for target in self.macos_subplatforms: + def make_fat_with(self, platforms, folder_name, context): + """create fat merged.a in {folder_name} install/lib with {platforms} archs""" + libs = [] + for target in platforms: plt = PlatformInfo.get_platform(target) - macos_libs.append(pj(plt.buildEnv.install_dir, "lib", "merged.a")) + libs.append(pj(plt.buildEnv.install_dir, "lib", "merged.a")) - fat_dir = pj(self.buildEnv.build_dir, "macOS_fat") + fat_dir = pj(self.buildEnv.build_dir, folder_name) os.makedirs(fat_dir, exist_ok=True) output_merged = pj(fat_dir, "merged.a") @@ -87,7 +94,7 @@ class AppleXCFramework(Dependency): "-create", "-output", output_merged, - *macos_libs + *libs ] run_command(command, self.buildEnv.build_dir, context) @@ -109,5 +116,16 @@ class AppleXCFramework(Dependency): xcf_libs = [] self.command("remove_if_exists", self._remove_if_exists) xcf_libs += self.command("merge_libs", self._merge_libs) - xcf_libs += self.command("make_macos_fat", self._make_macos_fat) + xcf_libs += self.command( + "make_macos_fat", + self.make_fat_with, + self.macos_subplatforms, + "macOS_fat", + ) + xcf_libs += self.command( + "make_simulator_fat", + self.make_fat_with, + self.iossimulator_subplatforms, + "iOS-simulator_fat", + ) self.command("build_xcframework", self._build_xcframework, xcf_libs) diff --git a/kiwixbuild/platforms/ios.py b/kiwixbuild/platforms/ios.py index fb16e28..89964b9 100644 --- a/kiwixbuild/platforms/ios.py +++ b/kiwixbuild/platforms/ios.py @@ -131,11 +131,20 @@ class iOSArm64(ApplePlatformInfo): min_iphoneos_version = '15.0' -class iOSx64(ApplePlatformInfo): - name = 'iOS_x86_64' +class iOSx64Simulator(ApplePlatformInfo): + name = 'iOSSimulator_x86_64' arch = cpu = 'x86_64' host = 'x86_64-apple-darwin' - target = 'x86_64-apple-ios' + target = 'x86-apple-ios-simulator' + sdk_name = 'iphonesimulator' + min_iphoneos_version = '15.0' + + +class iOSArm64Simulator(ApplePlatformInfo): + name = 'iOSSimulator_arm64' + arch = cpu = 'arm64' + host = 'arm-apple-darwin' + target = 'aarch64-apple-ios-simulator' sdk_name = 'iphonesimulator' min_iphoneos_version = '15.0'