在MacOS上编译jdk12

最近在跟着《深入理解Java虚拟机》复习 JVM 相关的知识,想再编译一下 JDK。因为之前有编译过 JDK8,因为版本较老,踩坑比较多;并且现在 Java 每半年一个大版本,新版本还没来得及了解就过时了,更别提去使用了,所以这次打算用 JDK11 来编译 OpenJDK12。

环境信息:

  • 系统版本:MacOS 13.6.1 (22G313)
  • boot jdk:idk-11.0.21
  • Xcode 版本:15.0

源码下载

要编译 JDK,肯定要先有源码才能编译。源码从 Oracle 官网下载:https://hg.openjdk.org/jdk/jdk12,左边菜单选择方便的格式下载即可,开始我下载的是 zip,但是解压的时候总是会提示意料之外的文件结尾,解压不出来,所以就换了 tar 格式下载。

前置操作

freetype

OpenJDK 中用到 freetype 渲染,所以需要先安装好 freetype。

brew install freetype

ccache

ccache,是一个编译器缓存,该工具会高速缓存编译生成的信息,并在编译的特定部分使用高速缓存的信息。

brew install ccache

安装 JDK11

要得到梯子,首先要有个梯子。同样的道理,要编译出 JDK,首先得有个 JDK。只能通过低版本的 JDK 编译出高版本的 JDK,不能反过来。下载 JDK11 ,配置好环境变量,就不多赘述了。

编译

接下来就可以开始编译了,如果有过编译记录,先运行 make cleanmake clean-dist 清空相关目录,然后在根目录运行 configure

./configure --disable-warnings-as-errors --with-debug-level=slowdebug --with-jvm-variants=server --enable-ccache

顺利的话会输出以下内容

====================================================
The existing configuration has been successfully updated in
/Users/lozhu/Documents/projects/jdk12-06222165c35f/build/macosx-x86_64-server-slowdebug
using configure arguments '--disable-warnings-as-errors --with-debug-level=slowdebug --with-jvm-variants=server --enable-ccache'.

Configuration summary:
* Debug level:    slowdebug
* HS debug level: debug
* JVM variants:   server
* JVM features:   server: 'aot cds cmsgc compiler1 compiler2 dtrace epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services shenandoahgc vm-structs'
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 12-internal+0-adhoc.root.jdk12-06222165c35f (12-internal)

Tools summary:
* Boot JDK:       openjdk version "11.0.21" 2023-10-17 LTS OpenJDK Runtime Environment Microsoft-8519785 (build 11.0.21+9-LTS) OpenJDK 64-Bit Server VM Microsoft-8519785 (build 11.0.21+9-LTS, mixed mode)  (at /opt/java/jdk-11.0.21+9/Contents/Home)
* Toolchain:      clang (clang/LLVM from Xcode 15.0.1)
* C Compiler:     Version 15.0.0 (at /usr/bin/clang)
* C++ Compiler:   Version 15.0.0 (at /usr/bin/clang++)

Build performance summary:
* Cores to use:   8
* Memory limit:   16384 MB
* ccache status:  Active (4.8.3)

WARNING: The result of this configuration has overridden an older
configuration. You *should* run 'make clean' to make sure you get a
proper build. Failure to do so might result in strange build problems.

然后运行 make images 进行编译,根据机器配置编译时间长短会有较大差异,2020款MacBook pro编译了15分钟。

Stopping sjavac server
Finished building target 'images' in configuration 'macosx-x86_64-server-slowdebug'

出现这个提示就说明编译成功了!编译出的 JDK 位于:./build/macosx-x86_64-server-slowdebug/jdk/bin 下,运行 ./java -version 可以看到自己编译出的 JDK 版本:

openjdk version "12-internal" 2019-03-19
OpenJDK Runtime Environment (slowdebug build 12-internal+0-adhoc.root.jdk12-06222165c35f)
OpenJDK 64-Bit Server VM (slowdebug build 12-internal+0-adhoc.root.jdk12-06222165c35f, mixed mode)

问题记录

问题1

./jdk12-06222165c35f/src/java.base/macosx/native/libosxsecurity/KeystoreImpl.m note: did not find header ‘JavaNativeFoundation.h’ in framework ‘JavaNativeFoundation’ (loaded from ‘/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk/System/Library/Frameworks’)

解决

这个问题很明显,就是系统路径的问题,但是一直苦于不知道 JavaNativeFoundation 在哪里引入的,在这里卡了两三个小时,搜索了很多国内外的网站也没找到解决方案。最后是关掉电脑后用手机搜这个问题,在 CSDN 上搜到了一些之前没搜到的资料,虽然说问题不完全相同,但是算是同一个问题,提供了很大的帮助。

参考:macOS Catalina + Xcode 12 编译OpenJDK 14 错误解决

系统版本升级,一部分文件路径有变化。新的路径:/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk。

在 ./build/macosx-x86_64-server-slowdebug/spec.gmk 中搜索 SYSROOT_CFLAGSSYSROOT_LDFLAGS ,注释掉这两个变量,新增下面的内容

SYSROOT_CFLAGS := -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -iframework /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks -F /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/JavaNativeFoundation.framework
SYSROOT_LDFLAGS := -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -iframework /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks -F /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/JavaNativeFoundation.framework


BUILD_SYSROOT_CFLAGS:=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -iframework /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks -F /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/JavaNativeFoundation.framework
BUILD_SYSROOT_LDFLAGS:=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -iframework /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks -F /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/JavaNativeFoundation.framework

在 ./build/macosx-x86_64-server-slowdebug/buildjdk-spec.gmk 中搜索 SYSROOT_CFLAGSSYSROOT_LDFLAGS ,注释掉这两个变量,新增下面的内容

BUILD_SYSROOT_CFLAGS:=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -iframework /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks -F /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/JavaNativeFoundation.framework
BUILD_SYSROOT_LDFLAGS:=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -iframework /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks -F /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/JavaNativeFoundation.framework

BUILD_SYSROOT_CFLAGS:=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -iframework /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks -F /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/JavaNativeFoundation.framework
BUILD_SYSROOT_LDFLAGS:=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk -iframework /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks -F /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/System/Library/Frameworks/JavaNativeFoundation.framework

问题2

assert 未定义

解决

直接将报错文件中有 assert 的行删除或者注释掉即可,只涉及一个文件,不到十行代码。


问题3

clang: error: SDK does not contain ‘libarclite’ at the path ‘/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_macosx.a’; try increasing the minimum deployment target

解决

参考:iOS Xcode 升级Xcode15报错: SDK does not contain ‘libarclite‘ at the path ‘/Applications/Xcode.app/Con…

文件缺失,从 https://codeload.github.com/kamyarelyasi/Libarclite-Files/zip/refs/heads/main 下载 libarclite_macosx.a 文件,复制到 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/ 路径下。这里有个问题就是,arc 路径需要自己创建,但是受系统权限限制,sudo 权限也创建不了这个目录,只能更改系统设置关闭 SIP。重启电脑,按住 ⌘ + R,进入保护模式后打开终端,输入命令:crsutil disable ,重启即可。要重新开启 SIP,命令为:crsutil enable

参考的链接里单词写错了,是 crsutil 不是 csrutil