mac 10.13.x編譯openjdk8

由于 mac high sierra 系統(tǒng)有較大的變動(dòng),所以專門寫一篇用mac high sierra 來編譯 openjdk8 的博客.

準(zhǔn)備階段

  • mac 系統(tǒng)版本 10.13.3
  • 由于 openjdk 官方的源碼不是用 git 管理的,源代碼存放在 http://hg.openjdk.java.net/,首先需要安裝版本管理工具mercurial, mac 上直接使用brew 進(jìn)行安裝即可.
brew install mercurial
  • 下載合適版本的源碼,我用的編譯版本是jdk8u-dev.
    克隆源代碼
hg clone http://hg.openjdk.java.net/jdk8u/jdk8u-dev/

克隆完成后,使用腳本獲取最新的源代碼

cd jdk8u-dev
sh ./get_source.sh # 根據(jù)網(wǎng)絡(luò)環(huán)境的不同,這一步花費(fèi)的時(shí)間不同,一般花費(fèi)時(shí)間較長(zhǎng)

下載完成后的結(jié)構(gòu)如下,已經(jīng)包含了各個(gè)模塊的代碼

 ? ~/hg/jdk8u-dev ll
total 632
-rw-r--r--   1 xx  staff   1.5K Nov 11 18:02 ASSEMBLY_EXCEPTION
-rw-r--r--   1 xx  staff    19K Nov 11 18:02 LICENSE
-rw-r--r--   1 xx  staff   6.1K Nov 11 18:02 Makefile
-rw-r--r--   1 xx  staff   1.5K Nov 11 18:02 README
-rw-r--r--   1 xx  staff   126K Nov 11 18:02 README-builds.html
-rw-r--r--   1 xx  staff   142K Nov 11 18:02 THIRD_PARTY_README
drwxr-xr-x   5 xx  staff   160B Nov 11 21:55 build
drwxr-xr-x   6 xx  staff   192B Nov 11 18:02 common
-rw-r--r--   1 xx  staff   1.2K Nov 11 18:02 configure
drwxr-xr-x  12 xx  staff   384B Nov 11 18:02 corba
-rw-r--r--   1 xx  staff   3.0K Nov 11 18:02 get_source.sh
drwxr-xr-x  17 xx  staff   544B Nov 11 21:51 hotspot
drwxr-xr-x  16 xx  staff   512B Nov 11 18:03 jaxp
drwxr-xr-x  16 xx  staff   512B Nov 11 18:03 jaxws
drwxr-xr-x  13 xx  staff   416B Nov 11 18:14 jdk
drwxr-xr-x  13 xx  staff   416B Nov 11 18:04 langtools
drwxr-xr-x  12 xx  staff   384B Nov 11 18:02 make
drwxr-xr-x  20 xx  staff   640B Nov 11 18:07 nashorn
drwxr-xr-x   4 xx  staff   128B Nov 11 18:02 test
  • 當(dāng)然,還需要準(zhǔn)備一些必須的工具,比如安裝最新版的 Xcode,有g++,gcc 的環(huán)境,安裝 freetype, 使用命令brew install freetype進(jìn)行安裝
~/hg/jdk8u-dev gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 ~/hg/jdk8u-dev g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

編譯階段

  • 設(shè)置環(huán)境變量
# 設(shè)定語言選項(xiàng),必須設(shè)置
export LANG=C
# Mac平臺(tái),C編譯器不再是GCC帚屉,是clang
export CC=clang
# 跳過clang的一些嚴(yán)格的語法檢查檐什,不然會(huì)將N多的警告作為Error
export COMPILER_WARNINGS_FATAL=false
# 鏈接時(shí)使用的參數(shù)
export LFLAGS='-Xlinker -lstdc++'
# 是否使用clang
export USE_CLANG=true
# 使用64位數(shù)據(jù)模型
export LP64=1
# 告訴編譯平臺(tái)是64位,不然會(huì)按32位來編譯
export ARCH_DATA_MODEL=64
# 允許自動(dòng)下載依賴
export ALLOW_DOWNLOADS=true
# 并行編譯的線程數(shù)深夯,編譯時(shí)間長(zhǎng)抖格,為了不影響其他工作,我選擇為2
export HOTSPOT_BUILD_JOBS=2
# 是否跳過與先前版本的比較
export SKIP_COMPARE_IMAGES=true
# 是否使用預(yù)編譯頭文件咕晋,加快編譯速度
export USE_PRECOMPILED_HEADER=true
# 是否使用增量編譯
export INCREMENTAL_BUILD=true
# 編譯內(nèi)容
export BUILD_LANGTOOLS=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_BUILD=false
export DEBUG_NAME=debug
# 避開javaws和瀏覽器Java插件之類的部分的build
export BUILD_DEPLOY=false
export BUILD_INSTALL=false
unset JAVA_HOME
  • 確保本地有現(xiàn)有的 java 環(huán)境,使用java -version 進(jìn)行查看
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
  • 開始configure, 我這里用到的 configure 命令為如下,防止找不到 freetype的路徑
bash ./configure  --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/

1.不出意外,接下來會(huì)有幾個(gè)錯(cuò)誤.Xcode版本不符合
錯(cuò)誤:

...
checking flags for boot jdk java command ...
checking flags for boot jdk java command for big workloads...  -Xms64M -Xmx1600M -XX:ThreadStackSize=1536 -XX:PermSize=32m -XX:MaxPermSize=160m
checking flags for boot jdk java command for small workloads...  -XX:+UseSerialGC -Xms32M -Xmx512M
checking for jtreg... no
checking Determining if we need to set DEVELOPER_DIR... no
checking for xcodebuild... /usr/bin/xcodebuild
configure: error: Xcode 4 is required to build JDK 8, the version found was 9.2. 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 文件并打開vim 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

繼續(xù)編譯.
2,接下面馬上又出現(xiàn)了另外一個(gè)錯(cuò)誤, 找不到gcc 編譯器,

...
checking for xcodebuild... /usr/bin/xcodebuild
checking Determining Xcode SDK path... /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk
checking for clang... /usr/local/opt/llvm@4/bin/clang
configure: Resolving CC (as /usr/local/opt/llvm@4/bin/clang) failed, using /usr/local/opt/llvm@4/bin/clang directly.
checking resolved symbolic links for CC... /usr/local/Cellar/llvm@4/4.0.1/bin/clang-4.0
checking if CC is disguised ccache... no, keeping CC
configure: The C compiler (located as /usr/local/Cellar/llvm@4/4.0.1/bin/clang-4.0) does not seem to be the required GCC compiler.
configure: The result from running with --version was: "clang version 4.0.1 (tags/RELEASE_401/final)"
configure: error: GCC compiler is required. Try setting --with-tools-dir.
configure exiting with result code 1

解決方案:
還是找到剛剛的文件common/autoconf/generated-configure.sh,有兩處校驗(yàn)的地方,找到并且注釋掉校驗(yàn)的邏輯.

 if test $? -ne 0; then
      { $as_echo "$as_me:${as_lineno-$LINENO}: The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required GCC compiler." >&5
$as_echo "$as_me: The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required GCC compiler." >&6;}
      { $as_echo "$as_me:${as_lineno-$LINENO}: The result from running with --version was: \"$COMPILER_VERSION_TEST\"" >&5
$as_echo "$as_me: The result from running with --version was: \"$COMPILER_VERSION_TEST\"" >&6;}
      as_fn_error $? "GCC compiler is required. Try setting --with-tools-dir." "$LINENO" 5
    fi

然后繼續(xù)編譯.

  • 配置成功.不出意外,應(yīng)該就會(huì)配置成功,會(huì)出現(xiàn)如下信息;
====================================================
A new configuration has been successfully created in
/Users/lican/hg_bak/jdk8u-dev/build/macosx-x86_64-normal-server-release
using configure arguments '--with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/'.

Configuration summary:
* Debug level:    release
* JDK variant:    normal
* JVM variants:   server
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64

Tools summary:
* Boot JDK:       java version "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)  (at /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home)
* C Compiler:     clang version (tags/RELEASE_401/final) version 4.0.1 (at /usr/local/Cellar/llvm@4/4.0.1/bin/clang-4.0)
* C++ Compiler:    version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 (at /usr/bin/g++)

Build performance summary:
* Cores to use:   2
* Memory limit:   7883 MB
* ccache status:  not installed (consider installing)

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, 真正開始編譯
    接下來又會(huì)出現(xiàn)一堆錯(cuò)誤
    1,指針和0比較錯(cuò)誤
Compiling /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/opto/library_call.cpp
/Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/opto/library_call.cpp:3829:20: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
                   <<    Klass::_lh_array_tag_shift)
                   ^
/Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/opto/lcm.cpp:52:34: error: ordered comparison between pointer and zero ('address' (aka 'unsigned char *') and 'int')
  if (Universe::narrow_oop_base()>0) { // Implies UseCompressedOops.
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
1 error generated.
make[6]: *** [lcm.o] Error 1
make[6]: *** Waiting for unfinished jobs....
1 warning generated.
make[5]: *** [the_vm] Error 2
make[4]: *** [product] Error 2
make[3]: *** [generic_build2] Error 2
make[2]: *** [product] Error 2
make[1]: *** [/Users/lican/hg_bak/jdk8u-dev/build/macosx-x86_64-normal-server-release/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2

解決方法:將比較的地方換為Universe::narrow_oop_base() != NULL, 保存后,繼續(xù)編譯
2,不久的將來,可能會(huì)出現(xiàn)類似的錯(cuò)誤

Compiling /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/services/virtualMemoryTracker.cpp
Compiling /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/runtime/virtualspace.cpp
/Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/runtime/virtualspace.cpp:345:13: error: ordered comparison between pointer and zero ('char *' and 'int')
  if (base()>0) {
      ~~~~~~^~
1 error generated.
make[6]: *** [virtualspace.o] Error 1
make[6]: *** Waiting for unfinished jobs....
make[5]: *** [the_vm] Error 2
make[4]: *** [product] Error 2
make[3]: *** [generic_build2] Error 2
make[2]: *** [product] Error 2
make[1]: *** [/Users/lican/hg_bak/jdk8u-dev/build/macosx-x86_64-normal-server-release/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2

解決方法和上面類似,替換為if (base()!=NULL),保存,重新 make all
3,接下來我這里又出現(xiàn)了invalid argument '-std=gnu++98' not allowed with 'C/ObjC' 的錯(cuò)誤,仔細(xì)看錯(cuò)誤,一臉懵,不知如何看起.

Compiling /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/utilities/xmlstream.cpp
Compiling /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/utilities/yieldingWorkgroup.cpp
Making signal interposition lib...
Making SA debugger back-end...
error: invalid argument '-std=gnu++98' not allowed with 'C/ObjC'
make[6]: *** [libsaproc.dylib] Error 1
make[6]: *** Waiting for unfinished jobs....
warning: (x86_64) /var/folders/s3/bzyrc_3d56gfl4w34ccx62_00000gn/T/jsig-d50157.o unable to open object file: No such file or directory
warning: no debug symbols in executable (-arch x86_64)
make[5]: *** [the_vm] Error 2
make[4]: *** [product] Error 2
make[3]: *** [generic_build2] Error 2
make[2]: *** [product] Error 2
make[1]: *** [/Users/lican/hg_bak/jdk8u-dev/build/macosx-x86_64-normal-server-release/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2

解決方法:連蒙帶猜,在最開始我們使用使用 configure 的文件中(common/autoconf/generated-configure.sh)找到了相關(guān)的配置

  case $COMPILER_NAME in
    gcc )
      COMMON_CCXXFLAGS_JDK="$COMMON_CCXXFLAGS $COMMON_CCXXFLAGS_JDK -W -Wall -Wno-unused -Wno-parentheses \
      -pipe \
      -D_GNU_SOURCE -D_REENTRANT -D_LARGEFILE64_SOURCE"
      CXXSTD_CXXFLAG="-std=gnu++98"

解決方法:注釋掉CXXSTD_CXXFLAG="-std=gnu++98"這一行,然后make clean,重新開始 configure, 即

bash ./configure  --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/
  • 哦了 其他錯(cuò)誤暫時(shí)沒有發(fā)現(xiàn)

編譯完成

如果編譯完成,會(huì)顯示每一項(xiàng)編譯的進(jìn)度,所消耗的時(shí)間信息,大概需要10多分鐘

----- Build times -------
Start 2018-03-01 15:28:52
End   2018-03-01 15:45:10
00:00:47 corba
00:01:10 demos
00:04:16 docs
00:00:29 hotspot
00:02:12 images
00:00:25 jaxp
00:00:38 jaxws
00:05:55 jdk
00:00:00 langtools
00:00:25 nashorn
00:16:18 TOTAL
-------------------------
Finished building OpenJDK for target 'all'

驗(yàn)證

cd build/macosx-x86_64-normal-server-release/jdk/bin && ./java -version

openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-lican_2018_03_01_15_28-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00, mixed mode)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末雹拄,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子掌呜,更是在濱河造成了極大的恐慌滓玖,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評(píng)論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件质蕉,死亡現(xiàn)場(chǎng)離奇詭異势篡,居然都是意外死亡翩肌,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門禁悠,熙熙樓的掌柜王于貴愁眉苦臉地迎上來念祭,“玉大人,你說我怎么就攤上這事碍侦“艟恚” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵祝钢,是天一觀的道長(zhǎng)比规。 經(jīng)常有香客問我,道長(zhǎng)拦英,這世上最難降的妖魔是什么蜒什? 我笑而不...
    開封第一講書人閱讀 58,157評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮疤估,結(jié)果婚禮上灾常,老公的妹妹穿的比我還像新娘。我一直安慰自己铃拇,他們只是感情好钞瀑,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,171評(píng)論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著慷荔,像睡著了一般雕什。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上显晶,一...
    開封第一講書人閱讀 51,125評(píng)論 1 297
  • 那天贷岸,我揣著相機(jī)與錄音,去河邊找鬼磷雇。 笑死偿警,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的唯笙。 我是一名探鬼主播螟蒸,決...
    沈念sama閱讀 40,028評(píng)論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼崩掘!你這毒婦竟也來了七嫌?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤呢堰,失蹤者是張志新(化名)和其女友劉穎抄瑟,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡皮假,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,533評(píng)論 2 332
  • 正文 我和宋清朗相戀三年鞋拟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片惹资。...
    茶點(diǎn)故事閱讀 39,690評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡贺纲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出褪测,到底是詐尸還是另有隱情猴誊,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評(píng)論 5 343
  • 正文 年R本政府宣布侮措,位于F島的核電站懈叹,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏分扎。R本人自食惡果不足惜澄成,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,004評(píng)論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望畏吓。 院中可真熱鬧墨状,春花似錦、人聲如沸菲饼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宏悦。三九已至镐确,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肛根,已是汗流浹背辫塌。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評(píng)論 1 268
  • 我被黑心中介騙來泰國打工漏策, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留派哲,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,693評(píng)論 2 368
  • 正文 我出身青樓掺喻,卻偏偏與公主長(zhǎng)得像芭届,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子感耙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,577評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容