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打断点开始调试在这里插入图片描述

四、注意

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