• MacOS 环境编译 JVM 源码


    一、Mac 当前工具环境

    系统环境

    在这里插入图片描述

    需要的工具

    在这里插入图片描述

    二、资源下载

    XCode 下载

    苹果官网 xcode 下载

    xcode_13.4.1 下载

    JDK 下载

    JDK 的网址

    推荐下载 jdk 11

    在这里插入图片描述

    三、编译步骤

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

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

    # 全量编译
    make all
    
    • 1
    • 2

    四、遇见问题和解决方案

    错误一

    $  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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    根据提示,需要修改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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    根据,提示,我们需要修改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)));
    你没看错。。就是增加了两个括号而已,但是就是这么神奇,不加就报错,加了就不报错
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    错误三

    重新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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    从错误信息里面可以看出,我们这里需要修改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)) {
    基本没啥差别,就是加了个括号。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    错误四

    这时再次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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    可以看到,这里需要修改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;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    修改后:

    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;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    错误五

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

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

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

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

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

  • 相关阅读:
    MySQL导出数据为csv的方法(亲测)
    YOLOv5改进实战 | 更换主干网络Backbone(三)之轻量化模型Shufflenetv2
    YOLOv7改进:ConvNeXt(backbone改为CNeB)
    MySql ocp认证之主从复制(六)-如何解决复制延迟的问题
    【读书笔记】【Effective C++】让自己习惯 C++
    Python中的Socket编程
    基于nodejs+vue学籍管理系统
    【活体检测】“深度学习驱动的人脸反欺诈检测系统:性能提升与多模型支持“
    【原创】基于SpringBoot的灾情救助系统(疫情援助系统)(SpringBoot毕业设计)
    【Flutter小记10】apk 提交各大应用市场,出现armeabi与arm64 版本标识/版本号不一致无法上传审核的解决方案
  • 原文地址:https://blog.csdn.net/u010022158/article/details/126681490