1. 程式人生 > >Mac 編譯 openjdk

Mac 編譯 openjdk

一、編譯環境

1. MacOS High Sierra 10.13.2

2. XCode 9.4.1(Command Line Tools)

3. jdk版本:jdk7u71

4. openjdk: jdk8u-dev

  • 或者通過mercurial工具下載

由於 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 # 根據網路環境的不同,這一步花費的時間不同,一般花費時間較長

5. FreeType安裝

sudo ./configure 

make

make install

6. Ant1.9.13安裝

(1)下載

(2)解壓到指定目錄,並配置環境變數

export ANT_HOME=$HOME/Documents/Software/JVM/apache-ant-1.9.13
export PATH=$PATH:$ANT_HOME/bin
export PATH=$PATH:$ANT_HOME/lib

二、編譯

1. 開啟terminal定位到jdk8u-dev根目錄

2. 輸入如下命令

#語言選項,必須設定
export LANG=C
export CC=clang #mac平臺,c編譯器不再是GCC,而是clang

export COMPILER_WARNINGS_FATAL=false #跳過clang一些嚴格的語法檢查

#允許自動下載依賴
export ALLOW_DOWNLOADS=true

export LFLAGS='-Xlinker -lc++ -lstdc++'

#並行編譯的執行緒數,設定為和cpu核心數量一致即可
export HOTSPOT_BUILD_JOBS=4
export ALT_PARALLEL_COMPILE_JOBS=4

export SKIP_COMPARE_IMAGES=true

export USE_PRECOMPILED_HEADER=true

#要編譯的內容
export BUILD_LANGTOOLS=true
#export BUILD_JAXP=false
#export BUILD_JAXWS=false
#export BUILD_CORBA=false
export BUILD_HOTSPOT=true
export BUILD_JDK=true

BUILD_DEPLOY=false

BUILD_INSTALL=false

unset JAVA_HOME
unset CLASSPATH

3. 執行配置檔案校驗

bash ./configure

開始校驗環境配置,正常會遇到如下問題:

(1)error: Xcode 4 is required to build JDK 8

(2)error: A gcc compiler is required

(3)error: Could not find freetype

請分別參考如下《三、問題解決》來解決。

4. 校驗完成後的大概提示如下

====================================================
A new configuration has been successfully created in
/Users/davi/Documents/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_71" Java(TM) SE Runtime Environment (build 1.7.0_71-b14) Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)  (at /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home)
* Toolchain:      gcc (GNU Compiler Collection)
* C Compiler:     Version 9.1.0 (at /usr/bin/clang)
* C++ Compiler:   Version 9.1.0 (at /usr/bin/g++)

Build performance summary:
* Cores to use:   4
* Memory limit:   16384 MB

5. 開始編譯

make all

編譯時需要耐心等待,若出現如下提示,表示已成功編譯

----- Build times -------
Start 2018-09-23 11:34:32
End   2018-09-23 11:43:34
00:00:20 corba
00:00:25 demos
00:01:39 docs
00:02:06 hotspot
00:00:51 images
00:00:13 jaxp
00:00:22 jaxws
00:02:24 jdk
00:00:29 langtools
00:00:12 nashorn
00:09:02 TOTAL
-------------------------
Finished building OpenJDK for target 'all'

編譯過程中可能會出現的問題:

error: invalid argument '-std=gnu++98' not allowed with 'C'

請參考《三、問題解決》來解決。

其他問題請參考如下博文:

三、問題解決

1. error: Xcode 4 is required to build JDK 8

error: Xcode 4 is required to build JDK 8, the version found was . Use --with-xcode-path to specify the location of Xcode 4 or make Xcode 4 active by using xcode-select.

解決辦法:

找到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. error: A gcc compiler is required(找不到gcc 編譯器)

configure: error: A gcc compiler is required. Try setting --with-tools-dir.
configure exiting with result code 1

解決辦法:

找到configure 檔案並開啟vim 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 $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 it was: \"$COMPILER_VERSION_OUTPUT\"" >&5
$as_echo "$as_me: The result from running it was: \"$COMPILER_VERSION_OUTPUT\"" >&6;}
      as_fn_error $? "A $TOOLCHAIN_TYPE compiler is required. Try setting --with-tools-dir." "$LINENO" 5
    fi

3. error: Could not find freetype

configure: error: Could not find freetype!  
configure exiting with result code 1

解決辦法:

bash ./configure  --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/

4. error: invalid argument '-std=gnu++98' not allowed with 'C'

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]: *** [product] Error 2
make[3]: *** [generic_build2] Error 2
make[2]: *** [product] Error 2
make[1]: *** [/Users/davi/Documents/jdk8u-dev/build/macosx-x86_64-normal-server-release/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2

解決辦法:

找到configure 檔案並開啟vim common/autoconf/generated-configure.sh,找到如下程式碼並註釋掉

CXXSTD_CXXFLAG="-std=gnu++98"

然後make clean,重新執行 configure, 即

bash ./configure  --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/

然後重新make all

5. clang: error: unknown argument: '-fpch-deps'

解決方法:

(1)首先查詢對應的配置檔案

find . -type f ! -name "*.java" | xargs grep -r "\-fpch\-deps"

1.1 匹配的查詢結果如下(Mac來源於BSD,選擇BSD)

./hotspot/make/bsd/makefiles/gcc.make:DEPFLAGS = -fpch-deps -MMD -MP -MF $(DEP_DIR)/$(@:%=%.d) 
./hotspot/make/linux/makefiles/gcc.make:DEPFLAGS = -fpch-deps -MMD -MP -MF $(DEP_DIR)/$(@:%=%.d) 
./hotspot/make/solaris/makefiles/gcc.make:DEPFLAGS = -fpch-deps -MMD -MP -MF $(DEP_DIR)/$(@:%=%.d) 

2.2 修改hotspot/make/bsd/makefiles/gcc.make

註釋216-218行

# Flags for generating make dependency flags.
# ifneq ("${CC_VER_MAJOR}", "2") 
# DEPFLAGS = -fpch-deps -MMD -MP -MF $(DEP_DIR)/$(@:%=%.d) 
# endif

6. Symbol not found: __cg_jpeg_resync_to_restart

Using java runtime at: /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre
Error occurred during initialization of VM
Unable to load native library: dlopen(/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/libjava.dylib, 1): Symbol not found: __cg_jpeg_resync_to_restart
  Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib
  Expected in: /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/libJPEG.dylib
 in /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib

解決辦法:

sudo ln -sf libTIFF.dylib /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/libJPEG.dylib

sudo ln -sf libPng.dylib /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/libPng.dylib

sudo ln -sf libTIFF.dylib /Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home/jre/lib/libTIFF.dylib