环境准备和下载依赖
最近看了《深入理解java虚拟机 第3版》,要继续阅读下去,需要下载jdk源码并编译。
官方下载地址:,在书中也有提供,但是非常慢
这边笔者推荐百度云盘下载:,提取码:c6mn
书中推荐在macOS或者Linux系统上安装编译,由于笔者是macOS,所以其他系统编译不涉及。
- 系统:MacBook Pro 10.14.6
- 依赖:
安装xcode,并安装Command Line Tools, 用于编译openjdk中c/c++实现
安装ccache,加速编译,使用brew安装(国内会有网络慢问题,参考##遇到的问题)
安装freetype,尽量upgrade到最新的版本 - n-1版本的jdk,用于编译openjdk中java实现(这里n指将编译的openjdk版本),所以这里要下载jdk11,下载地址
编译命令
cd 到 openjdk12的目录,可以使用 bash configure –help 查看configure参数信息。
参数说明:1
2
3
4
5
6--with-debug-level=slowdebug 启用slowdebug级别调试
--with-jvm-variants=server 编译server类型JVM
--enable-ccache 启用ccache,加快编译
--with-freetype=/usr/local/Cellar/freetype/2.10.1(你的版本)
--disable-warnings-as-errors 忽略警告
--with-boot-jdk=/Users/.../Downloads/openjdk11/Contents/Home 启动jdk的设置
有了以上的参数说明,我们可以使用以下命令验证当前的环境是否能编译成功:1
2>>bash configure --with-debug-level=slowdebug --with-jvm-variants=server --enable-ccache --with-freetype=/usr/local/Cellar/freetype/2.10.1 --with-boot-jdk=/Users/.../Downloads/openjdk11/Contents/Home --disable-warnings-as-errors
按照书中执行会出现问题,参考##遇到的问题
如果能编译成功,会返回如下结果:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23====================================================
A new configuration has been successfully created in
/Users/wyc/company/jdk12-06222165c35f/build/macosx-x86_64-server-slowdebug
using configure arguments '--with-debug-level=slowdebug --with-jvm-variants=server --enable-ccache --with-freetype-include=/usr/local/Cellar/freetype/2.9.1/include --with-freetype-lib=/usr/local/Cellar/freetype/2.9.1/lib --with-boot-jdk=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home --disable-warnings-as-errors'.
Configuration summary:
* Debug level: slowdebug
* HS debug level: debug
* 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 shenandoahgc vm-structs'
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 12-internal+0-adhoc.wyc.jdk12-06222165c35f (12-internal)
Tools summary:
* Boot JDK: openjdk version "11.0.7" 2020-04-14 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.7+10) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.7+10, mixed mode) (at /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home)
* Toolchain: clang (clang/LLVM from Xcode 11.3.1)
* C Compiler: Version 11.0.0 (at /usr/bin/clang)
* C++ Compiler: Version 11.0.0 (at /usr/bin/clang++)
Build performance summary:
* Cores to use: 4
* Memory limit: 8192 MB
* ccache status: Active (3.6)
同一个目录下,执行以下命令编译:1
>>make images
如果编译成功,会返回如下结果:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15......
Creating support/demos/image/jfc/CodePointIM/CodePointIM.jar
Creating support/demos/image/jfc/FileChooserDemo/FileChooserDemo.jar
Creating support/demos/image/jfc/SwingSet2/SwingSet2.jar
Creating support/demos/image/jfc/Font2DTest/Font2DTest.jar
Creating support/demos/image/jfc/J2Ddemo/J2Ddemo.jar
Creating support/demos/image/jfc/Metalworks/Metalworks.jar
Creating support/demos/image/jfc/Notepad/Notepad.jar
Creating support/demos/image/jfc/Stylepad/Stylepad.jar
Creating support/demos/image/jfc/SampleTree/SampleTree.jar
Creating support/demos/image/jfc/TableExample/TableExample.jar
Creating support/demos/image/jfc/TransparentRuler/TransparentRuler.jar
Creating jdk image
Creating CDS archive for jdk image
Finished building target 'images' in configuration 'macosx-x86_64-server-slowdebug'
遇到的问题
1.brew install 时遇到网络很慢的问题,需要切源,这边推荐稳定的中科大源。参考
2.bash configure 验证时出现第一个错误:1
2
3>>bash configure --with-debug-level=slowdebug --with-jvm-variants=server --enable-ccache --with-freetype=/usr/local/Cellar/freetype/2.9.1 --with-boot-jdk=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home --disable-warnings-as-errors
......
>>configure: error: 'valid values for --with-freetype are 'system' and 'bundled'
需要将freetype分开成include和lib,即 –with-freetype=/usr/local/Cellar/freetype/2.9.1 修改成–with-freetype-include=/usr/local/Cellar/freetype/2.9.1/include –with-freetype-lib=/usr/local/Cellar/freetype/2.9.1/lib
参考
3.修改后 bash configure 验证时出现第二个错误:1
2
3>>bash configure --with-debug-level=slowdebug --with-jvm-variants=server --enable-ccache --with-freetype-include=/usr/local/Cellar/freetype/2.9.1/include --with-freetype-lib=/usr/local/Cellar/freetype/2.9.1/lib --with-boot-jdk=/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home --disable-warnings-as-errors
......
>>configure: error: Only bundled freetype can be specified on Mac and Windows
需要注释 openjdk12目录/make/autoconf/lib-freetype.m4中的一段程序:1
2
3
4
5
6......
#if (test "x$FREETYPE_TO_USE" = "xsystem") && \
# (test "x$OPENJDK_TARGET_OS" = "xwindows" || test "x$OPENJDK_TARGET_OS" = "xmacosx"); then
# AC_MSG_ERROR([Only bundled freetype can be specified on Mac and Windows])
#fi
......