一、下載源碼
直接下載源碼包,使用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。