当前位置:   article > 正文

OpenJDK-编译 (mac)_mac编译openjdk 16

mac编译openjdk 16

一、准备

1、openjdk源码下载

openjdk源码可以到github上去下载,openjdk有一个tag列表 (https://github.com/openjdk/jdk/tags),可以从中找到合适的版本,例如 openjdk 11 : https://github.com/openjdk/jdk/releases/tag/jdk-11+28

2、boot-jdk

openjdk编译时需要使用到jdk,一般是需要编译版本的当前版本或者前一个版本,比如编译openjdk11,就需要openjdk11或者openjdk10 ( boot-jdk最好是使用openjdk,官方文档中介绍说使用其它的jdk可能会出现其它情况)。
openjdk的编译版本可以从adoptium上下载,网址: https://adoptium.net/temurin/releases

3、环境准备

需要安装相关工具

brew install FreeType
brew install Autoconf
brew install ccache
  • 1
  • 2
  • 3

以及gcc和g++,并且要高一些的版本

brew install gcc
  • 1

查看gcc 或g++版本

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc-11
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/11.2.0_3/bin/../libexec/gcc/x86_64-apple-darwin17/11/lto-wrapper
Target: x86_64-apple-darwin17
Configured with: ../configure --prefix=/usr/local/opt/gcc --libdir=/usr/local/opt/gcc/lib/gcc/11 --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran,d --program-suffix=-11 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-zstd=/usr/local/opt/zstd --with-pkgversion='Homebrew GCC 11.2.0_3' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --enable-libphobos --build=x86_64-apple-darwin17 --with-system-zlib --disable-multilib
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (Homebrew GCC 11.2.0_3)

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++-11
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/11.2.0_3/bin/../libexec/gcc/x86_64-apple-darwin17/11/lto-wrapper
Target: x86_64-apple-darwin17
Configured with: ../configure --prefix=/usr/local/opt/gcc --libdir=/usr/local/opt/gcc/lib/gcc/11 --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran,d --program-suffix=-11 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-zstd=/usr/local/opt/zstd --with-pkgversion='Homebrew GCC 11.2.0_3' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --enable-libphobos --build=x86_64-apple-darwin17 --with-system-zlib --disable-multilib
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (Homebrew GCC 11.2.0_3)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

本文档中使用的是11.2.0版本

如果原因已经低版本的gcc工具,要使用新增,需要修改~/.zshrc文件和〜/.bash_profile文件

# 打开文件
open -e ~/.zshrc
# 或
open -e ~/.bash_profile
  • 1
  • 2
  • 3
  • 4

添加如下内容

alias gcc='gcc-11'
alias g++="g++-11"
alias cc="gcc-11"
alias c++="c++-11"
  • 1
  • 2
  • 3
  • 4

然后执行下面的命令使用变更生效

source ~/.zshrc
# 或
source ~/.bash_profile
  • 1
  • 2
  • 3

二、编译

接下来开始编译openjdk,将下载下来的openjdk编译版本和源码解压缩,然后进入openjdk源码的目录,执行命令

$ cd jdk-jdk-11-28

# 移除Java环境变量配置,避免影响openjdk的编译工作
$ unset JAVA_HOME

# --with-boot-jdk jdk路由,需要替换成自己的
# --with-debug-level=slowdebug参数是为了显示更多调试信息.debug level有4种:release, fastdebug, slowdebug, optimized. 默认是 release
# --with-native-debug-symbols hotspot编译之后是一个名为libjvm.so的动态库,在java命令里通过dlopen加载,如果不加上--with-native-debug-symbols=internal,需要手动解压jdk/lib/amd64/server里的libjvm.debuginfo,或者指定动态库路径,调试起来会很复杂.
$ bash ./configure  --with-boot-jdk=/User/saleson/dev_tools/openjdk/jdk-11.0.14.1+1/Contents/Home --with-debug-level=slowdebug --with-native-debug-symbols=internal --with-target-bits=64
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

执行完成后出现如下内容表示执行成功

configure: creating /Users/saleson/Desktop/openjdk/jdk-jdk-11-28/build/macosx-x86_64-normal-server-slowdebug/configure-support/config.status
config.status: creating /Users/saleson/Desktop/openjdk/jdk-jdk-11-28/build/macosx-x86_64-normal-server-slowdebug/spec.gmk
config.status: creating /Users/saleson/Desktop/openjdk/jdk-jdk-11-28/build/macosx-x86_64-normal-server-slowdebug/bootcycle-spec.gmk
config.status: creating /Users/saleson/Desktop/openjdk/jdk-jdk-11-28/build/macosx-x86_64-normal-server-slowdebug/buildjdk-spec.gmk
config.status: creating /Users/saleson/Desktop/openjdk/jdk-jdk-11-28/build/macosx-x86_64-normal-server-slowdebug/compare.sh
config.status: creating /Users/saleson/Desktop/openjdk/jdk-jdk-11-28/build/macosx-x86_64-normal-server-slowdebug/Makefile

====================================================
A new configuration has been successfully created in
/Users/saleson/Desktop/openjdk/jdk-jdk-11-28/build/macosx-x86_64-normal-server-slowdebug
using configure arguments '--with-boot-jdk=/Users/saleson/dev_tools/openjdk/jdk-11.0.14.1+1/Contents/Home --with-debug-level=slowdebug --with-native-debug-symbols=internal --with-target-bits=64'.

Configuration summary:
* Debug level:    slowdebug
* HS debug level: debug
* JVM variants:   server
* JVM features:   server: 'aot cds cmsgc compiler1 compiler2 dtrace epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services vm-structs'
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 11-internal+0-adhoc.saleson.jdk-jdk-11-28 (11-internal)

Tools summary:
* Boot JDK:       openjdk version "11.0.14.1" 2022-02-08 OpenJDK Runtime Environment Temurin-11.0.14.1+1 (build 11.0.14.1+1) OpenJDK 64-Bit Server VM Temurin-11.0.14.1+1 (build 11.0.14.1+1, mixed mode)  (at /Users/saleson/dev_tools/openjdk/jdk-11.0.14.1+1/Contents/Home)
* Toolchain:      clang (clang/LLVM)
* C Compiler:     Version 10.0.0 (at /usr/bin/clang)
* C++ Compiler:   Version 10.0.0 (at /usr/bin/clang++)

Build performance summary:
* Cores to use:   4
* Memory limit:   16384 MB
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

接下来开始执行编译命令

make images
  • 1

然后cpu疯狂运行,开始漫长的等待,直到出现下面的内容,表示编译成功

Creating ......
Creating support/demos/image/jfc/TableExample/TableExample.jar
Creating support/demos/image/jfc/TransparentRuler/TransparentRuler.jar
Creating jdk image
Finished building target 'images' in configuration 'macosx-x86_64-normal-server-slowdebug'
  • 1
  • 2
  • 3
  • 4
  • 5

编译过程中会出现下面这些警告,可以忽略掉

注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: 某些输入文件使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

进行编译好的jdk目录运行一下java -version命令检查是否正常

$ cd build/macosx-x86_64-normal-server-slowdebug/jdk/bin

$ ./java -version
openjdk version "11-internal" 2018-09-25
OpenJDK Runtime Environment (slowdebug build 11-internal+0-adhoc.saleson.jdk-jdk-11-28)
OpenJDK 64-Bit Server VM (slowdebug build 11-internal+0-adhoc.saleson.jdk-jdk-11-28, mixed mode)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
参考

https://openjdk.java.net/groups/build/doc/building.html
https://www.jianshu.com/p/108c984f8872
https://blog.csdn.net/u012667477/article/details/119842942
https://www.jianshu.com/p/6cc741fa7cff
https://www.shangmayuan.com/a/c2a2464dba5e47f0918b2c67.html
https://blog.csdn.net/on_1y/article/details/38761511
https://zhuanlan.zhihu.com/p/398067307

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/582641
推荐阅读
相关标签
  

闽ICP备14008679号