跳到主要内容

MacOS编译JDK源码

· 阅读需 4 分钟

想要看jdk源码中的东西,并且想要调试里面的内容,本地编译就有用了。

openjdk 源码地址:https://github.com/openjdk/jdk

接下来编译openjdk8

源码地址:https://github.com/openjdk/jdk8u

官方 wiki:https://wiki.openjdk.org/display/jdk8u

使用最近 GA 版本 Latest GA release: 8u402

编译环境:macos Monterey 版本 12.5.1

下载源码,或者直接 zip 源码包。

git clone --branch jdk8u402-ga git@github.com:openjdk/jdk8u.git

下载 bootjdk ,https://jdk.java.net/java-se-ri/7 编译 JDK 需要使用 jdk,可以下载一个旧版本或者使用同版本都可以。鸡生蛋,蛋生鸡

安装 Homebrew

省略... 为了能快速安装下面的工具。

安装 Xcode

下载地址:https://developer.apple.com/download/all/?q=Xcode 编译 JDK 要的是 LLVM 的编译命令 clang,只要单独安装 LLVM 就可以; 但安装 Xcode 是最简单的方法。

注意版本的选择 Xcode 6, 9-12 is required to build JDK 8

该安装包很大,十多个 G

安装 autoconf 、freetype 工具

//软件源码包的自动配置工具
brew install autoconf

// 字体引擎,编译中要依赖的freetype
brew install freetype

检查工具版本 clang 版本

❯ clang --version
Apple clang version 14.0.0 (clang-1400.0.29.202)

make 版本

make --version
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.

freetype 版本

❯ freetype-config --version
24.3.18

bootjdk 版本

java -version
java version "1.8.0_341"
Java(TM) SE Runtime Enviro

执行配置命令 config

configure执行权限,然后执行如下命令进行自动配置

./configure --with-target-bits=64 --with-jvm-variants=server --with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log

参数说明:

  • --with-target-bits=64 设置 32 为/64 编译
  • --with-jvm-variants 设置要构建的 JVM 的变体,目前可以选择 server、client、minimal、core、zero、zeroshark、custom
  • --with-debug-level 启用 slowdebug 级别调试
  • 2>&1 | tee xxx.log 2>&1 错误重定向到标准输出;并将执行打印的信息全部保存到 xxx.log 文件中
  • 更多参数见:源码包中doc/building.md文件
A new configuration has been successfully created in
/Users/lichenghao/my/mysite/myjdk/jdk8u/build/macosx-x86_64-normal-server-slowdebug
using configure arguments '--with-target-bits=64 --with-jvm-variants=server --with-debug-level=slowdebug'.

Configuration summary:
* Debug level: slowdebug
* JDK variant: normal
* JVM variants: server
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64

Tools summary:
* Boot JDK: java version "1.8.0_341" Java(TM) SE Runtime Environment (build 1.8.0_341-b10) Java HotSpot(TM) 64-Bit Server VM (build 25.341-b10, mixed mode) (at /Library/Java/JavaVirtualMachines/jdk1.8.0_341.jdk/Contents/Home)
* Toolchain: clang (clang/LLVM)
* C Compiler: Version 12.0.0 (at /usr/bin/clang)
* C++ Compiler: Version 12.0.0 (at /usr/bin/clang++)

Build performance summary:
* Cores to use: 5
* Memory limit: 16384 MB

编译

make all

等待编译结果

----- Build times -------
Start 2024-03-26 16:27:37
End 2024-03-26 16:35:31
00:00:17 corba
00:00:11 demos
00:01:30 docs
00:02:19 hotspot
00:00:27 images
00:00:10 jaxp
00:00:14 jaxws
00:02:16 jdk
00:00:21 langtools
00:00:08 nashorn
00:07:54 TOTAL
-------------------------
Finished building OpenJDK for target 'all'

结果测试

进入编译好的目录 ...jdk8u/build/macosx-x86_64-normal-server-slowdebug/jdk/bin

❯ ./java -version
openjdk version "1.8.0_402-internal-debug"
OpenJDK Runtime Environment (build 1.8.0_402-internal-debug-lichenghao_2024_03_26_16_26-b00)
OpenJDK 64-Bit Server VM (build 25.402-b00-debug, mixed mode)

修改源码

编译源码不是最终目的,目的是能修改源码加上代码来理解代码。 常用的代码在 jdk/src/share 目录下 ,比如我们修改 println 方法的源码。 源码路径:jdk/src/share/java/io/PrintStream 增加打印print("-by lichlaughing");

    public void println(String x) {
synchronized (this) {
print(x);
print("-by lichlaughing");
newLine();
}
}

然后重新编译jdk目录,多个目录用逗号隔开

 make SUBDIRS="jdk"

使用 IDEA 工具,新建项目,使用我们重新构建的 JDK

public class App {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

结果

Hello World!-by lichlaughing

FAQ

参考文档

https://blog.csdn.net/weixin_42464282/article/details/130991709

安装 ccache 报错

`==> Installing dependencies for ccache: hiredis, lz4 and zstd`
`==> Installing ccache dependency: hiredis`
`==> Pouring hiredis-1.0.2.monterey.bottle.tar.gz`
Error: No such file or directory @ rb_sysopen

说明依赖 hiredis 没有安装成功,那么单独安装即可。如果有其他类似错误都可以这样处理。

brew install hiredis

configure: error Xcode

configure: error: Xcode 6, 9-12 is required to build JDK 8, the version found was 14.2. Use --with-xcode-path to specify the location of Xcode or make Xcode active by using xcode-select.

Xcdoe 的版本问题,jdk8 的编译需要 12 及以下的版本。