为了更好的体阅读验请到原文章 转送门
开始
这次编译的JDK为OpenJDK是开源的和OracleJDK(平常使用的Java)是差不多相似一样是使用HotSpot虚拟机。想更深入了解JVM就得去自己编译和调试JDK。带着内存是如何布局、内存是如何回收、类是如何加载等这些问题去学习会得到一个好的学习效果。
吐槽: (Write Once、Run Anywhere) 这个JDK花费我很多时间才编译好的,所以不想编译第二次了 /dog
以下需要安装的环境或软件可能需要外网,请自行配置代理。
官方编译参考文档:%OpenJDK% /README-builds.html
环境
OS:macOS Catalina 10.15.16
编译器:xcode11
Java:JDk7 (用于编译)
OpenJDK:JDK8u
准备条件
1、Homebrew: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
2、Mercurial:brew install Mercurial
3、freetype:brew install freetype
4、llvm:brew install llvm
下载源码
hg clone http://hg.openjdk.java.net/jdk8u/jdk8u60 OpenJDK
cd OpenJDK
vim batchget.sh
chmod +x batchget.sh
./batchget.sh
(batchget.sh)代码 网络差用
#!/bin/bash
for i in {1..28}
do
sh ./get_source.sh;
done
设置环境变量
export LC_ALL=en_US.UTF-8 //解决乱码
vim bashconfig.sh
chmod +x bashconfig.sh
./bashconfig.sh
(bashconfig.sh)代码
# 设定语言选项,必须设置
export LANG=C
# Mac平台,C编译器不再是GCC,而是clang
export CC=clang
export CXX=clang++
export CXXFLAGS=-stdlib=libc++
# 是否使用clang,如果使用的是GCC编译,该选项应该设置为false
export USE_CLANG=true
# 跳过clang的一些严格的语法检查,不然会将N多的警告作为Error
export COMPILER_WARNINGS_FATAL=false
# 链接时使用的参数
export LFLAGS='-Xlinker -lstdc++'
# 使用64位数据模型
export LP64=1
# 告诉编译平台是64位,不然会按照32位来编译
export ARCH_DATA_MODEL=64
# 允许自动下载依赖
export ALLOW_DOWNLOADS=true
# 并行编译的线程数,编译时长,为了不影响其他工作,可以选择2
export HOTSPOT_BUILD_JOBS=4
export PARALLEL_COMPILE_JOBS=2 #ALT_PARALLEL_COMPILE_JOBS=2
# 是否跳过与先前版本的比较
export SKIP_COMPARE_IMAGES=true
# 是否使用预编译头文件,加快编译速度
export USE_PRECOMPILED_HEADER=true
# 是否使用增量编译
export INCREMENTAL_BUILD=true
# 编译内容
export BUILD_LANGTOOL=true
export BUILD_JAXP=true
export BUILD_JAXWS=true
export BUILD_CORBA=true
export BUILD_HOTSPOT=true
export BUILD_JDK=true
# 编译版本
export SKIP_DEBUG_BUILD=true
export SKIP_FASTDEBUG_BULID=false
export DEBUG_NAME=debug
# 避开javaws和浏览器Java插件之类部分的build
export BUILD_DEPLOY=false
export BUILD_INSTALL=false
# 最后需要干掉这两个环境变量(如果你配置过),不然会发生诡异的事件
unset JAVA_HOME
unset CLASSPATH
Configure
//release版本
bash ./configure --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/
//Xcode可断点调试的slow-debug版本 --with-boot-jdk:Java路径
bash ./configure --with-debug-level=slowdebug --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home --enable-debug-symbols ZIP_DEBUGINFO_FILES=0
Configure成功~
编译
make all
完成
----- Build times -------
Start 2020-07-19 14:54:23
End 2020-07-19 14:56:57
00:00:00 corba
00:00:19 demos
00:01:12 docs
00:00:00 hotspot
00:00:41 images
00:00:00 jaxp
00:00:01 jaxws
00:00:10 jdk
00:00:00 langtools
00:00:10 nashorn
00:02:34 TOTAL
-------------------------
Finished building OpenJDK for target 'all'
验证
编译完成后JDK存放在/build/macosx-x86_64-normal-server-slowdebug/jdk
终端执行命令
cd ./build/macosx-x86_64-normal-server-slowdebug/jdk/bin
./java -version
提示:
OpenJDK Runtime Environment (build 1.8.0-internal-debug-error0_2020_07_19_14_21-b00)
OpenJDK 64-Bit Server VM (build 25.60-b23-debug, mixed mode)
报错
如果make all
执行出现错误,解决问题后应该先执行make clean
然后再接着make all
,要学会利用Google。
提示:configure: error: GCC compiler is required. Try setting --with-tools-dir.
解决:编辑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 GCC compiler." >&5
$as_echo "$as_me: The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required GCC compiler." >&6;}
{ $as_echo "$as_me:${as_lineno-$LINENO}: The result from running with --version was: \"$COMPILER_VERSION_TEST\"" >&5
$as_echo "$as_me: The result from running with --version was: \"$COMPILER_VERSION_TEST\"" >&6;}
as_fn_error $? "GCC compiler is required. Try setting --with-tools-dir." "$LINENO" 5
fi
提示:error: friend declaration specifying a default argument must be a definition inline friend relocInfo prefix_relocInfo(int datalen = 0);
解决:编辑hotspot/src/share/vm/code/relocInfo.hpp
原:inline friend relocInfo prefix_relocInfo(int datalen = 0);
修改后:inline friend relocInfo prefix_relocInfo(int datalen);
提示:error: ordered comparison between pointer and zero ('const TypeInt *' and 'int')
解决:编辑hotspot/src/share/vm/opto/loopPredicate.cpp
原:assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() >= 0, "must be");
修改后:assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int()->_lo >= 0, "must be");
提示:virtualspace.cpp:331:14: error: ordered comparison between pointer and zero ('char *' and 'int')
解决:编辑hotspot/src/share/vm/runtime/virtualspace.cpp
原: if (base() > 0) {
修改后:if (base() != NULL) {
提示:MacosxDebuggerLocal.m:27:9: fatal error: 'JavaNativeFoundation/JavaNativeFoundation.h' file not found
解决:终端命令执行 find / -name "*JavaNativeFoundation.h*"
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/JavaVM.framework/Versions/A/Frameworks/JavaNativeFoundation.framework/Versions/A/Headers/JavaNativeFoundation.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/JavaVM.framework/Versions/A/Frameworks/JavaNativeFoundation.framework/Versions/A/Headers/JavaNativeFoundation.h
编辑hotspot/make/bsd/makefiles/saproc.make:67
原: SALIBS = -g -framework Foundation -F/System/Library/Frameworks/JavaVM.framework/Frameworks -framework JavaNativeFoundation -framework Security -framework CoreFoundation
修改后: SALIBS = -g -framework Foundation -F/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks -framework JavaNativeFoundation -framework Security -framework CoreFoundation
编辑hotspot/make/bsd/makefiles.saproc.make:106 (注意代码前面有一个tab)
原: -I/System/Library/Frameworks/JavaVM.framework/Headers
修改后: -I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library
/Frameworks/JavaVM.framework/Versions/A/Frameworks
/JavaNativeFoundation.framework/Versions/A/Headers
提示:error: '&&' within '||' [-Werror,-Wlogical-op-parentheses] if ( instr->is_ideal_branch() && instr->label_position() == -1 ||
解决:终端执行export COMPILER_WARNINGS_FATAL=false
提示: error: unknown argument: '-fpch-deps'
解决:编辑hotspot/make/bsd/makefiles/gcc.make 注释以下代码
# Compiler warnings are treated as errors
ifneq ($(COMPILER_WARNINGS_FATAL),false)
WARNINGS_ARE_ERRORS = -Werror
endif
ifeq ($(USE_CLANG),)
ifneq ($(CC_VER_MAJOR), 2)
DEPFLAGS += -fpch-deps
endif
endif
提示:include path for libstdc++ headers not found
解决:打开https://github.com/imkiwa/xcode-missing-libstdc- clone到本地根据install.sh配置
提示:lcm.cpp:52:35: error: ordered comparison between pointer and zero ('address' (aka 'unsigned char *') and 'int')
解决:编辑/hotspot/src/share/vm/opto/lcm.cpp:52
原:if (Universe::narrow_oop_base() > 0)
修改后: if (Universe::narrow_oop_base()!= NULL) { // Implies UseCompressedOops.
提示:Undefined symbols for architecture x86_64
解决:编辑jdk/src/macosx/native/sun/osxapp/ThreadUtilities.m
原
inline void attachCurrentThread(void** env) {
修改后:
static inline void attachCurrentThread(void** env) {