Compile your own JDK, OpenJDK, OpenJDK11 on the Mac side

the cover

Original link: Compile your own JDK, OpenJDK, OpenJDK11 on the Mac side | Elvin

foreword

When studying the book "In-depth Understanding of Java Virtual Machine", if you want to deeply understand the underlying code of java and understand the operation mode of jvm, the compilation of jdk is the basic operation; so this article records the process of compiling jdk.

⚠️:此片文章仅关于macOS操作系统


1. Environmental preparation

1. Native system version

macOS Ventura 13.2

2、Boot JDK

Boot JDK refers to the necessary prerequisite for compiling JDK. JDK must be installed on the local machine, and JDK can be compiled only by relying on the local JDK; and the installed version is the compiled version N-1 (in some cases, it can be n-2) version It can be compiled, that is to say, if you want to compile JDK 11, then you need JDK 10 or JDK 11 locally

3. Install Homebrew

1、进入Homebrew的官网:https://brew.sh/
2、复制Install Homebrew标题下的代码:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
3、将该代码粘贴到终端运行即可安装Homebrew

4. Install ccache, freetype dependencies, and autoconf configuration tools

//加速编译工具
brew install ccache
//字体引擎,编译中要依赖的freetype
brew install freetype
//软件源码包的自动配置工具
brew install autoconf

5. Install Xcode

You don’t need to install xcode. What you need to compile JDK is LLVM’s compilation command clang. You only need to install LLVM separately; but installing xcode is the easiest way, and it is also the most suitable for java development, so it is better to install Xcode.

The version I downloaded is: 11.6

method one

Click to enter the official website to download

Method Two

Search and download in the mac app store

⚠️: Installing different versions of xcode may cause different errors! ! ! A too new xcode version may cause various errors when compiling a lower version of jdk; to avoid problems that cannot be solved by yourself and there is no precedent for a solution, the selected xcode version should be higher than xcode8, and the recommended version for compiling jdk documents is 8 and above


2. Obtain OpenJDK source code

1. Use Mercurial to download(不推荐)

OpenJDK source code is managed through Mercurial. It is not recommended because many people say that the download is slow and the network will be disconnected, but I haven't tried it.

具体步骤如下:
  1、安装 Mercurial 版本控制工具
  		brew install mercurial
  2、下载源代码
  		hg clone https://hg.openjdk.java.net/jdk/jdk12

2. Directly enter the URL to download

I downloaded 11, and other versions are optional on the left (currently only 8, 11, and 17 are long-term support versions)

Click to download JDK source code

pic_1

下载后解压到自己选择的文件位置,最好是改个名字,且文件夹名称不要带中文

My own is placed in the newly created JDK_Compilt folder in the personal root directory, and the decompressed file is renamed csm_openjdk11


3. Preparation before compilation

1. View the official compilation documentation

The location of the document is in the downloaded openjdk source code (need to decompress the downloaded jdk file)

pic_2

First look at the instructions in the readme file

Open the building file: select the description of macOS to view the adapted environment or instructions

2. View the environment version

2.1、clang

~ clang --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

2.2、clang++

~ clang++ --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

2.3, make version

~ 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 i386-apple-darwin11.3.0

2.4、freetype

~ freetype-config --version
25.0.19

2.5、ccache

~ ccache --version
ccache version 4.8.1
Features: file-storage http-storage redis+unix-storage redis-storage

Copyright (C) 2002-2007 Andrew Tridgell
Copyright (C) 2009-2023 Joel Rosdahl and other contributors

See <https://ccache.dev/credits.html> for a complete list of contributors.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any later
version.

2.6、JDK

~ java -version
java version "11.0.19" 2023-04-18 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.19+9-LTS-224)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.19+9-LTS-224, mixed mode)

2.7、autoconf

~ autoconf --version
autoconf (GNU Autoconf) 2.71
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+/Autoconf: GNU GPL version 3 or later
<https://gnu.org/licenses/gpl.html>, <https://gnu.org/licenses/exceptions.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David J. MacKenzie and Akim Demaille.

mistake

~ clang -version
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
  
//如果出现以上错误,请执行下面的命令~ xcode-select --install

3. Automatic configuration before compilation

3.1, automatic configuration

//cd 到解压出来的源码位置,执行sh configure命令即可~ cd JDK_Compilt/csm_openjdk11
➜  csm_openjdk11 sh ./configure --with-target-bits=64 --with-freetype=bundled --enable-ccache --with-jvm-variants=server,client --with-boot-jdk-jvmargs="-Xlint:deprecation -Xlint:unchecked" --disable-warnings-as-errors --with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log
  
//参数说明
  --with-target-bits=64 设置32/64编译
  --with-freetype=bundled 指定freetype目录
  --enable-ccache 启用ccache,加快编译
  --with-jvm-variants 设置要构建的JVM的变体,目前可以选择server、client、minimal、core、zero、zeroshark、custom
  --with-boot-jdk-jvmargs 设置运行boot JDK所需要的JVM参数
  --disable-warnings-as-errors	忽略警告,mac端使用xcode编译,官方要求加上这个参数
  --with-debug-level	启用slowdebug级别调试
  2>&1 | tee xxx.log  2>&1错误重定向到标准输出;并将执行打印的信息全部保存到xxx.log文件中

出现如图所示的代码则执行配置成功

pic_3

3.2. Errors

error 1

configure: error: No xcodebuild tool and no system framework headers found, use --with-sysroot or --with-sdk-name to provide a path to a valid SDK

If the above error occurs, it means that xcode has not been installed after downloading. If it is already installed, it still does not work; then execute the following command

sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer/

error 2

--with-freetype error
When you use it –with-freetype=/usr/local/Cellar/freetype/x.x.x/ , you will encounter the following error:

configure: error: Valid values for --with-freetype are ‘system’ and ‘bundled’

Change to:

--with-freetype=bundled


Fourth, the official compilation

Make

//在源码目录下执行make all命令进行全量编译
➜  csm_openjdk11 make all

出现如图所示的代码则执行编译成功

pic_4

mistake

If an error is reported and recompilation is required, execute make cleanthe command to clear the previous compilation legacy before compiling; then execute make allthe command


5. Simply verify the compiled JDK

1. The directory where the JDK is compiled

/Users/csm/JDK_Compilt/csm_openjdk11/build/macosx-x86_64-normal-serverANDclient-slowdebug/jdk

or mirror

/Users/csm/JDK_Compilt/csm_openjdk11/build/macosx-x86_64-normal-serverANDclient-slowdebug/images/jdk

2. Verification

//1、执行cd命令到编译后的jdk目录下的/bin文件夹~ cd /Users/csm/JDK_Compilt/csm_openjdk11/build/macosx-x86_64-normal-serverANDclient-slowdebug/jdk/bin
➜  bin
//或~ cd /Users/csm/JDK_Compilt/csm_openjdk11/build/macosx-x86_64-normal-serverANDclient-slowdebug/images/jdk/bin
➜  bin
  
//2、执行./java -version验证
➜  bin ./java -version
openjdk version "11.0.0.1-internal" 2023-05-09
OpenJDK Runtime Environment (slowdebug build 11.0.0.1-internal+0-adhoc.csm.csmopenjdk11)
OpenJDK 64-Bit Server VM (slowdebug build 11.0.0.1-internal+0-adhoc.csm.csmopenjdk11, mixed mode)
➜  bin
  
//出现上面的结果;至此,编译完成


reference link


Mac compile openjdk source code

Compile JDK by yourself (MacOS)

OpenJDK download and compilation process

MacOS compile OpenJDK13


More knowledge is being continuously updated!!!


statement

The source of the original text is indicated in the reference part, and the source of the original text can be obtained at the reference link of the article

If the content in the article involves the original copyright, please contact [email protected] by email , and the related articles or content will be changed or canceled in time

Guess you like

Origin blog.csdn.net/weixin_42464282/article/details/130991709