1-CentOS7.3编译Jdk7.0

JDK源码下载 http://openjdk.java.net/install/
一、系统环境
cat /etc/redhat-release  #查询发行版本
	uname -m
	uname -r

ant 指定java路径 ant编译指定jdk版本_ant 指定java路径

二、编译环境配置
2.1安装Apache Ant (版本要求1.7.1以上)

下载apache-ant-1.9.9-bin.tar.gz安装包上传到 /usr/local目录下,

ant 指定java路径 ant编译指定jdk版本_sed_02

执行命令,设置ant环境

tar -xvf apache-ant-1.9.9-bin.tar.gz #解压ant安装包
	ln -s /usr/local/apache-ant-1.9.9/bin/ant /usr/bin/ant #安装ant到本地
	ant #查询ant是否可用

ant 指定java路径 ant编译指定jdk版本_java_03

2.2安装Bootstrap JDK (尽量不要使用跟目标版本相同的版本或者更高版本)

这里要进行编译的jdk版本为openjdk-7u40,所以我选择的Bootstrap JDK的版本为jdk-6u45-linux-x64.bin

从oracle官网下载jdkjdk-6u45-linux-x64.bin 然后上传到/usr/local目录下

ant 指定java路径 ant编译指定jdk版本_java_04

解压安装到local中

chmod a+x jdk-6u45-linux-x64.bin  
	./jdk-6u45-linux-x64.bin

ant 指定java路径 ant编译指定jdk版本_ant 指定java路径_05


ant 指定java路径 ant编译指定jdk版本_sed_06

注:检查系统是否默认安装了openJDK环境

2.3 安装GCC依赖包
#安装alsa包
yum -y install alsa-lib-devel
安装cups-devel
yum -y install cups-devel
安装X相关的库 
yum -y install libX*
yum -y install libXi-devel
安装gcc
yum -y install gcc gcc-c++
安装freetype 
yum -y install freetype
安装stdc库,(不安装会报cannot access 'libjvm.so': No such file or directory错误)
yum install libstdc++-static
2.4 配置要编译的环境变量
2.4.1 取消java环境变量

如果之前设定了JAVA_HOME和CLASSPATH两个环境变量,在编译前必须取消,否则在Makefile脚本中检查到有这两个变量存在,会有警告提示

unset JAVA_HOME
	unset CLASSPATH
2.4.2 语言选项,必须设置
export LANG=C
2.4.3 Bootstrap JDK的安装路径,必须设置
export ALT_BOOTDIR=/usr/local/jdk1.6.0_45
2.4.4 Ant的安装路径
export ANT_HOME=/usr/local/apache-ant-1.9.9
2.4.5 允许自动下载依赖
export ALLOW_DOWNLOADS=true
2.4.6 使用预编译头文件,不加这个编译会更慢一些
export USE_PRECOMPILED_HEADER=true
2.4.7 并行编译的线程数,设置为和CPU内核数量一致即可
export HOTSPOT_BUILD_JOB=4 
	export ALT_PARALLEL_COMPILE_JOBS=4
2.4.8 64位jdk
export ARCH_DATA_MODEL=64
2.4.9 比较本次build出来的映像与先前版本的差异(这对我们来说没有意思)

必须设置为false,否则sanity检查会报缺少先前版本JDK的映像的错误提示
如果已经设置dev或者DEV_ONLY=true 这个不显式设置也行

export SKIP_COMPARE_IMAGES=false
2.4.10 编译内容
export BUILD_LANGTOOLS=true
	# export BUILD_JAXP=false
	# export BUILD_JAXWS=false
	# export BUILD_CORBA=false
	# export BUILD_HOTSPOT=true
	export BUILD_JDK=true
2.4.11 编译的版本
export SKIP_DEBUG_BUILD=false 
	export SKIP_FASTDEBUG_BUILD=true 
	export DEBUG_NAME=debug
2.4.12 把它设置为false可以避开javaws和浏览器java插件之类的部分的build
export BUILD_DEPLOY=false
2.4.13 把它设置为false就不会build出安装包,因为安装包里有些奇怪的依赖

但即便不build出它也已经能得到完整的JDK映像,所以还是别build它好了

export BUILD_INSTALL=false
2.4.14 编译结果所存放的路径
export ALT_OUTPUTDIR=/home/jdkbuild

ant 指定java路径 ant编译指定jdk版本_linux_07

三、开始编译
3.1 上传jdk源码,解压到本地

ant 指定java路径 ant编译指定jdk版本_java_08

jdk源码目录结构

ant 指定java路径 ant编译指定jdk版本_ant 指定java路径_09

3.2 编译检查
make sanity

ant 指定java路径 ant编译指定jdk版本_java_10

( cd  ./jdk/make && \
  make sanity HOTSPOT_IMPORT_CHECK=false JDK_TOPDIR=/home/openjdk/jdk JDK_MAKE_SHARED_DIR=/home/openjdk/jdk/make/common/shared EXTERNALSANITYCONTROL=true SOURCE_LANGUAGE_VERSION=7 TARGET_CLASS_VERSION=7 MILESTONE=internal BUILD_NUMBER=b00 JDK_BUILD_NUMBER=b00 FULL_VERSION=1.7.0-internal-debug-root_2017_06_16_16_58-b00 PREVIOUS_JDK_VERSION=1.6.0 JDK_VERSION=1.7.0 JDK_MKTG_VERSION=7 JDK_MAJOR_VERSION=1 JDK_MINOR_VERSION=7 JDK_MICRO_VERSION=0 PREVIOUS_MAJOR_VERSION=1 PREVIOUS_MINOR_VERSION=6 PREVIOUS_MICRO_VERSION=0 ARCH_DATA_MODEL=64 VARIANT=DBG DEBUG_CLASSFILES=true COOKED_BUILD_NUMBER=0 ANT_HOME="/usr/local/apache-ant-1.9.9" ALT_OUTPUTDIR=/home/jdkbuild ALT_LANGTOOLS_DIST=/home/jdkbuild/langtools/dist ALT_CORBA_DIST=/home/jdkbuild/corba/dist ALT_JAXP_DIST=/home/jdkbuild/jaxp/dist ALT_JAXWS_DIST=/home/jdkbuild/jaxws/dist ALT_HOTSPOT_IMPORT_PATH=/home/jdkbuild/hotspot/import BUILD_HOTSPOT=true ; )
INFO: ENABLE_FULL_DEBUG_SYMBOLS=1
INFO: /usr/bin/objcopy cmd found so will create .debuginfo files.
INFO: STRIP_POLICY=min_strip
INFO: ZIP_DEBUGINFO_FILES=1
make[1]: Entering directory `/home/openjdk/jdk/make'
INFO: ENABLE_FULL_DEBUG_SYMBOLS=1
INFO: /usr/bin/objcopy cmd found so will create .debuginfo files.
INFO: STRIP_POLICY=min_strip
INFO: ZIP_DEBUGINFO_FILES=1
freetypecheck.c: In function 'main':
freetypecheck.c:77:14: warning: unused parameter 'argc' [-Wunused-parameter]
 int main(int argc, char** argv) {
              ^
freetypecheck.c:77:27: warning: unused parameter 'argv' [-Wunused-parameter]
 int main(int argc, char** argv) {
                           ^
make[1]: Leaving directory `/home/openjdk/jdk/make'

Build Machine Information:
   build machine = localhost.localdomain

Build Directory Structure:
   CWD = /home/openjdk
   TOPDIR = .
   LANGTOOLS_TOPDIR = ./langtools
   JAXP_TOPDIR = ./jaxp
   JAXWS_TOPDIR = ./jaxws
   CORBA_TOPDIR = ./corba
   HOTSPOT_TOPDIR = ./hotspot
   JDK_TOPDIR = ./jdk

External File/Binary Locations:
   PREVIOUS_JRE_BUNDLE = USING-PREVIOUS_RELEASE_IMAGE 
   PREVIOUS_JDK_BUNDLE = USING-PREVIOUS_RELEASE_IMAGE 

Build Directives:
   BUILD_LANGTOOLS = true 
   BUILD_JAXP = true 
   BUILD_JAXWS = true 
   BUILD_CORBA = true 
   BUILD_HOTSPOT = true 
   BUILD_JDK    = true 
   DEBUG_CLASSFILES =  
   DEBUG_BINARIES =  

Hotspot Settings: 
      HOTSPOT_BUILD_JOBS  =  
      HOTSPOT_OUTPUTDIR   = /home/jdkbuild/hotspot/outputdir 
      HOTSPOT_EXPORT_PATH = /home/jdkbuild/hotspot/import 
 
 Bootstrap Settings:
  BOOTDIR = /usr/local/jdk1.6.0_45
    ALT_BOOTDIR = /usr/local/jdk1.6.0_45
  BOOT_VER = 1.6.0 [requires at least 1.6]
  OUTPUTDIR = /home/jdkbuild
    ALT_OUTPUTDIR = /home/jdkbuild
  ABS_OUTPUTDIR = /home/jdkbuild
 
Build Tool Settings:
  SLASH_JAVA = /NOT-SET
    ALT_SLASH_JAVA = 
  VARIANT = DBG
  JDK_DEVTOOLS_DIR = /NOT-SET/devtools
    ALT_JDK_DEVTOOLS_DIR = 
  ANT_HOME = /usr/local/apache-ant-1.9.9
  UNIXCOMMAND_PATH = /bin/
    ALT_UNIXCOMMAND_PATH = 
  COMPILER_PATH = /usr/bin/
    ALT_COMPILER_PATH = 
  DEVTOOLS_PATH = /usr/bin/
    ALT_DEVTOOLS_PATH = 
  UNIXCCS_PATH = /usr/ccs/bin/
    ALT_UNIXCCS_PATH = 
  USRBIN_PATH = /usr/bin/
    ALT_USRBIN_PATH = 
  COMPILER_NAME = GCC4
  COMPILER_VERSION = GCC4
  CC_VER = 4.8.5 [requires at least 4.3.0]
  ZIP_VER = 3.0 [requires at least 2.2]
  UNZIP_VER = 6.00 [requires at least 5.12]
  ANT_VER = 1.9.9 [requires at least 1.7.1]
  TEMPDIR = /home/jdkbuild/tmp
 
Build Directives:
  OPENJDK = true
  USE_HOTSPOT_INTERPRETER_MODE = 
  PEDANTIC = 
  DEV_ONLY = 
  NO_DOCS = 
  NO_IMAGES = 
  TOOLS_ONLY = 
  INSANE = 
  COMPILE_APPROACH = parallel
  PARALLEL_COMPILE_JOBS = 4
    ALT_PARALLEL_COMPILE_JOBS = 4
  FASTDEBUG = 
  COMPILER_WARNINGS_FATAL = false
  COMPILER_WARNING_LEVEL = 
  SHOW_ALL_WARNINGS = 
  INCREMENTAL_BUILD = false
  CC_HIGHEST_OPT = 
  CC_HIGHER_OPT = 
  CC_LOWER_OPT = 
  CXXFLAGS =  -g -fPIC -DCC_NOEX -W -Wall  -Wno-unused -Wno-parentheses -fno-omit-frame-pointer -D_LITTLE_ENDIAN  
  CFLAGS =  -g   -fno-strict-aliasing -fPIC -W -Wall  -Wno-unused -Wno-parentheses -pipe -fno-omit-frame-pointer -D_LITTLE_ENDIAN  
  BOOT_JAVA_CMD = /usr/local/jdk1.6.0_45/bin/java -XX:-PrintVMOptions -XX:+UnlockDiagnosticVMOptions -XX:-LogVMOutput -Xmx512m -Xms512m -XX:PermSize=32m -XX:MaxPermSize=160m
  BOOT_JAVAC_CMD = /usr/local/jdk1.6.0_45/bin/javac  -J-XX:ThreadStackSize=1536 -J-XX:-PrintVMOptions -J-XX:+UnlockDiagnosticVMOptions -J-XX:-LogVMOutput -J-Xmx512m -J-Xms512m -J-XX:PermSize=32m -J-XX:MaxPermSize=160m -encoding ascii -source 6 -target 6 -XDignore.symbol.file=true
  BOOT_JAR_CMD = /usr/local/jdk1.6.0_45/bin/jar
  BOOT_JARSIGNER_CMD = /usr/local/jdk1.6.0_45/bin/jarsigner
  JAVAC_CMD = /NOT-SET/re/jdk/1.7.0/promoted/latest/binaries/linux-amd64/bin/javac  -J-XX:ThreadStackSize=1536 -J-XX:-PrintVMOptions -J-XX:+UnlockDiagnosticVMOptions -J-XX:-LogVMOutput -J-Xmx512m -J-Xms512m -J-XX:PermSize=32m -J-XX:MaxPermSize=160m  -g -source 7 -target 7 -encoding ascii -Xbootclasspath:/home/jdkbuild/classes 
  JAVAH_CMD = /NOT-SET/re/jdk/1.7.0/promoted/latest/binaries/linux-amd64/bin/javah -bootclasspath /home/jdkbuild/classes
  JAVADOC_CMD = /NOT-SET/re/jdk/1.7.0/promoted/latest/binaries/linux-amd64/bin/javadoc -J-XX:-PrintVMOptions -J-XX:+UnlockDiagnosticVMOptions -J-XX:-LogVMOutput -J-Xmx512m -J-Xms512m -J-XX:PermSize=32m -J-XX:MaxPermSize=160m -bootclasspath /home/jdkbuild/classes
 
Build Platform Settings:
  USER = root
  PLATFORM = linux
  ARCH = amd64
  LIBARCH = amd64
  ARCH_FAMILY = amd64
  ARCH_DATA_MODEL = 64
  ARCHPROP = amd64
  ALSA_VERSION = 1.1.1
  OS_VERSION = 3.10.0-514.10.2.el7.x86_64 [requires at least 2.6]
  OS_VARIANT_NAME = RedHat
  OS_VARIANT_VERSION = 
  MB_OF_MEMORY = 3783
 
GNU Make Settings:
  MAKE = make
  MAKE_VER = 3.82 [requires at least 3.81]
  MAKECMDGOALS = sanity
  MAKEFLAGS = w
  SHELL = /bin/sh
 
Target Build Versions:
  JDK_VERSION = 1.7.0
  MILESTONE = internal
  RELEASE = 1.7.0-internal-debug
  FULL_VERSION = 1.7.0-internal-debug-root_2017_06_16_16_58-b00
  BUILD_NUMBER = b00
 
External File/Binary Locations:
  USRJDKINSTANCES_PATH = /opt/java
  BUILD_JDK_IMPORT_PATH = /NOT-SET/re/jdk/1.7.0/promoted/latest/binaries
    ALT_BUILD_JDK_IMPORT_PATH = 
  JDK_IMPORT_PATH = /NOT-SET/re/jdk/1.7.0/promoted/latest/binaries/linux-amd64
    ALT_JDK_IMPORT_PATH = 
  LANGTOOLS_DIST = 
    ALT_LANGTOOLS_DIST = /home/jdkbuild/langtools/dist
  CORBA_DIST = 
    ALT_CORBA_DIST = /home/jdkbuild/corba/dist
  JAXP_DIST = 
    ALT_JAXP_DIST = /home/jdkbuild/jaxp/dist
  JAXWS_DIST = 
    ALT_JAXWS_DIST = /home/jdkbuild/jaxws/dist
  HOTSPOT_DOCS_IMPORT_PATH = /NO_DOCS_DIR
    ALT_HOTSPOT_DOCS_IMPORT_PATH = 
  HOTSPOT_IMPORT_PATH = /home/jdkbuild/hotspot/import
    ALT_HOTSPOT_IMPORT_PATH = /home/jdkbuild/hotspot/import
  HOTSPOT_SERVER_PATH = /home/jdkbuild/hotspot/import/jre/lib/amd64/server
    ALT_HOTSPOT_SERVER_PATH = 
  CACERTS_FILE = ./../src/share/lib/security/cacerts
    ALT_CACERTS_FILE = 
  CUPS_HEADERS_PATH = /usr/include
    ALT_CUPS_HEADERS_PATH = 
 
OpenJDK-specific settings:
  FREETYPE_HEADERS_PATH = /usr/include
    ALT_FREETYPE_HEADERS_PATH = 
  FREETYPE_LIB_PATH = /usr/lib
    ALT_FREETYPE_LIB_PATH = 
 
Previous JDK Settings:
  PREVIOUS_RELEASE_PATH = USING-PREVIOUS_RELEASE_IMAGE
    ALT_PREVIOUS_RELEASE_PATH = 
  PREVIOUS_JDK_VERSION = 1.6.0
    ALT_PREVIOUS_JDK_VERSION = 
  PREVIOUS_JDK_FILE = 
    ALT_PREVIOUS_JDK_FILE = 
  PREVIOUS_JRE_FILE = 
    ALT_PREVIOUS_JRE_FILE = 
  PREVIOUS_RELEASE_IMAGE = /usr/local/jdk1.6.0_45
    ALT_PREVIOUS_RELEASE_IMAGE = 

Sanity check passed.

若输出结果为:Sanity check passed,表示设置正确

3.3 编译前修改源码
3.3.1 日期修改 (所有设计到时间的修改为10年以内的时间)
错误提示:# time is more than 10 years from present
jdk/src/share/classes/java/util/CurrencyData.properties

修改108行
AZ=AZM;2009-12-31-20-00-00;AZN
修改381行
MZ=MZM;2009-06-30-22-00-00;MZN
修改443行
RO=ROL;2009-06-30-21-00-00;RON
修改535行
TR=TRL;2009-12-31-22-00-00;TRY
修改561行
VE=VEB;2009-01-01-04-00-00;VEF
3.3.2 修改虚拟机make文件hotspot/make/linux/Makefile
这个会导致编译jvm失败,报Err 2,不支持的操作系统,原因是不支持当前linux内核版本
hotspot/make/linux/Makefile  234行
SUPPORTED_OS_VERSION = 2.4% 2.5% 2.6% 3%
改为
SUPPORTED_OS_VERSION = 2.4% 2.5% 2.6% 3% 4%
3.3 执行编译
make 2>&1 | tee $ALT_OUTPUTDIR/build.log

说明:当检查过程看到"Sanity check passed",说明检查过程通过,可以输入"make"执行整个OpenJDK编译,(make不加参数,默认make all)

ant 指定java路径 ant编译指定jdk版本_java_11

配置debug版jdk
/root/openjdk/build-debug/j2sdk-image下内容为debug版jdk,将其复制到%JAVA_HOME%下,替换Oracle JDK。
查看jdk版本信息

java -version

ant 指定java路径 ant编译指定jdk版本_java_12

四、编译过程中出现的问题
4.1 /usr/bin/ld: cannot find -lstdc++
Linking vm...
/usr/bin/ld: cannot find -lstdc++
collect2: error: ld returned 1 exit status
/usr/bin/chcon: cannot access 'libjvm.so': No such file or directory
ERROR: Cannot chcon libjvm.so
/usr/bin/objcopy --only-keep-debug libjvm.so libjvm.debuginfo
/usr/bin/objcopy: 'libjvm.so': No such file
make[4]: *** [libjvm.so] Error 1
make[4]: Leaving directory `/usr/src/openjdk/hotspot/build/hotspot_debug/linux_amd64_compiler2/debug'
make[3]: *** [the_vm] Error 2
make[3]: Leaving directory `/usr/src/openjdk/hotspot/build/hotspot_debug/linux_amd64_compiler2/debug'
make[2]: *** [debug] Error 2
make[2]: Leaving directory `/usr/src/openjdk/hotspot/build/hotspot_debug'
make[1]: *** [generic_build2] Error 2
make[1]: Leaving directory `/usr/src/openjdk/hotspot/make'
make: *** [debug] Error 2

解决方法:
yum search libc++
yum install libstdc+±static

4.2 Error: time is more than 10 years from present: 1120165200000
Error: time is more than 10 years from present: 1120165200000
java.lang.RuntimeException: time is more than 10 years from present: 1120165200000
    at build.tools.generatecurrencydata.GenerateCurrencyData.makeSpecialCaseEntry(GenerateCurrencyData.java:285)
    at build.tools.generatecurrencydata.GenerateCurrencyData.buildMainAndSpecialCaseTables(GenerateCurrencyData.java:225)
    at build.tools.generatecurrencydata.GenerateCurrencyData.main(GenerateCurrencyData.java:154)
make[5]: *** [/root/openjdk/build/../build-debug/lib/currency.data] Error 1
make[5]: Leaving directory `/root/Downloads/openjdk/jdk/make/java/java'
make[4]: *** [all] Error 1
make[4]: Leaving directory `/root/Downloads/openjdk/jdk/make/java'
make[3]: *** [all] Error 1
make[3]: Leaving directory `/root/Downloads/openjdk/jdk/make'
make[2]: *** [jdk-build] Error 2
make[2]: Leaving directory `/root/Downloads/openjdk'
make[1]: *** [generic_debug_build] Error 2
make[1]: Leaving directory `/root/Downloads/openjdk'
make: *** [build_debug_image] Error 2

解决方法:修改CurrentDate.properties文件,将所有的时间改为10年以内

五、参考资料

《深入理解Java虚拟机》 第二版 周志明 著
CentOS7下编译openjdkCentOS6.5下编译安装jdk7