在 MacOS 上编译 OpenJDK

在 MacOS 系统上编译 OpenJDK 遇到的一些问题及对应的解决方案的记录。因为 Java8 发布时间已经比较久,到现在各个操作系统很多地方都已经发生很大变化,所以小问题比较多,建议编译更好版本的 JDK,坑应该会少一些。

准备工作

1、Mac 上安装 Mercurial 总是报错,没找到什么原因,所以用不了 hg clone http://hg.openjdk.java.net/jdk8 这种方式。

jdk8 源码地址:jdk8u41源码下载 zip文件下载

2、安装 freetype 和 ccache:

brew install freetype

brew install ccache

编译

运行 configure:

./configure --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/

遇到的报错及解决方案

1、The C compiler (located as /usr/bin/gcc) does not seem to be the required GCC compiler.

打开文件common/autoconf/generated-configure.sh,搜索compiler is required
注释或把报错命令改为echo即可。

比较顺利,只遇到上面一个错误。

成功输出:

....
....
config.status: creating /Users/chenxii/Documents/Java/jdk8/openjdk8u41/openjdk/build/macosx-x86_64-normal-server-release/spec.sh
config.status: creating /Users/chenxii/Documents/Java/jdk8/openjdk8u41/openjdk/build/macosx-x86_64-normal-server-release/Makefile
config.status: creating /Users/chenxii/Documents/Java/jdk8/openjdk8u41/openjdk/build/macosx-x86_64-normal-server-release/config.h

====================================================
A new configuration has been successfully created in
/Users/chenxii/Documents/Java/jdk8/openjdk8u41/openjdk/build/macosx-x86_64-normal-server-release
using configure arguments '--with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/'.

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

Tools summary:
* Boot JDK:       openjdk version "1.8.0_292" OpenJDK Runtime Environment (Zulu 8.54.0.21-CA-macosx) (build 1.8.0_292-b10) OpenJDK 64-Bit Server VM (Zulu 8.54.0.21-CA-macosx) (build 25.292-b10, mixed mode)  (at /opt/java/zulu8.54.0.21-ca-jdk8.0.292-macosx_x64)
* C Compiler:      version  (at /usr/bin/gcc)
* C++ Compiler:    version  (at /usr/bin/g++)

Build performance summary:
* Cores to use:   4
* Memory limit:   16384 MB
* ccache status:  installed, but disabled (version older than 3.1.4)

Build performance tip: ccache gives a tremendous speedup for C++ recompilations.
You have ccache installed, but it is a version prior to 3.1.4. Try upgrading.

运行 make all

报错及解决:

1、clang: error: include path for libstdc++ headers not found; pass ‘-stdlib=libc++’ on the command line to use the libc++ standard library instead [-Werror,-Wstdlibcxx-not-found]

MAC编译OpenJDK8:clang: error: include path for libstdc++ headers not found

The missing libstdc++ headers and libraries for Xcode 10 or above.

2、

l-op-parentheses]
    if ( instr->is_ideal_branch() && instr->label_position() == -1 ||
         ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
/Users/chenxii/Documents/Java/jdk8/openjdk8u41/openjdk/hotspot/src/share/vm/adlc/archDesc.cpp:336:35: note: place parentheses around the '&&' expression to silence this warning
    if ( instr->is_ideal_branch() && instr->label_position() == -1 ||
                                  ^
         (                                                        )

其他可能遇到的错误

./configure –with-freetype-include=/usr/local/include/freetype2 –with-freetype-lib=/usr/local/lib/

报错:The C compiler (located as /usr/bin/gcc) does not seem to be the required GCC compiler.
打开文件common/autoconf/generated-configure.sh,搜索compiler is required
注释或把报错命令改为echo即可。


(base) ➜ jdk8 make
Building OpenJDK for target ‘default’ in configuration ‘macosx-x86_64-normal-server-release’

Starting langtools

/bin/sh: line 0: cd: /Users/chenxii/Documents/Java/jdk8/jdk8/langtools/make: No such file or directory
make: *** [langtools-only] Error 1

解决方案参考:https://stackoverflow.com/questions/34703703/jdk8-build-error-langtools


执行:./get_source.sh

(base) ➜ jdk8 ./get_source.sh

Repositories: corba jaxp jaxws langtools jdk hotspot nashorn

            corba:   hg clone http://hg.openjdk.java.net/jdk8/jdk8/corba corba
             jaxp:   hg clone http://hg.openjdk.java.net/jdk8/jdk8/jaxp jaxp
            corba:   requesting all changes

Waiting 5 secs before spawning next background command.

lcm.cpp:52:35: error: ordered comparison between pointer and zero (‘address’ (aka ‘unsigned char *’) and ‘int’)

解决:编辑/hotspot/src/share/vm/opto/lcm.cpp:52 原:if (Universe::narrow_oop_base() > 0) 修改后: if (Universe::narrow_oop_base()!= NULL) { // Implies UseCompressedOops.


error: ordered comparison between pointer and zero

修改openjdk/hotspot/src/share/vm/runtime/virtualspace.cpp
  修改331行为
  if (base() != 0) {


fatal error: ‘JavaNativeFoundation/JavaNativeFoundation.h’ file not found
#import <JavaNativeFoundation/JavaNativeFoundation.h>

(base) ➜ openjdk sudo find / -name “JavaNativeFoundation.h
Password:
find: /Library/Application Support/com.apple.TCC: Operation not permitted
/Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/JavaNativeFoundation.framework/Versions/A/Headers/JavaNativeFoundation.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/JavaVM.framework/Versions/A/Frameworks/JavaNativeFoundation.framework/Versions/A/Headers/JavaNativeFoundation.h

编辑:
vim hotspot/make/bsd/makefiles/saproc.make 61行:
SALIBS = -g -framework Foundation -F/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/JavaVM.framework/Frameworks -framework JavaNativeFoundation -framework Security -framework CoreFoundation

105行:
-I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/JavaVM.framework/Headers


执行:make CONF=macosx-x86_64-normal-server-release clean
make CONF=macosx-x86_64-normal-server-release install

—– Build times ——-
Start 2021-07-11 10:50:45
End 2021-07-11 10:59:52
00:00:16 corba
00:00:50 demos
00:01:24 docs
00:02:10 hotspot
00:01:01 images
00:00:09 jaxp
00:00:14 jaxws
00:02:35 jdk
00:00:18 langtools
00:00:08 nashorn

00:09:07 TOTAL

Finished building OpenJDK for target ‘all’

WARNING: You have the following ALT_ variables set:
ALT_PARALLEL_COMPILE_JOBS=2
ALT_ variables are deprecated and will be ignored. Please clean your environment.

[1] 90140 bus error build/macosx-x86_64-normal-server-release/jdk/bin/java -version

/Users/chenxii/Documents/Java/jdk8/openjdk/build/macosx-x86_64-normal-server-release/jdk/bin