前言
越学习,越觉得自己无知。最近准备把年前买的《深入理解Java虚拟机》第三版看完,趁着有时间,深入底层去看看,努力成为一个优秀的人吧。本篇对应书的第一章的后半部分,openjdk12的安装部分,同时记录一下自己踩的坑,给后来者提供一些建议,文章如果哪里有问题,欢迎大家批评。
环境准备
- 操作系统:macOS 10.15.3
- 软件:Xcode、Command Line Tools for Xcode
(下载地址:https://developer.apple.com/download/more/) - Boot JDK: 因为openjdk12的编译需要jdk环境,需要自行先安装jdk,这里我使用之前已安装好的jdk12(禁止套娃)
- openjdk12源码:https://hg.openjdk.java.net/jdk
因为外网访问下载过慢,这里附上openjdk12源码(链接:https://pan.baidu.com/s/1d3Qr3Z5A0bcAp3JA9APMRw 密码:32gz) - 开发工具:CLion
环境搭建
- 解压openjdk源码,进到源码目录下,建议有时间的同学阅读下doc下的building内容。
- 编译时有很多参数可以供使用,如设置编译一个FastDebug版的,只有Server模式的HotSpot的虚拟机。
编译成功会出现如下提示:bash configure --enable-debug --with-jvm-variants=server
Tools summary: * Boot JDK: java version "12.0.2" 2019-07-16 Java(TM) SE Runtime Environment (build 12.0.2+10) Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing) (at /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home) * Toolchain: clang (clang/LLVM from Xcode 11.4) * 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
- 编译openjdk12
成功则显示如下内容:make images
至此,完成openjdk12源码编译,可以到java/bin下面通过-version进行验证。Creating jdk image Creating CDS archive for jdk image Stopping sjavac server Finished building target 'images' in configuration 'macosx-x86_64-server-fastdebug'
➜ bin ./java -version openjdk version "12-internal" 2019-03-19 OpenJDK Runtime Environment (fastdebug build 12-internal+0-adhoc.xiaoguaixiansheng.jdk12) OpenJDK 64-Bit Server VM (fastdebug build 12-internal+0-adhoc.xiaoguaixiansheng.jdk12, mixed mode)
环境搭建问题
在搭建环境时,实际其实并没有上面说的那么顺利,还是经历了大量的坑之后才完成的。这里对问题进行总结和记录。
1.Autoconf is not found on the PATH, and AUTOCONF is not set.
configure --enable-dubug --with-jvm-variants=server时出现问题:
➜ jdk12 bash configure --enable-dubug --with-jvm-variants=server
Runnable configure script is not present
Generating runnable configure script at /Users/xiaoguaixiansheng/clionProject/jdk12/build/.configure-support/generated-configure.sh
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
解决方案:
➜ jdk12 brew install autoconf
2.configure: error: No xcodebuild tool and no system framework headers found, use --with-sysroot or --with-sdk-name to provide a path to a valid SDK
configure --enable-dubug --with-jvm-variants=server时出现问题:
configure: error: No xcodebuild tool and no system framework headers found, use --with-sysroot or --with-sdk-name to provide a path to a valid SDK
/Users/xiaoguaixiansheng/clionProject/jdk12/build/.configure-support/generated-configure.sh: line 82: 5: Bad file descriptor
configure exiting with result code 1
解决方案:
xcode下载完未安装,安装完即可,如还不行,执行如下命令:
sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/
3.test_symbolTable.cpp:62:6: error: explicitly assigning value of variable of type ‘TempNewSymbol’ to itself [-Werror,-Wself-assign-overloaded]
在make images时出现问题:
ERROR: Build failed for target 'images' in configuration 'macosx-x86_64-server-fastdebug' (exit code 2)
Stopping sjavac server
=== Output from failing command(s) repeated here ===
* For target hotspot_variant-server_libjvm_gtest_objs_test_symbolTable.o:
/Users/xiaoguaixiansheng/clionProject/jdk12/test/hotspot/gtest/classfile/test_symbolTable.cpp:62:6: error: explicitly assigning value of variable of type 'TempNewSymbol' to itself [-Werror,-Wself-assign-overloaded]
s1 = s1; // self assignment
~~ ^ ~~
1 error generated.
解决方法(因为查了很多资料都没有找个一个完美的答案,所以只能基于报错对源码进行调整,如果有更好的解决方法,欢迎评论区留言):
因为这个是因为一个test包下的test_symbolTable.cpp里一句代码s1 = s1;报错的,进入源码发现该行代码(在我的理解下)是无用代码,因此注释掉再进行编译,即可成功。
#注释test/hotspot/gtest/classfile/test_symbolTable.cpp:62该行s1=s1;代码。
make clean # 清理之前编译失败的残留文件 make dist-clean 清理包括configure阶段产生的build目录
make images #再次编译
源码调试
- 使用CLion导入整个jdk项目
- Import CMake Project
- 配置启动配置
其中Executable用刚刚编译生成的bin/java
加上参数-version -XX:+TraceBytecodes -XX:StopInterpreterAt=<n>
去掉BeforeLaunch的Build
- 启动测试
java.c的JavaMain为工程的入口,debug模式下打断点启动,进行启动测试。
总结
至此,完成了openjdk12源码的编译和环境搭建,虽然流程很简单,但上手总是会有各种奇奇怪怪的错误,毕竟"Talk is cheap,show me your code."。可能有些同学有疑问,为什么这个地方写这个,那个地方写那个,我个人的心得是先暂且把这些东西放一边,先动手,行有余力,再深入研究,不要想一口吃成个胖子。我在做的时候也有很多疑问,就好像上面的第三个问题,但绕过它并不妨碍我接下来的学习,所以不妨先放一放,可能一段时间回来看它,会觉得豁然开朗。