【openJDK系列1】macos编译openJDK 8

左心房为你撑大大i 提交于 2020-11-24 16:00:52

背景

因为之前一直在研究jdk源码,但是基于自己比较懒的缘故,一直没有手动编译过jdk源码,也就只是看看书本上的东西,似懂非懂的看懂了也就草草了事了,这次终于下定决心编译一把openJDK8的源码

基础环境

macos High Sierra 10.13.1
openJDK 8

编译

直接找到 http://hg.openjdk.java.net/ ,执行以下命令

# 安装mercurial
brew install mercurial
# clone openJDK源码
hg clone http://hg.openjdk.java.net/jdk8/jdk8 openjdk8

当然在git JDK也有对应的JDK源码,git clone之后checkout到对应的tag上进行编译也是可以的

git clone https://github.com/openjdk/jdk
git checkout -b openjdk8  jdk8-b80

注意如果是用hg clone的方式我们还得执行sh ./get_source.sh命令来获取额外的文件,对于Mercurial和Git的区别,网上一大堆,网友可以自行查找,操作方式可以参考hg git 操作

我们这里选择hg clone的方式,执行

cd openjdk8 && sh ./get_source.sh

安装依赖

brew install freetype

Running Configure

bash ./configure --with-debug-level=slowdebug  --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home --with-freetype-include=/usr/local/Cellar/freetype/2.10.4/include/freetype2 --with-freetype-lib=/usr/local/Cellar/freetype/2.10.4/lib --with-target-bits=64 --with-jvm-variants=server --with-jdk-variant=normal --with-milestone=internal  --with-num-cores=2 --with-jobs=4 CC=clang CXX=clang++

其中, 名词|说明 ---|--- --with-boot-jdk | 用于bootstrap build,指向本地已有的jdk --with-freetype-include |freetype include文件目录 --with-freetype-lib |freetype lib目录 --with-target-bits|编译成64还是32位jdk --with-jvm-variants|jvm 的模式如server,client,kernel等,多个用逗号分开 --with-jdk-variant|jdk的模式,会在build目录下生成的文件下有体现 --with-milestone|build的里程碑,也就是阶段,会在build目录下生成的文件下有体现 --with-num-cores|用于此次build的核数 --with-jobs|build的并发任务数 CC|C 编译器命令,如:clang,gcc CXX|C++编译器命令,如:clang++,g++

关于clang和gcc的区别,参考clang和gcc的区别

make编译 执行如下命令:

make COMPILER_WARNINGS_FATAL=false LFLAGS='-Xlinker -lstdc++' CC=clang USE_CLANG=true LP64=1 

其中,
名词|解释 ---|--- COMPILER_WARNINGS_FATAL|设置为false来跳过严格的编译器的语法检查 LFLAGS|链接时使用的参数 CC|C编译器命令 LP64|使用64位数据模型

编译的过程出现如下问题,以及解决:

  • 修改relocInfo.hpp的367行(hotspot/src/share/vm/code/relocInfo.hpp),没验证为啥需要修改
    修改前:
inline friend relocInfo prefix_relocInfo(int datalen=0);

修改后:

inline friend relocInfo prefix_relocInfo(int datalen)
  • 修改generated-configure.sh的20061和21640行(common/autoconf/generated-configure.sh),解决configure: error: GCC compiler is required 错误 修改前:
as_fn_error $? "GCC compiler is required. Try setting --with-tools-dir." "$LINENO" 6
as_fn_error $? "GCC compiler is required. Try setting --with-tools-dir." "$LINENO" 5

修改后:

# as_fn_error $? "GCC compiler is required. Try setting --with-tools-dir." "$LINENO" 6
# as_fn_error $? "GCC compiler is required. Try setting --with-tools-dir." "$LINENO" 5
  • 修改hotspot/src/share/vm/opto/loopPredicate.cpp 755行,解决error: ordered comparison between pointer and zero ('const TypeInt *' and 'int') 修改前:
assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() ->_lo >= 0, "must be");

修改后:

// assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() ->_lo >= 0, "must be");
  • 安装XQuartz,解决'X11/Xlib.h' file not found
    安装详细信息见关于 Mac 版 X11,意思即使Mac 不再随附 X11,得自己手动安装,安装完后做软连:
     ln -s /opt/X11/include/X11 /usr/local/include/X11
    

如果build成功则会出现:

----- Build times -------
Start 2020-11-24 14:18:38
End   2020-11-24 14:18:50
00:00:01 corba
00:00:06 hotspot
00:00:00 jaxp
00:00:00 jaxws
00:00:02 jdk
00:00:01 langtools
00:00:12 TOTAL
-------------------------
Finished building OpenJDK for target 'default'

验证

build/macosx-x86_64-normal-server-slowdebug/jdk/bin/java -version

居然报错,what??? s s s

   #
   # A fatal error has been detected by the Java Runtime Environment:
   #
   #  SIGILL (0x4) at pc=0x000000010f1ed218, pid=74259, tid=6915
   #
   # JRE version: OpenJDK Runtime Environment (8.0) (build 1.8.0-internal-debug-ljh_2020_11_23_23_19-b00)
   # Java VM: OpenJDK 64-Bit Server VM (25.0-b70-debug mixed mode bsd-amd64 compressed oops)
   # Problematic frame:
   # V  [libjvm.dylib+0x9ed218]  PerfData::~PerfData()+0x8
   #
   # Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
   #
   # If you would like to submit a bug report, please visit:
   #   http://bugreport.sun.com/bugreport/crash.jsp
   #

修改hotspot/src/share/vm/runtime/perfData.cpp 285行,此处参考了stackoverflow简书
修改前:

delete p;

修改后:

// delete p;

之后执行make clean再重新编译,

再重新执行

build/macosx-x86_64-normal-server-slowdebug/jdk/bin/java -version

openjdk version "1.8.0-internal-debug"
OpenJDK Runtime Environment (build 1.8.0-internal-debug-ljh_2020_11_24_11_02-b00)
OpenJDK 64-Bit Server VM (build 25.0-b70-debug, mixed mode)

至此,openJDK8在macOS上的编译完成,具体编译的步骤在源码REDAME中

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!