Is there or will there ever be an open source version of Swift for Arduino?
Swift for Arduino is open source! The compiler frontend is standard swift (see links below) and the compiler backend is llvm (see below). We use the gcc AVR binutils to link the code and avrdude to upload it to an arduino, all of which are 100% open source and our own builds (currently) have no significant source code modifications. When/if we build patches in the future for swiftc and llvm we are planning to release them as open source, in some appropriate place for people who like to make their own compiler. The small bugfixes/tests/documentation/etc. contributions Carl made to LLVM are all in the standard upstream LLVM tree or are going through the standard process for submission, vetting, etc. as with any big open source project (much like you would with Linux).
That said, very few people can be bothered to compile their own compiler! Both swiftc and llvm can take many hours to compile even on our super fast 2017 Macbook Pro (depending on config) and it’s a very fiddly process as you’re effectively building a cross compiler on the back end, arguably even a Candian Cross (it has to work on old macs). This stuff isn’t trivial. Plus the compiler and full toolchain is very hard for many people to use without an editor/IDE.
The IDE that is for sale on the website is our own Mac OSX app for simplifying Swift for Arduino use, which we want to release to the mac app store for sale soon. But in theory you don’t need it to program for Swift on Arduino, it just wraps up a bundle with an editor, version 3.1.1 of swiftc for OSX, a version of llvm and a handy component library and makes it all hopefully much, much easier to use.
How do I build the AVR compatible version of the swift compiler?Disclaimer: this is really only for propellerheads, it does not make a complete working tool and is only really for completeness because I don't want to mess with the readme file in github.com/carlos4242/swift.
The swift you build will just be a partial, non functioning compiler (it needs a standard library) and will be of little use except to compiler nerds.
- (read this in conjunction with the swift build instructions which are more correct)
- Check what the swift build needs for dependencies, e.g. CMake, Ninja and Xcode on your machine
- Open a terminal, make a directory to hold your swift code tree, like
- From github, clone the swift repository into this area. Use something like
git clone -b release/5.3 https://github.com/carlos4242/swift
- Note, you will end up with something like
.../Code/swift/swiftthis is how it should be because swift will check out other repositories adjacent to swift for the other libraries it needs like llvm and clang
- Use the update checkout script to get all the dependencies
- utils/update-checkout --clone --scheme release/5.3 --skip-repository swift-tools-support-core
(this will take a fairly long time)
- After that is finished, change directory into the swift folder and use
git checkout avr-support-2to get the avr compatible version of swift
- Make a build directory, we usually put it next to the swift, llvm and clang directories, then change into it
- Now swift is ready to build, the recommended approach from the swift team is to use their build script, we suggest these settings
../swift/utils/build-script --clean -R -S --host-target macosx-x86_64 --cross-compile-hosts embedded-avr --extra-cmake-options="-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=AVR -DLLVM_ENABLE_BACKTRACES=Off"
- This runs the build script in configure only mode, which effectively just runs CMake and sets up build files for ninja
- Finally do the build itself, change into the cmark directory then run
- Next change to llvm and run
ninjait will take a long time
- Finally change to swift and run
ninja swiftto make just the swift executables, not the standard library, which won't compile
- The swift executable will be in build folder in a sub folder like
- (instructions untested, corrections are welcome)