ubuntu20下编译openjdk12及碰上的问题

在看周志明的《深入理解java虚拟机》,跟着教程编译openjdk12,其中碰上点问题,所以记录下来。

环境:ubuntu20

首先安装gcc命令

sudo apt-get install build-essential

接着安装以下依赖库:

命令 工具
sudo apt-get install libfreetype6-dev FreeType
sudo apt-get install libcups2-dev CUPS
sudo apt-get install libx11-dev libxext-dev libxrender-dev libxrandr-dev libxtst-dev libxt-dev X11
sudo apt-get install libasound2-dev ALSA
sudo apt-get install libffi-dev libffi
sudo apt-get install autoconf Autoconf
sudo apt-get install libfontconfig1-dev fontconfig

由于openjdk很多部分由java实现的,假设需要编译的版本为N,需要装一个版本号大于等于N-1的jdk,我们需要编译openjdk12,所以需要装JDK11及之后的版本,命令如下:

sudo apt-get install openjdk-11-jdk

接着开始编译:

bash configure --enable-debug --with-jvm-variants=server

关于configure的命令可以参考 “bash configure --help”。
到目前位置一切顺利,出现如下界面:

A new configuration has been successfully created in
/home/xiang/win_d/java/jdk12-06222165c35f/build/linux-x86_64-server-fastdebug
using configure arguments '--enable-debug --with-jvm-variants=server'.

Configuration summary:
* Debug level:    fastdebug
* HS debug level: fastdebug
* JVM variants:   server
* JVM features:   server: 'aot cds cmsgc compiler1 compiler2 epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services shenandoahgc vm-structs zgc' 
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
* Version string: 12-internal+0-adhoc.xiang.jdk12-06222165c35f (12-internal)

Tools summary:
* Boot JDK:       openjdk version "11.0.7" 2020-04-14 OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1) OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)  (at /usr/lib/jvm/java-11-openjdk-amd64)
* Toolchain:      gcc (GNU Compiler Collection)
* C Compiler:     Version 9.3.0 (at /usr/bin/gcc)
* C++ Compiler:   Version 9.3.0 (at /usr/bin/g++)

Build performance summary:
* Cores to use:   4
* Memory limit:   7858 MB

接着执行:

make images

就在我以为大功告成,准备庆祝的时候,出现如下提示:

ERROR: Build failed for target 'images' in configuration 'linux-x86_64-server-fastdebug' (exit code 2) 
Stopping sjavac server

=== Output from failing command(s) repeated here ===
* For target hotspot_variant-server_libjvm_objs_arguments.o:
In file included from /usr/include/string.h:495,
                 from /home/xiang/win_d/java/jdk12-06222165c35f/src/hotspot/share/utilities/globalDefinitions_gcc.hpp:35,
                 from /home/xiang/win_d/java/jdk12-06222165c35f/src/hotspot/share/utilities/globalDefinitions.hpp:32,
                 from /home/xiang/win_d/java/jdk12-06222165c35f/src/hotspot/share/utilities/align.hpp:28,
                 from /home/xiang/win_d/java/jdk12-06222165c35f/src/hotspot/share/runtime/globals.hpp:29,
                 from /home/xiang/win_d/java/jdk12-06222165c35f/src/hotspot/share/memory/allocation.hpp:28,
                 from /home/xiang/win_d/java/jdk12-06222165c35f/src/hotspot/share/classfile/classLoaderData.hpp:28,
                 from /home/xiang/win_d/java/jdk12-06222165c35f/src/hotspot/share/precompiled/precompiled.hpp:34:
In function ‘char* strncpy(char*, const char*, size_t)’,
    inlined from ‘static jint Arguments::parse_each_vm_init_arg(const JavaVMInitArgs*, bool*, JVMFlag::Flags)’ at /home/xiang/win_d/java/jdk12-06222165c35f/src/hotspot/share/runtime/arguments.cpp:2472:29:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:34: error: ‘char* __builtin_strncpy(char*, const char*, long unsigned int)’ output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
   ... (rest of output omitted)

* All command lines available in /home/xiang/win_d/java/jdk12-06222165c35f/build/linux-x86_64-server-fastdebug/make-support/failure-logs.
=== End of repeated output ===

No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: See doc/building.html#troubleshooting for assistance.

找了很久的原因,最后猜测是gcc版本问题。默认安装gcc9,重新安装gcc7

sudo apt-get install gcc-7 gcc-7-multilib g++-7 g++-7-multilib

查看gcc和g++分别有那些软件:

ll /usr/bin/gcc*
ll /usr/bin/g++*

最后将/usr/bin下面以下文件通过软连接重新指向版本7

/usr/bin/gcc
/usr/bin/gcc-ar
/usr/bin/gcc-nm
/usr/bin/gcc-ranlib
/usr/bin/g++ 

此时 gcc及g++版本已经将为7的,且版本9依旧保留,需要的时候通过软连接随时能改回去。

接下来回到jdk的源码目录,清除之前编译生成的文件

make clean
make dist-clean

重新执行编译命令(这次用默认参数,和之前不太一样):

bash configure

看到

Tools summary:
* Boot JDK:       openjdk version "11.0.7" 2020-04-14 OpenJDK Runtime Environment (build 11.0.7+10-post-Ubuntu-3ubuntu1) OpenJDK 64-Bit Server VM (build 11.0.7+10-post-Ubuntu-3ubuntu1, mixed mode, sharing)  (at /usr/lib/jvm/java-11-openjdk-amd64)
* Toolchain:      gcc (GNU Compiler Collection)
* C Compiler:     Version 7.5.0 (at /usr/bin/gcc)
* C++ Compiler:   Version 7.5.0 (at /usr/bin/g++)

其中C Compiler 和C++ Compiler 已经使用Version 7.5.0的gcc和g++。
接着执行

make images

编译成功,测试命令:

build/linux-x86_64-server-release/jdk/bin/java -version

显示:

openjdk version "12-internal" 2019-03-19
OpenJDK Runtime Environment (build 12-internal+0-adhoc.xiang.jdk12-06222165c35f)
OpenJDK 64-Bit Server VM (build 12-internal+0-adhoc.xiang.jdk12-06222165c35f, mixed mode)

大功告成。

你可能感兴趣的:(ubuntu20下编译openjdk12及碰上的问题)