MacOS 环境编译 JVM 源码

本文详细记录了在Mac系统中编译JDK11时遇到的一系列错误,包括源码中未指定的字符串比较、数组大小计算错误、类型转换警告、assert宏定义问题以及vfork函数的弃用警告。针对每个问题,提供了具体的源码修改方案,如增加括号、类型转换等,最终成功解决了编译问题。
摘要由CSDN通过智能技术生成

一、Mac 当前工具环境

系统环境

在这里插入图片描述

需要的工具

在这里插入图片描述

二、资源下载

XCode 下载

苹果官网 xcode 下载

xcode_13.4.1 下载

JDK 下载

JDK 的网址

推荐下载 jdk 11

在这里插入图片描述

三、编译步骤

# 检查当前环境是否满足
sh configure

环境满足
如果出现上面的图表示环境满足

# 全量编译
make all

四、遇见问题和解决方案

错误一

$  make
Building target 'default (exploded-image)' in configuration 'macosx-x86_64-normal-server-release'
Warning: No mercurial configuration present and no .src-rev
Compiling 8 files for BUILD_TOOLS_LANGTOOLS

~/Java/openjdk11/openjdk/src/hotspot/share/runtime/arguments.cpp:1461:35: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]
      if (old_java_vendor_url_bug != DEFAULT_VENDOR_URL_BUG) {
                                  ^  ~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[3]: *** [/Users/lzq/Java/openjdk11/openjdk/build/macosx-x86_64-normal-server-release/hotspot/variant-server/libjvm/objs/arguments.o] Error 1
make[3]: *** Waiting for unfinished jobs....
Compiling 90 properties into resource bundles for java.desktop
Compiling 2961 files for java.base
make[2]: *** [hotspot-server-libs] Error 2
make[2]: *** Waiting for unfinished jobs....

ERROR: Build failed for target 'default (exploded-image)' in configuration 'macosx-x86_64-normal-server-release' (exit code 2) 
Stopping sjavac server

=== Output from failing command(s) repeated here ===
* For target hotspot_variant-server_libjvm_objs_arguments.o:
~/Java/openjdk11/openjdk/src/hotspot/share/runtime/arguments.cpp:1461:35: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]
      if (old_java_vendor_url_bug != DEFAULT_VENDOR_URL_BUG) {
                                  ^  ~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

* All command lines available in /Users/lzq/Java/openjdk11/openjdk/build/macosx-x86_64-normal-server-release/make-support/failure-logs.
=== End of repeated output ===

No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: See doc/building.html#troubleshooting for assistance.

make[1]: *** [main] Error 2
make: *** [default] Error 2

根据提示,需要修改src/hotspot/share/runtime/arguments.cpp文件的源码: 将第1461行的常量从DEFAULT_VENDOR_URL_BUG更改为0 更改前:

1461 if (old_java_vendor_url_bug != DEFAULT_VENDOR_URL_BUG) {
更改后:

1461 if (old_java_vendor_url_bug != 0) {

错误二

更改源码之后,重新make clean,然后make
$ make clean

Building target 'clean' in configuration 'macosx-x86_64-normal-server-release'
... ...
$  make
... ...

=== Output from failing command(s) repeated here ===
* For target hotspot_variant-server_libjvm_objs_sharedRuntime.o:
~/Java/openjdk11/openjdk/src/hotspot/share/runtime/sharedRuntime.cpp:2798:85: error: expression does not compute the number of elements in this array; element type is 'double', not 'relocInfo' [-Werror,-Wsizeof-array-div]
      buffer.insts()->initialize_shared_locs((relocInfo*)locs_buf, sizeof(locs_buf) / sizeof(relocInfo));
                                                                          ~~~~~~~~  ^
~/Java/openjdk11/openjdk/src/hotspot/share/runtime/sharedRuntime.cpp:2797:14: note: array 'locs_buf' declared here
      double locs_buf[20];
             ^
~/Java/openjdk11/openjdk/src/hotspot/share/runtime/sharedRuntime.cpp:2798:85: note: place parentheses around the 'sizeof(relocInfo)' expression to silence this warning
      buffer.insts()->initialize_shared_locs((relocInfo*)locs_buf, sizeof(locs_buf) / sizeof(relocInfo));
                                                                                    ^
1 error generated.

* All command lines available in /Users/lzq/Java/openjdk11/openjdk/build/macosx-x86_64-normal-server-release/make-support/failure-logs.
=== End of repeated output ===

No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: See doc/building.html#troubleshooting for assistance.

make[1]: *** [main] Error 2
make: *** [default] Error 2

根据,提示,我们需要修改src/hotspot/share/runtime/sharedRuntime.cpp这个文件的源码。 更改前:

2798       buffer.insts()->initialize_shared_locs((relocInfo*)locs_buf, sizeof(locs_buf) / sizeof(relocInfo));
更改后:

2798       buffer.insts()->initialize_shared_locs((relocInfo*)locs_buf, (sizeof(locs_buf)) / (sizeof(relocInfo)));
你没看错。。就是增加了两个括号而已,但是就是这么神奇,不加就报错,加了就不报错

在这里插入图片描述

错误三

重新make clean后,再次make,这时候出现了第三个编译错误。。。

$ make clean
Building target ‘clean’ in configuration ‘macosx-x86_64-normal-server-release’
$ make

=== Output from failing command(s) repeated here ===
* For target support_native_java.desktop_libawt_lwawt_CSystemColors.o:
/Users/lzq/Java/openjdk11/openjdk/src/java.desktop/macosx/native/libawt_lwawt/awt/CSystemColors.m:134:9: error: converting the result of '?:' with integer constants to a boolean always evaluates to 'true' [-Werror,-Wtautological-constant-compare]
    if (colorIndex < (useAppleColor) ? sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS : java_awt_SystemColor_NUM_COLORS) {
        ^
1 error generated.

* All command lines available in /Users/lzq/Java/openjdk11/openjdk/build/macosx-x86_64-normal-server-release/make-support/failure-logs.
=== End of repeated output ===

No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: See doc/building.html#troubleshooting for assistance.

make[1]: *** [main] Error 2
make: *** [default] Error 2

从错误信息里面可以看出,我们这里需要修改src/java.desktop/macosx/native/libawt_lwawt/awt/CSystemColors.m这个文件,修改方式和第2个错误基本相同,也是加括号=_=

修改前:

134     if (colorIndex < (useAppleColor) ? sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS : java_awt_SystemColor_NUM_COLORS) {
修改后:

134     if (colorIndex < ((useAppleColor) ? sun_lwawt_macosx_LWCToolkit_NUM_APPLE_COLORS : java_awt_SystemColor_NUM_COLORS)) {
基本没啥差别,就是加了个括号。

错误四

这时再次make,还会出现一个错误

$ make clean
$ make

ERROR: Build failed for target 'default (exploded-image)' in configuration 'macosx-x86_64-normal-server-release' (exit code 2)
Stopping sjavac server

=== Output from failing command(s) repeated here ===
* For target support_native_java.desktop_libjsound_PLATFORM_API_MacOSX_MidiUtils.o:
/Users/lzq/Java/openjdk11/openjdk/src/java.desktop/macosx/native/libjsound/PLATFORM_API_MacOSX_MidiUtils.c:263:31: error: cast to smaller integer type 'MIDIClientRef' (aka 'unsigned int') from 'void *' [-Werror,-Wvoid-pointer-to-int-cast]
static MIDIClientRef client = (MIDIClientRef) NULL;
                              ^~~~~~~~~~~~~~~~~~~~
/Users/lzq/Java/openjdk11/openjdk/src/java.desktop/macosx/native/libjsound/PLATFORM_API_MacOSX_MidiUtils.c:264:29: error: cast to smaller integer type 'MIDIPortRef' (aka 'unsigned int') from 'void *' [-Werror,-Wvoid-pointer-to-int-cast]
static MIDIPortRef inPort = (MIDIPortRef) NULL;
                            ^~~~~~~~~~~~~~~~~~
/Users/lzq/Java/openjdk11/openjdk/src/java.desktop/macosx/native/libjsound/PLATFORM_API_MacOSX_MidiUtils.c:265:30: error: cast to smaller integer type 'MIDIPortRef' (aka 'unsigned int') from 'void *' [-Werror,-Wvoid-pointer-to-int-cast]
static MIDIPortRef outPort = (MIDIPortRef) NULL;
                             ^~~~~~~~~~~~~~~~~~
/Users/lzq/Java/openjdk11/openjdk/src/java.desktop/macosx/native/libjsound/PLATFORM_API_MacOSX_MidiUtils.c:471:32: error: cast to smaller integer type 'MIDIEndpointRef' (aka 'unsigned int') from 'void *' [-Werror,-Wvoid-pointer-to-int-cast]
    MIDIEndpointRef endpoint = (MIDIEndpointRef) NULL;
                               ^~~~~~~~~~~~~~~~~~~~~~
   ... (rest of output omitted)

* All command lines available in /Users/coachhe/Tools/jdk-compile/openjdk11/build/macosx-x86_64-normal-server-release/make-support/failure-logs.
=== End of repeated output ===

No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: See doc/building.html#troubleshooting for assistance.

make[1]: *** [main] Error 2
make: *** [default] Error 2

可以看到,这里需要修改src/java.desktop/macosx/native/libjsound/PLATFORM_API_MacOSX_MidiUtils.c文件的4个地方。但是修改方式是统一的,都是将括号里的内容(MIDIClientRef,MIDIPortRef,MIDIEndpointRef)修改为unsigned long。

修改前:

263 static MIDIClientRef client = (MIDIClientRef) NULL;
264 static MIDIPortRef inPort = (MIDIPortRef) NULL;
265 static MIDIPortRef outPort = (MIDIPortRef) NULL;

471 MIDIEndpointRef endpoint = (MIDIEndpointRef) NULL;

修改后:

263 static MIDIClientRef client = (unsigned long) NULL;
264 static MIDIPortRef inPort = (unsigned long) NULL;
265 static MIDIPortRef outPort = (unsigned long) NULL;

471 MIDIEndpointRef endpoint = (unsigned long) NULL;

错误五

jfrNetworkUtilization.cpp:58:25: error: too many arguments provided to function-like macro invocatio

assert 宏定义有问题

解决方案

把用到的几处先注释掉 assert
在这里插入图片描述

错误六

ld: warning: dylib (/Users/lzq/Java/openjdk11/openjdk/build/macosx-x86_64-normal-server-release/support/modules_libs/java.base/server/libjvm.dylib) was built for newer macOS version (12.0) than being linked (10.9)

ERROR: Build failed for target 'all' in configuration 'macosx-x86_64-normal-server-release' (exit code 2)

=== Output from failing command(s) repeated here ===
* For target support_native_java.base_libjava_ProcessImpl_md.o:
/Users/lzq/Java/openjdk11/openjdk/src/java.base/unix/native/libjava/ProcessImpl_md.c:364:17: error: 'vfork' is deprecated: Use posix_spawn or fork [-Werror,-Wdeprecated-declarations]
    resultPid = vfork();
                ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/unistd.h:604:1: note: 'vfork' has been explicitly marked deprecated here
__deprecated_msg("Use posix_spawn or fork")
^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/sys/cdefs.h:208:48: note: expanded from macro '__deprecated_msg'
        #define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
                                                      ^
1 error generated.

* All command lines available in /Users/lzq/Java/openjdk11/openjdk/build/macosx-x86_64-normal-server-release/make-support/failure-logs.
=== End of repeated output ===

No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: See doc/building.html#troubleshooting for assistance.

make[1]: *** [main] Error 2
make: *** [all] Error 2

resultPid = vfork(); 改成 resultPid = fork();

编译成功
在这里插入图片描述

JDK11编译过程(超详细,超多图)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值