Mac OSX之Catalina 10.15中編譯OpenJDK 8


關(guān)注我的都變美變帥了
不信你試試
虛懷若谷
為什么編譯JDK如果想深入學(xué)習(xí)JDK內(nèi)部的實(shí)現(xiàn)機(jī)制,最便捷的路徑是自己手動(dòng)編譯一套JDK,通過閱讀和跟蹤debug JDK源碼深入了解Java技術(shù)體系的原理,然后結(jié)合各類書籍、文章和博客,對(duì)比理解,更容易讓你了解JDK是怎么運(yùn)作的,底層都做了什么。
環(huán)境準(zhǔn)備Mac OS X Catalina 10.15.1
安裝依賴軟件
Xocde 11
xcode-select
$ xcode-select installHomebrew
Mercurial
$ brew install mercurialXQuartz
JDK 8
$ java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)freetype
FreeType是一個(gè)用C語言實(shí)現(xiàn)的一個(gè)字體光柵化庫。它可以用來將字符柵格化并映射成位圖以及提供其他字體相關(guān)業(yè)務(wù)的支持。
$ brew install freetypegcc
brew search gcc
brew install [email protected]llvm
brew install llvm添加軟連接
為了防止openJDK找不不到庫,所以需要加個(gè)軟鏈鏈接到他們歷史上曾經(jīng)存在位置。
// llvm
$ sudo ln -s /usr/bin/llvm-gcc /Applications/Xcode.app/Contents/Developer/usr/bin/llvm-gcc
$ sudo ln -s /usr/bin/llvm-g++ /Applications/Xcode.app/Contents/Developer/usr/bin/llvm-g++
// XQuartz
$ sudo ln -s /usr/X11/include/X11 /usr/include/X11
$ sudo ln -s /usr/X11/include/freetype2/freetype/ /usr/X11/include/freetype新版本Mac OS X Catalina中,升級(jí)Xcode 11,其SDK中已經(jīng)安裝有clang以及clang++的依賴,llvm 則無需再次安裝,在/usr/bin/目錄我們可以通過命令查看到:
$ ll /usr/bin/llvm-g++
lrwxr-xr-x 1 root wheel 7B Nov 12 22:02 /usr/bin/llvm-g++ -> clang++
$ ll /usr/bin/llvm-gcc
lrwxr-xr-x 1 root wheel 5B Nov 12 22:02 /usr/bin/llvm-gcc -> clang我們再查看clang和clang++的版本信息發(fā)現(xiàn)這兩個(gè)編譯工具均來源于Xcode的toolchains:
$ clang++ -v
Apple clang version 11.0.0 (clang-1100.0.33.12)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ clang -v
Apple clang version 11.0.0 (clang-1100.0.33.12)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin再來說說XQuartz,XQuartz下載完是一個(gè)dmg鏡像包,點(diǎn)擊安裝直到完成,在安裝過程中,自動(dòng)會(huì)把/opt/X11軟連接到/usr/include/X11,XQuartz是安裝到/var/select/X11/目錄下的,如果你發(fā)現(xiàn)沒有/usr/include/X11,則需要添加軟鏈接:ln -s /opt/X11 /usr/include/X11。
下載 OpenJDK
OpenJDK 是用Mercurial做版本控制的。如果您不熟悉 Mercurial,請(qǐng)參閱《Mercurial入門指南》。
Mac 環(huán)境需要安裝類似 Git 或 SVN 的版本控制工具 Mercurial,打開官網(wǎng)https://www.mercurial-scm.org/,在官網(wǎng)右側(cè)有Download按鈕,下面能看到Mac OS X(你可以點(diǎn)擊當(dāng)前鏈接進(jìn)入)。我們看到mercurial有兩種安裝方式:第一種下載?DMG?文件安裝,第二種通過命令安裝,上面安裝依賴中已經(jīng)給出。
在 OpenJDK 官網(wǎng)的首頁可以看到Hack on the JDK itself字樣,里面就有克隆倉庫(clone a Mercurial repository)的鏈接,點(diǎn)擊該鏈接進(jìn)去就會(huì)看到相關(guān)clone的命令,hg tclone http://hg.openjdk.java.net/jdk9/dev 9dev。我們需要編譯JDK8,所以命令需要改動(dòng)一下,OpenJDK的 mercurial 地址為:http://hg.openjdk.java.net/,根據(jù)自身情況選擇適合自己的JDK版本,如圖1,左側(cè)是選擇 JDK 大版本,右側(cè)是選擇 JDK 具體的版本,我們選擇jdk8u60。

克隆代碼:
hg tclone http://hg.openjdk.java.net/jdk8u/jdk8u60 jdk8u
cd jdk8u
bash ./get_source.sh # 克隆完成后,使用腳本獲取最新的源代碼,根據(jù)網(wǎng)絡(luò)環(huán)境的不同,這一步花費(fèi)的時(shí)間不同,一般花費(fèi)時(shí)間較長拉取完成的 jdk 源碼存儲(chǔ)目錄如下:
| Repository | Contains | 備注 |
|---|---|---|
| . (root) | 通用配置和生成makefile編譯文件的邏輯 | 主要是configure、MakeFile |
| hotspot | 用于構(gòu)建 OpenJDK 虛擬機(jī)的源代碼和make文件 | JVM源碼 |
| langtools | OpenJDK javac 和 語言工具源碼 | jdk中提供的開發(fā)工具,比如javac,javap,jvisual以及一些國際化實(shí)現(xiàn) |
| jdk | JDK源碼和用于構(gòu)建 OpenJDK 運(yùn)行時(shí)庫和雜項(xiàng)的make文件 | jdk源碼,編譯后生成的則是Java運(yùn)行時(shí)庫和一些雜項(xiàng)庫lib |
| jaxp | OpenJDK JAXP功能的源代碼 | |
| jaxws | OpenJDK JAX-WS功能的源代碼 | |
| corba | OpenJDK Corba功能的源代碼 | |
| nashorn | OpenJDK JavaScript 引擎實(shí)現(xiàn)的源代碼 | Java調(diào)用JavaScript時(shí)需要的一個(gè)引擎 |

環(huán)境變量設(shè)置
# 設(shè)定語言選項(xiàng),必須設(shè)置
export LANG=C
# Mac平臺(tái),C編譯器不再是GCC,而是clang
export CC=clang
export CXX=clang++
export CXXFLAGS=-stdlib=libc++
# 是否使用clang,如果使用的是GCC編譯,該選項(xiàng)應(yīng)該設(shè)置為false
export USE_CLANG=true
# 跳過clang的一些嚴(yán)格的語法檢查,不然會(huì)將N多的警告作為Error
export COMPILER_WARNINGS_FATAL=false
# 鏈接時(shí)使用的參數(shù)
export LFLAGS='-Xlinker -lstdc++'
# 使用64位數(shù)據(jù)模型
export LP64=1
# 告訴編譯平臺(tái)是64位,不然會(huì)按照32位來編譯
export ARCH_DATA_MODEL=64
# 允許自動(dòng)下載依賴
export ALLOW_DOWNLOADS=true
# 并行編譯的線程數(shù),編譯時(shí)長,為了不影響其他工作,可以選擇2
export HOTSPOT_BUILD_JOBS=2
export PARALLEL_COMPILE_JOBS=2 #ALT_PARALLEL_COMPILE_JOBS=2
# 是否跳過與先前版本的比較
export SKIP_COMPARE_IMAGES=true
# 是否使用預(yù)編譯頭文件,加快編譯速度
export USE_PRECOMPILED_HEADER=true
# 是否使用增量編譯
export INCREMENTAL_BUILD=true
# 編譯內(nèi)容
export BUILD_LANGTOOL=true
export BUILD_JAXP=true
export BUILD_JAXWS=true
export BUILD_CORBA=true
export BUILD_HOTSPOT=true
export BUILD_JDK=true
# 編譯版本
export SKIP_DEBUG_BUILD=true
export SKIP_FASTDEBUG_BULID=false
export DEBUG_NAME=debug
# 避開javaws和瀏覽器Java插件之類部分的build
export BUILD_DEPLOY=false
export BUILD_INSTALL=false
# 最后需要干掉這兩個(gè)環(huán)境變量(如果你配置過),不然會(huì)發(fā)生詭異的事件
unset JAVA_HOME
unset CLASSPATH將上面的環(huán)境變量設(shè)置 bash 窗口執(zhí)行(只在當(dāng)前 bash 窗口生效)或者將其保存在當(dāng)前用于目錄的?~/.bahs_profile中(全局生效,bash 窗口需要重新打開)或者保存在單獨(dú)的一個(gè)腳本envsetup.sh中均可。
open ~/.bash_profile # source envsetup.sh復(fù)制粘貼之后,保存~/.bahs_profile,然后 source 應(yīng)用使其生效:source ~/.bash_profile。
需要注意是,編譯 jdk 需要預(yù)先安裝一個(gè)JDK,可以是不同的版本,用于啟動(dòng),通過參數(shù)
--with-boot-jdk=${YOUR_JDK_HOME}來指定。
configure
bash ./configure --with-debug-level=slowdebug --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home --with-native-debug-symbols=internal–with-debug-level 設(shè)置編譯級(jí)別,在這里我們要編譯為能夠debug的版本,因此設(shè)為slowdebug。
–with-native-debug-symbols 生成調(diào)試需要debug符號(hào)信息,并將這些信息寫入到編譯生成的二進(jìn)制文件中,該參數(shù)一定要指定,否則用gdb調(diào)試的時(shí)候會(huì)提示找不到符號(hào)信息,無法設(shè)置斷點(diǎn)。
如果配置成功,configure 執(zhí)行結(jié)束時(shí)則在控制臺(tái)最后輸出如下信息:
====================================================
A new configuration has been successfully created in
/Users/zhoujunwen/Documents/workspace/jdk8u/build/macosx-x86_64-normal-server-slowdebug
using configure arguments '--with-debug-level=slowdebug --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home --with-native-debug-symbols=internal'.
Configuration summary:
* Debug level: slowdebug
* JDK variant: normal
* JVM variants: server
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
Tools summary:
* Boot JDK: java version "1.8.0_221" Java(TM) SE Runtime Environment (build 1.8.0_221-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.221-b11, mixed mode) (at /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home)
* Toolchain: gcc (GNU Compiler Collection)
* C Compiler: Version Apple clang version 11.0.0 (clang-1100.0.33.12) Target: x86_64-apple-darwin19.0.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin (at /usr/bin/clang)
* C++ Compiler: Version Apple clang version 11.0.0 (clang-1100.0.33.12) Target: x86_64-apple-darwin19.0.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin (at /usr/bin/clang++)
Build performance summary:
* Cores to use: 2
* Memory limit: 16384 MB
WARNING: You have old-style ALT_ environment variables set.
These are not respected, and will be ignored. It is recommended
that you clean your environment. The following variables are set:
ALT_PARALLEL_COMPILE_JOBS=2
WARNING: The result of this configuration has overridden an older
configuration. You *should* run 'make clean' to make sure you get a
proper build. Failure to do so might result in strange build problems.make all
make allmake如果不出錯(cuò),則會(huì)在10分鐘左右輸出下面信息:
----- Build times -------
Start 2019-11-19 10:30:24
End 2019-11-19 10:51:10
00:00:34 corba
00:01:25 demos
00:03:02 docs
00:04:18 hotspot
00:02:39 images
00:00:36 jaxp
00:00:54 jaxws
00:06:05 jdk
00:00:58 langtools
00:00:15 nashorn
00:20:46 TOTAL
-------------------------
Finished building OpenJDK for target 'all'測試一下是否編譯OK:
build/macosx-x86_64-normal-server-slowdebug/jdk/bin/java -version輸出如下說明編譯成功:
openjdk version "1.8.0-internal-debug"
OpenJDK Runtime Environment (build 1.8.0-internal-debug-zhoujunen_2019_11_19_11_50-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00-debug, mixed mode)
configure階段遇到的錯(cuò)誤問題1:Cannot locate autoconf, unable to correct situation
錯(cuò)誤信息
Configure source code has been updated, checking time stamps
Warning: The configure source files is newer than the generated files.
Cannot locate autoconf, unable to correct situation.
Please install autoconf and run 'bash autogen.sh' to update the generated files.
Error: Cannot continue解決方法
brew install autoconf問題2:Xcode 4 is required to build JDK 8, the version found was 11.1
錯(cuò)誤信息

checking for xcodebuild... /usr/bin/xcodebuild
configure: error: Xcode 4 is required to build JDK 8, the version found was 11.1. Use --with-xcode-path to specify the location of Xcode 4 or make Xcode 4 active by using xcode-select.
configure exiting with result code 1解決方法
找到 configure 文件并打開common/autoconf/generated-configure.sh,找到判斷版本的地方,將這一段全部注釋掉。
# Fail-fast: verify we're building on Xcode 4, we cannot build with Xcode 5 or later
XCODE_VERSION=`$XCODEBUILD -version | grep '^Xcode ' | sed 's/Xcode //'`
XC_VERSION_PARTS=( ${XCODE_VERSION//./ } )
if test ! "${XC_VERSION_PARTS[0]}" = "4"; then
as_fn_error $? "Xcode 4 is required to build JDK 8, the version found was $XCODE_VERSION. Use --with-xcode-path to specify the location of Xcode 4 or make Xcode 4 active by using xcode-select." "$LINENO" 5
fi問題3:A gcc compiler is required. Try setting –with-tools-dir
錯(cuò)誤信息

checking for xcodebuild... /usr/bin/xcodebuild
checking Determining Xcode SDK path... /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk
configure: Will use user supplied compiler CC=clang
checking for clang... /usr/bin/clang
checking resolved symbolic links for CC... no symlink
configure: The C compiler (located as /usr/bin/clang) does not seem to be the required gcc compiler.
configure: The result from running with --version was: ""
configure: error: A gcc compiler is required. Try setting --with-tools-dir.
configure exiting with result code 1解決方法
還是找到剛剛的文件common/autoconf/generated-configure.sh,有五處校驗(yàn)的地方(從27881行開始),找到并且注釋掉校驗(yàn)的邏輯。
if test $? -ne 0; then
ALT_VERSION_OUTPUT=`$COMPILER --version 2>&1`
{ $as_echo "$as_me:${as_lineno-$LINENO}: The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler." >&5
$as_echo "$as_me: The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler." >&6;}
{ $as_echo "$as_me:${as_lineno-$LINENO}: The result from running with -V was: \"$COMPILER_VERSION_OUTPUT\"" >&5
$as_echo "$as_me: The result from running with -V was: \"$COMPILER_VERSION_OUTPUT\"" >&6;}
{ $as_echo "$as_me:${as_lineno-$LINENO}: The result from running with --version was: \"$ALT_VERSION_OUTPUT\"" >&5
$as_echo "$as_me: The result from running with --version was: \"$ALT_VERSION_OUTPUT\"" >&6;}
as_fn_error $? "A $TOOLCHAIN_TYPE compiler is required. Try setting --with-tools-dir." "$LINENO" 5
fi問題4:fatal error: ‘iostream’ file not found
錯(cuò)誤信息
Compiling /Users/zhoujunwen/Documents/workspace/jdk8u/hotspot/src/share/vm/adlc/adlparse.cpp
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
Compiling /Users/zhoujunwen/Documents/workspace/jdk8u/hotspot/src/share/vm/adlc/archDesc.cpp
In file included from /Users/zhoujunwen/Documents/workspace/jdk8u/hotspot/src/share/vm/adlc/adlparse.cpp:27:
/Users/zhoujunwen/Documents/workspace/jdk8u/hotspot/src/share/vm/adlc/adlc.hpp:35:10: fatal error: 'iostream' file not found
#include
^~~~~~~~~~
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
In file included from /Users/zhoujunwen/Documents/workspace/jdk8u/hotspot/src/share/vm/adlc/archDesc.cpp:27:
/Users/zhoujunwen/Documents/workspace/jdk8u/hotspot/src/share/vm/adlc/adlc.hpp:35:10: fatal error: 'iostream' file not found
#include
^~~~~~~~~~
1 warning and 1 error generated.
1 warning and 1 error generated.
make[6]: *** [../generated/adfiles/adlparse.o] Error 1
make[6]: *** Waiting for unfinished jobs....
make[6]: *** [../generated/adfiles/archDesc.o] Error 1
make[5]: *** [ad_stuff] Error 2
make[4]: *** [debug] Error 2
make[3]: *** [generic_build2] Error 2
make[2]: *** [debug] Error 2
make[1]: *** [/Users/zhoujunwen/Documents/workspace/jdk8u/build/macosx-x86_64-normal-server-slowdebug/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2解決方法
更新最新的Xcode11,并且安裝xcode命令行工具:
# export LDFLAGS='-Xlinker -stdlib=libc++'
xcode-select --install問題5:clang: error: linker command failed with exit code 1 (use -v to see invocation)
錯(cuò)誤信息
## Starting hotspot
make[2]: warning: -jN forced in submake: disabling jobserver mode.
Warning: The jvmg target has been replaced with debug
Warning: Please update your usage
INFO: ENABLE_FULL_DEBUG_SYMBOLS=1
INFO: ENABLE_FULL_DEBUG_SYMBOLS=1
Making adlc
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]
ld: library not found for -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[6]: *** [../generated/adfiles/adlc] Error 1
make[5]: *** [ad_stuff] Error 2
make[4]: *** [debug] Error 2
make[3]: *** [generic_build2] Error 2
make[2]: *** [debug] Error 2
make[1]: *** [/Users/zhoujunwen/Documents/workspace/jdk8u/build/macosx-x86_64-normal-server-slowdebug/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2解決方法
原因:這個(gè)原因是Xcode升級(jí)到10以后就沒有包含lstdc++庫了。而 hotspot 居然還一直用著這個(gè),于是編譯器找不到 libstdc++ 的頭文件就罷工了
解決辦法:
打開?這個(gè)鏈接, clone 到本地,參考 install.sh 將文件鏈接或者復(fù)制到對(duì)應(yīng)位置(慎重直接執(zhí)行,請(qǐng)一定事先核對(duì)路徑是否正確)!
sudo mkdir Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++
sudo cp -R ../ios/xcode-missing-libstdc-/include/c++/* /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++
sudo cp ../ios/xcode-missing-libstdc-/lib/* /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/或者,將/usr/lib中的庫文件拷貝到Xcode相應(yīng)的文件夾中,并設(shè)置相關(guān)的軟連接:
sudo cp /usr/lib/libstdc++.6.0.9.dylib /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/
cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/
sudo ln -s libstdc++.6.0.9.dylib libstdc++.6.dylib
sudo ln -s libstdc++.6.dylib libstdc++.dylib問題6:configure: error: C compiler cannot create executables
See `config.log’ for more details
configure: Using default toolchain gcc (GNU Compiler Collection)
checking Determining if we need to set DEVELOPER_DIR... no
checking for xcodebuild... /usr/bin/xcodebuild
checking Determining Xcode SDK path... /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk
configure: Will use user supplied compiler CC=clang
checking for clang... /usr/bin/clang
checking resolved symbolic links for CC... no symlink
configure: Using gcc C compiler version Apple clang version 11.0.0 (clang-1100.0.33.8) Target: x86_64-apple-darwin19.0.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin [Apple clang version 11.0.0 (clang-1100.0.33.8) Target: x86_64-apple-darwin19.0.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin]
checking whether the C compiler works... no
configure: error: in `/Users/zhoujunwen/Documents/workspace/jdk8u':
configure: error: C compiler cannot create executables
See `config.log' for more details
configure exiting with result code 77查看config.log,發(fā)現(xiàn)如下錯(cuò)誤:
Apple clang version 11.0.0 (clang-1100.0.33.8)
Target: x86_64-apple-darwin19.0.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
configure:28126: $? = 0
configure:28115: /usr/bin/clang -V >&5
clang: error: unsupported option '-V -Wno-framework-include-private-from-public'
clang: error: no input files
configure:28126: $? = 1
configure:28115: /usr/bin/clang -qversion >&5
clang: error: unknown argument '-qversion', did you mean '--version'?
clang: error: no input files
configure:28126: $? = 1
configure:28146: checking whether the C compiler works
configure:28168: /usr/bin/clang -Xlinker -stdlib=libc++ conftest.c >&5
ld: unknown option: -stdlib=libc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
configure:28172: $? = 1
configure:28210: result: no
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME "OpenJDK"
| #define PACKAGE_TARNAME "openjdk"
| #define PACKAGE_VERSION "jdk8"
| #define PACKAGE_STRING "OpenJDK jdk8"
| #define PACKAGE_BUGREPORT "[email protected]"
| #define PACKAGE_URL "http://openjdk.java.net"
| /* end confdefs.h. */
|
| int
| main ()
| {
|
| ;
| return 0;
| }
configure:28215: error: in `/Users/zhoujunwen/Documents/workspace/jdk8u':
configure:28217: error: C compiler cannot create executables
See `config.log' for more details解決方法
安裝最新版本的Xcode11,同時(shí)xcode-select需要重新安裝:
xcode-select install然后,
sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/export CC=clang
export CXX=clang++
export CXXFLAGS=-stdlib=libc++問題7:Undefined symbols for architecture x86_64
錯(cuò)誤信息
## Starting hotspot
make[2]: warning: -jN forced in submake: disabling jobserver mode.
Warning: The jvmg target has been replaced with debug
Warning: Please update your usage
INFO: ENABLE_FULL_DEBUG_SYMBOLS=1
INFO: ENABLE_FULL_DEBUG_SYMBOLS=1
Making adlc
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]
Undefined symbols for architecture x86_64:
"std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
std::__1::num_put<char, std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > > const& std::__1::use_facet<std::__1::num_put<char, std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > > >(std::__1::locale const&) in filebuff.o
std::__1::ctype<char> const& std::__1::use_facet<std::__1::ctype<char> >(std::__1::locale const&) in filebuff.o
"std::__1::ios_base::getloc() const", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(int) in filebuff.o
std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in filebuff.o
"std::__1::ctype::id" , referenced from:
std::__1::ctype<char> const& std::__1::use_facet<std::__1::ctype<char> >(std::__1::locale const&) in filebuff.o
"std::__1::locale::~locale()", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >::operator<<(int) in filebuff.o
std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in filebuff.o
"std::__1::num_put > >::id" , referenced from:
std::__1::num_put<char, std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > > const& std::__1::use_facet<std::__1::num_put<char, std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > > >(std::__1::locale const&) in filebuff.o
"std::__1::ios_base::clear(unsigned int)", referenced from:
std::__1::ios_base::setstate(unsigned int) in filebuff.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[6]: *** [../generated/adfiles/adlc] Error 1
make[5]: *** [ad_stuff] Error 2
make[4]: *** [debug] Error 2
make[3]: *** [generic_build2] Error 2
make[2]: *** [debug] Error 2
make[1]: *** [/Users/zhoujunwen/Documents/workspace/jdk8u/build/macosx-x86_64-normal-server-slowdebug/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2解決方法
先設(shè)置環(huán)境變量,重新configure:
export CC=clang
export CXX=clang++ # 或者 export CXX=g++
export CXXFLAGS=-stdlib=libc++make階段遇到的問題問題1:invalid argument ‘-std=gnu++98’ not allowed with ‘C’
錯(cuò)誤信息
Making signal interposition lib...
error: invalid argument '-std=gnu++98' not allowed with 'C'
make[6]: *** [libjsig.dylib] Error 1
make[6]: *** Waiting for unfinished jobs....
make[5]: *** [the_vm] Error 2
make[4]: *** [debug] Error 2
make[3]: *** [generic_build2] Error 2
make[2]: *** [debug] Error 2
make[1]: *** [/Users/zhoujunwen/Documents/workspace/jdk8u/build/macosx-x86_64-normal-server-slowdebug/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2解決方法
在${Your_JDK_DIR}/common/autoconf/generated-configure.sh文件中找到參數(shù)“-std=gnu++98”,將代碼注釋如下:
if test "x$TOOLCHAIN_TYPE" = xsolstudio; then
if test "x$OPENJDK_TARGET_CPU_ARCH" = "xsparc"; then
CFLAGS_JDKLIB_EXTRA="${CFLAGS_JDKLIB_EXTRA} -xregs=no%appl"
CXXFLAGS_JDKLIB_EXTRA="${CXXFLAGS_JDKLIB_EXTRA} -xregs=no%appl"
fi
elif test "x$TOOLCHAIN_TYPE" = xxlc; then
LDFLAGS_JDK="${LDFLAGS_JDK} -q64 -brtl -bnolibpath -liconv -bexpall"
CFLAGS_JDK="${CFLAGS_JDK} -qchars=signed -q64 -qfullpath -qsaveopt"
CXXFLAGS_JDK="${CXXFLAGS_JDK} -qchars=signed -q64 -qfullpath -qsaveopt"
elif test "x$TOOLCHAIN_TYPE" = xgcc; then
LEGACY_EXTRA_CFLAGS="$LEGACY_EXTRA_CFLAGS -fstack-protector"
LEGACY_EXTRA_CXXFLAGS="$LEGACY_EXTRA_CXXFLAGS -fstack-protector"
if test "x$OPENJDK_TARGET_OS" != xmacosx; then
LDFLAGS_JDK="$LDFLAGS_JDK -Wl,-z,relro"
LEGACY_EXTRA_LDFLAGS="$LEGACY_EXTRA_LDFLAGS -Wl,-z,relro"
fi
# CXXSTD_CXXFLAG="-std=gnu++98" # 注釋掉這行之后重新運(yùn)行configure,再執(zhí)行make all。
問題2:A fatal error has been detected by the Java Runtime Environment
錯(cuò)誤信息
執(zhí)行命令:
build/macosx-x86_64-normal-server-slowdebug/jdk/bin/java -version報(bào)錯(cuò)信息如下:
openjdk version "1.8.0-internal-debug"
OpenJDK Runtime Environment (build 1.8.0-internal-debug-zhoujunen_2019_11_19_10_28-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00-debug, mixed mode)
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGILL (0x4) at pc=0x0000000110c3ae38, pid=84856, tid=0x0000000000002803
#
# JRE version: OpenJDK Runtime Environment (8.0) (build 1.8.0-internal-debug-zhoujunen_2019_11_19_10_28-b00)
# Java VM: OpenJDK 64-Bit Server VM (25.71-b00-debug mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# V [libjvm.dylib+0xa3ae38] PerfData::~PerfData()+0x8
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/zhoujunwen/Documents/workspace/jdk8u/hs_err_pid84856.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
[error occurred during error reporting , id 0x4]
[2] 84856 abort build/macosx-x86_64-normal-server-slowdebug/jdk/bin/java -version解決方法
根據(jù)這個(gè)錯(cuò)誤提示,我們可以知道錯(cuò)誤發(fā)生在動(dòng)態(tài)鏈接庫 libjvm.dylib中,根據(jù)提示,錯(cuò)誤代碼是在PerfData::~PerfData()函數(shù)中。因此我們需要對(duì)這個(gè)函數(shù)進(jìn)行修改,即文件 hotspot/src/share/vm/runtime/perfData.cpp,函數(shù)內(nèi)容如下:
PerfData::~PerfData() {
if (_name != NULL) {
FREE_C_HEAP_ARRAY(char, _name, mtInternal);
}
if (is_on_c_heap()) {
FREE_C_HEAP_ARRAY(PerfDataEntry, _pdep, mtInternal);
}
}發(fā)現(xiàn)并有其他東西,可以完全注釋掉整個(gè)函數(shù)體內(nèi)容,但在在這兩篇博客編譯openjdk遇到的問題和MacOS Mojave(10.14)編譯openjdk9?中找到的發(fā)現(xiàn)他們的解決思路是找到PerfDataManager::destroy()函數(shù),將代碼”delete p”注釋掉,如下:
void PerfDataManager::destroy() {
if (_all == NULL)
// destroy already called, or initialization never happened
return;
for (int index = 0; index < _all->length(); index++) {
PerfData* p = _all->at(index);
// delete p;
}
delete(_all);
delete(_sampled);
delete(_constants);
_all = NULL;
_sampled = NULL;
_constants = NULL;
}溫馨提示
如果你喜歡本文,請(qǐng)分享到朋友圈,想要獲得更多信息,請(qǐng)關(guān)注我,公眾號(hào)后續(xù)分享文章計(jì)劃,請(qǐng)繼續(xù)關(guān)注。
1.分布式鎖的實(shí)現(xiàn)
zookeeper原生API實(shí)現(xiàn)分布獨(dú)占鎖和讀寫鎖
zkClient實(shí)現(xiàn)分布式鎖
Curator實(shí)現(xiàn)分布式鎖
Redis實(shí)現(xiàn)分布式鎖
etcd實(shí)現(xiàn)分布式鎖
2.淺析jvm原理
手動(dòng)編譯JDK
深入JVM內(nèi)存模型及自動(dòng)管理機(jī)制
類文件結(jié)構(gòu)及類加載機(jī)制
虛擬機(jī)性能監(jiān)控與故障處理工具
深入Java GC與調(diào)優(yōu)
理解Java堆內(nèi)存和原始內(nèi)存核心概念
JVM調(diào)優(yōu)
字符串及字符串常量池
3.探究Java并發(fā)
Java內(nèi)置鎖
深入jvm內(nèi)存模型
字符串及字符串常量池
Java內(nèi)置鎖
偏向鎖
4.設(shè)計(jì)模式實(shí)踐
5.中間件源碼分析
Kafka源碼分析
dubbo源碼分析
RocketMQ源碼分析
高性能現(xiàn)成消息傳遞庫disruptor
閱讀原文
