使用 DMD 引导 GDC
Bootstrapping GDC with DMD

原始链接: https://briancallahan.net/blog/20260713.html

Brian Robert Callahan 博士演示了如何在 FreeBSD 上利用参考 D 语言编译器 DMD 来引导(bootstrap)GNU D 编译器(GDC)。 尽管 GCC 官方文档建议构建 GDC 时需要现有的 GDC 编译器,但 Callahan 对此提出了挑战。他认为,由于所有 D 语言编译器都使用相同的前端,因此理论上任何 D 语言编译器都应满足要求。目前的主要障碍并非编译器的能力,而是 GDC(遵循 GCC 惯例)与 DMD 之间的标志集(flag sets)不兼容。 为了解决这个问题,Callahan 开发了一个轻量级的“gdc-wrapper”脚本。该工具通过简单的变通方法绕过依赖文件生成并忽略不支持的警告,从而将 GDC 特有的标志自动转换为 DMD 兼容的参数。 通过使用该封装脚本和标准的 DMD 二进制文件,Callahan 成功在 FreeBSD 上构建了 GDC,无需为了获得现代 GDC 而去编译陈旧的旧版本 GCC。对于 FreeBSD、NetBSD 或其他缺乏官方 GDC 软件包的平台开发者而言,只要 DMD 支持底层架构(目前为 x86/x86_64),这种直接的方法就提供了一个切实可行的解决方案。

Sorry.
相关文章

原文
I bootstrapped GDC with DMD - Dr. Brian Robert Callahan

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



[prev]
[next]

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

联系我们 contact @ memedata.com