一、下载源码
直接下载源码包,使用mercurial下载速度慢,容易失败。
源码网站:
Jdk9源码地址
https://download.java.net/openjdk/jdk9/ri/openjdk-9_src.zip
或者从:https://github.com/unofficial-openjdk/openjdk/releases 下载,我没试过。
二、环境
Xcode版本不要用10及以上版本,据说删除了一个过期库(stdlibc++库被替换成了 libc++),会使编译失败。
操作系统:masOS High Sierra 10.13.6
BootStrap:Java HotSpot(TM) 64-Bit 1.8.0_231
Xcode:9.3
三、依赖
brew install ccache //加速编译
brew install freetype //字体引擎,编译过程中会被依赖到
四、编译前准备
- hotspot/src/share/vm/memory/virtualspace.cpp 搜索其中if (base() > 0) {改为if (base() != NULL) {
- hotspot/src/share/vm/opto/lcm.cpp 搜索其中if (Universe::narrow_oop_base() > 0) { 改为 if (Universe::narrow_oop_base() != NULL) {
- hotspot/src/share/vm/opto/loopPredicate.cpp 搜索其中assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() >= 0, "must be"); 改为 assert(rng->Opcode() == Op_LoadRange || iff->is_RangeCheck() || _igvn.type(rng)->is_int()->_lo >= 0, "must be");
- hotspot/src/share/vm/runtime/perfData.cpp 搜索PerfDataManager::destroy()函数,注释掉:delete p;
五、编译
进入源码路径
- chmod a+x configure
- ./configure --with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.10.1 --enable-ccache --with-jvm-variants=server,client --with-boot-jdk-jvmargs="-Xlint:deprecation -Xlint:unchecked" --disable-warnings-as-errors --with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log
注意修改freetype的路径。
结果如下
====================================================
A new configuration has been successfully created in
/Users/zhzhl/Documents/jdk/openjdk/build/macosx-x86_64-normal-serverANDclient-slowdebug
using configure arguments '--with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.10.1 --enable-ccache --with-jvm-variants=server,client --with-boot-jdk-jvmargs='-Xlint:deprecation -Xlint:unchecked' --disable-warnings-as-errors --with-debug-level=slowdebug'.
Configuration summary:
* Debug level: slowdebug
* HS debug level: debug
* JDK variant: normal
* JVM variants: server client
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 9-internal+0-adhoc.zhzhl.openjdk (9-internal)
Tools summary:
* Boot JDK: java version "1.8.0_231" Java(TM) SE Runtime Environment (build 1.8.0_231-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode) (at /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home)
* Toolchain: clang (clang/LLVM)
* C Compiler: Version 10.0.0 (at /usr/bin/clang)
* C++ Compiler: Version 10.0.0 (at /usr/bin/clang++)
Build performance summary:
* Cores to use: 8
* Memory limit: 16384 MB
* ccache status: Active (3.7.6)
NOTE: You have requested to build more than one version of the JVM, which
will result in longer build times.
- export LANG=C
- make all LOG=debug 2>&1 | tee make_mac_x64.log
六、验证
openjdk/build/macosx-x86_64-normal-serverANDclient-slowdebug/jdk/bin目录下,执行:./java -version。