自己动手编译 JDK(MacOS)

Posted 冬瓜白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自己动手编译 JDK(MacOS)相关的知识,希望对你有一定的参考价值。

This browser does not support music or audio playback. Please play it in WeChat or another browser.

相关文章

去年就尝试过自己编译 JDK,但是失败了,最近在分析 Java 虚拟机是如何处理 Linux 信号的时候,虽然大体流程清晰了,但是还是有很多方面无法理解,所以编译调试 JDK 势在必行。

我的操作系统版本:macOS Catalina 10.15.5。

安装 XCode 和 Commnad Line Tools for XCode

这个应该是可以不安装的,但是为了避免出现依赖问题,我还是安装了。

安装版本:11.5 (11E608c)。

获取 OpenJDK 源码

OpenJDK 是使用 Mercurial 进行代码管理的,Mercurial 很小众,那还要再安装 Mercurial,感觉没必要,而且听说拉源码的时候网络很容易断。这里直接下载下来:http://jdk.java.net/

自己动手编译 JDK(MacOS)

我一开始选择了 8,源码下载下来后,查看一下说明:

自己动手编译 JDK(MacOS)

坑爹的是看到了这个:

自己动手编译 JDK(MacOS)

想想也是,8 其实已经算是一个老版本了,印象中编辑 JDK 之前需要准备一个 N-1 版本的可用的 Bootstrap JDK,我本机已经装了 JDK 11:

 
   
   
 
  1. ~ java11

  2. ~ java -version

  3. java version "11.0.2" 2019-01-15 LTS

  4. Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)

  5. Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)

那我换一个下载 12:

自己动手编译 JDK(MacOS)

macOS

Apple is using a quite aggressive scheme of pushing OS updates, and coupling these updates with required updates of Xcode. Unfortunately, this makes it difficult for a project such as the JDK to keep pace with a continuously updated machine running macOS. See the section on Apple Xcode on some strategies to deal with this.

It is recommended that you use at least Mac OS X 10.13 (High Sierra). At the time of writing, the JDK has been successfully compiled on macOS 10.12 (Sierra).

The standard macOS environment contains the basic tooling needed to build, but for external libraries a package manager is recommended. The JDK uses homebrew in the examples, but feel free to use whatever manager you want (or none).

Apple Xcode

The oldest supported version of Xcode is 8.

看着版本勉强应该可以契合,先试试。

准备环境

根据网上资料显示,需要安装这么几个:

 
   
   
 
  1. brew install ccache //加速编译

  2. brew install freetype //字体引擎,编译过程中会被依赖到

安装过程中更新 Homebrew 花了很长时间。

Configure 检查

首先需要配置编译参数,building.html 也有参数说明:

Configure Arguments for Tailoring the Build

  • --enable-debug - Set the debug level to fastdebug (this is a shorthand for --with-debug-level=fastdebug)

  • --with-debug-level= - Set the debug level, which can be releasefastdebugslowdebug or optimized. Default is releaseoptimized is variant of release with additional Hotspot debug code.

  • --with-native-debug-symbols= - Specify if and how native debug symbols should be built. Available methods are noneinternalexternalzipped. Default behavior depends on platform. See Native Debug Symbols for more details.

  • --with-version-string= - Specify the version string this build will be identified with.

  • --with-version-= - A group of options, where ` can be any ofpre ,opt ,build ,major ,minor ,security orpatch .Usethese options to modify just the corresponding part of the versionstringfromthedefault,orthe value providedby--with-version-string`.

  • --with-jvm-variants=[,...] - Build the specified variant (or variants) of Hotspot. Valid variants are: serverclientminimalcorezerocustom. Note that not all variants are possible to combine in a single build.

  • --with-jvm-features=[,...] - Use the specified JVM features when building Hotspot. The list of features will be enabled on top of the default list. For the custom JVM variant, this default list is empty. A complete list of available JVM features can be found using bash configure--help.

  • --with-target-bits= - Create a target binary suitable for running on a `` platform. Use this to create 32-bit output on a 64-bit build platform, instead of doing a full cross-compile. (This is known as a reduced build.)

On Linux, BSD and AIX, it is possible to override where Java by default searches for runtime/JNI libraries. This can be useful in situations where there is a special shared directory for system JNI libraries. This setting can in turn be overriden at runtime by setting the java.library.path property.

  • --with-jni-libpath= - Use the specified path as a default when searching for runtime libraries.

为了便于以后再回过头看,我这里完成记录命令执行过程:

 
   
   
 
  1. 2.10.1 cd ~/Desktop/temp/openjdk12

  2. openjdk12 ls

  3. openjdk 2 openjdk-12+32_src.zip

  4. openjdk12 cd openjdk\ 2

  5. openjdk 2 ll

  6. total 80

  7. -rw-r--r--@ 1 dongguabai staff 2.1K 2 14 2019 ADDITIONAL_LICENSE_INFO

  8. -rw-r--r--@ 1 dongguabai staff 1.5K 2 14 2019 ASSEMBLY_EXCEPTION

  9. -rw-r--r--@ 1 dongguabai staff 19K 2 14 2019 LICENSE

  10. -rw-r--r--@ 1 dongguabai staff 2.7K 2 14 2019 Makefile

  11. -rw-r--r--@ 1 dongguabai staff 341B 2 14 2019 README

  12. drwxr-xr-x@ 9 dongguabai staff 288B 5 27 13:40 bin

  13. -rw-r--r--@ 1 dongguabai staff 1.6K 2 14 2019 configure

  14. drwxr-xr-x@ 7 dongguabai staff 224B 5 27 13:40 doc

  15. drwxr-xr-x@ 68 dongguabai staff 2.1K 5 27 13:40 make

  16. drwxr-xr-x@ 78 dongguabai staff 2.4K 5 27 13:41 src

  17. drwxr-xr-x@ 16 dongguabai staff 512B 5 27 13:41 test

  18. openjdk 2 ./configure --with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.10.1 --enable-ccache --with-jvm-variants=server,client --with-boot-jdk-jvmargs="-Xlint:deprecation -Xlint:unchecked" --disable-warnings-as-errors --with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log

  19. zsh: permission denied: ./configure

  20. openjdk 2 sudo ./configure --with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.10.1 --enable-ccache --with-jvm-variants=server,client --with-boot-jdk-jvmargs="-Xlint:deprecation -Xlint:unchecked" --disable-warnings-as-errors --with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log

  21. Password:

  22. sudo: ./configure: command not found

  23. openjdk 2 chmod u+x configure

  24. openjdk 2 sudo ./configure --with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.10.1 --enable-ccache --with-jvm-variants=server,client --with-boot-jdk-jvmargs="-Xlint:deprecation -Xlint:unchecked" --disable-warnings-as-errors --with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log

  25. /Users/dongguabai/Desktop/temp/openjdk12/openjdk: /Users/dongguabai/Desktop/temp/openjdk12/openjdk: No such file or directory

  26. openjdk 2 pwd

  27. /Users/dongguabai/Desktop/temp/openjdk12/openjdk 2

  28. openjdk 2 cd ..

  29. openjdk12 mv openjdk\ 2 openjdk

  30. openjdk12 ll

  31. total 361664

  32. drwxr-xr-x@ 20 dongguabai staff 640B 5 27 13:49 openjdk

  33. -rw-r--r--@ 1 dongguabai staff 173M 5 27 13:25 openjdk-12+32_src.zip

  34. openjdk12 cd openjdk

  35. openjdk ls

  36. ADDITIONAL_LICENSE_INFO Makefile configure make

  37. ASSEMBLY_EXCEPTION README configure_mac_x64.log src

  38. LICENSE bin doc test

  39. openjdk sudo ./configure --with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.10.1 --enable-ccache --with-jvm-variants=server,client --with-boot-jdk-jvmargs="-Xlint:deprecation -Xlint:unchecked" --disable-warnings-as-errors --with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log

  40. Runnable configure script is not present

  41. Generating runnable configure script at /Users/dongguabai/Desktop/temp/openjdk12/openjdk/build/.configure-support/generated-configure.sh


  42. Autoconf is not found on the PATH, and AUTOCONF is not set.

  43. You need autoconf to be able to generate a runnable configure script.

  44. You might be able to fix this by running 'brew install autoconf'.

  45. Error: Cannot find autoconf

发现报错了,缺少 autoconf,还是那句话,要认真读文档啊:

Build Tools Requirements

Autoconf

The JDK requires Autoconf on all platforms. At least version 2.69 is required.

  • To install on an apt-based Linux, try running sudo apt-getinstall autoconf.

  • To install on an rpm-based Linux, try running sudo yum install autoconf.

  • To install on macOS, try running brew install autoconf.

  • To install on Windows, try running /setup-x86_64-q-P autoconf.

If configure has problems locating your installation of autoconf, you can specify it using the AUTOCONF environment variable, like this:

  
    
    
  
  1. AUTOCONF=<path to autoconf> configure ...

安装 autoconf 后再走一遍:

 
   
   
 
  1. openjdk brew install autoconf

  2. Updating Homebrew...

  3. ==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.catalina.bottle.4.tar.gz

  4. ==> Downloading from https://akamai.bintray.com/ca/ca510b350e941fb9395522a03f9d2fb5df276085d806ceead763acb95889a368?__gda__=exp=1590559880~hmac

  5. ######################################################################## 100.0%

  6. ==> Pouring autoconf-2.69.catalina.bottle.4.tar.gz

  7. 以上是关于自己动手编译 JDK(MacOS)的主要内容,如果未能解决你的问题,请参考以下文章

    JNI用C加载JDK产生JVM虚拟机,并运行JAVA类main函数(MACOS/LINUX/WINDOWS)

    JNI用C加载JDK产生JVM虚拟机,并运行JAVA类main函数(MACOS/LINUX/WINDOWS)

    Android源码编译

    写 Java 这么久了,你编译过 JDK 源码吗

    白话开发——自己动手编译Android源码

    自己动手写编译器:中间代码生成1