Mac下編譯openjdk及調試

一、編譯環境

  1. MacOS High Sierra 10.13.2
  2. XCode 9.4.1(Command Line Tools)
  3. jdk版本:jdk7u71
  4. openjdk: jdk8u-dev

由於 openjdk 官方的源碼不是用 git 管理的,源代碼存放在 http://hg.openjdk.java.net/,首先需要安裝版本管理工具mercurial, mac 上直接使用brew 進行安裝即可.

brew install mercurial
hg clone http://hg.openjdk.java.net/jdk8u/jdk8u-dev/
cd jdk8u-dev
sh ./get_source.sh # 根據網絡環境的不同,這一步花費的時間不同,一般花費時間較長

  1. FreeType安裝

sudo ./configure
make
make install

  1. Ant1.9.13安裝

export ANT_HOME=HOME/Documents/Software/JVM/apacheant1.9.13exportPATH=HOME/Documents/Software/JVM/apache-ant-1.9.13 export PATH=PATH:ANTHOME/binexportPATH=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
  1. 執行配置文件校驗
bash ./configure

開始校驗環境配置,正常會遇到如下問題:
(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-boot-jdk=/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/

#
  1. 校驗完成後的大概提示如下
====================================================
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

  1. 開始編譯
 make all

(1) 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,make all.

(2).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
./hotspot/make/linux/makefiles/gcc.make
./hotspot/make/solaris/makefiles/gcc.make

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

三、調試

手頭上有編譯好的openjdk 1.8的版本,XCode 9.2,環境大同小異,應該值得借鑑吧。

  1. New -> Project -> Command Line Tool
    在這裏插入圖片描述

  2. 如圖,新建工程 OpenJDK在這裏插入圖片描述

  3. 先把新建工程的默認生成的文件全刪了,然後右擊OpenJDK,將編譯目錄的文件添加進來;
    在這裏插入圖片描述

  4. Product -> Scheme ->Edit Scheme
    在這裏插入圖片描述
    已編譯好了,先將Build下的Target刪掉;
    在這裏插入圖片描述
    設置好Run -> Info,如圖,Executable中選擇路徑位於/openjdk/jdk8u-dev/build/macosx-x86_64-normal-server-release/jdk/bin中的Java
    在這裏插入圖片描述
    設置java的執行參數,此處,我隨便選了一個編譯好的class文件來調試;

  5. 從main.c打斷點開始調試在這裏插入圖片描述

四、注意

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章