基于《深入理解 Java 虚拟机》第 3 版
1.配置环境
更新软件源
sudo apt update
获取源码
wget https://hg.openjdk.org/jdk/jdk12/archive/06222165c35f.zip
构建基础编译环境
sudo apt install build-essential
此处安装的 gcc 及 g++ 的版本为 11 ,需要替换为 7。参考这个链接,更新软件源后进行多版本管理。
sudo apt update
sudo apt install gcc-7
sudo apt install g++-7
# 多版本管理,数字越大优先级越高
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 50
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 30
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 30
安装其他依赖
参考文档,《深入理解 Java 虚拟机》在复制文档时未删去句尾的标点,这里要注意。
安装 Bootstrap JDK
sudo apt install openjdk-11-jdk
2.编译
# 编译FastDebug版、仅含Server模式的HotSpot虚拟机
bash configure --enable-debug --with-jvm-variants=server
此处 configure 命令会进行依赖项检查,缺失的直接按照提示安装即可。
一切顺利就会收到配置成功的提示,并输出摘要信息。如果多次编译,必须先执行make clean
和make dist-clean
。
执行整个 JDK 的编译
make images
此处 JDK12 对于 make 4.3 的适配有问题,需要手动替换代码。
# Param 2 - (optional) name of file to store value in
DependOnVariableHelper = \
$(strip \
$(eval -include $(call DependOnVariableFileName, $1, $2)) \
$(if $(call equals, $(strip $($1)), $(strip $($1_old))),,\
$(call MakeDir, $(dir $(call DependOnVariableFileName, $1, $2))) \
$(if $(findstring $(LOG_LEVEL), trace), \
$(info NewVariable $1: >$(strip $($1))<) \
$(info OldVariable $1: >$(strip $($1_old))<)) \
$(call WriteFile, $1_old:=$(call DoubleDollar,$(call EscapeHash,$($1))), \
$(call DependOnVariableFileName, $1, $2))) \
$(call DependOnVariableFileName, $1, $2) \
)
# Main macro
替换为
# Param 2 - (optional) name of file to store value in
DependOnVariableHelper = \
$(strip \
$(eval $1_filename := $(call DependOnVariableFileName, $1, $2)) \
$(if $(wildcard $($1_filename)), $(eval include $($1_filename))) \
$(if $(call equals, $(strip $($1)), $(strip $($1_old))),,\
$(call MakeDir, $(dir $($1_filename))) \
$(if $(findstring $(LOG_LEVEL), trace), \
$(info NewVariable $1: >$(strip $($1))<) \
$(info OldVariable $1: >$(strip $($1_old))<)) \
$(call WriteFile, $1_old:=$(call DoubleDollar,$(call EscapeHash,$($1))), \
$($1_filename))) \
$($1_filename) \
)
# Main macro
3.验证编译结果
./build/linux-x86_64-server-fastdebug/jdk/bin/java --version
4.参考链接
https://hg.openjdk.org/jdk/jdk12/file/06222165c35f/doc/building.html
https://blog.csdn.net/qq_25825005/article/details/127162939
https://askubuntu.com/questions/1406962/install-gcc7-on-ubuntu-22-04
https://askubuntu.com/questions/445162/trying-to-execute-update-alternatives-command-specified-in-an-instrutional-docum