Dr. Brian Robert Callahan

academic, developer, with an eye towards a brighter techno-social life



[prev]
[next]

2024-12-12
A GDC package for macOS/aarch64

Yesterday, my brand new MacBook Pro (November 2024/M4 Max/64 GB RAM/2 TB SSD) arrived. That means I have to get it all set up to build software, and some of the software I have is written in D.

Let's install GDC

My preferred D compiler is GDC. It is what I use pretty much exclusively on OpenBSD. I like GDC because it is integrated directly into GCC and uses the same GNU-style compiler flags that I'm used to everywhere else. DMD and LDC use Digital Mars-style flags. But most importantly, GDC works the best on OpenBSD in terms of codegen. So it's what I want to use on macOS.

The one downside to GDC is that you need GDC to build GDC. DMD is x86_64 only (for now...) and LDC has binaries for download that target macOS/aarch64. As of this writing, the Homebrew cask for LDC uses LLVM@18 while the default LLVM cask now is LLVM@19. I'm sure that will change with the next release of LDC. But you can't bootstrap GDC with LDC anyhow.

Bootstrapping GDC 11

Returning to bootstrapping GDC, versions prior to GCC 12 have a version of GDC written in C++. That is ideal for bootstrapping GDC. Unfortunately, GCC 11 doesn't support Apple Silicon upstream. I found this repo that hosts a port of GCC 11 for macOS/aarch64. Awesome.

Unfortunately, it doesn't build out of the box on my aarch64-apple-darwin24.2.0 machine. I had to modify libgcc/config/aarch64/lse.S and remove all instances of .cfi_startproc and .cfi_endproc. I also learned that _Float16 support was not added until GCC 13. I found a patch for GCC 12 to add this support, which I backported by hand to GCC 11.

Now I could successfully build GCC 11, including GDC, with these commands:

$ cc
<Install CLT>
$ brew install gcc
$ brew install make
$ mkdir build-gcc11
$ cd gcc-11-branch
$ ./contrib/download_prerequisites
$ cd ../build-gcc11
$ CC=gcc-14 CXX=g++-14 ../gcc-11-branch/configure --prefix=/opt/gcc11 --enable-languages=c,c++,d --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk --disable-bootstrap
$ gmake V=1 -j12
$ gmake V=1 DESTDIR=../fake-gcc11 install

It's so nice having a 16-core CPU and 64 GB RAM to run through compiles.

Now I could install our temporary GCC 11:

$ brew uninstall gcc
$ cd ../fake-gcc11
$ tar -cz -f gcc11.tgz opt
$ cp gcc11.tgz /
$ cd /
$ sudo tar xzf gcc11.tgz
$ rm gcc11.tgz

Now we have our temporary GCC 11 installed. We can use this to bootstrap the latest GCC 14.

Building GDC 14

The same person also has a repo with the latest (as of this writing) GCC 14.2. We follow very similar steps to build it:

$ export PATH=/opt/gcc11/bin:$PATH
$ mkdir build-gcc14
$ cd gcc-14-branch
$ ./contrib/download_prerequisites
$ cd ../build-gcc14
$ CC=gcc CXX=g++ ../gcc-14-branch/configure --prefix=/opt/gcc --enable-languages=c,c++,d,fortran,m2,objc,obj-c++ --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk
$ gmake V=1 -j12
$ gmake V=1 DESTDIR=../fake-gcc14 install
$ cd ../fake-gcc14
$ tar -cz -f gcc14.tgz opt
$ cp gcc14.tgz /
$ cd /
$ sudo rm -rf /opt/gcc11
$ sudo tar xzf gcc14.tgz
$ rm gcc14.tgz

Now all I have to do is stick /opt/gcc/bin in my PATH and I'll have a completely up-to-date GDC ready to use.

I don't keep multiple versions of GCC around on my machines, so I'm OK with the /opt/gcc location. That's also why I built gfortran and gm2 in the final build.

Conclusion

That's it. I don't know if anyone else is looking to do this, but if you are, here are the steps. If you just want a tarball, you can get one here.

Top

RSS