How to Build a Swift Executable for Linux on Macos

How to build a swift executable for Linux on macOS

This just means it couldn't locate the linked library. If your libswiftCore.so located at /usr/lib/swift/linux you can run LD_LIBRARY_PATH=/usr/lib/swift/linux ./<your executable for linux> and it will work like a charm.

You can also set LD_LIBRARY_PATH variable to just execute the binary.

Cross-compile Swift code for Raspberry Pi on macOS

To answer my own question, the current best solution (very recent) is:
https://github.com/CSCIX65G/swift-mac2arm-x-compile-toolchain

That provides the needed toolchains, etc. for building on macOS for the R Pi.
Best instructions for remote debugging (using lldb) can be found here:
https://lldb.llvm.org/use/remote.html

Note that on macOS you need to use the version of lldb provided by the toolchain, e.g.:

[path_to_toolchains]/Toolchains/arm64-swift.xctoolchain/usr/bin/lldb -o "platform select remote-linux" -o "platform connect connect://ipaddress:port" -o "file ./remoteProgram"

Still looking to connect the lldb debugger to Xcode run on the Mac. If that can be done, the development cycle is complete.

Swift package wrap a C library for macOS and Linux

Ok, the approach I took was to rely on the user to link the Firebird library, as "Arioch 'The" said.
The correct way to do it (in my opinion) is to specify the pkg-config file used for swift. In my case, I would have been libfbclient.pc.

To make swift use pkg-config, replace the following in the package description target:

.systemLibrary(name: "CFirebird", pkgConfig: "libfbclient")

Swift will retrieve the flags from pkg-config to make compilation possible.

How to compile Swift from command line for distribution

You can solve this for the cases where you only use the standard library using -static-stdlib.

When you compile a script with no options, the final executable contains rpaths to the various Swift standard libs, which you can verify using otool.

> swiftc menu.swift 
> otool -L menu
menu:
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1348.28.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)
/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1504.75.0)
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 48.0.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.25.0)
@rpath/libswiftAppKit.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCore.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCoreData.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftCoreImage.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftDarwin.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftDispatch.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftFoundation.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftIOKit.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftQuartzCore.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftSwiftOnoneSupport.dylib (compatibility version 1.0.0, current version 800.0.63)
@rpath/libswiftXPC.dylib (compatibility version 1.0.0, current version 800.0.63)

Using -static-stdlib ensures that the standard libraries are linked to as required.

> swiftc -static-stdlib menu.swift 
> otool -L menu
menu:
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1348.28.0)
/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.4.0)
/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1349.25.0)
/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1504.75.0)
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 48.0.0)
/System/Library/Frameworks/CoreData.framework/Versions/A/CoreData (compatibility version 1.0.0, current version 752.8.0)
/System/Library/Frameworks/CoreGraphics.framework/Versions/A/CoreGraphics (compatibility version 64.0.0, current version 1070.13.0)
>

I still do not know how to link 3rd party frameworks - but the above steps solves my original problem.

Related Linux Question - Compile Swift script with static Swift core library



Related Topics



Leave a reply



Submit