OpenJDK的下载和编译过程

文章目录

  • 一、背景
  • 二、 下载OpenJDK源码
  • 三、编译jdk的环境准备
    • 3.1 Bootstrap JDK 准备
    • 3.2 autoconf 安装
    • 3.3 ccache 和freetype安装
  • 四、编译OpenJDK
  • 五、注意事项
    • 5.1 --with-freetype错误

一、背景

平台:macOs Sierra 10.12.5
BootJDK: Sun jdk14.
OpenJDK:jdk-15+20
xcode:8.3.3

最近看了JVM的原理,特别想知道JVM的底层实现是什么的,只有研究的更深入,才能走的更远,遂决定打开jdk这扇神秘的大门。

二、 下载OpenJDK源码

openJdk从2018年10月发起向github进行代码托管的计划,目前github维护的仓库是:https://github.com/unofficial-openjdk/openjdk,
当然还有OpenJdk社区自身的版本库:
【Master和DEV】http://hg.openjdk.java.net/jdk/jdk/file/9b35dd9ce70a

目前github上已经更新到jdk-15+21,由于从github下载巨慢,所以试了很多方法都无法加速,只好从国内的gitee平台入手,目前有一个仓库维护到:jdk-15+20,大家可以从下面这个地址进行clone。我是从以下仓库进行clone的。

 git clone https://gitee.com/liweiwww/jdk.git

三、编译jdk的环境准备

编译jdk需要准备一些基础环境,比如编译OpenJDK的BootJDK版本、安装freetype、autoconf、ccache。接下来将详细讲解各个部分的安装过程
编译的命令如下:

bash configure --with-debug-level=slowdebug --with-jvm-variants=server --enable-ccache --with-freetype=bundled  --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home --disable-warnings-as-errors

3.1 Bootstrap JDK 准备

编译OpenJdk需要基础的jdk进行支撑,版本不应该和待编译的jdk差别过大,否则编译时会报错。报错信息如下:

configure: Found potential Boot JDK using well-known locations (in /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk)
configure: Potential Boot JDK found at /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home is incorrect JDK version (java version "1.8.0_144"); ignoring
configure: (Your Boot JDK version must be one of: 14 15)
configure: Could not find a valid Boot JDK. OpenJDK distributions are available at http://jdk.java.net/.
configure: This might be fixed by explicitly setting --with-boot-jdk
configure: error: Cannot continue

假设要编译大版本号为N的JDK,我们还要另外准备一个大版本号至少为N-1的、已经编译好的JDK,这是因为OpenJDK由多个部分(HotSpot、JDK类库、JAXWS、JAXP……)构成,其中一部分(HotSpot)代码使用C、C++编写,而更多的代码则是使用Java语言来实现,因此编译这些Java代码就需要用到另一个编译期可用的JDK,官方称这个JDK为“BootstrapJDK”。编译OpenJDK 15时,Bootstrap JDK必须使用JDK 14及之后的版本。

我机器上的java版本是:

bogon:openJdk15_20 bingsanlang$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

如果当前的系统java版本过低,需要先升级下jdk的版本。可以按照教程配置可切换的多版本jdk,教程:
https://blog.csdn.net/weixin_42112888/article/details/82851594

3.2 autoconf 安装

如果没有安装,报错信息如下:

Runnable configure script is not present
Generating runnable configure script at /Users/wanghanbing/soft/jdk/jdk/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

按照提示安装autoconf:

bogon:jdk bingsanlangf$ brew install autoconf
Updating Homebrew...

一般情况下会卡在这儿一直不执行,具体的解决办法如下:
https://www.jianshu.com/p/7cb05a2b39a5

ctrl+z 进行结束Updating Homebrew.出现两个命令都在更新,执行fg调出后台正在执行的更新,如下整个过程的输出:

^Z
[8]+  Stopped                 brew install autoconf
bogon:jdk bingdanlang$ cd "$(brew --repo)"
bogon:Homebrew bingdanlang$ git remote set-url origin https://mirrors.ustc.edu.cn/brew.git
bogon:Homebrew bingdanlang$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
bogon:homebrew-core bingdanlang$ git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
bogon:homebrew-core bingdanlang$ brew update
Error: Another active Homebrew update process is already in progress.
Please wait for it to finish or terminate it to continue.
bogon:homebrew-core bingdanlang$ fg
brew install autoconf	(wd: ~/soft/jdk/jdk)
^C
Warning: You are using macOS 10.12.
We (and Apple) do not provide support for this old version.
You will encounter build failures with some formulae.
Please create pull requests instead of asking for help on Homebrew's GitHub,
Discourse, Twitter or IRC. You are responsible for resolving any issues you
experience while you are running this old version.

==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.sierra.bottle.4.tar.gz
==> Downloading from https://akamai.bintray.com/a7/a76fca79a00f733c1c9f75600b906de4755dd3fbb595b1b55ded1347ac141607?__gda__=exp=1588497453
######################################################################## 100.0%
==> Pouring autoconf-2.69.sierra.bottle.4.tar.gz
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/autoconf
==> Summary
  /usr/local/Cellar/autoconf/2.69: 70 files, 3.0MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /usr/local/Cellar/pcre2/10.33... (226 files, 5.7MB)
Removing: /Users/bingdanlang/Library/Caches/Homebrew/pcre2--10.33.tar.bz2... (1.6MB)
Removing: /Users/bingdanlang/Library/Logs/Homebrew/gettext... (68B)
Removing: /Users/bingdanlang/Library/Logs/Homebrew/git... (10 files, 1MB)
Removing: /Users/bingdanlang/Library/Logs/Homebrew/pcre2... (7 files, 245.6KB)

只能说为了安装成功,网络下载速度太慢,我重复重试执行了很多次才安装成功,貌似每次失败后重新安装下载可以断点续传的感觉。
至此,安装autoconf成功。

3.3 ccache 和freetype安装

由于前面已经解决了brew update 卡顿的问题,所以ccache和freetype安装的比较顺利。

 brew install ccache
 brew install freetype

四、编译OpenJDK

执行编译命令:

bash configure --with-debug-level=slowdebug --with-jvm-variants=server --enable-ccache --with-freetype=bundled  --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home --disable-warnings-as-errors

当输出以下结果时表明build成功:

====================================================
A new configuration has been successfully created in
/Users/wanghanbing/soft/openJdk/openJdk15_20/build/macosx-x86_64-server-slowdebug
using configure arguments '--with-debug-level=slowdebug --with-jvm-variants=server --enable-ccache --with-freetype=bundled --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home --disable-warnings-as-errors'.

Configuration summary:
* Debug level:    slowdebug
* HS debug level: debug
* JVM variants:   server
* JVM features:   server: 'aot cds compiler1 compiler2 dtrace epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services shenandoahgc vm-structs zgc'
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 15-internal+0-adhoc.wanghanbing.openJdk1520 (15-internal)

Tools summary:
* Boot JDK:       java version "14.0.1" 2020-04-14 Java(TM) SE Runtime Environment (build 14.0.1+7) Java HotSpot(TM) 64-Bit Server VM (build 14.0.1+7, mixed mode, sharing)  (at /Library/Java/JavaVirtualMachines/jdk-14.0.1.jdk/Contents/Home)
* Toolchain:      clang (clang/LLVM)
* C Compiler:     Version 9.0.0 (at /usr/bin/clang)
* C++ Compiler:   Version 9.0.0 (at /usr/bin/clang++)

Build performance summary:
* Cores to use:   4
* Memory limit:   8192 MB
* ccache status:  Active (ccache version 3.7.7)

执行完build之后执行make images命令:

make images LOG=trace all

如果执行后结果输出如下,表示编译成功:

Creating jdk.jlink.jmod
Creating java.base.jmod
Creating jdk image
WARNING: Using incubator modules: jdk.incubator.jpackage, jdk.incubator.foreign
Creating CDS archive for jdk image
Stopping sjavac server
Finished building target 'images' in configuration 'macosx-x86_64-server-slowdebug'

测试下时候编译成功,进入目录
/Users/bingsanlang/soft/openJdk/openJdk15_20/build/macosx-x86_64-server-slowdebug/jdk/bin
或者
/Users/bingsanlang/soft/openJdk/openJdk15_20/build/macosx-x86_64-server-slowdebug/images/jdk/bin
执行如下命令:

bogon:bin wanghanbing$ ./java -version
openjdk version "15-internal" 2020-09-15
OpenJDK Runtime Environment (slowdebug build 15-internal+0-adhoc.wanghanbing.openJdk1520)
OpenJDK 64-Bit Server VM (slowdebug build 15-internal+0-adhoc.wanghanbing.openJdk1520, mixed mode)

五、注意事项

5.1 --with-freetype错误

当你使用–with-freetype=/usr/local/Cellar/freetype/x.x.x/ 时会遇到如下报错:

configure: error: Valid values for --with-freetype are 'system' and 'bundled'

改成:

--with-freetype=bundled

下一次我们将介绍怎么定制自己的jdk,以及如何debug模式jdk的源码。

你可能感兴趣的:(Java)