2021-09-28—MacOS编译openjdk9

MacOS编译openjdk9

下载JDK源码文件:

方式一,Git clone:https://github.com/campolake/openjdk9.git

方式二,官方下载jdk源码zip包:http://jdk.java.net/

各个版本JDK下载地址汇总https://blog.csdn.net/qq_23091073/article/details/83178848

以下命令均在openjdk9的路径下执行:

1、首先安装必要依赖:

brew install ccache
brew install freetype
brew install ant
brew install autoconf
brew install llvm
brew install binutils

2、配置configure权限:

chmod u+x configure

3、进行检查配置:

注意:参数--with-freetype=/usr/local/Cellar/freetype/2.11.0中的路径一定是你自己的安装路径。

sudo bash ./configure --with-target-bits=64 --with-freetype=/usr/local/Cellar/freetype/2.11.0 --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

配置xcode丢失的文件(Xcode 10以后删除了libstdc++库,转而使用libc++),
解决方法就是从github上下载库,然后设置环境变量后解决,此方法我编译过两次都通过。
克隆仓库:git clone https://github.com/imkiwa/xcode-missing-libstdc-.git
执行仓库下的install.sh进行安装。
接下来进行环境变量的配置:

export CFLAGS="-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1"
export CXXFLAGS="-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1"
export LDFLAGS=-L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/
export LIBRARY_PATH=$LIBRARY_PATH:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/

4、执行make进行编译

sudo make all JOBS=8 ZIP_DEBUGINFO_FILES=0 COMPILER_WARNINGS_FATAL=false  CC=clang USE_CLANG=true LP64=1 LOG=debug  2>&1 | tee make_mac_x64.log

5、错误汇总

参考博客,这里有些常见的错误对应的解决方法:https://blog.csdn.net/lizhengjava/article/details/105629780

此博客也有一些需要更改cpp代码的错误的解决方法:https://blog.csdn.net/qq_25905629/article/details/107485000

其他有关问题解决方案的博客:https://super2bai.github.io/JVM/build.html

其他错误1:

Building target 'default (exploded-image)' in configuration 'macosx- x86_64-normal-server-slowdebug' Warning: No mercurial configuration present and no .src-rev

其他错误1—解决方法博客地址:

https://stackoverflow.com/questions/50678467/building-openjdk-9-on-mac-os/54954805#54954805

其他错误2:

[error occurred during error reporting (), id 0x4]

make[3]: *** [/Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/jdk/_packages_attribute.done] Abort trap: 6
make[2]: *** [exploded-image-optimize] Error 2

ERROR: Build failed for target 'all' in configuration 'macosx-x86_64-normal-serverANDclient-slowdebug' (exit code 2)
[ -f /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/javacservers/server.port ] && /bin/echo Stopping sjavac server && /usr/bin/touch /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/javacservers/server.port.stop; true
/bin/date '+%Y %m %d %H %M %S' | /usr/bin/awk '{ print $1,$2,$3,$4,$5,$6,($4*3600+$5*60+$6) }' > /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/build-times/build_time_end_TOTAL
/bin/date '+%Y-%m-%d %H:%M:%S' > /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/build-times/build_time_end_TOTAL_human_readable
/bin/echo `/bin/cat /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/build-times/build_time_start_TOTAL` `/bin/cat /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/build-times/build_time_end_TOTAL` TOTAL | /usr/bin/awk '{ F=$7; T=$14; if (F > T) { T+=3600*24 }; D=T-F; H=int(D/3600); M=int((D-H*3600)/60); S=D-H*3600-M*60; printf("%02d:%02d:%02d %s\n",H,M,S,$15); }' > /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/build-times/build_time_diff_TOTAL
/usr/bin/printf -- "----- Build times -------\nStart %s\nEnd %s\n%s\n%s\n-------------------------\n" "`/bin/cat /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/build-times/build_time_start_TOTAL_human_readable`" "`/bin/cat /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/build-times/build_time_end_TOTAL_human_readable`" "`/bin/ls /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/build-times/build_time_diff_* | /usr/bin/grep -v _TOTAL | /usr/bin/xargs /bin/cat | /usr/bin/sort -k 2`" "`/bin/cat /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/build-times/build_time_diff_TOTAL`" > >(/usr/bin/tee -a /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/build.log) 2> >(/usr/bin/tee -a /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/build.log >&2) && wait
if /usr/bin/grep -q "recipe for target .* failed" /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/build.log 2> /dev/null; then /usr/bin/printf "\n=== Make failed targets repeated here ===\n" ; /usr/bin/grep "recipe for target .* failed" /Users/yier/Documents/openjdk/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/build.log ; /usr/bin/printf "=== End of repeated output ===\n" ; /usr/bin/printf "\nHint: Try searching the build log for the name of the first failed target.\n" ; else /usr/bin/printf "\nNo indication of failed target found.\n" ; /usr/bin/printf "Hint: Try searching the build log for '] Error'.\n" ; fi
----- Build times -------
Start 2019-10-16 20:52:13
End 2019-10-16 20:52:28

00:00:15 TOTAL
-------------------------

No indication of failed target found.
Hint: Try searching the build log for '] Error'.
/usr/bin/printf "Hint: See common/doc/building.html#troubleshooting for assistance.\n\n"
Hint: See common/doc/building.html#troubleshooting for assistance.

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

其他错误2—-解决方法:

注释文件hotspot/src/share/vm/runtime/perfMemory.cpp第75~77行:

// if (SafepointSynchronize::is_at_safepoint() && !StatSampler::is_active()) {
// PerfDataManager::destroy();
// }

解决方案:https://stackoverflow.com/questions/50678467/building-openjdk-9-on-mac-os/54954805

但是这样处理后,使用jstat监控JVM时可能会导致 内存泄露

6、编译成功结果

----- Build times -------
Start 2021-09-28 09:05:02
End 2021-09-28 09:17:43

00:12:41 TOTAL
-------------------------
/usr/bin/printf "Finished building target 'all' in configuration 'macosx-x86_64-normal-serverANDclient-slowdebug'\n" > >(/usr/bin/tee -a /Users/ellisonpei/Desktop/apache/sourceCode/java/openjdk9-master/build/macosx-x86_64-normal-serverANDclient-slowdebug/build.log) 2> >(/usr/bin/tee -a /Users/ellisonpei/Desktop/apache/sourceCode/java/openjdk9-master/build/macosx-x86_64-normal-serverANDclient-slowdebug/build.log >&2) && wait
Finished building target 'all' in configuration 'macosx-x86_64-normal-serverANDclient-slowdebug'

测试验证

ellisonpei@peimacbookpro bin % cd build/macosx-x86_64-normal-serverANDclient-slowdebug/jdk/bin
ellisonpei@peimacbookpro bin % ./java -version

控制台输出:

openjdk version "9-internal"
OpenJDK Runtime Environment (slowdebug build 9-internal+0-2021-09-28-085320.root.openjdk9-master)
OpenJDK 64-Bit Server VM (slowdebug build 9-internal+0-2021-09-28-085320.root.openjdk9-master, mixed mode)

7、使用Clion编译器进行断点调试

先去Jetbrains官网下载Clion下载软件。

下载链接:https://www.jetbrains.com/clion/

使用Clion打开openjdk9-master项目,添加Custom Build Application,然后配置targetExecutable

配置步骤具体参考博客:https://segmentfault.com/a/1190000040305260

最后断点启动,总是会断点到<unknown> 0x000000010ff59513,这时候

可以在点击 LLDB,输入
process handle SIGSEGV --stop=false
即可,这里告诉编译器忽略错误

然后跳过就会走到你的自定义断点处,就可以一步步调试了。

发布于

2021-09-28

更新于

2022-03-25

许可协议

评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...