乐趣区

关于jdk:openJDK系列1macos编译openJDK-8

背景

因为之前始终在钻研 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](https://stackoverflow.com/questions/50678467/building-openjdk-9-on-mac-os/54954805#54954805) 和[简书](https://www.jianshu.com/p/34c8a8c37169) 
批改前:

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 中

退出移动版