MacOS-10.15.6 编译 OpenJdk 16.15 源码及 Clion 调试
1. 下载最新版本源码
-
下载地址
https://github.com/openjdk/jdk/tags
-
编译 openjdk 过程中使用的具体版本下载地址
https://github.com/openjdk/jdk/archive/jdk-16+15.zip
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)
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 字节码
-
创建启动配置项
-
自定义构建应用
说明:
创建 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
-
点击箭头位置
-
-
配置参数
- 创建 External tools
- name 与 description 可以写 openjdk 编译后的文件夹名称 (macosx-x86_64-server-slowdebug)
- make 命令
- CONF 参数
- openjdk 源码的根路径
-
点击 Clion
debug
按钮 调试 Hello.class -
Hello.class 最终执行结果验证
-
参考地址
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/
http://www.txazo.com/jvm/jvm-startup.html
https://www.cnblogs.com/bolingcavalry/p/11495456.html