搬砖小抄

在ubuntu下编译OpenJDK源码

字数统计: 683阅读时长: 3 min
2021/08/24 Share

总体流程

基础环境准备

安装Linux系统

编译难度 : Windows > MacOS > Linux

下载Ubuntu20 LTS Desktop 的ISO镜像(https://ubuntu.com/#download) ,在虚拟机中安装好系统。

安装好之后,建议先配置好国内的软件源,后面的步骤假设你在一个全新安装的Ubuntu20系统中编译OpenJDK的源代码。

编译源码

准备工作

下载 JDK源码:jdk-11+28

也可以通过git仓库拉取

1
2
3
git clone https://github.com/openjdk/jdk.git openjdk-jdk  
cd openjdk-jdk
git checkout -b jdk11-28 jdk-11+28

安装编译工具

查看源码目录下的编译文档 doc/building.html 安装相关依赖,以下是Ubuntu 20 的执行过程

1
2
# 基础工具,Bootstrap JDK
sudo apt install build-essential openjdk-11-jdk

编译配置

1
2
3
4
5
6
7
# 进入源码根目录执行
bash configure \
--enable-debug \
--enable-dtrace \
--disable-ccache \
--disable-warnings-as-errors \
--with-jvm-variants=server

此步骤如果执行出错,一般是因为缺少依赖包,按照错误提示安装对应的工具包即可,我整理了一下,一键安装命令如下

1
2
3
sudo apt install \
autoconf systemtap-sdt-dev libx11-dev libxext-dev libxrender-dev \
libxtst-dev libxt-dev libcups2-dev libfontconfig1-dev libasound2-dev

成功后,会输出类似信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
====================================================
A new configuration has been successfully created in
/home/jclazz/jdk-dev/jdk11-76072a077ee1/build/linux-x86_64-normal-server-fastdebug
using configure arguments '--enable-debug --enable-dtrace --disable-ccache --disable-warnings-as-errors --with-jvm-variants=server'.

Configuration summary:
* Debug level: fastdebug
* HS debug level: fastdebug
* JVM variants: server
* JVM features: server: 'aot cds cmsgc compiler1 compiler2 dtrace epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services vm-structs'
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
* Version string: 11-internal+0-adhoc.jclazz.jdk11-76072a077ee1 (11-internal)

Tools summary:
* Boot JDK: openjdk version "11.0.11" 2021-04-20 OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04) OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing) (at /usr/lib/jvm/java-11-openjdk-amd64)
* Toolchain: gcc (GNU Compiler Collection)
* C Compiler: Version 9.3.0 (at /usr/bin/gcc)
* C++ Compiler: Version 9.3.0 (at /usr/bin/g++)

Build performance summary:
* Cores to use: 3
* Memory limit: 3901 MB
* ccache status: Disabled

编译

1
make images

执行结束后, build/linux-x86_64-normal-server-fastdebug/jdk 目录下就是对应的JDK文件。

你可以将 JAVA_HOME 指向这个目录,就能让很多软件用上你自己编译的JDK发行版本了。

make 命令还可以有以下选项

  • hotspot - Build all of hotspot (but only hotspot)
  • hotspot-<variant> - Build just the specified jvm variant
  • images or product-images - Build the JDK image
  • docs or docs-image - Build the documentation image
  • test-image - Build the test image
  • all or all-images - Build all images (product, docs and test)
  • bootcycle-images - Build images twice, second time with newly built JDK
    (good for testing)
  • clean - Remove all files generated by make, but not those generated by
    configure
  • dist-clean - Remove all files, including configuration

验证

进入 build/linux-x86_64-normal-server-fastdebug/jdk/bin ,验证java版本输出信息

测试

在根目录执行 make run-test-tier1 

时间有点长,耐心等。

CATALOG
  1. 1. 基础环境准备
    1. 1.1. 安装Linux系统
  2. 2. 编译源码
    1. 2.1. 准备工作
    2. 2.2. 安装编译工具
    3. 2.3. 编译配置
    4. 2.4. 编译
    5. 2.5. 验证
    6. 2.6. 测试