警告⚠️:本文耗時很長,先做好心理準備;編譯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版本要相對應,否則會出現很多問題!!!)
Mac os: 10.14.4
xcode:9.2
下載下傳xcode的官方連結:Sign In - Apple ( 直接搜尋9.2版本, 然後下載下傳安裝)
編譯:
./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");
四、編譯成功之後進行調試:
1.編寫Java程式并使用自己編譯的openjdk版本
修改idea編譯器的jdk: 改為build/macosx-x86_64-normal-serverANDclient-slowdebug/images/jdk
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
配置參數并運作:
Executable是你編譯生成的java指令,如我的目錄是 build/macosx-x86_64-normal-serverANDclient-slowdebug/images/jdk/bin/java
Program arguments就是你啟動Java程式的參數,用IDEA運作上邊編寫的Java程式時,IDEA會列印運作指令如下:
/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 ~]$
原創不易,轉載請标明出處,謝謝