mac 10.13.x編譯openjdk8

candyleer發表於2019-02-23

由於 mac high sierra 系統有較大的變動,所以專門寫一篇用mac high sierra 來編譯 openjdk8 的部落格.

準備階段

  • mac 系統版本 10.13.3
  • 由於 openjdk 官方的原始碼不是用 git 管理的,原始碼存放在 http://hg.openjdk.java.net/,首先需要安裝版本管理工具mercurial, mac 上直接使用brew 進行安裝即可.
brew install mercurial
複製程式碼
  • 下載合適版本的原始碼,我用的編譯版本是jdk8u-dev. 克隆原始碼
hg clone http://hg.openjdk.java.net/jdk8u/jdk8u-dev/
複製程式碼

克隆完成後,使用指令碼獲取最新的原始碼

cd jdk8u-dev
sh ./get_source.sh # 根據網路環境的不同,這一步花費的時間不同,一般花費時間較長
複製程式碼

下載完成後的結構如下,已經包含了各個模組的程式碼

 ✘ ~/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
複製程式碼
  • 當然,還需要準備一些必須的工具,比如安裝最新版的 Xcode,有g++,gcc 的環境,安裝 freetype, 使用命令brew install freetype進行安裝
~/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
複製程式碼

編譯階段

  • 設定環境變數
# 設定語言選項,必須設定
export LANG=C
# Mac平臺,C編譯器不再是GCC,是clang
export CC=clang
# 跳過clang的一些嚴格的語法檢查,不然會將N多的警告作為Error
export COMPILER_WARNINGS_FATAL=false
# 連結時使用的引數
export LFLAGS='-Xlinker -lstdc++'
# 是否使用clang
export USE_CLANG=true
# 使用64位資料模型
export LP64=1
# 告訴編譯平臺是64位,不然會按32位來編譯
export ARCH_DATA_MODEL=64
# 允許自動下載依賴
export ALLOW_DOWNLOADS=true
# 並行編譯的執行緒數,編譯時間長,為了不影響其他工作,我選擇為2
export HOTSPOT_BUILD_JOBS=2
# 是否跳過與先前版本的比較
export SKIP_COMPARE_IMAGES=true
# 是否使用預編譯標頭檔案,加快編譯速度
export USE_PRECOMPILED_HEADER=true
# 是否使用增量編譯
export INCREMENTAL_BUILD=true
# 編譯內容
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
複製程式碼
  • 確保本地有現有的 java 環境,使用java -version 進行檢視
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.不出意外,接下來會有幾個錯誤.Xcode版本不符合 錯誤:

...
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
複製程式碼

繼續編譯. 2,接下面馬上又出現了另外一個錯誤, 找不到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,有兩處校驗的地方,找到並且註釋掉校驗的邏輯.

 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
複製程式碼

然後繼續編譯.

  • 配置成功.不出意外,應該就會配置成功,會出現如下資訊;
====================================================
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, 真正開始編譯 接下來又會出現一堆錯誤 1,指標和0比較錯誤
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, 儲存後,繼續編譯 2,不久的將來,可能會出現類似的錯誤

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,接下來我這裡又出現了invalid argument '-std=gnu++98' not allowed with 'C/ObjC' 的錯誤,仔細看錯誤,一臉懵,不知如何看起.

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)找到了相關的配置

  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/
複製程式碼
  • 哦了 其他錯誤暫時沒有發現

編譯完成

如果編譯完成,會顯示每一項編譯的進度,所消耗的時間資訊,大概需要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'
複製程式碼

##驗證

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)
複製程式碼

相關文章