编译环境
Ubuntu 22.04.1 (Jammy Jellyfish)
注意:Ubuntu 配置 apt 安装源为阿里源可以提高加载速度
设置 apt 安装阿里源
备份
sudo cp /ect/apt/sources.list /etc/apt/sources.list.bak
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
更新
sudo apt-get update && sudo apt-get upgrade
安装JDK7
略... (最新的ubuntu下,apt 已经不提供jdk7的安装软件了,..F...)
1. 筛选tag:
打开github:https://github.com/openjdk/jdk/,筛选一下下一步需要构件的版本的tag
2. 下载代码
可以使用git clone。也可以点击点击Download Zip,这里使用Download Zip的方式。
注意:如果选择git clone注意只需要clone这个tag的就行。如果clone所有的代码量比较大,会很慢。
执行编译
1. configure检测
bash ./configure --with-target-bits=64 --with-jvm-variants=server --with-debug-level=slowdebug --disable-zip-debug-info --with-boot-jdk=/home/cap/software/jdk7/
bash ./configure --with-target-bits=64 --with-jvm-variants=server --with-debug-level=release --disable-zip-debug-info --with-boot-jdk=/home/cap/software/jdk7/
--with-target-bits=64: 构建64的JDK
--with-jvm-variants=server :构建server模式的JDK
--with-debug-level=slowdebug: slowdebug带有更多的调试信息
--disable-zip-debug-info:禁止压缩调试信息,可以方便调试
--with-boot-jdk=/export/soft/java-se-7u75-ri/ :表示boot jdk的路径,就是jdk7的路径
release: 使用此选项编译 JDK 以进行生产发布,通常会关闭调试信息和开启优化。
fastdebug:以调试信息编译 JDK,但不包含额外的调试功能和检查。
product: 编译用于生产环境的 JDK,类似于 release 选项。
optimize: 开启所有优化。
debug: 开启完整的调试信息和调试功能。
2. 可能碰到的错误
1~6
1. 错误提示:configure: error: Cannot find GNU make 3.81 or newer! Please put it in the path, or add e.g. MAKE=/opt/gmake3.81/make as argument to configure.
sudo apt-get install make
2. 错误提示:configure: error: Could not find a C compiler. You might be able to fix this by running 'sudo apt-get install build-essential'.
# 安装 build-essential
sudo apt-get install build-essential
3. 错误提示:configure: error: Could not find all X11 headers (shape.h Xrender.h XTest.h Intrinsic.h). You might be able to fix this by running 'sudo apt-get install libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev'.
# 安装 libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev
# 注意 libx11-dev 的 X 是小写
sudo apt-get install libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev
4. 错误提示:configure: error: Could not find cups! You might be able to fix this by running 'sudo apt-get install libcups2-dev'.
# 安装 libcups2-dev
sudo apt-get install libcups2-dev
5. 错误提示:configure: error: Could not find freetype! You might be able to fix this by running 'sudo apt-get install libfreetype6-dev'.
# 安装 libfreetype6-dev
sudo apt-get install libfreetype6-dev
6. 错误提示:configure: error: Could not find alsa! You might be able to fix this by running 'sudo apt-get install libasound2-dev'.
# 安装 libasound2-dev
sudo apt-get install libasound2-dev
7. gcc版本问题
根据官方文档()OpenJDK Build README,构建jdk8需要gcc 4.3。
sudo vi /etc/apt/sources.list
#install gcc 4.9
deb http://dk.archive.ubuntu.com/ubuntu/ xenial main
deb http://dk.archive.ubuntu.com/ubuntu/ xenial universe
Xenial Xerus 是 Ubuntu 的 16.04 版本的代号, 在22.04版本(Jammy Jellyfish)版本的deb中已经找不到gcc4.9的包
sudo apt-get update
sudo apt install gcc-4.9
sudo apt install g++-4.9
cap@cap:/usr/bin$ sudo mv gcc gcc-11
cap@cap:/usr/bin$ sudo mv g++ g++-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 50
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20
sudo update-alternatives --config gcc
cap@cap:/usr/bin$ sudo update-alternatives --config gcc
有 2 个候选项可用于替换 gcc (提供 /usr/bin/gcc)。
选择 路径 优先级 状态
------------------------------------------------------------
* 0 /usr/bin/gcc-11 50 自动模式
1 /usr/bin/gcc-11 50 手动模式
2 /usr/bin/gcc-4.9 20 手动模式
要维持当前值[*]请按<回车键>,或者键入选择的编号:2
sudo update-alternatives --config g++
cap@cap:/usr/bin$ sudo update-alternatives --config gcc
有 2 个候选项可用于替换 gcc (提供 /usr/bin/gcc)。
选择 路径 优先级 状态
0 /usr/bin/gcc-11 50 自动模式
1 /usr/bin/gcc-11 50 手动模式
2 /usr/bin/gcc-4.9 20 手动模式
要维持当前值[*]请按<回车键>,或者键入选择的编号:2
3. 检测通过
====================================================
A new configuration has been successfully created in
/home/cap/software/openjdk8/build/linux-x86_64-normal-server-release
using configure arguments '--with-target-bits=64 --with-jvm-variants=server --with-debug-level=release --disable-zip-debug-info --with-boot-jdk=/home/cap/software/jdk7/'.
Configuration summary:
* Debug level: release
* JDK variant: normal
* JVM variants: server
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
Tools summary:
* Boot JDK: openjdk version "1.7.0_75" OpenJDK Runtime Environment (build 1.7.0_75-b13) OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode) (at /home/cap/software/jdk7)
* Toolchain: gcc (GNU Compiler Collection)
* C Compiler: Version 4.9.3 (at /usr/bin/gcc)
* C++ Compiler: Version 4.9.3 (at /usr/bin/g++)
Build performance summary:
* Cores to use: 4
* Memory limit: 7728 MB
4. bear make构建
安装 bear
bear主要是用来生成 compilation database。用来给后续Clion使用,如果使用gdb调试不适用Clion的图形化调试可以不用bear。
cd build/linux_x86_64-normal-server-slowdebug
bear -- make all
cd build/linux_x86_64-normal-server-release
bear -- make all
或者可以这样做,但是这样,会导致compile_commands.json被覆盖,
bear -- make CONF=debug all
bear -- make CONF=release all
可能碰到的错误
*** This OS is not supported: Linux Lincoln 4.15.0-130-generic #134-Ubuntu SMP Tue Jan 5 20:46:26 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
make[5]: *** [check_os_version] Error 1
/home/parallels/workspace/openjdk/jdk-jdk8-b120/hotspot/make/linux/Makefile:234: recipe for target 'check_os_version' failed
修改Makefile文件
vi hotspot/make/linux/Makefile
SUPPORTED_OS_VERSION = 2.4% 2.5% 2.6% 3%
/usr/bin/make: invalid option -- '8'
/usr/bin/make: invalid option -- '-'
/usr/bin/make: invalid option -- '1'
/usr/bin/make: invalid option -- '2'
...
/usr/bin/make: invalid option -- 'c'
vi hotspot/make/linux/makefiles/adjust-mflags.sh
cc1plus: all warnings being treated as errors
vi hotspot/make/linux/makefiles/gcc.make
通过提示
Tools summary:
* Boot JDK: openjdk version "1.8.0_275" OpenJDK Runtime Environment (build 1.8.0_275-8u275-b01-0ubuntu1~20.04-b01) OpenJDK 64-Bit Server VM (build 25.275-b01, mixed mode) (at /usr/lib/jvm/java-8-openjdk-amd64)
* C Compiler: x86_64-linux-gnu-gcc-9 (Ubuntu 9.3.0-17ubuntu1~20.04) version 9.3.0 (at /usr/bin/x86_64-linux-gnu-gcc-9)
* C++ Compiler: x86_64-linux-gnu-g++-9 (Ubuntu 9.3.0-17ubuntu1~20.04) version 9.3.0 (at /usr/bin/x86_64-linux-gnu-g++-9)
Build performance summary:
* Cores to use: 1
* Memory limit: 3936 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.
You might be able to fix this by running 'sudo apt-get install ccache'.
编译 - make all
----- Build times -------
Start 2020-12-12 22:17:38
End 2020-12-12 22:51:31
00:00:54 corba
00:01:03 demos
00:04:57 docs
00:16:08 hotspot
00:01:06 images
00:00:35 jaxp
00:01:05 jaxws
00:06:49 jdk
00:00:54 langtools
00:00:21 nashorn
00:33:53 TOTAL
-------------------------
Finished building OpenJDK for target 'all'
确认编译成功
cd /javawork/jdk/build/linux-x86_64-normal-server-release/jdk/bin
./java -version
openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-zsh_2020_12_12_22_17-b00)
OpenJDK 64-Bit Server VM (build 25.0-b62, mixed mode)
Clion中使用
这里我从别处截过来的,当你只使用一种模式,比如debug 或 release时,可以这样操作,但是,如果你要在两个版本中做来回切换,就得稍微有点区别,暂时不列了。
1. 导入工程:
选择之前生成的:compile_commands.json ,选择open as project