第一章 Mac os下编译openJDK 7

本人和书上的作者一样,编译的是openjdk 7. 过程中也是遇到了不少坑,查阅了很多资料,尝试了使用jdk8,jdk7,jdk6,最终使用jdk7u71编译成功,下面纪录一下流程,以便他人能够借鉴。

一、准备工作:

1.安装Xcode,直接在App Store上下载即可,还有命令行工具


2.下载ant,Mac会自带ant,在/usr/local/Cellar/ant/1.9.7,如果没有就去官网下一个,我是自己下了一个。然后,因为jdk/src/macosx/native/jobjc/JObjC.xcodeproj/project.pbxproj和jdk/make/java/jobjc/Makefile文件中ant路径写死为/usr/bin/ant,所以要在usr/bin下面做一个软连接,被坑了好久:

ln -s /Users/jiangchao08/Downloads/apache-ant-1.9.7/bin/ant /usr/bin/ant


3.下载openjdk 7的zip文件,然后解压缩,地址:https://jdk7.java.net/source.html


4.下载oracle jdk 7,我下载的是7u71 版本,地址:http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html


5.安装XQuartz


6.不要设置JAVA_HOME、CLASSPATH环境变量,可以在最后一行用unset JAVA_HOME、unset CLASSPATH来注释掉


二、环境变量设置:

下面是我的环境变量的配置,在~/.bash_profile文件里配置;

export M2_HOME=/Users/jiangchao08/Downloads/apache-maven-3.3.9
export ANT_HOME=/Users/jiangchao08/Downloads/apache-ant-1.9.7
export PATH=$PATH:$M2_HOME/bin:$ANT_HOME/bin:/usr/bin
export ALT_BOOTDIR=/Library/Java/JavaVirtualMachines/jdk1.7.0_71.jdk/Contents/Home
export LANG=C
export COMPILER_WARNINGS_FATAL=false
export CC=clang
export COMPILER_WARNINGS_FATAL=false
export USE_CLANG=true
export LP64=1
export BUILD_DEPLOY=false
export ARCH_DATA_MODEL=64
export LFLAGS='-Xlinker -lc++ -lstdc++'
export HOTSPOT_BUILD_JOBS=8
export USE_PRECOMPILED_HEADER=true
export SHOW_ALL_WARNINGS=false
export INCREMENTAL_BUILD=true
export SKIP_DEBUG_BUILD=false
export SKIP_FASTDEBUG_BUILD=true
unset JAVA_HOME

在正式编译之前,先make sanity一下,如何成功,再正式编译在openjdk路径下使用命令: make debug_build


三、遇到的问题:

因为我尝试了很多版本的jdk,在这里我将所有出现的bug都纪录下来,如果你编译的是openjdk7,那么非常不建议使用oracle jdk8 ,因为最后会出现很多的类找不到,或者方法找不到,毕竟openjdk7已经好多年了。所以推荐使用oracle jdk6和oracle jdk7来进行编译openjdk 7:

1.ERROR: The Compiler version is undefined.

 ERROR: FreeType version  2.3.0  or higher is required.

(1)第一个error,是因为现在Xcode里面没有自带gcc,所以进行链接:

sudo ln -s gcc /Applications/Xcode.app/Contents/Developer/usr/bin/llvm-gcc

(2)第二个error,是因为FreeType版本太低,所以我是直接安装了XQuartz


2.  BUILD FAILED

/home/xlf/jdk7u-dev/langtools/make/build.xml:452: The following error occurred while executing this line:
/home/xlf/jdk7u-dev/langtools/make/build.xml:795: Compile failed; see the compiler error output for details.
 
解决方法:将oracle jdk 8版本换成jdk 7解决

3.乱码问题:

# 具体报错
openjdk/build/../build-debug/corba/gensrc/org/omg/PortableServer/AdapterActivatorOperations.java:8: ´íÎó: ±àÂëasciiµÄ
    ²»¿ÉÓ³Éä×Ö·û
* 2016??7??12?? ?????? ????05??39??22?? CST

# 解决办法
find build-debug/corba/gensrc/org/ -name '*.java' | while read p; do native2ascii -encoding UTF-8 $p > tmpj; mv tmpj $p; done
export _JAVA_OPTIONS=-Dfile.encoding=ASCII
4.clang不支持参数-fpch-deps

# 具体报错
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 修改hotspot/make/bsd/makefiles/gcc.make
2.1 注释216-218
# Flags for generating make dependency flags.
# ifneq ("${CC_VER_MAJOR}", "2")
# DEPFLAGS = -fpch-deps -MMD -MP -MF $(DEP_DIR)/$(@:%=%.d)
# endif
2.2 218行下添加下面代码
DEPFLAGS = -MMD -MP -MF $(DEP_DIR)/$(@:%=%.d)
ifeq ($(USE_CLANG),)
  ifneq ($(CC_VER_MAJOR), 2)
    DEPFLAGS += -fpch-deps
  endif
endif
5.形参默认值问题

# 具体报错
openjdk/hotspot/src/share/vm/code/relocInfo.hpp:374:27: error: friend declaration specifying a default argument must
      be a definition
inline friend relocInfo prefix_relocInfo(int datalen = 0);

openjdk/hotspot/src/share/vm/code/relocInfo.hpp:469:18: error: friend declaration specifying a default argument must
      be the only declaration
inline relocInfo prefix_relocInfo(int datalen) {

openjdk/hotspot/src/share/vm/code/relocInfo.hpp:470:21: error: 'fits_into_immediate' is a protected member of 'reloc
     Info'
assert(relocInfo::fits_into_immediate(datalen), "datalen in limits");

openjdk/hotspot/src/share/vm/code/relocInfo.hpp:471:59: error: 'RAW_BITS' is a protected member of 'relocInfo'
return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);

openjdk/hotspot/src/share/vm/code/relocInfo.hpp:471:10: error: calling a protected constructor of class 'relocInfo'
return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);

# 解决办法
# 修改relocInfo.hpp(路径:hotspot/src/share/vm/code/relocInfo.hpp)
修改374
inline friend relocInfo prefix_relocInfo(int datalen);
修改469
inline relocInfo prefix_relocInfo(int datalen = 0) {
   assert(relocInfo::fits_into_immediate(datalen), "datalen in limits");
   return relocInfo(relocInfo::data_prefix_tag, relocInfo::RAW_BITS, relocInfo::datalen_tag | datalen);
}


6.十年问题

# 具体报错
Error: time is more than 10 years from present: 1136059200000
java.lang.RuntimeException: time is more than 10 years from present: 1136059200000
  at build.tools.generatecurrencydata.GenerateCurrencyData.makeSpecialCaseEntry(GenerateCurrencyData.java:285)
  at build.tools.generatecurrencydata.GenerateCurrencyData.buildMainAndSpecialCaseTables(GenerateCurrencyData.java:225)
  at build.tools.generatecurrencydata.GenerateCurrencyData.main(GenerateCurrencyData.java:154)

# 解决办法
# 修改CurrencyData.properties(路径:jdk/src/share/classes/java/util/CurrencyData.properties)
修改108
AZ=AZM;2009-12-31-20-00-00;AZN
修改381
MZ=MZM;2009-06-30-22-00-00;MZN
修改443
RO=ROL;2009-06-30-21-00-00;RON
修改535
TR=TRL;2009-12-31-22-00-00;TRY
修改561
VE=VEB;2009-01-01-04-00-00;VEF
7.非空函数不返回值的问题一

# 具体报错
../../../src/solaris/native/java/net/net_util_md.c:117:9: error: non-void function 'getDefaultScopeID' should return a value [-Wreturn-type]
        CHECK_NULL(c);
        ^
../../../src/share/native/java/net/net_util.h:45:40: note: expanded from macro 'CHECK_NULL'
#define CHECK_NULL(x) if ((x) == NULL) return;
                                       ^
../../../src/solaris/native/java/net/net_util_md.c:119:9: error: non-void function 'getDefaultScopeID' should return a value [-Wreturn-type]
        CHECK_NULL(c);
        ^
../../../src/share/native/java/net/net_util.h:45:40: note: expanded from macro 'CHECK_NULL'
#define CHECK_NULL(x) if ((x) == NULL) return;

# 解决办法
# 修改net_util_md.c(路径:jdk/src/solaris/native/java/net/net_util_md.c)
修改117119
CHECK_NULL_RETURN(c , 0);
8.权限问题

# 具体报错
Permission denied - ./src/core/PrimitiveCoder.hs (Errno::EACCES)

# 解决办法
chmod 755 jdk/src/macosx/native/jobjc/src/core/PrimitiveCoder.hs
9.clang不支持GC的问题

# 具体报错
clang: error: garbage collection is no longer supported

# 解决办法
1. 首先查找相应的配置文件
find . -type f ! -name "*.log" | xargs grep -r "GCC_ENABLE_OBJC_GC"
1.1 匹配的查找结果如下
nary file ./build-debug/JObjC.dst/dgph matches
./jdk/src/macosx/native/jobjc/JObjC.xcodeproj/project.pbxproj:                                GCC_ENABLE_OBJC_GC = supported;
./jdk/src/macosx/native/jobjc/JObjC.xcodeproj/project.pbxproj:                                GCC_ENABLE_OBJC_GC = supported;

2. 修改project.pbxproj(路径:./jdk/src/macosx/native/jobjc/JObjC.xcodeproj/project.pbxproj
修改705713
GCC_ENABLE_OBJC_GC = "";


10.返回值大于返回类型精度的问题

# 具体报错
openjdk/build-debug/JObjC.build/src/jobjc/com/apple/jobjc/appkit/AppKitFramework.java:355: ?????
     public final float NSEventDurationForever(){ return 1.797693134862316E+308f; }

# 解决办法
1. 修改bridgesupport.gmk(路径:jdk/src/macosx/native/jobjc/bridgesupport.gmk
修改54
all:

2. 修改AppKitFull.bridgesupport(路径:build-debug/stable_bridge_metadata/AppKitFull.bridgesupport
修改1485
<enum name='NSEventDurationForever' value='3.40282E+38'/>

3. 修改AppKitFramework.java(路径:build-debug/JObjC.build/src/jobjc/com/apple/jobjc/appkit/AppKitFramework.java
修改355
public final double NSEventDurationForever(){ return 3.40282E+38d; }


11.非空函数不返回值的问题二

# 具体报错
openjdk/jdk/src/macosx/native/sun/awt/AWTEvent.m:385:24: error: non-void function 'NsGetDeadKeyChar' should return a value [-Wreturn-type]
    if (uchr == nil) { return; }

# 解决办法
# 修改AWTEvent.m(路径:jdk/src/macosx/native/sun/awt/AWTEvent.m)
修改385
if (uchr == nil) { return 0; }


12.严格检查导致的错误

类似这几种:

equality comparison with extraneous parentheses

      [-Werror,-Wparentheses-equality]


'this' pointer cannot be null in well-defined C++ code; pointer may be

      assumed to always convert to true [-Werror,-Wundefined-bool-conversion]


'&&' within '||' [-Werror,-Wlogical-op-parentheses]

因为一开始没有在环境变量面去设置,解决方法:在环境变量里面加上:
export COMPILER_WARNINGS_FATAL=false


13.test_gamma报错:

没找到原因,个人认为由于我原来的JDK为oracle JDK7,而编译出的为OpenJDK7,所以导致了这个问题;
我采用的做法是注释build/macosx-x86_64-debug/hotspot/outputdir/bsd_amd64_compiler2/jvmg/test_gamma中的测试代码,在最后一行

#./${GAMMA_PROG} -Xbatch -showversion Queens < /dev/null


14.找不到相应的类库和方法

Error occurred during initialization of VM

Unable to load native library: dlopen(/Library/Java/JavaVirtualMachines/jdk1.8.0_111.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.8.0_111.jdk/Contents/Home/jre/lib/libJPEG.dylib

 in /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libTIFF.dylib



Exception in thread "main" java.lang.NoSuchMethodError: sun.util.calendar.ZoneInfoFile.getFileName(Ljava/lang/String;)Ljava/lang/String;

at build.tools.javazic.Gen.processZoneinfo(Gen.java:60)

at build.tools.javazic.Main.compile(Main.java:120)

at build.tools.javazic.Main.main(Main.java:151)


解决方法:将oracle jdk 8换成oracle jdk 7


15.还有一个bug是类似提示bash/sh: ant : /usr/bin/ant no found,记不清了

解决方法:回到最上面看,就是准备工作步骤中的2,在/usr/bin/下面做一个ant的软连接


16.奇怪的错误

openjdk/jdk/src/macosx/native/jobjc/build.xml:187: exec returned: 1

解决方法:将oracle jdk6换成了oracle jdk7


最后成功用oracle jdk7u71版本编译了openjdk 7,一路艰辛啊~~~,附编译成功的截图:



参考文章:

Mac编译OpenJDK7(8)和Eclipse调试Hotspot》http://blog.csdn.net/csujiangyu/article/details/52045443#reply

os x环境编译jdk7》http://www.jianshu.com/p/a7ac81d38bb1

  oracle jdk 7的下载地址:http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html

没有更多推荐了,返回首页