Porting D to FreeBSD
written by Walter Bright
April 28, 2009
It took me about 6 weeks to get D ported to the Mac OSX. During this, I learned that OSX was based on BSD unix. The Digital Mars web server is hosted on a FreeBSD system, and Jan Knepper who manages that had been encouraging me to look at FreeBSD for years. So I thought it was an ideal time to try it out.
First I needed a box running FreeBSD. Being in the programming business for a long time, I had a bunch of older machines moldering away in the basement, unusued. FreeBSD did not have demanding hardware requirements, so I wondered if one of them could be recycled with FreeBSD. It wouldn’t hurt to try.
The first box I try wouldn’t even turn on. Click on the power, and absolutely nothing happened. I put that back on the shelf. The next one coughed, turned over, put out a cloud of blue smoke, and sputtered back into life. It was around 9 years old, with a 20G drive and maybe 100 Mb of ram.
I looked around online, downloaded the FreeBSD ISO’s and burned them on CDs. (The machine only has a CD drive, DVD installs won’t work!) Putting in the CD and rebooted, the install recognized the ancient hardware and the install went smoothly. Mostly smoothly, anyway. The install isn’t as polished as Ubuntu’s is, and when it was finished there were a number of things, especially about the network, that needed manual editting of configuration files. Fortunately, Jan Knepper is an expert with FreeBSD and he walked me through all that. Looking at how Ubuntu had configured itself also helped.
I brought up the GUI interface, tried the browser, and was delighted that the system worked well on such old, slow, memory deficient hardware. It did sound like a GM turbine car with a rusted out tailpipe, though, so I configured it for remote operation via SSH and banished it to the basement alongside the Mac.
Now was the time to do the real work. First of all, the library (archive) format was identical to that on Linux. The object file format was also ELF, same as on Linux. Some of the function ABI conventions matched that on Mac OSX. The header files tended to match OSX. So doing a FreeBSD port turned out to be mixing and matching parts from the Linux and OSX ports.
The most tedious part turned out to be writing the import translations for the various C header files. They were almost the same as under OSX, but had to be carefully checked line by line as out of nowhere one of the values of a #define would be different.
The most annoying part was that gcc on FreeBSD has poor support for long doubles. Half of the long double math functions are unimplemented. LDBL_MAX comes out of gcc as infinity, even though the float.h header file defines it correctly. It looks like in order to get correct, accurate and portable math functions for D, we’ll just have to reimplement them all.
The best part of this was it took a whole three days, and the FreeBSD D compiler was up and passing its test suite. I’ll defer the D 2.0 version for the moment, as its runtime library is in a state of flux. Trying to add FreeBSD support to it would be too disruptive for now.