编译OpenJDK8
背景
随着JDK17的发布,越来越多的程序猿表示要JDK8用到死,虽然我也是这么想的😂,但是毕竟即将发布的Spring6和Spring Boot3都是基于JDK17的,所以在这之前,逆一下潮流编译一次OpenJDK8,之所以一直没尝试过编译OpenJDK主要是觉得太简单,毕竟AOSP都无压力,但是谁让今天心血来潮呢,走起。
准备工作
Unbutu系统的电脑或者虚拟机一台,配置越高越好(我用的是16.04)。
选择你喜欢的获取OpenJDK8源码的方法,我选择使用ZIP包,打开链接,点击RI Source Code下的zip file。(使用此种方式获取源码是最方便快捷的,下载完zip文件记得校验md5)。
准备工作
GNU make (版本须大于等于3.81)
Bootstrap JDK (也就是JDK7,源码的构建文档写的很明确,下载AZUL的即可,链接)
Building JDK 8 requires use of a version of JDK 7 that is at Update 7 or newer. JDK 8 developers should not use JDK 8 as the boot JDK, to ensure that JDK 8 dependencies are not introduced into the parts of the system that are built with JDK 7.
编译流程
解压源码,可以使用任何一款压缩包软件解压或者命令行,命令如下:
unzip openjdk-8u41-src-b04-14_jan_2020.zip
进入源码目录:
cd openjdk
安装需要的依赖:
sudo apt-get install make gcc build-essential libasound2-dev libcups2-dev libxext-dev libxrender-dev libxtst-dev libxt-dev
PS:之后报错需要什么就继续安装什么即可,因为我使用的机器可能之前有安装过编译需要的工具。
configure一下(因为我用的zsh,如果你用的bash,请自行将下面的zsh改为bash):
zsh configure --enable-debug --with-boot-jdk=/home/yourname/jdk7/zulu7.48.0.11-ca-jdk7.0.312-linux_x64 --with-num-cores=4 --with-jvm-variants=server --with-freetype-include=/usr/include/freetype2 --with-freetype-lib=/usr/lib/x86_64-linux-gnu
参数介绍如下
1
2
3
4
5
6
7
8# Bootstrap JDK的路径,我不信你的电脑里还装有JDK7,替换成你自己的JDK7路径
--with-boot-jdk=/home/yourname/jdk7/zulu7.48.0.11-ca-jdk7.0.312-linux_x6
# 使用4个核心编译,如果你的电脑是8核改成8
--with-num-cores=4
# JVM variants 支持server, client, kernel, zero , zeroshark
--with-jvm-variants=server
# 指定freetype的include和lib目录,你的位置可能和我的不同,报相关错误的自行指定路径
--with-freetype-include=/usr/include/freetype2 --with-freetype-lib=/usr/lib/x86_64-linux-gnu成功执行会看到下面的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18A new configuration has been successfully created in
/home/yourname/openjdk/build/linux-x86_64-normal-server-fastdebug
using configure arguments '--enable-debug --with-boot-jdk=/home/yourname/jdk7/zulu7.48.0.11-ca-jdk7.0.312-linux_x64 --with-num-cores=4 --with-jvm-variants=server --with-freetype-include=/usr/include/freetype2 --with-freetype-lib=/usr/lib/x86_64-linux-gnu'.
Configuration summary:
* Debug level: fastdebug
* JDK variant: normal
* JVM variants: server
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
Tools summary:
* Boot JDK: openjdk version "1.7.0_312" OpenJDK Runtime Environment (Zulu 7.48.0.11-CA-linux64) (build 1.7.0_312-b01) OpenJDK 64-Bit Server VM (Zulu 7.48.0.11-CA-linux64) (build 24.312-b01, mixed mode) (at /home/ddbes/jdk7/zulu7.48.0.11-ca-jdk7.0.312-linux_x64)
* C Compiler: gcc-5 (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 version 20160609 (at /usr/bin/gcc-5)
* C++ Compiler: g++-5 (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 version 20160609 (at /usr/bin/g++-5)
Build performance summary:
* Cores to use: 4
* Memory limit: 7983 MB
编译走起:
make all DISABLE_HOTSPOT_OS_VERSION_CHECK=ok
PS:DISABLE_HOTSPOT_OS_VERSION_CHECK=ok是为了屏蔽系统内核版本的检查,太旧的内核不行,当然太新的也不行,Makefile的官方注释如下
# We do not want people accidentally building on old systems (e.g. Linux 2.2.x,
# Solaris 2.5.1, 2.6).
# Disable this check by setting DISABLE_HOTSPOT_OS_VERSION_CHECK=ok.
几分钟的等待过后:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16----- Build times -------
Start 2021-09-23 19:55:00
End 2021-09-23 20:03:11
00:00:18 corba
00:00:11 demos
00:01:28 docs
00:03:18 hotspot
00:00:20 images
00:00:10 jaxp
00:00:15 jaxws
00:01:41 jdk
00:00:20 langtools
00:00:10 nashorn
00:08:11 TOTAL
-------------------------
Finished building OpenJDK for target 'all'总耗时8分钟多一点。
验证成果:
cd build/linux-x86_64-normal-server-fastdebug/jdk/bin
./java -version
1
2
3openjdk version "1.8.0-internal-fastdebug"
OpenJDK Runtime Environment (build 1.8.0-internal-fastdebug-damon_2021_09_23_19_54-b00)
OpenJDK 64-Bit Server VM (build 25.40-b25-fastdebug, mixed mode)完结撒花
总结
总体来说流程十分的顺利,如果编译过AOSP真的觉得编译OpenJDK太简单了😂,构建文档十分详细。
墙裂不建议在MacOS上编译JDK8,因为MacOS的编译环境依赖XCode,版本差异巨大,clang和gcc之间的差异,include和lib的差异也很大,除非你用官方建议的骨灰版本的系统和XCode,否则估计遇坑连连,当然如果编译JDK12就会简单的多。