【Java】macOS下编译JDK8

Posted by 西维蜀黍 on 2018-11-12, Last Modified on 2021-09-21

安装mercurial

brew install mercurial

下载源码

hg clone http://hg.openjdk.java.net/jdk8/jdk8 java-source
cd java-source
chmod +x get_source.sh
./get_source.sh

安装依赖

brew install freetype

修改源代码

1. 修改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);
2. 修改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" 5

修改后为:

#as_fn_error $? "GCC compiler is required. Try setting --with-tools-dir." "$LINENO" 5

本机的xcode为7.3.1,编译器为clang,版本为Apple LLVM version 7.3.0 (clang-703.0.31);而jdk编译默认需要gcc编译器,由于clang703已经支持大部分的gcc语法,因此此处是直接注释掉编译器检查,当然也可以安装gcc编译器进行编译; 注意:编译器的版本很重要,否则会报一大堆语法错误;如果采用gcc,要求>=3.81

3. 修改hotspot/src/share/vm/opto/loopPredicate.cpp,
_igvn.type(rng)->is_int() >= 0 改成 _igvn.type(rng)->is_int()->_lo >= 0

否则会出现以下错误:

/Volumes/Working/java-source/hotspot/src/share/vm/opto/loopPredicate.cpp:775:73: error: ordered comparison between pointer and zero ('const TypeInt *' and 'int')
      assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() >= 0, "must be");
                                              ~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~
4. 修改hotspot/src/share/vm/runtime/virtualspace.cpp

base() > 0 改成 base() != 0

否则会出现以下错误:

/Volumes/Working/java-source/hotspot/src/share/vm/runtime/virtualspace.cpp:331:14: error: ordered comparison between pointer and zero ('char *' and 'int')
  if (base() > 0) {
      ~~~~~~ ^ ~

编译

chmod a+x configure
编译配置
./configure --with-debug-level=slowdebug  --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home --with-freetype-include=/usr/local/Cellar/freetype/2.9.1/include/freetype2 --with-freetype-lib=/usr/local/Cellar/freetype/2.9.1/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++ 

注意:上面带版本号的地方(本机JDK路径,freetype路径)根据实际情况替换

若正常,会出现以下输出:

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

完成

最后我们看到了这样的输出

----- Build times -------
Start 2018-09-13 10:47:14
End   2018-09-13 10:58:24
00:02:12 corba
00:00:39 hotspot
00:00:56 jaxp
00:00:59 jaxws
00:06:21 jdk
00:00:00 langtools
00:11:10 TOTAL
-------------------------
Finished building OpenJDK for target 'default'

测试

$ cd 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-kiva_2018_02_24_20_52-b00)
OpenJDK 64-Bit Server VM (build 25.0-b70-debug, mixed mode)

Reference

https://imkiva.com/2018/02/24/building-openjdk8-on-macos/

https://www.jianshu.com/p/4e01daf8c357

https://juejin.im/post/5a6d7d106fb9a01ca47abd8b