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)