vgorloff / swift-everywhere-toolchain

Automated workflow to compile Swift Toolchain, for making Android apps with Swift.
MIT License
85 stars 25 forks source link

Swift package using C sources fail to link #78

Closed gshahbazian closed 3 years ago

gshahbazian commented 4 years ago

Hey @vgorloff thanks for all the great work here!

I am running into a linker error when importing a swift package that wraps a C library. Package being imported: https://github.com/gshahbazian/SwiftGumbo

Exports.swift:

...
import SwiftGumbo

@_cdecl("Java_com_home_helloNDK_SwiftLib_sayHello")
public func sayHello() {
   let g = SwiftGumbo(html: #"<html><body><h1>HEY <span>WHATS</span> UP</h1><p>Nothing</p></body></html>"#)
   let text = g.root.textContent

   AndroidLogger.info("SA - SwiftCore: Works! \(text)")
   probeDispatch()
   probeOperation()
   probeSerialization()
   tester.test()
}
...

Package.swift:

// swift-tools-version:5.0

import PackageDescription

let package = Package(
   name: "HelloJNI",
   products: [
      .library(name: "HelloJNICore", type: .dynamic, targets: ["HelloJNICore"]),
      .library(name: "NDKLog", targets: ["NDKLog"])
   ],
   dependencies: [
      .package(url: "https://github.com/gshahbazian/SwiftGumbo", .exact("1.0.0")),
   ],
   targets: [
      .target(name: "HelloJNICore", dependencies: ["NDKLog", "SwiftGumbo"]),
      .target(name: "NDKLog", dependencies: ["sysNDKLog"]),
      .systemLibrary(name: "sysNDKLog"),
   ]
)

Compile error:

...
[18/19] Compiling HelloJNICore Exports.swift
<unknown>:0: error: error opening input file '/Users/gabepop/Downloads/swift-everywhere-samples-master/Android/app/build/swift-x86_64/x86_64-apple-macosx/release/CGumboParser.build/ascii.c.o' (Don't know how to extract from object fileformat)
<unknown>:0: error: swift-autolink-extract command failed with exit code 1 (use -v to see invocation)
[18/19] Linking libHelloJNICore.dylib
vgorloff commented 3 years ago

The debug configuration still broken. But i was able to build SwiftGumbo release configuration with following command:

export SWIFT_EXEC=/PathToDownloadedToolchain/swift-android-toolchain/usr/bin/swiftc-arm-linux-androideabi
swift build -c release --destination ./android-armv7.json

Where ./android-armv7.json is below:

{
  "version": 1,
  "sdk": "/PathToDownloadedToolchain/swift-android-toolchain",
  "toolchain-bin-dir": "/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/bin",
  "target": "armv7-none-linux-androideabi",
  "dynamic-library-extension": "so",
  "extra-cc-flags": [
    "-v",
    "-I/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include",
    "-I/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/arm-linux-androideabi"
  ],
  "extra-swiftc-flags": [
    "-v",
    "-tools-directory",
    "/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/bin",
    "-Xcc",
    "-I/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include",
    "-Xcc",
    "-I/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/include/arm-linux-androideabi",
    "-Xclang-linker",
    "--sysroot=/usr/local/ndk/20.1.5948944/platforms/android-24/arch-arm",
    "-Xclang-linker",
    "--gcc-toolchain=/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64",
    "-L",
    "/usr/local/ndk/20.1.5948944/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a",
    "-L",
    "/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x",
    "-L",
    "/usr/local/ndk/20.1.5948944/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi/24",
    "-L",
    "/PathToDownloadedToolchain/swift-android-toolchain/usr/lib/swift/android/armv7"
  ],
  "extra-cpp-flags": [
    "-v"
  ]
}
vgorloff commented 3 years ago

This is an archived sample project with two build scripts (for macOS and Android). SwiftGumboTest.zip

vgorloff commented 3 years ago

Also possible now (fix still in develop branch) to build without --destination flag.

cd SwiftGumboProject
/...../swift-android-toolchain/usr/bin/swift-build-arm-linux-androideabi -c release
vgorloff commented 3 years ago

Ok. The issue with SPM Debug Builds now fixed in https://github.com/vgorloff/swift-everywhere-toolchain/releases/tag/1.0.57. Closing issue.

gshahbazian commented 3 years ago

Thanks for all your work on this @vgorloff!