Mac OSX 10.12.1 编译 Openjdk 9

推荐阅读:

本机环境:

  • 系统版本:macOS Sierra (10.12.1)
  • Boot JDK: java version “1.8.0_112” Java(TM) SE Runtime Environment (build 1.8.0_112-b16) Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode) (at /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home)
  • Toolchain: clang (clang/LLVM)
  • C Compiler: Version 8.0.0 (at /usr/bin/clang)
  • C++ Compiler: Version 8.0.0 (at /usr/bin/clang++)

编译前准备:

1.本机需要安装brew
参考:Homebrew — macOS 不可或缺的套件管理器
2.推荐使用iTerm2替换系统默认的“终端”
官网:iTerm2 – macOS Terminal Replacement
3.openjdk代码由hg管理,因此需要安装mercurial
$ brew install mercurial
若link失败,则:
$ sudo brew link mercurial
4、安装ccache提高编译速度
$ brew install ccache
5、安装freetype(编译时需要)
$ brew install freetype

开始编译:

1.获取源码

$ hg clone http://hg.openjdk.java.net/jdk9/jdk9 YourOpenJDK
$cd YourOpenJDK
$ bash ./get_source.sh

如果clone失败,建议按照如下方式修改get_source.sh,将

# Get clones of all absent nested repositories (harmless if already exist)
sh ./common/bin/hgforest.sh clone "$@" || exit $?

# Update all existing repositories to the latest sources
sh ./common/bin/hgforest.sh pull -u

修改为

# Get clones of all absent nested repositories (harmless if already exist)
sh ./common/bin/hgforest.sh clone "$@"

while [ $? -ne 0 ]
do
    sh ./common/bin/hgforest.sh clone "$@"
done

# Update all existing repositories to the latest sources
sh ./common/bin/hgforest.sh pull -u

while [ $? -ne 0 ]
do
    sh ./common/bin/hgforest.sh pull -u
done

该解决方法参考以下网页:http://www.jianshu.com/p/746963f28245
还有一种方法就是依次使用hg clone克隆以下链接到当前源码编译目录:

hg clone http://hg.openjdk.java.net/jdk9/jdk9/corba corba
hg clone http://hg.openjdk.java.net/jdk9/jdk9/hotspot hotspot
hg clone http://hg.openjdk.java.net/jdk9/jdk9/jaxp jaxp
hg clone http://hg.openjdk.java.net/jdk9/jdk9/jaxws jaxws
hg clone http://hg.openjdk.java.net/jdk9/jdk9/jdk jdk
hg clone http://hg.openjdk.java.net/jdk9/jdk9/langtools langtools
hg clone http://hg.openjdk.java.net/jdk9/jdk9/nashorn nashorn

获取所有源码完成以后开始继续配置编译
2.编译配置

./configure --with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.7 --enable-ccache --with-jvm-variants=server,client --with-boot-jdk-jvmargs="-Xlint:deprecation -Xlint:unchecked" --disable-zip-debug-info --disable-warnings-as-errors --with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log
……
====================================================
A new configuration has been successfully created in
/Volumes/Install/DocumentFls/Wor/Java/jdk9/build/macosx-x86_64-normal-serverANDclient-release
using configure arguments '--with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.7 --enable-ccache --with-jvm-variants=server,client --with-boot-jdk-jvmargs='-Xlint:deprecation -Xlint:unchecked' --disable-zip-debug-info --disable-warnings-as-errors --with-debug-level=release'.

Configuration summary:
* Debug level:    slowdebug
* HS debug level: debug
* JDK variant:    normal
* JVM variants:   server client
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 9-internal+0-2016-11-14-103347.virson.jdk9 (9-internal)

Tools summary:
* Boot JDK:       java version "1.8.0_112" Java(TM) SE Runtime Environment (build 1.8.0_112-b16) Java HotSpot(TM) 64-Bit Server VM (build 25.112-b16, mixed mode)  (at /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home)
* Toolchain:      clang (clang/LLVM)
* C Compiler:     Version 8.0.0 (at /usr/bin/clang)
* C++ Compiler:   Version 8.0.0 (at /usr/bin/clang++)

Build performance summary:
* Cores to use:   8
* Memory limit:   16384 MB
* ccache status:  Active (3.3.3)

NOTE: You have requested to build more than one version of the JVM, which
will result in longer build times.

WARNING: Your build output directory is not on a local disk.
This will severely degrade build performance!
It is recommended that you create an output directory on a local disk,
and run the configure script again from that directory.

The following warnings were produced. Repeated here for convenience:
WARNING: Option --enable-zip-debug-info is deprecated and will be ignored.
WARNING: Please use --with-native-debug-symbols=zipped .

编译参数简介:
--with-target-bits:设置32位/64位编译
--with-freetype:设置freetype路径
--enable-ccache:设置启用ccache
--with-jvm-variants=client,server:为了保证兼容性,编译时JVM的Client和Server都会被编译
--with-boot-jdk-jvmargs:提供运行Bootstrap JDK所需要的JVM参数
--disable-zip-debug-info:禁用zip调试信息
--disable-warnings-as-errors:禁用将警告当做错误,避免因为警告而中断编译
--with-debug-level:设置调试等级
2>&1 | tee configure_mac_x64.log:将错误信息重定向至标准输出,并输出到configure_mac_x64.log

3.开始编译

export LANG=C

make all LOG=debug  2>&1 | tee make_mac_x64.log

编译完成后的结果如下:

Building target 'default (exploded-image)' in configuration 'macosx-x86_64-normal-serverANDclient-slowdebug'
Building JVM variant 'server' with features 'all-gcs cds compiler1 compiler2 dtrace fprof jni-check jvmci jvmti management nmt services vm-structs'
Building JVM variant 'client' with features 'all-gcs cds compiler1 dtrace fprof jni-check jvmci jvmti management nmt services vm-structs'
Creating support/modules_libs/java.desktop/libsplashscreen.dylib from 1 file(s)
Compiling 4 files for BUILD_JIGSAW_TOOLS
Stopping sjavac server
Finished building target 'default (exploded-image)' in configuration 'macosx-x86_64-normal-serverANDclient-slowdebug'

测试:

MacBook-Pro-2:jdk9 virson$ cd ./build/macosx-x86_64-normal-serverANDclient-slowdebug/
MacBook-Pro-2:macosx-x86_64-normal-serverANDclient-slowdebug virson$ ls
Makefile        build.log.old       compare.sh      hotspot         make-support
bootcycle-spec.gmk  buildjdk-spec.gmk   configure-support   images          spec.gmk
build.log       buildtools      configure.log       jdk         support
MacBook-Pro-2:macosx-x86_64-normal-serverANDclient-slowdebug virson$ cd jdk/bin
MacBook-Pro-2:bin virson$ ./java -version
openjdk version "9-internal"
OpenJDK Runtime Environment (slowdebug build 9-internal+0-2016-11-14-103347.virson.jdk9)
OpenJDK 64-Bit Server VM (slowdebug build 9-internal+0-2016-11-14-103347.virson.jdk9, mixed mode)

【编译完成】

编译过程中可能会遇到的问题:

下面是我在编译过程中遇到的一些问题和对应的解决方法,不一定正确,有些甚至是偏方,仅供参考,如果你有更优秀的解决方法,欢迎在下面留言评论!

问题一:

Traceback (most recent call last):
  File "/usr/bin/xattr-2.7", line 7, in <module>
    from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

请使用以下命令修正:

sudo mv /usr/bin/xattr-2.7 /usr/bin/xattr-2.7.bak
sudo cp /usr/bin/xattr-2.6 /usr/bin/xattr-2.7

接着重新执行“开始编译”中的第2步:编译配置,即:
./configure --with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.7 --enable-ccache --with-jvm-variants=server,client --with-boot-jdk-jvmargs="-Xlint:deprecation -Xlint:unchecked" --disable-zip-debug-info --disable-warnings-as-errors --with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log
然后重新使用make all JOBS=4 LOG=debug 2>&1 | tee make_mac_x64.log编译即可
分析:这里会出现这个问题的原因是因为本机安装了多个版本的Python,如果本机没有安装额外其他版本的Python(例如我这里的2.7.12),应该不会出现这个问题。

问题二:
错误日志如下:

=== Output from failing command(s) repeated here ===
/usr/bin/printf "* For target support_native_java.desktop_libosxapp_NSApplicationAWT.o:\n"
* For target support_native_java.desktop_libosxapp_NSApplicationAWT.o:
(/usr/bin/grep -v -e "^Note: including file:" <  /Volumes/Install/DocumentFls/Wor/Java/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/failure-logs/support_native_java.desktop_libosxapp_NSApplicationAWT.o.log || true) | /usr/bin/head -n 12
../jdk/src/java.desktop/macosx/native/libosxapp/NSApplicationAWT.m:335:48: error: conflicting parameter types in implementation of 'nextEventMatchingMask:untilDate:inMode:dequeue:': 'NSEventMask' (aka 'enum NSEventMask') vs 'NSUInteger' (aka 'unsigned long') [-Werror,-Wmismatched-parameter-types]
- (NSEvent *)nextEventMatchingMask:(NSUInteger)mask untilDate:(NSDate *)expiration inMode:(NSString *)mode dequeue:(BOOL)deqFlag {
                                    ~~~~~~~~~~ ^
/System/Library/Frameworks/AppKit.framework/Headers/NSApplication.h:281:58: note: previous definition is here
- (nullable NSEvent *)nextEventMatchingMask:(NSEventMask)mask untilDate:(nullable NSDate *)expiration inMode:(NSRunLoopMode)mode dequeue:(BOOL)deqFlag;
                                             ~~~~~~~~~~~ ^
1 error generated.
if test `/usr/bin/wc -l < /Volumes/Install/DocumentFls/Wor/Java/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/failure-logs/support_native_java.desktop_libosxapp_NSApplicationAWT.o.log` -gt 12; then /bin/echo "   ... (rest of output omitted)" ; fi
/usr/bin/printf "=== End of repeated output ===\n"
=== End of repeated output ===
if /usr/bin/grep -q "recipe for target .* failed" /Volumes/Install/DocumentFls/Wor/Java/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/build.log 2> /dev/null; then /usr/bin/printf "=== Make failure sequence repeated here ===\n" ; /usr/bin/grep "recipe for target .* failed" /Volumes/Install/DocumentFls/Wor/Java/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/build.log ; /usr/bin/printf "=== End of repeated output ===\n" ; /usr/bin/printf "Hint: Try searching the build log for the name of the first failed target.\n" ; else /usr/bin/printf "No indication of failed target found.\n" ; /usr/bin/printf "Hint: Try searching the build log for '] Error'.\n" ; fi
No indication of failed target found.
Hint: Try searching the build log for '] Error'.
/usr/bin/printf "Hint: If caused by a warning, try configure --disable-warnings-as-errors.\n\n"
Hint: If caused by a warning, try configure --disable-warnings-as-errors.

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

解决办法:
按照提示,将NSApplicationAWT.m中的
- (NSEvent *)nextEventMatchingMask:(NSUInteger)mask untilDate:(NSDate *)expiration inMode:(NSString *)mode dequeue:(BOOL)deqFlag {
替换为
- (nullable NSEvent *)nextEventMatchingMask:(NSEventMask)mask untilDate:(nullable NSDate *)expiration inMode:(NSRunLoopMode)mode dequeue:(BOOL)deqFlag {
即可解决

问题三:
错误日志如下,有2个类型的问题,共涉及3个代码文件MacosxDebuggerLocal.mjdhuff.cjdphuff.c

../hotspot/src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m:691:21: error: 'ePtAttachDeprecated' is deprecated: PT_ATTACH is deprecated. See PT_ATTACHEXC [-Werror,-Wdeprecated-declarations]
  if ((res = ptrace(PT_ATTACH, pid, 0, 0)) < 0) {
                    ^
/usr/include/sys/ptrace.h:85:19: note: expanded from macro 'PT_ATTACH'
#define PT_ATTACH       ePtAttachDeprecated     /* trace some running process */
                        ^
/usr/include/sys/ptrace.h:71:2: note: 'ePtAttachDeprecated' has been explicitly marked deprecated here
        ePtAttachDeprecated __deprecated_enum_msg("PT_ATTACH is deprecated. See PT_ATTACHEXC") = 10
        ^
1 error generated.
make[3]: *** [/Volumes/Install/DocumentFls/Wor/Java/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/support/native/jdk.hotspot.agent/libsa/MacosxDebuggerLocal.o] Error 1
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [jdk.hotspot.agent-libs] Error 2
=== Output from failing command(s) repeated here ===
* For target support_native_java.desktop_libjavajpeg_jdhuff.o:
../jdk/src/java.desktop/share/native/libjavajpeg/jdhuff.c:458:13: error: shifting a negative signed value is undefined [-Werror,-Wshift-negative-value]
  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
        ~~~~^
../jdk/src/java.desktop/share/native/libjavajpeg/jdhuff.c:458:28: error: shifting a negative signed value is undefined [-Werror,-Wshift-negative-value]
  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
                       ~~~~^
../jdk/src/java.desktop/share/native/libjavajpeg/jdhuff.c:458:43: error: shifting a negative signed value is undefined [-Werror,-Wshift-negative-value]
  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
                                      ~~~~^
../jdk/src/java.desktop/share/native/libjavajpeg/jdhuff.c:458:58: error: shifting a negative signed value is undefined [-Werror,-Wshift-negative-value]
  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
                                                     ~~~~^
   ... (rest of output omitted)
* For target support_native_java.desktop_libjavajpeg_jdphuff.o:
../jdk/src/java.desktop/share/native/libjavajpeg/jdphuff.c:218:13: error: shifting a negative signed value is undefined [-Werror,-Wshift-negative-value]
  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
        ~~~~^
../jdk/src/java.desktop/share/native/libjavajpeg/jdphuff.c:218:28: error: shifting a negative signed value is undefined [-Werror,-Wshift-negative-value]
  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
                       ~~~~^
../jdk/src/java.desktop/share/native/libjavajpeg/jdphuff.c:218:43: error: shifting a negative signed value is undefined [-Werror,-Wshift-negative-value]
  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
                                      ~~~~^
../jdk/src/java.desktop/share/native/libjavajpeg/jdphuff.c:218:58: error: shifting a negative signed value is undefined [-Werror,-Wshift-negative-value]
  { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
                                                     ~~~~^
   ... (rest of output omitted)
* For target support_native_jdk.hotspot.agent_libsa_MacosxDebuggerLocal.o:
../hotspot/src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m:691:21: error: 'ePtAttachDeprecated' is deprecated: PT_ATTACH is deprecated. See PT_ATTACHEXC [-Werror,-Wdeprecated-declarations]
  if ((res = ptrace(PT_ATTACH, pid, 0, 0)) < 0) {
                    ^
/usr/include/sys/ptrace.h:85:19: note: expanded from macro 'PT_ATTACH'
#define PT_ATTACH       ePtAttachDeprecated     /* trace some running process */
                        ^
/usr/include/sys/ptrace.h:71:2: note: 'ePtAttachDeprecated' has been explicitly marked deprecated here
        ePtAttachDeprecated __deprecated_enum_msg("PT_ATTACH is deprecated. See PT_ATTACHEXC") = 10
        ^
1 error generated.
=== End of repeated output ===
No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: If caused by a warning, try configure --disable-warnings-as-errors.

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

解决办法:
从日志中可以看出问题出在
../hotspot/src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m的691行,
../jdk/src/java.desktop/share/native/libjavajpeg/jdhuff.c的458行和
../jdk/src/java.desktop/share/native/libjavajpeg/jdphuff.c的218行
需要下载下面的2个patch文件,解压后放到对应路径下,我这里放在编译主目录/Volumes/Install/DocumentFls/Wor/Java/jdk9/下:
hotspot.patch.zip
jdk.patch.zip
放好后执行以下命令打上对应的patch,然后重新编译即可:

cd hotspot
patch -p1 <../hotspot.patch
cd ../jdk
patch -p1 <../jdk.patch

该解决方法参考以下网页:https://developer.jboss.org/blogs/scott.stark/2016/09/14/compiling-openjdk-9-on-osx-1011x-el-capitan-xcode-731?_sscc=t

问题四:
错误日志如下:

../jdk/src/java.desktop/macosx/native/libsplashscreen/splashscreen_sys.m:397:5: error: 'objc_registerThreadWithCollector' is deprecated: it does nothing. Define OBJC_SILENCE_GC_DEPRECATIONS=1 to temporarily silence this diagnostic. [-Werror,-Wdeprecated-declarations]
    objc_registerThreadWithCollector();
    ^
/usr/include/objc/objc-auto.h:245:25: note: 'objc_registerThreadWithCollector' has been explicitly marked deprecated here
static OBJC_INLINE void objc_registerThreadWithCollector() { }
                        ^
1 error generated.
make[3]: *** [/Volumes/Install/DocumentFls/Wor/Java/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/support/native/java.desktop/libsplashscreen/splashscreen_sys.o] Error 1
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [java.desktop-libs] Error 2

ERROR: Build failed for target 'default (exploded-image)' in configuration 'macosx-x86_64-normal-serverANDclient-slowdebug' (exit code 2)
=== Output from failing command(s) repeated here ===
* For target support_native_java.desktop_libsplashscreen_splashscreen_sys.o:
../jdk/src/java.desktop/macosx/native/libsplashscreen/splashscreen_sys.m:397:5: error: 'objc_registerThreadWithCollector' is deprecated: it does nothing. Define OBJC_SILENCE_GC_DEPRECATIONS=1 to temporarily silence this diagnostic. [-Werror,-Wdeprecated-declarations]
    objc_registerThreadWithCollector();
    ^
/usr/include/objc/objc-auto.h:245:25: note: 'objc_registerThreadWithCollector' has been explicitly marked deprecated here
static OBJC_INLINE void objc_registerThreadWithCollector() { }
                        ^
1 error generated.
=== End of repeated output ===
No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: If caused by a warning, try configure --disable-warnings-as-errors.

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

解决方法:
注释掉splashscreen_sys.m文件的397行即可,即//objc_registerThreadWithCollector();

    原文作者:Virson
    原文地址: https://www.jianshu.com/p/26bd050610a4
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞