• 安装mercurial版本管理

brew install mercurial

  • 安装ccache提高编译速度

brew install ccache

  • 安装freetype

brew install freetype

  • 获取源码

hg clone http://hg.openjdk.java.net/jdk9/jdk9 YourOpenJDK

cd YourOpenJDK

chmod +x get_source.sh

./get_source.sh

  • 修改文件属性

chmod +x configure

  • 输入一下命令激活后会生成一个编译配置。

sudo ./configure --enable-debug --with-target-bits=64

  • 修改get_resource.sh,下载过程中,如果失败,则一直尝试,直至成功
1
2
3
4
5
# Get clones of all absent nested repositories (harmless if already exist)
sh ./common/bin/hgforest.sh clone "$@" || exit $?

# Update all existing repositories to the latest sources
sh ./common/bin/hgforest.sh pull -u

修改为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Get clones of all absent nested repositories (harmless if already exist)
sh ./common/bin/hgforest.sh clone "$@"

while [ $? -ne 0 ]
do
sh ./common/bin/hgforest.sh clone "$@"
done

# Update all existing repositories to the latest sources
sh ./common/bin/hgforest.sh pull -u

while [ $? -ne 0 ]
do
sh ./common/bin/hgforest.sh pull -u
done
  • 配置
1
sudo ./configure --disable-warnings-as-errors --with-boot-jdk=/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/

参数解释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
--with-target-bits:设置32位/64位编译

--with-freetype:设置freetype路径

--enable-ccache:设置启用ccache

--with-jvm-variants=client,server:为了保证兼容性,编译时JVM的Client和Server都会被编译

--with-boot-jdk-jvmargs:提供运行Bootstrap JDK所需要的JVM参数

--disable-zip-debug-info:禁用zip调试信息

--disable-warnings-as-errors:禁用将警告当做错误,避免因为警告而中断编译

--with-debug-level:设置调试等级

2>&1 | tee configure_mac_x64.log:将错误信息重定向至标准输出,并输出到configure_mac_x64.log

configure

sudo ./configure --enable-debug --with-target-bits=64

如果是正式版本,./configure后边不需要加参数。configure过程会检查一些配置,然后在YourOpenJDK/build目录下会有macosx-x86_64-normal-server-fastdebug目录,如果不是fastdebug版本,会有macosx-x86_64-normal-server-release目录。这个过程中遇到一个问题:

configure: error: GCC compiler is required

对于这个问题,我查到是YourOpenJDK/common/autoconf/generated-configure.sh里边校验抛出,我直接找到下边文本出现的两个地方给注释掉了……

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

Markdown

编译

sudo make CONF=macosx-x86_64-normal-server-release

验证

Markdown

问题1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
=== Output from failing command(s) repeated here ===
/usr/bin/printf "* For target hotspot_variant-server_libjvm_objs_lcm.o:\n"
* For target hotspot_variant-server_libjvm_objs_lcm.o:
(/usr/bin/grep -v -e "^Note: including file:" < /Users/2bai/Documents/code/jdk9/build/macosx-x86_64-normal-server-fastdebug/make-support/failure-logs/hotspot_variant-server_libjvm_objs_lcm.o.log || true) | /usr/bin/head -n 12
/Users/2bai/Documents/code/jdk9/hotspot/src/share/vm/opto/lcm.cpp:42:35: error: ordered comparison between pointer and zero ('address' (aka 'unsigned char *') and 'int')
if (Universe::narrow_oop_base() > 0) { // Implies UseCompressedOops.
~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
1 error generated.
if test `/usr/bin/wc -l < /Users/2bai/Documents/code/jdk9/build/macosx-x86_64-normal-server-fastdebug/make-support/failure-logs/hotspot_variant-server_libjvm_objs_lcm.o.log` -gt 12; then /bin/echo " ... (rest of output omitted)" ; fi
/usr/bin/printf "\n* All command lines available in /Users/2bai/Documents/code/jdk9/build/macosx-x86_64-normal-server-fastdebug/make-support/failure-logs.\n"

* All command lines available in /Users/2bai/Documents/code/jdk9/build/macosx-x86_64-normal-server-fastdebug/make-support/failure-logs.
/usr/bin/printf "=== End of repeated output ===\n"
=== End of repeated output ===
if /usr/bin/grep -q "recipe for target .* failed" /Users/2bai/Documents/code/jdk9/build/macosx-x86_64-normal-server-fastdebug/build.log 2> /dev/null; then /usr/bin/printf "\n=== Make failed targets repeated here ===\n" ; /usr/bin/grep "recipe for target .* failed" /Users/2bai/Documents/code/jdk9/build/macosx-x86_64-normal-server-fastdebug/build.log ; /usr/bin/printf "=== End of repeated output ===\n" ; /usr/bin/printf "\nHint: Try searching the build log for the name of the first failed target.\n" ; else /usr/bin/printf "\nNo indication of failed target found.\n" ; /usr/bin/printf "Hint: Try searching the build log for '] Error'.\n" ; fi

=== Make failed targets repeated here ===
lib/CompileJvm.gmk:207: recipe for target '/Users/2bai/Documents/code/jdk9/build/macosx-x86_64-normal-server-fastdebug/hotspot/variant-server/libjvm/objs/lcm.o' failed
make/Main.gmk:263: recipe for target 'hotspot-server-libs' failed
=== End of repeated output ===

Hint: Try searching the build log for the name of the first failed target.
/usr/bin/printf "Hint: See common/doc/building.html#troubleshooting for assistance.\n\n"
Hint: See common/doc/building.html#troubleshooting for assistance.

gmake[2]: Leaving directory '/Users/2bai/Documents/code/jdk9'
make[1]: *** [main] Error 2
make: *** [all] Error 2

解决

修改jdk9/hotspot/src/share/vm/opto/lcm.cpp

1
2
3
if (base() > 0) { 
//修改为
if (base() != NULL) {
1
2
3
if (Universe::narrow_oop_base() > 0) { // Implies UseCompressedOops.
//修改为
if (Universe::narrow_oop_base() != NULL) { // Implies UseCompressedOops.

问题2

1
2
3
4
5
6
7
8
9
jdk9/hotspot/src/jdk.hotspot.agent/macosx/native/libsaproc/MacosxDebuggerLocal.m:692:21: warning: 'ePtAttachDeprecated' is deprecated: PT_ATTACH is deprecated. See PT_ATTACHEXC [-Wdeprecated-declarations]
if ((res = ptrace(PT_ATTACH, pid, 0, 0)) < 0) {
^
/usr/include/sys/ptrace.h:85:19: note: expanded from macro 'PT_ATTACH'
#define PT_ATTACH ePtAttachDeprecated /* trace some running process */
^
/usr/include/sys/ptrace.h:71:22: note: 'ePtAttachDeprecated' has been explicitly marked deprecated here
ePtAttachDeprecated __deprecated_enum_msg("PT_ATTACH is deprecated. See PT_ATTACHEXC") = 10
^

解决2

尝试直接把上面报错地方的PT_ATTACH改成10试试

1
2
3
if ((res = ptrace(PT_ATTACH, pid, 0, 0)) < 0) {
//修改为
if ((res = ptrace(10, pid, 0, 0)) < 0) {

问题3

1
2
3
jdk9/jdk/src/java.desktop/share/native/libjavajpeg/jdhuff.c:458:13: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
{ 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1,
~~~~^

解决3

修改文件:

  • jdk9/jdk/src/java.desktop/share/native/libjavajpeg/jdphuff.c:221
  • jdk9/jdk/src/java.desktop/share/native/libjavajpeg/jdhuff.c:458
1
2
3
4
5
static const int extend_offset[16] = /* entry n is (-1 << n) + 1 */
{ 0, (-(1<<1)) + 1, (-(1<<2)) + 1, (-(1<<3)) + 1, (-(1<<4)) + 1,
(-(1<<5)) + 1, (-(1<<6)) + 1, (-(1<<7)) + 1, (-(1<<8)) + 1,
(-(1<<9)) + 1, (-(1<<10)) + 1, (-(1<<11)) + 1, (-(1<<12)) + 1,
(-(1<<13)) + 1, (-(1<<14)) + 1, (-(1<<15)) + 1 };

问题4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
=== Output from failing command(s) repeated here ===
* For target hotspot_variant-server_libjvm_objs_loopPredicate.o:
../src/share/vm/opto/loopPredicate.cpp:903: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");
~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~
../src/share/vm/utilities/debug.hpp:141:33: note: expanded from macro 'assert'
#define assert(p, ...) vmassert(p, __VA_ARGS__)
^
../src/share/vm/utilities/debug.hpp:130:9: note: expanded from macro 'vmassert'
if (!(p)) { \
^
1 error generated.

* All command lines available in /Users/2bai/Documents/code/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug/make-support/failure-logs.
=== End of repeated output ===

解决4

修改jdk9/hotspot/src/share/vm/opto/loopPredicate.cpp:903

1
assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int() >= 0, "must be");

修改为

1
assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int()->_lo >= 0, "must be");

备份

和编译openJDK9无关!!!!

之前编译的时候取消了JAVA_HOME,怕自己忘记,记一下。

  • JAVA_HOME

/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home