ttypic / swift-klib-plugin

Gradle Plugin for injecting Swift code into Kotlin Multiplatform Mobile shared module
MIT License
159 stars 8 forks source link

Incorrect build target selected #24

Open markst opened 5 months ago

markst commented 5 months ago

Appears the incorrect build target is used.

When attempting to import a iOS dependency such as:

import UIKit

The following output occurs:

<unknown>:0: warning: using sysroot for 'iPhoneSimulator' but targeting 'MacOSX'
<unknown>:0: error: unable to load standard library for target 'arm64-apple-macosx12.0'
error: Process 'command 'xcrun'' finished with non-zero exit value 1

The target should be an iOS target such as arm64-apple-ios11.0-simulator

markst commented 5 months ago

Full output:

> Task :composeApp:swiftklibHelloSwiftIosSimulatorArm64 FAILED
error: 'swiftbuild': Invalid manifest (compiled with: ["/Applications/Xcode-14.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc", "-vfsoverlay", "/var/folders/p9/3bx5r6fx0_3grq0pp4rzkqnm0000gn/T/TemporaryDirectory.0vVABL/vfs.yaml", "-L", "/Applications/Xcode-14.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI", "-lPackageDescription", "-Xlinker", "-rpath", "-Xlinker", "/Applications/Xcode-14.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI", "-target", "arm64-apple-macosx12.0", "-sdk", "/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.4.sdk", "-F", "/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks", "-I", "/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib", "-L", "/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib", "-swift-version", "5", "-I", "/Applications/Xcode-14.3.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/pm/ManifestAPI", "-sdk", "/Applications/Xcode-14.3.0.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.4.sdk", "-package-description-version", "5.5.0", "{removed}/composeApp/build/swiftklib/HelloSwift/iosSimulatorArm64/swiftBuild/Package.swift", "-Xfrontend", "-disable-implicit-concurrency-module-import", "-Xfrontend", "-disable-implicit-string-processing-module-import", "-o", "/var/folders/p9/3bx5r6fx0_3grq0pp4rzkqnm0000gn/T/TemporaryDirectory.zdr69v/swiftbuild-manifest"])
<unknown>:0: warning: using sysroot for 'iPhoneSimulator' but targeting 'MacOSX'
<unknown>:0: error: unable to load standard library for target 'arm64-apple-macosx12.0'
error: Process 'command 'xcrun'' finished with non-zero exit value 1
markst commented 5 months ago

Worth mentioning that it is possible to use swift build to build iOS targets. https://forums.swift.org/t/use-swiftpm-to-build-ios-target/25436/13

For instance I can build my local swift package

mt@mt-imac MyLibrary % xcrun swift build -c release --arch arm64 -Xswiftc -sdk -Xswiftc `xcrun --sdk iphonesimulator --show-sdk-path` -Xswiftc -target -Xswiftc arm64-apple-ios11.0-simulator

Building for production...
Build complete! (0.15s)

Which imports UIKit:

import UIKit

public struct MyLibrary {
    public private(set) var text = "Hello, World!"

    public init() {
    }
}
markst commented 5 months ago

Here's my Target Configuration:

kotlin {
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach { iosTarget ->
        iosTarget.compilations {
            val main by getting {
                cinterops {
                    create("HelloSwift")
                }
            }
        }
        iosTarget.binaries.framework {
            baseName = "ComposeApp"
            isStatic = true
        }
    }
markst commented 5 months ago

seems I can get it to build if I try setting a high number for minMacos

minMacos = 99

and only defining the iosSimulatorArm64:

kotlin {
    listOf(
        iosSimulatorArm64()
    ).forEach { iosTarget ->

which works when syncing gradle. however it appears to fail again when attempting to build a second time.

arcslash commented 5 months ago

You can compile the native swift module to the exact build by running, if you are running on real device,

./gradlew swiftklibNativeModuleIosArm64

If it's simulator

./gradlew swiftklibNativeModeuleIosSimulatorArm64

Task names might be different, check all tasks using

./gradlew :shared:tasks --all 

Might not be a exact fix, but seems like it's not getting compiled in the build - so have to do it separately.

Hope this might help.

realcbnewham commented 3 months ago

I get exactly the same problems, although mine is around the other way to your error message:

:0: warning: using sysroot for 'MacOSX' but targeting 'iPhone' and I get problems such as: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.4.sdk/System... It wants to target MacOS and the suggested fixes (which they really aren't) do not change anything. @markst did you find a solution?