Dr. Brian Robert Callahan

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



[prev]
[next]

2026-07-13
I bootstrapped GDC with DMD

Get the source code here.

FreeBSD does not have a package for GDC, the GNU D Compiler, which is the D frontend of GCC. I frequently use GDC on FreeBSD as a research tool. As such, I had to actually bootstrap GDC for FreeBSD. I forgot to share how I did it; this post is intended to fix that.

It's very straightforward and easy. If you have DMD, then you can easily get GDC. I wrote a small wrapper program to make it as easy and quick as possible.

What GCC claims

GCC in their documentation says that GDC must be built with GDC. Specifically, they say, "In order to build GDC, the D compiler, you need a working GDC compiler (GCC version 9.4 or later) and D runtime library, 'libphobos', as the D front end is written in D." The documentation goes on to say, "Versions of GDC prior to 12 can be built with an ISO C++11 compiler, which can then be installed and used to bootstrap newer versions of the D front end."

Parts of that are inarguably true: GDC is written in D as of GCC 12. But parts of it feel untested: what is so special about GDC that it is the only D compiler that can build GDC? All the D compilers share a frontend, and the D frontend does not use any esoteric parts of the language. And even if it did, all three D compilers share a frontend, so in theory any D compiler should be able to bootstrap GDC. This is what I wanted to test, if for no other reason than it would take incredibly long to build an old version of GCC just to build a new version of GCC. It takes seconds to build DMD.

What reality demonstrates

In order to build GDC, you need a D compiler and libphobos version sufficient to build only the stage1 GDC. The stage1 GDC builds the stage1 libphobos. If we have a D compiler that can do just enough to build a working stage1 GDC, then we are golden. That's the only hurdle we need to overcome.

The DMD compiler, the reference D compiler, is perfectly capable of doing this. The impediment isn't whether or not DMD can build the stage1 GDC; it is the fact that of all three D compiler frontends, DMD, GDC, and LDC, they all use different flag sets. DMD and LDC are close to one another; GDC uses a very different flag style to match GCC. If we can write a small wrapper program that translates those flag differences, we should be able to build the stage1 GDC with DMD.

gdc-wrapper

The little program just mechanically translates flags, adds all the other arguments unchanged, and then calls DMD with the transformed argument list. I mostly went through trial-and-error until DMD built the stage1 GDC. I would just let the GDC build run and when something failed, I would add that flag to the wrapper. The most difficult flags to deal with were -MF and -MT, which create dependency rules files. What I did here was ignore -MT and create the file specified with -MF but with only a single newline character in it. That's a bit of a hack but we're not interested in getting it exactly right; we're interested in getting stage1 GDC built and moving on.

Other than that, we ignore warning flags, convert -o output.o to -ofoutput.o, change -finline to -inline, change -fversion=VERSION to -version=VERSION, and a few more small changes to make sure things linked correctly.

And it works

I downloaded the FreeBSD x86_64 DMD 2.112.0 binaries from the D website, untarred it to my HOME, and then built gdc-wrapper with ./configure --dmd=/home/briancallahan/dmd2/freebsd/bin64/dmd && make and I was able to build GDC without issue by adding /home/briancallahan/gdc-wrapper to the end of my PATH temporarily with export PATH=$PATH:$PWD. It really was that simple. I can build GDC 15.2.0 and GDC 16.1.0 with this on FreeBSD 15.

Conclusion

Hopefully this helps some FreeBSD, NetBSD, GNU Hurd, or Illumos people. Or anyone on a Linux distro that doesn't have a GDC package.

The obvious limitation is that for now, DMD only has backends for x86 and x86_64 (though an ARM64 backend is on the way!). So if you want a GDC for a different CPU architecture, you will first need to bootstrap a native GDC with this procedure and then you can follow one of our previous posts to build a cross-GDC en route to a native GDC for a different CPU architecture.

Top

RSS