博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己动手编译JDK
阅读量:4053 次
发布时间:2019-05-25

本文共 4678 字,大约阅读时间需要 15 分钟。

源码编译openjdk8

macOS High Sierra 编译openjdk 8

本次编译使用的系统是 macOS High Sierra,版本为 10.13.2。使用的 jdk 是 openjdk 8 。

概述

openjdk 的模块,部分使用 C/C++ 编写实现,部分使用 Java 实现。因此除了需要 C/C++ 相关编译工具外,还需要有一个 JDK (Bootstrap JDK)。编译 openjdk8 时可使用 jdk1.7 作为 Bootstrap JDK 。

我当前系统已经安装了jdk1.7 :

$ java -versionjava version "1.7.0_79"Java(TM) SE Runtime Environment (build 1.7.0_79-b15)Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

安装准备

源码下载

因为代码比较大,国内采用镜像下载:

git clone https://gitee.com/gorden5566/jdk8u.gitcd jdk8u/git checkout --track origin/fixsh ./getModules.sh

安装依赖

  • 安装freetype
brew install freetype

或者进入官网下载dmg安装。

  • 安装xcode

直接从 App Store 中下载安装 或命令行安装 xcode-select --install

  • 安装gcc编译器

不要安装编译器版本高于5的,因为默认启用c++14 导致编译中断

brew install gcc@4.9
  • 链接gcc编译器(4.9版本)
sudo ln -s /usr/local/Cellar/gcc@4.9/4.9.4/bin/gcc-4.9 /usr/bin/gccsudo ln -s /usr/local/Cellar/gcc@4.9/4.9.4/bin/g++-4.9 /usr/bin/g++

如果安装gcc版本和我的不一样,需要自行替换。

  • 添加环境变量(~/.bash_profile)
export LFLAGS='-Xlinker -lstdc++'

添加执行命令生效:

source ~/.bash_profile
  • 源码修改

修改openjdk/hotspot/src/share/vm/opto/loopPredicate.cpp 第775行

assert(rng->Opcode() == Op_LoadRange || _igvn.type(rng)->is_int()->_lo >= 0, "must be");

在is_int()后在添加 ->_lo 。

修改openjdk/jdk/src/macosx/native/sun/osxapp/ThreadUtilities.m 第一个函数

static inline void attachCurrentThread(void** env);

函数名前面添加static 关键字。

开始编译

为了方便我直接指定我当前bootstrap jdk1.7的版本,我的~/.bash_profile :

export ANT_HOME=/Users/Jason/tools/apache-ant-1.10.1export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home# export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Homeexport CLASSPATH=.:${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib:${ANT_HOME}/lib

生成配置

export MACOSX_DEPLOYMENT_TARGET=10.13.2bash ./configure --with-target-bits=64 --enable-ccache --with-boot-jdk-jvmargs="-Xlint:deprecation -Xlint:unchecked"  --disable-zip-debug-info --with-freetype-include=/usr/local/Cellar/freetype/2.9/include/freetype2 --with-freetype-lib=/usr/local/Cellar/freetype/2.9/lib --with-debug-level=slowdebug

其中freetype是前面安装的路径,可以进/usr/local/Cellar目录查看自己对应版本

执行命令后我电脑输出:

====================================================A new configuration has been successfully created in/Users/Jason/openjdk/jdk8u-default/build/macosx-x86_64-normal-server-slowdebugusing configure arguments '--with-target-bits=64 --enable-ccache --with-boot-jdk-jvmargs=-Xlint:deprecation -Xlint:unchecked --disable-zip-debug-info --with-freetype-include=/usr/local/Cellar/freetype/2.9/include/freetype2 --with-freetype-lib=/usr/local/Cellar/freetype/2.9/lib --with-debug-level=slowdebug'.Configuration summary:* Debug level:    slowdebug* JDK variant:    normal* JVM variants:   server* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64Tools summary:* Boot JDK:       java version "1.7.0_79" Java(TM) SE Runtime Environment (build 1.7.0_79-b15) Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)  (at /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home)* C Compiler:      version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 (at /Applications/Xcode.app/Contents/Developer/usr/bin/gcc)* C++ Compiler:    version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 (at /Applications/Xcode.app/Contents/Developer/usr/bin/gcc)Build performance summary:* Cores to use:   4* Memory limit:   16384 MB* ccache status:  installed, but disabled (version older than 3.1.4)WARNING: The result of this configuration has overridden an olderconfiguration. You *should* run 'make clean' to make sure you get aproper build. Failure to do so might result in strange build problems.

生成jdk8

make all COMPILER_WARNINGS_FATAL=false

生成jdk8成功会输出耗时信息:

## Finished docs (build time 00:01:59)----- Build times -------Start 2018-01-25 11:08:51End   2018-01-25 11:22:2300:00:22 corba00:00:28 demos00:01:59 docs00:05:21 hotspot00:00:59 images00:00:14 jaxp00:00:23 jaxws00:02:50 jdk00:00:41 langtools00:00:12 nashorn00:13:32 TOTAL-------------------------Finished building OpenJDK for target 'all'

使用openjdk8

  1. 生成的jdk Home 在源码目录build :
build/macosx-x86_64-normal-server-slowdebug/images/j2sdk-bundle/jdk1.8.0.jdk/Contents/Home

直接在intellij idea 或者 eclipse 中指定上面的Home即可。

  1. 验证jdk版本
$ cd build/macosx-x86_64-normal-server-slowdebug/images/j2sdk-bundle/jdk1.8.0.jdk/Contents/Home$ ./bin/java -version -versionopenjdk version "1.8.0-internal-debug"OpenJDK Runtime Environment (build 1.8.0-internal-debug-jason_2018_01_25_11_07-b00)OpenJDK 64-Bit Server VM (build 25.71-b00-debug, mixed mode)

为什么要编译JDK源码

  1. 已发布jdk版本去除了调试信息和运行时信息,降低内存占用提升运行速度,但是不适合开发者调试jdk代码

  2. 深入jvm细节,自己动手编译为深入学习打基础

转载地址:http://ditci.baihongyu.com/

你可能感兴趣的文章
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>
自然计算时间复杂度杂谈
查看>>
当前主要目标和工作
查看>>
使用 Springboot 对 Kettle 进行调度开发
查看>>
一文看清HBase的使用场景
查看>>
解析zookeeper的工作流程
查看>>