OpenJDK编译(Build OpenJDK)

JDK发布至今,大家的使用方式基本是从官网下载一个版本,然后直接安装(windows)或者解压至指定的目录,配置环境变量(linux),本文这里尝试从开源的OpenJDK自己编译出一个JDK,从而希望能够对其底层的一些能有更多的理解。

本文的所有操作,大部分参考官方最新的指导http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html完成

新版介绍

  • 新版使用简单的“configure && make”即可完成
  • GNU make需要3.8.1版本以上
  • 多核的机器在编译的时候使用时间更短
  • 显著减少了嵌套和递归的编译调用,同时减少子线程的创建,即优化了编译过程
  • 不再支持Windows MKS
  • Windows Visual Studio中的 vsvars*.batvcvars*.bat现在回自动运行
  • 不再依赖ant
  • 不再支持ALT_*的环境变量

构建步骤

获取源码

OpenJDK的源码现在使用的是Mercurial来管理的,如果没有用过,可以参考 新手指引或者查看 手册
以下操作均在Ubuntu上演示
执行hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK

root@ubuntu-blade2:/opt/open-compiler# hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK
The program 'hg' is currently not installed.  You can install it by typing:
apt-get install mercurial
root@ubuntu-blade2:/opt/open-compiler#

此处提示需要安装mercurial,我们执行apt-get install mercurial即可


update-alternatives: using /usr/bin/xterm to provide /usr/bin/x-terminal-emulator (x-terminal-emulator) in auto mode
update-alternatives: using /usr/bin/lxterm to provide /usr/bin/x-terminal-emulator (x-terminal-emulator) in auto mode
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
root@ubuntu-blade2:/opt/open-compiler# hg
Mercurial Distributed SCM

basic commands:

 add           add the specified files on the next commit
 annotate      show changeset information by line for each file
 clone         make a copy of an existing repository
 commit        commit the specified files or all outstanding changes
 diff          diff repository (or selected files)
 export        dump the header and diffs for one or more changesets
 forget        forget the specified files on the next commit
 init          create a new repository in the given directory
 log           show revision history of entire repository or files
 merge         merge working directory with another revision
 pull          pull changes from the specified source
 push          push changes to the specified destination
 remove        remove the specified files on the next commit
 serve         start stand-alone webserver
 status        show changed files in the working directory
 summary       summarize working directory state
 update        update working directory (or switch revisions)

use "hg help" for the full list of commands or "hg -v" for details
root@ubuntu-blade2:/opt/open-compiler#

这里我们看到,hg命令已支持,现在我们再执行hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK

root@ubuntu-blade2:/opt/open-compiler# hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK
requesting all changes
adding changesets
adding manifests
adding file changes
added 942 changesets with 1309 changes to 136 files
updating to branch default
82 files updated, 0 files merged, 0 files removed, 0 files unresolved
root@ubuntu-blade2:/opt/open-compiler#

执行如下命令
cd YourOpenJDK
bash ./get_source.sh

root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK# bash ./get_source.sh
# Repositories:  corba jaxp jaxws langtools jdk hotspot nashorn

                corba:   hg clone http://hg.openjdk.java.net/jdk8/jdk8/corba corba
                 jaxp:   hg clone http://hg.openjdk.java.net/jdk8/jdk8/jaxp jaxp
                 jaxp:   requesting all changes
                corba:   requesting all changes
                 jaxp:   adding changesets
                corba:   adding changesets
Waiting 5 secs before spawning next background command.
...
...
...
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK#

我们可以随时执行bash ./get_source.sh来获取、更新最新的源码

root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK# ll
total 424
drwxr-xr-x 14 root root   4096 Aug 14 14:56 ./
drwxr-xr-x  3 root root   4096 Aug 14 14:54 ../
-rw-r--r--  1 root root   1503 Aug 14 14:54 ASSEMBLY_EXCEPTION
drwxr-xr-x  6 root root   4096 Aug 14 14:54 common/
-rw-r--r--  1 root root   1235 Aug 14 14:54 configure
drwxr-xr-x  6 root root   4096 Aug 14 14:56 corba/
-rw-r--r--  1 root root   1384 Aug 14 14:54 get_source.sh
drwxr-xr-x  4 root root   4096 Aug 14 14:54 .hg/
-rw-r--r--  1 root root     70 Aug 14 14:54 .hgignore
-rw-r--r--  1 root root  12881 Aug 14 14:54 .hgtags
drwxr-xr-x  8 root root   4096 Aug 14 14:58 hotspot/
drwxr-xr-x  7 root root   4096 Aug 14 14:57 jaxp/
drwxr-xr-x  7 root root   4096 Aug 14 14:57 jaxws/
drwxr-xr-x  2 root root   4096 Aug 14 14:54 .jcheck/
drwxr-xr-x  7 root root   4096 Aug 14 15:04 jdk/
drwxr-xr-x  7 root root   4096 Aug 14 14:56 langtools/
-rw-r--r--  1 root root  19263 Aug 14 14:54 LICENSE
drwxr-xr-x  6 root root   4096 Aug 14 14:54 make/
-rw-r--r--  1 root root   6430 Aug 14 14:54 Makefile
drwxr-xr-x 13 root root   4096 Aug 14 14:56 nashorn/
-rw-r--r--  1 root root   1549 Aug 14 14:54 README
-rw-r--r--  1 root root 129333 Aug 14 14:54 README-builds.html
drwxr-xr-x  2 root root   4096 Aug 14 14:54 test/
-rw-r--r--  1 root root 178445 Aug 14 14:54 THIRD_PARTY_README
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK#

构建

环境准备

  • GNU make版本必须大于3.8.1,可以执行make --version来查看make版本
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK# make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for x86_64-pc-linux-gnu
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK#
  • 安装一个启动JDK(Install a Bootstrap JDK
    )。所有OpenJDK的编译均依赖一个称为bootstrap JDK或者boot JDK。注意本例中编译JDK8,我们的使用的boot JDK的版本不能高于8,建议使用JDK7的版本来作为boot JDK,以免引入不必要的JDK8的依赖。

  • 安装必须依赖软件
    sudo apt-get install libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev libcups2-dev libasound2-dev ccache

编译

环境准备好之后,执行bash ./configure,一切顺利的话

====================================================
A new configuration has been successfully created in
/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release
using default settings.

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:       java version "1.7.0_80" Java(TM) SE Runtime Environment (build 1.7.0_80-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)  (at /opt/jdk-7u80-linux-x64)
* C Compiler:     gcc-4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3) version 4.8.4 (at /usr/bin/gcc-4.8)
* C++ Compiler:   g++-4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3) version 4.8.4 (at /usr/bin/g++-4.8)

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

执行make all之前,请检查内存剩余是否大于1G,磁盘剩余空间是否大于5G,请尽量留多一些的空间,此过程会持续较长时间,请耐心等待

## Finished docs (build time 00:02:00)

----- Build times -------
Start 2018-08-14 17:04:51
End   2018-08-14 17:14:19
00:00:26 corba
00:00:07 demos
00:02:00 docs
00:02:34 hotspot
00:00:19 images
00:00:24 jaxp
00:00:30 jaxws
00:02:49 jdk
00:00:00 langtools
00:00:19 nashorn
00:09:28 TOTAL
-------------------------
Finished building OpenJDK for target 'all'
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK#

当看到如上提示的时候,表示已经编译完成

测试

编译出来的jdk和jre在

root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images# ll
total 24020
drwxr-xr-x  9 root root     4096 Aug 14 17:12 ./
drwxr-xr-x 14 root root     4096 Aug 14 19:47 ../
drwxr-xr-x  5 root root     4096 Aug 14 19:47 j2re-image/
drwxr-xr-x  9 root root     4096 Aug 14 19:47 j2sdk-image/
drwxr-xr-x  4 root root     4096 Aug 14 17:12 lib/
-rw-r--r--  1 root root   717434 Aug 14 17:12 sec-bin.zip
drwxr-xr-x  3 root root     4096 Aug 14 17:12 src/
-rw-r--r--  1 root root 23836708 Aug 14 17:12 src.zip
drwxr-xr-x  4 root root     4096 Aug 14 17:12 _strip_jdk/
drwxr-xr-x  4 root root     4096 Aug 14 17:12 _strip_jre/
drwxr-xr-x  3 root root     4096 Aug 14 17:12 symbols/
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images#

分别为j2re-image/j2sdk-image/,简单验证下

root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images/j2sdk-image# bin/java -version
openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-root_2018_08_14_17_01-b00)
OpenJDK 64-Bit Server VM (build 25.0-b70, mixed mode)

至此完成!

后结

看过这个链接http://hg.openjdk.java.net/jdk8/jdk8/raw-file/tip/README-builds.html#make的同学可能会发现,其最后一步Test如下

Testing

When the build is completed, you should see the generated binaries and associated files in the j2sdk-image directory in the output directory. In particular, the build/***/images/j2sdk-image/bin directory should contain executables for the OpenJDK tools and utilities for that configuration. The testing tool jtreg will be needed and can be found at: the jtreg site. The provided regression tests in the repositories can be run with the command:

**cd test && make PRODUCT_HOME=pwd/../build/*/images/j2sdk-image all**

由于一直尝试未执行成功,所以就以简单的验证尝试了下,其一直出这个错


root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/test# make PRODUCT_HOME=`pwd`/../build/*/images/j2sdk-image
make -k -C  ../jdk/test  TEST=jdk_core jdk_core
make[1]: Entering directory `/opt/open-compiler/YourOpenJDK/jdk/test'
echo "Running tests: jdk_core"
Running tests: jdk_core
for each in jdk_core; do \
                make -j 1 TEST_SELECTION=":$each" UNIQUE_DIR=$each jtreg_tests; \
        done
make[2]: Entering directory `/opt/open-compiler/YourOpenJDK/jdk/test'
make[2]: *** No rule to make target `/java/re/jtreg/4.1/promoted/latest/binaries/jtreg', needed by `/java/re/jtreg/4.1/promoted/latest/binaries/jtreg/win32/bin/jtreg'.
make[2]: Target `jtreg_tests' not remade because of errors.
make[2]: Leaving directory `/opt/open-compiler/YourOpenJDK/jdk/test'
make[1]: *** [jdk_core] Error 2
make[1]: Leaving directory `/opt/open-compiler/YourOpenJDK/jdk/test'
make: *** [jdk_core] Error 2
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/test# 

java和jtreg版本如下

root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images# jtreg -version
jtreg, version 4.1 src b02
Installed in /usr/share/java/jtreg.jar
Running on platform version 1.7.0_80 from /opt/jdk-7u80-linux-x64/jre.
Built with 1.6.0_23 on 07/28/2011 12:18 AM.
Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
Use is subject to license terms.
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images# java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
root@ubuntu-blade2:/opt/open-compiler/YourOpenJDK/build/linux-x86_64-normal-server-release/images#

期间一直google了半天,未有解决方案,若哪位小伙伴解决这个问题,可以下面留言,先感谢一下!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,123评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,031评论 2 384
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,723评论 0 345
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,357评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,412评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,760评论 1 289
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,904评论 3 405
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,672评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,118评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,456评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,599评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,264评论 4 328
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,857评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,731评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,956评论 1 264
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,286评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,465评论 2 348

推荐阅读更多精彩内容