MacOS 编译 OpenJDK 13

环境:
macOS Catalina 10.15.6
Xcode 11.6
Command Line Tools for Xcode 11.5

1、下载源码

  • 直接下载 OpenJDK 源码压缩包

  • Mercurial 下载 OpenJDK 源码

2、执行 configure 命令

在源码根目录下执行 configure 命令。

$ bash configure --with-debug-level=slowdebug --with-target-bits=64 --disable-warnings-as-errors --enable-dtrace --with-jvm-variants=server

参数说明【更多】:

参数 含义
--with-boot-jdk 指定 Bootstrap JDK 路径,不指定的话也会自动搜寻合适的 JDK 来使用。
--with-debug-level 设置编译的级别 (release | fastdebug | slowdebug | optimized),越往后进行的优化措施越少,带的调试信息越多。默认 release
--with-target-bits 指定编译 32 位还是 64 位的虚拟机。
--disable-warnings-as-errors 避免因为警告而导致编译过程中断。
--enable-dtrace 开启 DTrace
--with-jvm-variants 编译特定模式下的虚拟机,可选值为:serverclientminimalcorezerocustom
--with-conf-name 指定编译配置的名称,如果没有指定,MacOS 下会生成默认的配置名称 macosx-x86_64-server-slowdebug

执行出现如下报错信息:

configure: error: No xcodebuild tool and no system framework headers found, use --with-sysroot or --with-sdk-name to provide a path to a valid SDK
/Users/../../jdk13u/build/.configure-support/generated-configure.sh: line 82: 5: Bad file descriptor
configure exiting with result code 1

然后在网上搜索了很多相关的文档,很多都是因为 Xcode 的版本问题造成的。其实这时候我的系统是没有安装 Xcode 的,但是安装了 Command Line Tools for Xcode,因为在官方编译文档中有明确表示仅需要 Command Line Tools for Xcode 即可完成编译。

Apple Xcode

The oldest supported version of Xcode is 8.

You will need the Xcode command lines developers tools to be able to build the JDK. (Actually, only the command lines tools are needed, not the IDE.) The simplest way to install these is to run:

之后尝试安装了几个老版本的 Command Line Tools for Xcode,但是依旧报相同的错误,而且 Command Line Tools for Xcode 9.4.1 版本系统已经不兼容,最后尝试安装最新版的 Xcode,问题才得以解决。

但出现了新的问题...

configure: (Your Boot JDK version must be one of: 12 13)
checking for javac... /usr/bin/javac
checking for java... /usr/bin/java
configure: Found potential Boot JDK using well-known locations (in /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk)
configure: Potential Boot JDK found at /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home is incorrect JDK version (openjdk version "1.8.0_252"); ignoring
configure: (Your Boot JDK version must be one of: 12 13)
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

这是因为 JDK 源码中包含了部分的 Java 代码,这就需要编译时系统中安装有可用的 JDK,一般是使用和当前编译版本相同或上一个版本的 JDK 作为 Boot JDK,比如我们这里是编译的是 OpenJDK13,所以需要使用 12 或者 13 版本的 JDK 进行编译,安装之后,执行通过。

MacOS Homebrew 安装 OpenJDK

3、编译

$ make compile-commands

Building target 'compile-commands' in configuration 'macosx-x86_64-server-slowdebug'
Cleaning compile-commands make support artifacts ... done
Compiling 8 files for BUILD_TOOLS_LANGTOOLS
Compiling 1 files for BUILD_JFR_TOOLS
...
Compiling 1009 files for jdk.hotspot.agent
Updating compile_commands.json
Stopping sjavac server
Finished building target 'compile-commands' in configuration 'macosx-x86_64-server-slowdebug'

该编译命令的作用是,在编译 OpenJDK 源码的同时,也为编译的代码生成 Compilation Database,体现在,编译之后,生成的 ${source_root}/build/macosx-x86_64-server-slowdebug 目录下会存在 compile_commands.json 文件。

对于某些模块,使用预编译的头文件。因此,我们需要在导入 CLion 之前再次编译,否则 CLion 会在索引过程中提示找不到各种各样的文件。 此时只需调用 make 命令即可。

$ make

4、导入 CLion

打开 CLion,首先确认已经配置好 Toolchains

然后通过主菜单 File -> Open... 打开 ${source_root}/build/macosx-x86_64-server-slowdebug/compile_commands.json 文件,选择 As a project 打开。

默认情况下,项目的根目录是包含 compile_commands.json 文件的目录,是看不到源码的,可以通过 Tools -> Compilation Database -> Change Project Root 来指定你的根目录为 ${source_root}

5、Debug 配置

1. Custom Build Targets 配置

打开 Preferences -> Build, Execution, Deployment -> Custom Build Targets

新建一个 Custom Build Targets,设置好 Name 后,需要为 BuildClean 步骤增加 External Tools 配置,具体如下:

Build
Clean

这样我们就可以在 CLion 中对 JDK 源码进行编译了。

2. Run/Debug 配置

打开 Run -> Edit Configurations,创建一个基于 Custom Build Application 模板的配置,做如下配置:

  • Target:选择之前创建的目标。
  • Executable:选择 ${source_root}/build/macosx-x86_64-server-slowdebug/jdk/bin/java,或者其它你想调试的可执行文件,比如 javac
  • Program Arguments:执行参数,为了做测试,配置了 --version
  • Before luanch:默认在每次执行前都去 Build,因为是以增量的方式编译的,所以会很快。

3. Debug 实战

${source_root}/src/java.base/share/native/libjli/java.cJavaMain 方法首行添加断点,然后点击 debug 按钮,效果如下:

参考文档
  • Tips & Tricks: Develop OpenJDK in CLion with Pleasure
  • Building the JDK
  • 在MacOS系统上编译OpenJDK12并使用CLion调试

你可能感兴趣的:(MacOS 编译 OpenJDK 13)