警告⚠️:本文耗時很長,先做好心理準備;編譯openjdk原始碼需要很大的耐心,因為要踩很多坑,解決很多問題,本人從編譯開始到結束用了兩天時間,按照本篇教程踩坑會少許;謝謝觀看
一、獲取openjdk原始碼:
官網地址: http://jdk.java.net/
二、準備環境
安裝 homwbrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
安裝依賴:
brew install ccache //加速編譯
brew install freetype //字型引擎,編譯過程中會被依賴到
三:環境編譯(此處需要注意,mac系統版本與xcode版本及openjdk版本要相對應,否則會出現很多問題!!!)
./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
注意: 將--with-freetype指向你自己的freetype安裝路徑, 可能會出現許可權不足情況: 請在./config加上 -bash (或者自行查詢解決方案)
輸出如下內容,基本就說明你的環境沒問題了:
A new configuration has been successfully created in
/Users/yourname/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug
using configure arguments '--with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.9.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.daiyongzhi.jdk9 (9-internal)
Tools summary:
* Boot JDK: java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode) (at /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home)
* Toolchain: clang (clang/LLVM from Xcode 9.2)
* C Compiler: Version 9.0.0 (at /usr/bin/clang)
* C++ Compiler: Version 9.0.0 (at /usr/bin/clang++)
Build performance summary:
* Cores to use: 8
* Memory limit: 16384 MB
* ccache status: Active (3.5)
NOTE: You have requested to build more than one version of the JVM, which
will result in longer build times.
進行編譯 openjdk1.9:
export LANG=C
make all LOG=debug 2>&1 | tee make_mac_x64.log
如果一切順利,看到此結果就算編譯成功了:
最後,驗證一下:
cd openjdk/build/macosx-x86_64-normal-serverANDclient-slowdebug/jdk/bin
執行: ./java -version
警告⚠️:編譯過程中遇到的問題(此處列舉幾次常見錯誤,如若出現其他錯誤請自行查詢解決方案):
總結一下編譯過程中遇到的問題:
vi src/share/vm/memory/virtualspace.cpp (char *):
vi src/share/vm/opto/loopPredicate.cpp (const TypeInt *):
vi src/share/vm/opto/loopPredicate.cpp (const TypeInt *):
修改報錯資訊( 錯誤解決方案):
#1\. src/hotspot/share/memory/virtualspace.cpp # l585
if (base() != NULL) {
#2\. src/hotspot/share/opto/lcm.cpp # l42
if (Universe::narrow_oop_base() != NULL) { // Implies UseCompressedOops.
#3\. src/hotspot/share/opto/loopPredicate.cpp # l915
assert(rng->Opcode() == Op_LoadRange || iff->is_RangeCheck() || _igvn.type(rng)->is_int()->_lo >= 0, "must be");
四、編譯成功之後進行除錯:
java程式碼:
public class Main {
public static int size_1m = 1024*1024;
public static List<byte[]> list = new ArrayList<>();
public static void main(String[] args) throws Exception {
System.out.println("Hello World!");
for(int i=0;i<10;i++){
System.out.println(i+1);
list.add(new byte[size_1m]);
Thread.sleep(1000L);
}
}
}
配置idea虛擬機器引數:
-XX:NewSize=6m
-XX:SurvivorRatio=4
-XX:MaxNewSize=6m
-Xlog:gc*:stdout:time,level,tags
-XX:+UseParallelGC
位置如下圖所示:
然後執行:
2.通過執行Java程式除錯openjdk:
將openjdk目錄下的hotspot倒入到CLion
配置引數並執行:
/Users/yourname/jvm/openjdk/build/macosx-x86_64-normal-serverANDclient-slowdebug/images/jdk/bin/java -XX:NewSize=6m -XX:SurvivorRatio=4 -XX:MaxNewSize=6m -Xlog:gc*:stdout:time,level,tags -XX:+UseParallelGC -Dfile.encoding=UTF-8 -classpath /Users/yourname/IdeaProjects/mytest_projects/test_myjdk9/out/production/test_myjdk9 Main
與java聯調:
Compile java:
Build Jvm:
引數:
COMPILER_WARNINGS_FATAL=false
LFLAGS="'-Xlinker -lstdc++'"
CC=clang
USE_CLANG=true
LP64=1
隨便修改原始碼方法,並在java原始碼呼叫(此處修改了openjdk原始碼的獲取時間方法),展現效果如下:
如上所示:整個編譯了及聯調完畢,可以進行對原始碼為所欲為了;
附贈:centOs下編譯openjdk:(該方法由好友提供,本人未真正測試,僅供參考):
1、安裝程式碼管理工具mercurial/獲取自己獲取程式碼包
2、安裝外掛:
yum install alsa-lib-devel cups-devel libX* gcc gcc-c++ freetype-devel libstdc++-static ant make
3、檢視當前路徑
[***@VM_0_3_centos openjdk-jdk8u-jdk8u]$ pwd
/home/liangding/openjdk-jdk8u-jdk8u
[***@VM_0_3_centos openjdk-jdk8u-jdk8u]$
4、修改引數配置
[***@VM_0_3_centos openjdk-jdk8u-jdk8u]$ chmod +x configure
[***@VM_0_3_centos openjdk-jdk8u-jdk8u]$ ./configure —prefix=/home/liangding/jdk8u —enable-debug
5、編譯
[***@VM_0_3_centos openjdk-jdk8u-jdk8u]$ make
看到以下資訊表示編譯成功
## Finished jdk (build time 00:06:20)
—— Build times ———
Start 2019-07-08 22:07:16
End 2019-07-08 22:36:47
00:00:52 corba
00:20:13 hotspot
00:00:33 jaxp
00:00:42 jaxws
00:06:20 jdk
00:00:51 langtools
00:29:31 TOTAL
————————————
6、測試編譯後生成jdk的路徑
[***@VM_0_3_centos build]$ pwd
/home/liangding/openjdk-jdk8u-jdk8u/build
[***@VM_0_3_centos build]$ ls -lrt
total 4
drwxrwxr-x 9 liangding liangding 4096 Jul 8 22:30 linux-x86_64-normal-server-fastdebug
7、寫一個測試程式
[***@VM_0_3_centos ~]$ ls -lrt
total 127804
-rw-rw-r———————————— 1 liangding liangding 130719042 Jul 8 21:09 jdk8u.zip
drwxrwxr-x 2 liangding liangding 4096 Jul 8 21:48 jdk8u
drwxrwxr-x 14 liangding liangding 4096 Jul 8 21:53 openjdk-jdk8u-jdk8u
-rw-rw-r———————————— 1 liangding liangding 147 Jul 8 22:41 HelloWorld.java
-rw-rw-r———————————— 1 liangding liangding 432 Jul 8 22:45 HelloWorld.class
[***@VM_0_3_centos ~]$ cat HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println(“HelloWorld openJDK”);
}
}
8、測試
[***@VM_0_3_centos ~]$ ./openjdk-jdk8u-jdk8u/build/linux-x86_64-normal-server-fastdebug/jdk/bin/javac HelloWorld.java
9、執行
[***@VM_0_3_centos ~]$ ./openjdk-jdk8u-jdk8u/build/linux-x86_64-normal-server-fastdebug/jdk/bin/java HelloWorld
HelloWorld openJDK
[***@VM_0_3_centos ~]$
原創不易,轉載請標明出處,謝謝