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安装

  • 可直接下载安装XQuartz
  • 或者下载:https://mac.softpedia.com/get/Development/Libraries/FreeType.shtml,解压后通过如下命令安装:
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'

请参考《三、问题解决》来解决。

其他问题请参考如下博文:

  • mac 10.13.x编译openjdk8
  • Mac os下编译openJDK 7

三、问题解决

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

 

 

 

 

 

你可能感兴趣的:(openjdk,JVM)