Realizing my clang is obsolete

On Mac, it’s recommended to use Xcode command line to build OpenJDK. On my box, it’s installed at /Library/Developer/CommandLineTools, which
provides the real executables for development related tools, e.g. clang. (I was firstly a bit confused because which clang gives me
/usr/bin/clang, which is not a symlink. I don’t know what’s the relation between /usr/bin/clang and
/Library/Developer/CommandLineTools/usr/bin/clang. A quick experiment (renaming) reveals that /usr/bin/clang calls
/Library/Developer/CommandLineTools/usr/bin/clang somehow, and it would crash if it’s not found.

Since Xcode command line is already installed for me, I should be able to build OpenJDK. bash configure works fine; however, the build failed when I
call make, and the error is something like error: unknown warning option '-Wno-tautological-undefined-compare'. Apparently, this option wasn’t
understood by my obsolete clang.

Building clang-5

Since I have Homebrew, the easiest way to install clang-5 is brew install llvm, which failed because of my obsolete clang (of course). Then, I tried
if gcc could be installed: brew install gcc worked fortunately, making gcc-7 available on my box, so I tried to specify it as the C compiler on
installing llvm via brew install --cc=gcc-7 llvm, which surprisingly failed on building libunwind, a runtime dependency of llvm. I searched on
google concerning the error message, error: storage class specified..., but didn’t find anything useful. (Possibly, my setup is too weird.)

Building OpenJDK using gcc-7

Since I have a working gcc-7, I thought I could tell OpenJDK to use gcc instead of clang: CC=gcc-7 CXX=g++-7 bash configure
--with-toolchain-type=gcc. gcc-7 was indeed used, but failed with errors like “gcc-7: error: unrecognized command line option
‘-mstack-alignment=16’”. Some googling indicates that -mstack-alignment is specific to clang. It seems that OpenJDK doesn’t support compilers on Mac
other than clang.

Using clang-5 binary with hacky linker

All of the sudden, I realized that I actually can try downloading the binary directly from http://releases.llvm.org/download.html. After unpacking and
adding it into my PATH, I restarted with bash configure which failed complaining that the C compiler (clang-5) can’t build executable. On reading
the verbose log, I found that my clang-5 can’t compiler the following trivial C program:

I was able to find the latest source code for ld64 at https://opensource.apple.com/source/ld64/, but the build system is Xcode, which I don’t have
on my box. Apparently, ld64 was not something in huge demand separately, so I can’t find any places to download the binary. When all hopes are
forsaken, I decided to “hack” around the problem; filtering out this unknown option before calling the real linker.

12

#!/bin/zsh/usr/bin/ld "${(@)@:#-no_deduplicate}"

This way, I was able to trick bash configure to believe that I have a working C compiler. However, the actual building process wasn’t smooth; the
error is that “…/clang+llvm-5.0.0-x86_64-apple-darwin/lib/arc/libarclite_macosx.a can’t be found”. After locating the path of libarclite_macosx.a
is /Library/Developer/CommandLineTools/usr/lib/arc/libarclite_macosx.a, I just copied arc folder in .../clang+llvm-5.0.0-x86_64-apple-darwin/lib
to mirror the original layout. With this change, I was finally able to build OpenJDK.

For this kind of low level programming, Linux is surely more friendly, for more developers use that env.