Cross-compile Swift code for Raspberry Pi on macOS - swift

What is the easiest / best way to use a macOS Xcode setup to build code for a Raspberry Pi (3b+)?
Some of the builds can be pretty heavy, thus building them on the Raspberry would take too long. Thus it would be preferred to build on macOS. How should I go about doing this?
Note: I'm aware that there is quite some information out there.
However, some of it is a few years old, and Swift is changing quickly.

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.

Related

Xcode macOS Development Target version and actual compatibility

I wrote a screensaver in Swift.
Although I had set the macOS Deployment Target to macOS 10.13, it failed to run on an x86_64 Mid 2010 Mac Pro running macOS High Sierra 10.13.6.
Meanwhile, it runs fine on my arm64 2020 MBP M1 with macOS Big Sur 11.5.2, and on an x86_64 2019 MacBook Air with macOS Catalina 10.15.6.
The screensaver installs fine on all machines, but on the machine running High Sierra it says that the screensaver is not compatible.
The error message shown is as follows ("vibe" is the name of the screensaver that I made. So anyone else running across this same problem will see a similar message but with the name of their own screensaver in place of that of course):
You cannot use the vibe screen saver with this version of macOS.
Please contact the vendor to get a newer version of the screen saver.
I am wondering if there is a way to automatically figure out if there are any APIs or other features that my code is using which were not available in macOS 10.13. And also I am wondering if it is usual that you can build for a given macOS Deployment Target and then when you try to run the program on a machine that is within the range that should be supported, the program will not run after all. I had thought that there would be a build time error to build for a macOS Deployment Target if the built program will not run on it.
I am using Xcode 12.5.1 running on my MBP M1, macOS 11.5.2, and as mentioned I am building for macOS Deployment Target set to macOS 10.13. The screensaver runs fine on both Intel with macOS 10.15 and ARM with macOS 11.5. I am using Swift Language Version Swift 5.
Solution
Install "Swift 5 Runtime Support for Command Line Tools" from https://support.apple.com/kb/dl1998?locale=en_US on the macOS 10.13 machine I wanted to run the screensaver on.
Background, details and alternative solution
Thanks to the comment of #Alexander, where he suggested:
Have you tried poking around Console.app to see if anything intersting was logged?
I was able to figure it out.
In Console.app, in the default view that shows device messages for the computer, I applied a search filter for any occurence of the name of the screensaver and then in System Preferences > Desktop & Screen Saver, I selected my screensaver and clicked "Preview" hoping that this would result in something in the logs. And it did!
Relevant log message:
Error loading /Users/erikn/Library/Screen Savers/vibe.saver/Contents/MacOS/vibe: dlopen(/Users/erikn/Library/Screen Savers/vibe.saver/Contents/MacOS/vibe, 265): Library not loaded: #rpath/libswiftCore.dylib
Referenced from: /Users/erikn/Library/Screen Savers/vibe.saver/Contents/MacOS/vibe
Reason: image not found
With this in hand, I was then able to find a similar root cause and some suggested solutions at dyld: Library not loaded: #rpath/libswiftCore.dylib / Image not found
Which amount to setting "Always Embed Swift Standard Libraries" to YES, and possibly also adding #executable_path/Frameworks to Runpath Search Paths.
This may be one acceptable way of doing it.
However, not knowing the specifics of how this will interact on other systems and in the future I was a bit hesitant.
Fortunately, I then found https://developer.apple.com/forums/thread/687847 which while not directly relevant directed me to look for "Swift 5 Runtime Support for Command Line Tools", as this would provide the missing libswiftCore.dylib for macOS 10.13.

Swift programming without using mac

I am new to mobile application development and I have decided to use Swift for iOS development but I don't want to buy a Mac , can I install macOS on another computer and start developing?
Thanks
If you want to get started with Swift programming this is also possible on Windows. There is an open source Swift Compiler for Windows. This is basically useful for learning Swift and possibly for writing desktop applications, e.g. using wxSwift, a binding from the portable GUI library wxWidgets to Swift. If you want to program mobile applications with Swift you'll end up with MacOS, but it can possibly be setup as a virtual machine or installed on a regular PC hardware. See also: https://learnappmaking.com/develop-ios-apps-on-windows-pc/
Of course, it is also possible to program Swift on Ubuntu Linux as officially supported by the Swift project. However, Xcode is not available for Linux, so it is basically useful to learn the language

How can I compile my own copy of Swift from source for Raspbian

I see that I can get a pre-built version of Swift 4.1.2 from swift-arm.com: Swift 4.1.2 Updated For RaspberryPi 2/3
However, I'm inherently distrusting of anonymously posted binaries, and would like to understand how I can build it myself.
I also want (or think I want) to keep using Raspbian, rather than switch to Ubuntu.
If you go to https://github.com/uraimo/buildSwiftOnARM you will find all the info you need to build your own Swift on Raspbian.
There is also an updated Repository for Swift installs on Arm Devices including the Raspberry Pi. - https://swift-arm.com/2019/01/07/official-swift-arm-community-releases/
The Raspbian builds on the repo are from uraimo that I have packaged myself.
I understand your "inherently distrusting" but these are not anonymously posted binaries but the work of the "swift on arm" community.
You are welcome to join the swift-arm slack and talk to the people doing the building. They are more than happy to help you achieve your own builds.
https://launchpass.com/swift-arm
Neil.

What is fake-libgcc? Is fake-libgcc required in order to compile on iPhone?

I started out developing on C. I know what gcc is, libgcc, and so on . I've done Windows/.NET development for years, and now I am playing with an iphone.
I understand that it's possible to compile apps on the iphone itself.
Wanted to start with a hello,world. I have openssh on the phone, and am connected from the Windows machine via putty.exe.
using Cydia to install GCC, it tells me "Cannot Comply".
The post I cited above says I may need fake-libgcc. Why? What's fake about it, and why do I need it? If this is a pre-req for gcc, why doesn't the GCC package in Cydia install it automatically?
I just barely got this figured out myself, and since Google doesn't seem to have any recent and sufficient information, I'll go ahead and explain what I've found out even though this question is rather old.
The quick of it is this, gcc needs to see libgcc installed. I installed fake-gcc from here and it let me install gcc, and it appears to be running just fine, however, it still needs the headers. Here is a walkthrough that should help get the appropriate header files so you can get it to work.
As for why this all is, this is what I've been able to gather from searching through Google for a little while. Let me just start by saying, this is pretty much pure speculation, and if someone points me to a more correct reference, I'll gladly update this.
It appears that up through version 2.0 of the toolchain, libgcc was installable through Cydia to provide the basic libraries for gcc. However, since version 3.0, these libraries are (maybe?) now built in by default, so installing libgcc again through Cydia broke things. But as gcc has it as a dependency, it can't be installed until it sees libgcc somewhere.
That's where fake-libgcc comes in. It registers itself as that package, but doesn't really do anything besides that.

iPhone dev on Linux

Actually I have read about that I can write Objective-C app on Linux (using GNUStep).
But my question is, Am I will be able to develop completely iPhone app on Linux machine?
Or I'll need eventually need to use Mac machine?
Especially when I read that there are some some syntax diffs between NeXT/Apple and GCC (according to http://en.wikipedia.org/wiki/Objective-C)
Short answer - yes it can be done but is not as easy as the Mac way.
Duplicate of how-much-of-an-iphone-app-can-be-developed-and-tested-on-linux, which will lead you to still more links, and 75-100 others.
I'd try giving VirtualBox and a Mac OS X guest a shot. Some people say they've been able to run it.
http://forums.virtualbox.org/viewtopic.php?t=2076
If you manage to run it, then you could install X-Code and things should be a lot easier.