系统需求
- 宿主机操作系统:Windows 11 专业工作站版
- 虚拟机软件:VMware® Workstation 16 Pro
- 虚拟机操作系统:ubuntu-22.04.2-desktop-amd64
更换ubuntu源
💡 Tips:这里使用的是清华大学开源软件镜像站
# 备份/etc/apt/sources.list后,将该文件替换为下面内容
sudo vi /etc/apt/sources.list
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
更新软件源
sudo apt update
获取源码
----
> 💡 Tips:在国内推荐使用zip包方式下载,比起从Mercurial复制一堆零散文件要快非常多
方式一:下载源码zip包,[https://hg.openjdk.org/jdk/jdk12/archive/06222165c35f.zip](https://hg.openjdk.org/jdk/jdk12/archive/06222165c35f.zip)
方式二:hg clone https://hg.openjdk.java.net/jdk/jdk12
构建基础编译环境
--------
> 💡 Tips:基础编译工具,例如gcc,g++,make等
sudo apt install build-essential
上面命令安装的gcc,g版本为11,但是openjdk12需要版本7,所以需要安装低版本gcc,g
ubuntu软件源配置文件
sudo vi /etc/apt/sources.list
向文件中添加如下代码,追加一个软件源,推荐国内镜像,官方镜像速度很慢
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main universe
网上一般提供的是官方镜像,比较慢
deb [arch=amd64] http://archive.ubuntu.com/ubuntu focal main universe
更新源文件
sudo apt update
安装 gcc-7 g+±7
sudo apt install gcc-7
sudo apt install g+±7
多版本管理 最后的数字越大,优先级越高 这里维护了7 11两个个版本 默认是版本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
查看当前gcc,g++版本
sudo gcc --version
sudo g++ --version
安装其他依赖
------
> 💡 Tips:这些依赖都是执行bash configure后提示缺少的依赖
bash configure
configure: error: Could not find fontconfig! You might be able to fix this by running ‘sudo apt-get install libfontconfig1-dev’.
configure exiting with result code 1
sudo apt install -y libfreetype6-dev
sudo apt install -y libcups2-dev
sudo apt install -y libx11-dev libxext-dev libxrender-dev libxrandr-dev libxtst-dev libxt-dev
sudo apt install -y libasound2-dev
sudo apt install -y libffi-dev
sudo apt install -y autoconf
sudo apt install -y libfontconfig1-dev
```
安装Bootstrap JDK
---------------
其中一部分(HotSpot)代码使用C、C++编写,而更多的代码则是使用Java语言来实现,因此编译这些Java代
码就需要用到另一个编译期可用的JDK
```
sudo apt install openjdk-11-jdk
```
进行编译
----
```
# 编译FastDebug版、仅含Server模式的HotSpot虚拟机
bash configure --enable-debug --with-jvm-variants=server
```
顺利的话会出现如下内容
```
A new configuration has been successfully created in
/home/damimg/Downloads/jdk12-06222165c35f/build/linux-x86_64-server-release
using default settings.
Configuration summary:
* Debug level: release
* HS debug level: product
* JVM variants: server
* JVM features: server: 'aot cds cmsgc compiler1 compiler2 epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services shenandoahgc vm-structs zgc'
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
* Version string: 12-internal+0-adhoc.damimg.jdk12-06222165c35f (12-internal)
Tools summary:
* Boot JDK: openjdk version "11.0.18" 2023-01-17 OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu122.04) OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Ubuntu-0ubuntu122.04, mixed mode, sharing) (at /usr/lib/jvm/java-11-openjdk-amd64)
* Toolchain: gcc (GNU Compiler Collection)
* C Compiler: Version 7.5.0 (at /usr/bin/gcc)
* C++ Compiler: Version 7.5.0 (at /usr/bin/g++)
Build performance summary:
* Cores to use: 1
* Memory limit: 1940 MB
```
开始编译
----
```
make images
```
> 💡 Tips:笔者在这一步,不顺利,遇到了异常,后来发现为jdk12对make4.3的支持不够,jdk15修复了,原文:[https://github.com/openjdk/panama-foreign/commit/af5c725b](https://github.com/openjdk/panama-foreign/commit/af5c725b)
```
Building target 'images' in configuration 'linux-x86_64-server-release'
gmake[3]: *** No rule to make target '/home/damimg/Downloads/jdk12-06222165c35f/build/linux-x86_64-server-release/buildtools/langtools_tools_classes/_the.BUILD_TOOLS_LANGTOOLS.vardeps', needed by '/home/damimg/Downloads/jdk12-06222165c35f/build/linux-x86_64-server-release/buildtools/langtools_tools_classes/_the.BUILD_TOOLS_LANGTOOLS_batch'. Stop.
gmake[2]: *** [make/Main.gmk:73: buildtools-langtools] Error 2
ERROR: Build failed for target 'images' in configuration 'linux-x86_64-server-release' (exit code 2)
No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: See doc/building.html#troubleshooting for assistance.
make[1]: *** [/home/damimg/Downloads/jdk12-06222165c35f/make/Init.gmk:310: main] Error 2
make: *** [/home/damimg/Downloads/jdk12-06222165c35f/make/Init.gmk:186: images] Error 2
```
修改MakeBase.gmk
--------------
修改之前(make/common/MakeBase.gmk)
```
# 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
```
再次编译
----
顺利话会得到如下内容
```
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /home/damimg/Downloads/jdk12-06222165c35f/src/demo/share/jfc/TableExample/TableExample4.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Creating support/demos/image/jfc/TableExample/TableExample.jar
Updating support/demos/image/jfc/TableExample/src.zip
Compiling 1 files for BUILD_DEMO_TransparentRuler
Creating support/demos/image/jfc/TransparentRuler/TransparentRuler.jar
Updating support/demos/image/jfc/TransparentRuler/src.zip
Warning: No SCM configuration present and no .src-rev
Creating jdk image
Creating CDS archive for jdk image
Updating images/sec-bin.zip
Stopping sjavac server
Finished building target 'images' in configuration 'linux-x86_64-server-release'
```
验证编译结果
------
```
./build/linux-x86_64-server-release/jdk/bin/java --version
```
成功的话,会输出以下内容
```
openjdk 12-internal 2019-03-19
OpenJDK Runtime Environment (build 12-internal+0-adhoc.damimg.jdk12-06222165c35f)
OpenJDK 64-Bit Server VM (build 12-internal+0-adhoc.damimg.jdk12-06222165c35f, mixed mode)
```