8

I have a GUI wrapper of UNIX command line program. I noticed that Apple announced Apple Silicon Mac. Sooner or later I need to build Universal Binary of my GUI wrapper. But at first, I need to build UNIX command line program for Apple Silicon. Currently I only have Intel-based Mac. Is it possible to compile UNIX command line program for Apple Silicon architecture on Intel-based Mac?

Note: I know Apple provides Apple Silicon Mac mini for $500, but I don't have the a money now.

5
  • for your first step of validating your command line program on arm, compile it for the raspberry pi or similar arm based sbc. this will catch all sort of portability errors. you may even be able to get your gui working by compiling against next step. this is not guaranteed to catch every issue, but a lot of them will show up early ( along wit any linux vs darwn issues which you may not care about ). – hildred Oct 15 '20 at 16:43
  • The MacOS format for executable files does not as far as I know allow for having multiple architectures in the same binary. Hence you need separate compilation. – Thorbjørn Ravn Andersen Nov 28 '20 at 12:52
  • 1
    Uh @ThorbjørnRavnAndersen, a multi architecture binary is called fat. however no compiler makes them so you need separate compilation then merge. – hildred Nov 30 '20 at 5:53
  • 1
    @hildred I looked it up. Mach-O supports multiple architectures in itself, so it "just" require the compiler tool chain to generate them. en.wikipedia.org/wiki/Mach-O#Multi-architecture_binaries – Thorbjørn Ravn Andersen Nov 30 '20 at 11:11
  • @ThorbjørnRavnAndersen, right but afik no compiler creates multi arch binaries, thus a thin Mach-O binary is he default. but with multiple compilers you can merge these to make a fat Mach-O binary – hildred Dec 20 '20 at 22:10
5

The process of building a binary for a different architecture than the one you're running is called cross compilation.

As highlighted by the other answers, this is a relatively common occurence, especially in the history of Apple which switched from 68K processors to Power PC, then to Intel, and now to ARM.

Mind you, anyone building an app for iPhone or iPad on their Mac has been cross-compiling, as those already use ARM architecture while their Mac currently uses the Intel architecture. It's even common to build multi-architecture iOS apps, as various CPU versions actually use different variants of the ARM architecture (armv7, armv8...).

Likewise, it's common to build i386 (Intel 32-bit) and amd64/x64 (Intel 64-bit) variants, either independently or as fat/universal binaries.

The process for cross-compiling and building fat/universal binaries depends a lot on the environment and toolchain you use. If you use Xcode, then it's going to be a matter of changing project settings, and voilà!, it's done.

If you are using Unix-type tools (mostly on the command line), including make or variants, gcc or clang, etc, then it may require more or less tinkering of your build process to achieve all that, with quite a few options to change (architecture, but also header files and libraries locations), as well as adding the step which combines the multiple architecture-dependent binaries into a single fat/universal binary.

So it all really depends on your current build process and the tools you use. But it will definitely be possible.

Of course, this is all based on the code to compile actually being portable, i.e. not making assumptions about things (like integer or pointer sizes) and not requiring any assembler code. That used to be a big issue in the past (especially with pure C code), but nowadays with the number of architectures software is currently built for, it's much less common to encounter issues with this.

1
  • Thanks for your explanation. All 3 answer are good enough, but I felt your answer is the best!! – xanadu6291 Oct 16 '20 at 0:26
5

The whole point of Universal Binaries is that you can compile the same code for 2 architectures, one of which (by necessity) is not the hardware you're running.

It has previously been possible to compile Universal Binaries for PPC and Intel, or 32-bit and 64-bit, regardless of the hardware doing the compiling.

Apple's documentation on porting to Apple Silicon (AS) is here: https://developer.apple.com/documentation/xcode/porting_your_macos_apps_to_apple_silicon

Note the diagram which has the words "Any Mac computer".

enter image description here

The Apple Silicon Mac mini is for developers to test that their AS code (and Apple's) runs well. There are no AS Macs available for purchase by non-developers (as of 14th October).

Apple has said that Intel binaries will run on Apple Silicon using Rosetta 2 emulation. So you will have at least the life-span of Big Sur before you need to create a Universal Binary.

I'd say you don't need to worry about this until:

a) AS Macs have been released,
b) people who have bought AS Macs are interested in your tool;
c) Xcode 12 is officially released (along with Big Sur)

at which time, compiling a Universal Binary for a command line tool should be pretty straightforward, just by changing some options.

2

Is it possible to compile UNIX command line program for Apple Silicon architecture on Intel-based Mac?

Yes, it is.

It's a broad answer and depending on your particular specifics you will need to tweak/adapt your code as necessary.

How do we know this? History is our guide and it's not limited to Apple.

  • OS X was compiled on PowerPC then migrated to Intel
  • OS X was derived from NeXTStep which originally ran on Motorola 6000, then Intel, Sparc, and RISC-PA
  • Open Source operating systems like FreeBSD are (re)compiled for different platforms including i386, AMD64, IA64, Sparc64 and ARM
  • On a personal note, I've compiled XOrg (FreeBSD) to run on an XServe G4.

Switching architectures isn't as daunting as it sounds. Apple will make the tools (XCode) and development hardware available (as you've already mentioned). From a purely business perspective, they have to. Delivering a new OS and literally starting with an App marketplace of virtually zero would be the surest way to kill your product line.

You will have the tools - whether it's from Apple (XCode) or from the Open Source community. The hardware will become more available which will ultimately lower the cost and depending on your market strategy, you may not have to worry about this until a few years after Apple Silicon has been official released due to market adoption rates.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .