Mac下编译OpenJdk8

1.下载和解压源码

  • 本地环境 OS version 10.13.6
  • 网上也有说通过mercurial下载,太慢了,去http://jdk.java.net/下载源码包然后解压。

2.配置环境

本地需要一个可用的JDK,直接java -verison可用即可,最好版本低于编译版本

2.1 安装依赖 和配置环境

  • 缺少什么就尝试brew install 一下
  • 主要 breaw install binutils freetype
    1
    2
    3
    4
    5
    6
    If you need to have binutils first in your PATH run:
    echo 'export PATH="/usr/local/opt/binutils/bin:$PATH"' >> ~/.zshrc

    For compilers to find binutils you may need to set:
    export LDFLAGS="-L/usr/local/opt/binutils/lib"
    export CPPFLAGS="-I/usr/local/opt/binutils/include"
  • 设置以下环境变量,new一个env.sh
    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
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    # 设定语言选项,必须设置
    export LANG=C
    # Mac平台,C编译器不再是GCC,是clang
    export CC=clang
    export CXX=clang++
    # 跳过clang的一些严格的语法检查,不然会将N多的警告作为Error
    export COMPILER_WARNINGS_FATAL=false
    # 链接时使用的参数
    export LFLAGS='-Xlinker -lstdc++'
    # 是否使用clang
    export USE_CLANG=true
    # 使用64位数据模型
    export LP64=1
    # 告诉编译平台是64位,不然会按32位来编译
    export ARCH_DATA_MODEL=64
    # 允许自动下载依赖
    export ALLOW_DOWNLOADS=true
    # 并行编译的线程数,编译时间长,为了不影响其他工作,我选择为2
    export HOTSPOT_BUILD_JOBS=2
    # 是否跳过与先前版本的比较
    export SKIP_COMPARE_IMAGES=true
    # 是否使用预编译头文件,加快编译速度
    export USE_PRECOMPILED_HEADER=true
    # 是否使用增量编译
    export INCREMENTAL_BUILD=true
    # 编译内容
    export BUILD_LANGTOOLS=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_BUILD=false
    export DEBUG_NAME=debug
    # 避开javaws和浏览器Java插件之类的部分的build
    export BUILD_DEPLOY=false
    export BUILD_INSTALL=false
    unset JAVA_HOME

2.2 为什么区分release和debug版本?

首先编译release版本比较简单,遇到的错误会比较少。我前面编译releae的时候,也比较顺利,后面编译debug版本出了很多问题,有些只在debug下才会有。我见有的博客里面描述的问题,我也没有遇到,所以遇到具体问题,具体环境分析,看到错误,尽量往前看第一个error,后面的是由前面的error引起的,搜索关键的error。

2.3 配置debug版本 的configure

1
./configure  --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/ --with-target-bits=64 --with-debug-level=slowdebug --enable-debug-symbols ZIP_DEBUGINFO_FILES=0 OBJCOPY=gobjcopy
  • with-target-bits=64 :指定生成64位jdk;
  • with-debug-level=slowdebug:编译时debug的级别,有release, fastdebug, slowdebug 三种级别;
  • enable-debug-symbols ZIP_DEBUGINFO_FILES=0:生成调试的符号信息,并且不压缩

2.4 配置release版本 的configure

1
./configure  --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/
  • configure成功的信息
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    Configuration summary:
    * Debug level: release
    * JDK variant: normal
    * JVM variants: server
    * OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64

    Tools summary:
    * Boot JDK: java version "1.8.0_211" Java(TM) SE Runtime Environment (build 1.8.0_211-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode) (at /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home)
    * C Compiler: Apple LLVM version (clang-1000.10.44.4) version 10.0.0 (clang-1000.10.44.4) (at /usr/bin/clang)
    * C++ Compiler: version (at /usr/bin/g++)

    Build performance summary:
    * Cores to use: 4
    * Memory limit: 8192 MB
    * ccache status: not installed (consider installing)

    Build performance tip: ccache gives a tremendous speedup for C++ recompilations.
    You do not have ccache installed. Try installing it.

2.4 configure 可能遇到的错误

1
2
3
4
5
6
7
8
checking for clang... /usr/bin/clang
configure: Resolving CC (as /usr/bin/clang) failed, using /usr/bin/clang directly.
checking resolved symbolic links for CC... /usr/bin/clang
checking if CC is disguised ccache... no, keeping CC
configure: The C compiler (located as /usr/bin/clang) does not seem to be the required GCC compiler.
configure: The result from running with --version was: "Apple LLVM version 10.0.0 (clang-1000.10.44.4)"
configure: error: GCC compiler is required. Try setting --with-tools-dir.
configure exiting with result code 1
  • 修改vim common/autoconf/generated-configure.sh中的2个地方,把判断去掉
    1
    2
    3
    4
    5
    6
    7
    #     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

3.编译可能遇到的错误

  • 最常见的就是编译器办版本和代码的历史原因,语法格式不支持了,常见的做法是修改代码,或升降编译器。看报错的具体文件路径和行数

  • 若报下面的错替换 为 Universe::narrow_oop_base() != NULL

    1
    2
    3
    4
    5
      if (Universe::narrow_oop_base()>0) { // Implies UseCompressedOops.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
    1 error generated.
    make[6]: *** [lcm.o] Error 1
    make[6]: *** Waiting for unfinished jobs....
  • 若报下面类似的错误 替换语法 if (base()!=NULL),保存,重新 make

    1
    2
    3
    4
    5
    6
    7
    8
    9
      if (base()>0) {
    ~~~~~~^~
    1 error generated.
    make[6]: *** [virtualspace.o] Error 1
    make[6]: *** Waiting for unfinished jobs....
    make[5]: *** [the_vm] Error 2
    make[4]: *** [product] Error 2
    make[3]: *** [generic_build2] Error 2
    make[2]: *** [product] Error 2
  • 最后可能遇到找不到 x86_64 符号

    1
    2
    3
    4
    5
    6
    Undefined symbols for architecture x86_64:
    "_attachCurrentThread", referenced from:
    +[ThreadUtilities getJNIEnv] in ThreadUtilities.o
    +[ThreadUtilities getJNIEnvUncached] in ThreadUtilities.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • 解决办法:

    1
    2
    3
    4
    5
    vi ./jdk/src/macosx/native/sun/osxapp/ThreadUtilities.m
    然后把
    inline void attachCurrentThread(void** env) {
    改为
    static inline void attachCurrentThread(void** env) {

4.编译成功

1
2
3
4
5
6
7
8
9
10
----- Build times -------
Start 2019-05-11 19:58:39
End 2019-05-11 20:02:59
00:00:16 corba
00:01:20 hotspot
00:00:10 jaxp
00:00:15 jaxws
00:02:00 jdk
00:00:18 langtools
00:04:20 TOTAL
  • 到build目录下检查版本./java -version

    1
    2
    3
    openjdk version "1.8.0-internal-debug"
    OpenJDK Runtime Environment (build 1.8.0-internal-debug-kenchan_2019_05_11_19_58-b00)
    OpenJDK 64-Bit Server VM (build 25.40-b25-debug, mixed mode)

    如果检查版本,你恰好也遇到的了运行时错误

  • 可见 https://bugs.openjdk.java.net/browse/JDK-8204325

    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
    openjdk version "1.8.0-internal-debug"
    OpenJDK Runtime Environment (build 1.8.0-internal-debug-kenchan_2019_05_11_19_58-b00)
    OpenJDK 64-Bit Server VM (build 25.40-b25-debug, mixed mode)
    #
    # A fatal error has been detected by the Java Runtime Environment:
    #
    # SIGILL (0x4) at pc=0x000000010f42e268, pid=67347, tid=8451
    #
    # JRE version: OpenJDK Runtime Environment (8.0) (build 1.8.0-internal-debug-kenchan_2019_05_11_19_58-b00)
    # Java VM: OpenJDK 64-Bit Server VM (25.40-b25-debug mixed mode bsd-amd64 compressed oops)
    # Problematic frame:
    # V [libjvm.dylib+0xa2e268] 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
    #
    # An error report file with more information is saved as:
    # /Users/kenchan/Downloads/openjdk/build/macosx-x86_64-normal-server-slowdebug/jdk/bin/hs_err_pid67347.log
    #
    # If you would like to submit a bug report, please visit:
    # http://bugreport.java.com/bugreport/crash.jsp
    #

    [error occurred during error reporting , id 0x4]

    [1] 67347 abort ./java -version
  • 解决办法 参考了https://stackoverflow.com/questions/50678467/building-openjdk-9-on-mac-os/54954805#54954805

    1. 修改 hotspot/src/share/vm/runtime /perfMemory.cpp
      1
      2
      3
      // if (SafepointSynchronize::is_at_safepoint() && !StatSampler::is_active()) {
      // PerfDataManager::destroy();
      // }
  • 这个明显是去掉了内存回收,可能导致内存泄漏,不过我们是本地测试学习,先忽略这个错误。

    1. 修改 hotspot/src/share/vm/runtime/perfData.cpp PerfDataManager::destroy() 函数
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      void PerfDataManager::destroy() {

      if (_all == NULL)
      // destroy already called, or initialization never happened
      return;

      for (int index = 0; index < _all->length(); index++) {
      PerfData* p = _all->at(index);
      delete p;//注释掉我
      }
      delete(_all);
      delete(_sampled);
      delete(_constants);

      _all = NULL;
      _sampled = NULL;
      _constants = NULL;
      }
  • 怎么找到这个错的呢?请看 ?Mac下怎么用Clion debug openJdk8?

5.参考