MacOS-10.15.6 編譯 OpenJdk 16.15 源碼及 Clion 調試
1. 下載最新版本源碼
-
下載地址
-
編譯 openjdk 過程中使用的具體版本下載地址
2. 源碼編譯
# 主流程
Get the complete source code:
bash get_source.sh
Run configure:
bash configure
If configure fails due to missing dependencies (to either the toolchain, external libraries or the boot JDK), most of the time it prints a suggestion on how to resolve the situation on your platform. Follow the instructions, and try running bash configure again.
Run make:
make images
Verify your newly built JDK:
./build/*/images/jdk/bin/java -version
Run basic tests:
make run-test-tier1
# 實際操作
1. 安裝依賴工具
brew install autoconf
2. 配置
bash ./configure --enable-dtrace --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home --with-debug-level=slowdebug --with-target-bits=64 --with-native-debug-symbols=internal --disable-warnings-as-errors --with-jvm-variants=server
3. 編譯
make images
4. 驗證編譯后的 jdk
./build/*/images/jdk/bin/java -version
./build/macosx-x86_64-server-slowdebug/jdk/bin/java -version
openjdk version "16-internal" 2021-03-16
OpenJDK Runtime Environment (slowdebug build 16-internal+0-adhoc.Tao.jdk-jdk-16-15)
OpenJDK 64-Bit Server VM (slowdebug build 16-internal+0-adhoc.Tao.jdk-jdk-16-15, mixed mode)
3. 過程日志
## 部分日志
## 配置
1. bash ./configure --enable-dtrace --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home --with-debug-level=slowdebug --with-target-bits=64 --with-native-debug-symbols=internal --disable-warnings-as-errors --with-jvm-variants=server
Configuration summary:
* Debug level: slowdebug
* HS debug level: debug
* JVM variants: server
* JVM features: server: 'aot cds compiler1 compiler2 dtrace epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services shenandoahgc vm-structs zgc'
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 16-internal+0-adhoc.Tao.jdk-jdk-16-15 (16-internal)
Tools summary:
* Boot JDK: java version "14.0.2" 2020-07-14 Java(TM) SE Runtime Environment (build 14.0.2+12-46) Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing) (at /Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home)
* Toolchain: clang (clang/LLVM from Xcode 11.7)
* C Compiler: Version 11.0.3 (at /usr/bin/clang)
* C++ Compiler: Version 11.0.3 (at /usr/bin/clang++)
Build performance summary:
* Cores to use: 8
* Memory limit: 16384 MB
## 生成鏡像
2. make images
Updating support/demos/image/jfc/TransparentRuler/src.zip
Creating support/demos/image/jfc/CodePointIM/CodePointIM.jar
Creating support/demos/image/jfc/FileChooserDemo/FileChooserDemo.jar
Creating support/demos/image/jfc/SwingSet2/SwingSet2.jar
Creating support/demos/image/jfc/Font2DTest/Font2DTest.jar
Creating support/demos/image/jfc/J2Ddemo/J2Ddemo.jar
Creating support/demos/image/jfc/Metalworks/Metalworks.jar
Creating support/demos/image/jfc/Notepad/Notepad.jar
Creating support/demos/image/jfc/Stylepad/Stylepad.jar
Creating support/demos/image/jfc/SampleTree/SampleTree.jar
Creating support/demos/image/jfc/TableExample/TableExample.jar
Creating support/demos/image/jfc/TransparentRuler/TransparentRuler.jar
Creating jdk image
Creating CDS archive for jdk image
Creating CDS-NOCOOPS archive for jdk image
Stopping sjavac server
Finished building target 'images' in configuration 'macosx-x86_64-server-slowdebug'
## 查看 jdk 版本信息
3. ./build/macosx-x86_64-server-slowdebug/jdk/bin/java -version
openjdk version "16-internal" 2021-03-16
OpenJDK Runtime Environment (slowdebug build 16-internal+0-adhoc.Tao.jdk-jdk-16-15)
OpenJDK 64-Bit Server VM (slowdebug build 16-internal+0-adhoc.Tao.jdk-jdk-16-15, mixed mode)
4. Clion Debug class
4.1 編譯器忽略錯誤退出
# Works once enabled through the ~/.lldbinit in the home directory.
echo 'settings set target.load-cwd-lldbinit true' > ~/.lldbinit
4.2 Clion 打開 openjdk 源碼并在 java main 函數打斷點
main 函數路徑(src/java.base/share/native/launcher/main.c)
EYOKDT
4.3 Custom build targets for OpenJDK
4.3.1 編寫 java 測試代碼并生成 class 文件
// Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello World !!!\nWow! Hotspot debug mode !!!");
}
}
// 編譯
javac Hello.java
// 測試 Hello.class
java Hello
// 輸出結果
Hello World !!!
Wow! Hotspot debug mode !!!
4.3.2 配置 openjdk 調試 Hello.class 字節(jié)碼
-
創(chuàng)建啟動配置項
UC8ZUI -
自定義構建應用
xwSMsB說明:
創(chuàng)建 Custom Build Application
參考下面 Configure Custom Build Target
編譯后 openjdk java 命令(./build/macosx-x86_64-server-slowdebug/jdk/bin/java)
Hello.class 文件名稱
Hello.class 文件所在路徑(必須使用 class 文件所在的絕對路徑)
移除
Before launch
中的build
,我們的 openjdk 源碼是編譯完成的,不需要 build
-
Configure Custom Build Target
-
點擊箭頭位置
mqDiBm
-
-
配置參數
QZunmZ- 創(chuàng)建 External tools
- name 與 description 可以寫 openjdk 編譯后的文件夾名稱 (macosx-x86_64-server-slowdebug)
- make 命令
- CONF 參數
- openjdk 源碼的根路徑
-
點擊 Clion
debug
按鈕 調試 Hello.classJShfWH -
Hello.class 最終執(zhí)行結果驗證
9qxWt7 -
參考地址
https://blog.jetbrains.com/clion/2020/03/openjdk-with-clion/
5. 問題及建議
5.1 autoconf 異常
# 問題
Autoconf is not found on the PATH, and AUTOCONF is not set.
You need autoconf to be able to generate a runnable configure script.
You might be able to fix this by running 'brew install autoconf'.
Error: Cannot find autoconf
# 解決方案
brew install autoconf
5.2 jdk 版本問題
## 問題
configure: Potential Boot JDK found at /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home is incorrect JDK version (java version "10.0.2" 2018-07-17); ignoring
configure: (Your Boot JDK version must be one of: 14 15 16)
configure: Could not find a valid Boot JDK. OpenJDK distributions are available at http://jdk.java.net/.
configure: This might be fixed by explicitly setting --with-boot-jdk
## 解決方案
添加 --with-boot-jdk灵再,指定高版本 JDK 路徑尘分,JDK 版本必須為 14,15婴噩,16,可以去 oracle 官網下載對應版本的 jdk
bash ./configure --enable-dtrace --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home --with-debug-level=slowdebug --with-target-bits=64 --with-native-debug-symbols=internal --disable-warnings-as-errors --with-jvm-variants=server
5.3 Tips
編譯配置參數(--enable-dtrace 等)參考OpenJdk GitHub 官方編譯文檔說明
5.4 java 運行命令
## 啟動方式
jar : java -jar hello.jar
class : java Hello
6. 參考文檔
https://github.com/openjdk/jdk/blob/master/doc/building.md
https://blog.jetbrains.com/clion/2020/03/openjdk-with-clion/