Swift cross compile to single linux binary Swift cross compile to single linux binary swift swift

Swift cross compile to single linux binary


From reading the sources on github

  • target would be Linux
  • machine would be x86_64

This gets called by the primary build script

This how ever answers a part of the question

The exact value for -target seems to be rather elusive.

Install a GCC toolchain for Mac OSX that can retarget Linux, one repo that I can see is OSXCross, for example.

Supply the values to the environment variables to GCC prior to running the script, that references that toolchain.

Unfortunately, that does not guarantee it will work, but give it a try and see what happens.


Is it possible to compile a swift binary from an OS X computer so that it runs on a server running Linux as a single binary without no extra libraries that need to be dynamically linked?

The short answer? Of course it is! Anything is possible when you put your heart to it!

Is it efficient? Inherently, no.

While I'm sure everyone here is familiar with what a compiler does, for the sake of this question and its newest of users, a compiler is an application that converts human readable code and maps it to a binary format that a computer can understand. However it should be worth mentioning that not all computers are the same. Each computer operating system has a different binary mapping than the other so a simple operation like copying values could be expressed as 1010 on one machine, and 0101 on another. As stated before in many questions prior, and for example this one, many programming languages are buildable across a variety of machines, but very few of them are portable across them because each computer has a different binary mapping.

~~~~~~~So how do we fix this?~~~~~~~

  • Well there are a number of solutions to fix this. The most obviousmethod is to simply make your environment the target environment andbuild your program to your hearts content. You obviously already didthis through a virtual machine and this typically what manydevelopers will do. This is by far the easiest solution but itdefeats the purpose of the question where you want to simply buildfrom your OSX machine.

Earlier you said you heard people talking about compiling windows programs on linux machines. Cygwin is a a development platform aimed at obtaining the windows framework that is not normally present on linux machines and allows many programs to be built with the windows framework in mind. However all its doing is adding binaries so that the compiler has some proper places to map to when windows only commands are found within a configuration. All its doing is introducing the binary configurations necessary for a program to successfully be ported over. This ties into the second option.

  • Secondly comes a compiler that supports cross platform compilation.While I am currently uninformed and/or unfamiliar with suchcompilers, this is technically a valid solution but would I call itreliable? Probably not. If anything you're just adding more work toyour compiler as not only does it have to correctly map the syntax ofone computer program for one computer, it has to waste time linkingit to the new binaries. Additionally you would need to have thecompiler remember these linking's which could entail more wastedmemory space for having this one compiler.

Even then, systems like these are few and far between and whether its guaranteed to work depends upon how well the compilers maintainer knows their stuff, how frequently they update it etc. etc. And the chances that they even performed to correct mappings of binaries in the first place is not something I'd stake my life on.

  • The third and perhaps most ideal solution is to look into container technologies such as docker. Their containers are essentially ways for you to build your app and port it to new machines without having to change or modify anything about how you build and compile it. Simply build one, store it in a container, port it to your machine of choice and integrate it into your current project. Come to think of it, container systems like docker were built to prevent the very thing that you are currently experiencing, where you have a source code working on your one machine but no place else. Something like docker will be able to run your code on any machine without having to recompile it for each new machine.

Docker provides an interesting framework for container to container communication, application examples and has a fairly straightforward documentation that it would be worthwhile to look at to see if your project could have some of its parts ported to docker.

This being said, there are multiple ways to fix the issue you are currently facing, so as you being a software engineer, its up to you in what would be the most ideal way of handling your project.

// EDIT //

Will edit this to be a better response once im not drop-dead tired.