浏览器打开地址:https://hg.openjdk.java.net/jdk/jdk12/
点击左侧Browse,将显示源码根目录页面:
点击左侧bz2、zip、gz 中任意一个,即可下载压缩好的源码包。
如果下载较慢或者下载失败,可使用百度网盘下载。建议从官网下载,可以获取最新源码。
链接: OpenJDK12源码
提取密码: sbcf
由于JDK源码中大部分代码还是采用JAVA语言实现,所以在编译前需要准备好另一个编译期可用的JDK,官方称此JDK为Bootstrap JDK。假设需要编译的JDK大版本号为N,我们必须准备好另外一个大版本号至少为N-1的JDK。
App Store安装
从App Store上下载的Xcode,默认是不会安装Command Line Tools的,Command Line Tools是在Xcode中的一款工具,可以在命令行中运行C程序。
打开终端,在终端中输入命令
xcode-select --install
安装完成后,在终端中执行 gcc -v 命令查看是否安装成功。
如果显示如下内容,则安装成功。
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
进入Homebrew官网获取最新下载链接;
打开终端,运行拷贝的命令:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
执行过程中会要求输入密码,输入密码回车继续安装;
安装完成后,会出现 Installation successful! 字样,即安装成功。
终端运行如下命令,可查看安装的版本信息:
brew -v
输出示例:
Homebrew 2.2.5
Homebrew/homebrew-core (git revision e6c5f; last commit 2020-02-09)
打开终端,执行如下命令:
brew install ccache
brew install freetype
brew install autoconf
编译参数可通过在JDK源码目录下执行以下命令查询:
bash configure --help
查询结果示例如下:
`configure' configures OpenJDK openjdk to adapt to many kinds of systems.
Usage: /Users/fcc/Project/JDKSource/jdk12-06222165c35f/configure [OPTION]... [VAR=VALUE]...
To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE. See below for descriptions of some of the useful variables.
Defaults for the options are specified in brackets.
Configuration:
-h, --help display this help and exit
--help=short display options specific to this package
--help=recursive display the short help of all the included packages
-V, --version display version information and exit
-q, --quiet, --silent do not print `checking ...' messages
--cache-file=FILE cache test results in FILE [disabled]
-C, --config-cache alias for `--cache-file=config.cache'
-n, --no-create do not create output files
--srcdir=DIR find the sources in DIR [configure dir or `..']
Installation directories:
--prefix=PREFIX install architecture-independent files in PREFIX
[/usr/local]
--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
[PREFIX]
By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc. You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.
For better control, use the options below.
Fine tuning of the installation directories:
--bindir=DIR user executables [EPREFIX/bin]
--sbindir=DIR system admin executables [EPREFIX/sbin]
--libexecdir=DIR program executables [EPREFIX/libexec]
--sysconfdir=DIR read-only single-machine data [PREFIX/etc]
--sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
--localstatedir=DIR modifiable single-machine data [PREFIX/var]
--libdir=DIR object code libraries [EPREFIX/lib]
--includedir=DIR C header files [PREFIX/include]
--oldincludedir=DIR C header files for non-gcc [/usr/include]
--datarootdir=DIR read-only arch.-independent data root [PREFIX/share]
--datadir=DIR read-only architecture-independent data [DATAROOTDIR]
--infodir=DIR info documentation [DATAROOTDIR/info]
--localedir=DIR locale-dependent data [DATAROOTDIR/locale]
--mandir=DIR man documentation [DATAROOTDIR/man]
--docdir=DIR documentation root [DATAROOTDIR/doc/openjdk]
--htmldir=DIR html documentation [DOCDIR]
--dvidir=DIR dvi documentation [DOCDIR]
--pdfdir=DIR pdf documentation [DOCDIR]
--psdir=DIR ps documentation [DOCDIR]
X features:
--x-includes=DIR X include files are in DIR
--x-libraries=DIR X library files are in DIR
System types:
--build=BUILD configure for building on BUILD [guessed]
--host=HOST cross-compile to build programs to run on HOST [BUILD]
--target=TARGET configure for building compilers for TARGET [HOST]
Optional Features:
--disable-option-checking ignore unrecognized --enable/--with options
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--enable-openjdk-only suppress building custom source even if present
[disabled]
--enable-debug set the debug level to fastdebug (shorthand for
--with-debug-level=fastdebug) [disabled]
--enable-headless-only only build headless (no GUI) support [disabled]
--enable-full-docs build complete documentation [enabled if all tools
found]
--disable-unlimited-crypto
Disable unlimited crypto policy [enabled]
--disable-keep-packaged-modules
Do not keep packaged modules in jdk image [enable]
--enable-static-build enable static library build [disabled]
--disable-warnings-as-errors
do not consider native warnings to be an error
[enabled]
--enable-native-coverage
enable native compilation with code coverage
data[disabled]
--enable-asan enable AddressSanitizer if possible [disabled]
--enable-dtrace[=yes/no/auto]
enable dtrace. Default is auto, where dtrace is
enabled if all dependencies are present.
--enable-aot[=yes/no/auto]
enable ahead of time compilation feature. Default is
auto, where aot is enabled if all dependencies are
present.
--enable-cds[=yes/no/auto]
enable class data sharing feature in non-minimal VM.
Default is auto, where cds is enabled if supported
on the platform.
--disable-hotspot-gtest Disables building of the Hotspot unit tests
[enabled]
--enable-libffi-bundling
enable bundling of libffi.so to make the built JDK
runnable on more systems
--enable-jtreg-failure-handler
forces build of the jtreg failure handler to be
enabled, missing dependencies become fatal errors.
Default is auto, where the failure handler is built
if all dependencies are present and otherwise just
disabled.
--disable-generate-classlist
forces enabling or disabling of the generation of a
CDS classlist at build time. Default is to generate
it when either the server or client JVMs are built
and enable-cds is true.
--disable-manpages Set to disable copy of static man pages [enabled]
--disable-cds-archive Set to disable generation of a default CDS archive
in the product image [enabled]
--enable-sjavac use sjavac to do fast incremental compiles
[disabled]
--disable-javac-server disable javac server [enabled]
--enable-icecc enable distribted compilation of native code using
icecc/icecream [disabled]
--disable-precompiled-headers
disable using precompiled headers when compiling C++
[enabled]
--enable-ccache enable using ccache to speed up recompilations
[disabled]
Optional Packages:
--with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
--without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
--with-target-bits build 32-bit or 64-bit binaries (for platforms that
support it), e.g. --with-target-bits=32 [guessed]
--with-debug-level set the debug level (release, fastdebug, slowdebug,
optimized) [release]
--with-jvm-variants JVM variants (separated by commas) to build
(server,client,minimal,core,zero,custom) [server]
--with-devkit use this devkit for compilers, tools and resources
--with-sys-root alias for --with-sysroot for backwards compatability
--with-sysroot use this directory as sysroot
--with-tools-dir alias for --with-toolchain-path for backwards
compatibility
--with-toolchain-path prepend these directories when searching for
toolchain binaries (compilers etc)
--with-extra-path prepend these directories to the default path
--with-sdk-name use the platform SDK of the given name. [macosx]
--with-conf-name use this as the name of the configuration [generated
from important configuration options]
--with-output-sync set make output sync type if supported by make.
[recurse]
--with-default-make-target
set the default make target [exploded-image]
--with-log [default vaue for make LOG argument [warn]]
--with-cacerts-file specify alternative cacerts file
--with-copyright-year Set copyright year value for build [current year]
--with-jni-libpath override default JNI library search path
--with-vendor-name Set vendor name. Among others, used to set the
'java.vendor' and 'java.vm.vendor' system
properties. [not specified]
--with-vendor-url Set the 'java.vendor.url' system property [not
specified]
--with-vendor-bug-url Set the 'java.vendor.url.bug' system property [not
specified]
--with-vendor-vm-bug-url
Sets the bug URL which will be displayed when the VM
crashes [not specified]
--with-version-string Set version string [calculated]
--with-version-pre Set the base part of the version 'PRE' field
(pre-release identifier) ['internal']
--with-version-opt Set version 'OPT' field (build metadata)
[..]
--with-version-build Set version ' BUILD' field (build number) [not
specified]
--with-version-feature Set version 'FEATURE' field (first number) [current
source value]
--with-version-interim Set version 'INTERIM' field (second number) [current
source value]
--with-version-update Set version 'UPDATE' field (third number) [current
source value]
--with-version-patch Set version 'PATCH' field (fourth number) [not
specified]
--with-version-extra1 Set 1st version extra number [not specified]
--with-version-extra2 Set 2nd version extra number [not specified]
--with-version-extra3 Set 3rd version extra number [not specified]
--with-version-date Set version date [current source value]
--with-vendor-version-string
Set vendor version string [not specified]
--with-boot-jdk path to Boot JDK (used to bootstrap build) [probed]
--with-boot-jdk-jvmargs specify additional arguments to be passed to Boot
JDK tools [none]
--with-build-jdk path to JDK of same version as is being built[the
newly built JDK]
--with-import-modules import a set of prebuilt modules either as a zip
file or an exploded directory
--with-toolchain-type the toolchain type (or family) to use, use '--help'
to show possible values [platform dependent]
--with-extra-cflags extra flags to be used when compiling jdk c-files
--with-extra-cxxflags extra flags to be used when compiling jdk c++-files
--with-extra-ldflags extra flags to be used when linking jdk
--with-toolchain-version
the version of the toolchain to look for, use
'--help' to show possible values [platform
dependent]
--with-build-devkit Devkit to use for the build platform toolchain
--with-jtreg Regression Test Harness [probed]
--with-jmh Java Microbenchmark Harness for building the OpenJDK
Microbenchmark Suite
--with-jib Jib dependency management tool [not used]
--with-macosx-version-max
error on use of newer functionality. [macosx]
--with-abi-profile specify ABI profile for ARM builds
(arm-vfp-sflt,arm-vfp-hflt,arm-sflt,
armv5-vfp-sflt,armv6-vfp-hflt,aarch64) [toolchain
dependent]
--with-native-debug-symbols
set the native debug symbol configuration (none,
internal, external, zipped) [varying]
--with-jcov jcov library location
--with-jcov-input-jdk jdk image to instrument
--with-stdc++lib=,,
force linking of the C++ runtime on Linux to either
static or dynamic, default is static with dynamic as
fallback
--with-msvcr-dll path to microsoft C runtime dll (msvcr*.dll)
(Windows only) [probed]
--with-msvcp-dll path to microsoft C++ runtime dll (msvcp*.dll)
(Windows only) [probed]
--with-ucrt-dll-dir path to Microsoft Windows Kit UCRT DLL dir (Windows
only) [probed]
--with-x use the X Window System
--with-cups specify prefix directory for the cups package
(expecting the headers under PATH/include)
--with-cups-include specify directory for the cups include files
--with-fontconfig specify prefix directory for the fontconfig package
(expecting the headers under PATH/include)
--with-fontconfig-include
specify directory for the fontconfig include files
--with-freetype specify whether to use ' system' or 'bundled'
freetype. Other values are errors. The selected
option applies to both build time and run time. The
default behaviour can be platform dependent. If
using 'system' and either the include files or
libraries cannot be located automatically, then
additionally specify both using
--with-freetype-include and --with-freetype-lib.
--with-freetype-include specify directory for the freetype include files
--with-freetype-lib specify directory for the freetype library
--with-alsa specify prefix directory for the alsa package
(expecting the libraries under PATH/lib and the
headers under PATH/include)
--with-alsa-include specify directory for the alsa include files
--with-alsa-lib specify directory for the alsa library
--with-libffi specify prefix directory for the libffi package
(expecting the libraries under PATH/lib and the
headers under PATH/include)
--with-libffi-include specify directory for the libffi include files
--with-libffi-lib specify directory for the libffi library
--with-libjpeg use libjpeg from build system or OpenJDK source
(system, bundled) [bundled]
--with-giflib use giflib from build system or OpenJDK source
(system, bundled) [bundled]
--with-libpng use libpng from build system or OpenJDK source
(system, bundled) [bundled]
--with-zlib use zlib from build system or OpenJDK source
(system, bundled) [bundled]
--with-lcms use lcms2 from build system or OpenJDK source
(system, bundled) [bundled]
--with-graalunit-lib specify location of 3rd party libraries used by
Graal unit tests
--with-jvm-features JVM features to enable (foo) or disable (-foo),
separated by comma. Use '--help' to show possible
values [none]
--with-with-cpu-port Deprecated. Option is kept for backwards
compatibility and is ignored
--with-exclude-translations
a comma separated list of locales to exclude
translations for. Default is to include all
translations present in the source.
--with-num-cores number of cores in the build system, e.g.
--with-num-cores=8 [probed]
--with-memory-size memory (in MB) available in the build system, e.g.
--with-memory-size=1024 [probed]
--with-jobs number of parallel jobs to let make run [calculated
based on cores and memory]
--with-test-jobs number of parallel tests jobs to run [based on build
jobs]
--with-sjavac-server-java
use this java binary for running the sjavac
background server [Boot JDK java]
--with-ccache-dir where to store ccache files [~/.ccache]
Some influential environment variables:
BASENAME Override default value for BASENAME
BASH Override default value for BASH
CAT Override default value for CAT
CHMOD Override default value for CHMOD
CMP Override default value for CMP
COMM Override default value for COMM
CP Override default value for CP
CUT Override default value for CUT
DATE Override default value for DATE
DIFF Override default value for DIFF
DIRNAME Override default value for DIRNAME
ECHO Override default value for ECHO
EXPR Override default value for EXPR
FILE Override default value for FILE
FIND Override default value for FIND
HEAD Override default value for HEAD
GUNZIP Override default value for GUNZIP
GZIP Override default value for GZIP
LN Override default value for LN
LS Override default value for LS
MKDIR Override default value for MKDIR
MKTEMP Override default value for MKTEMP
MV Override default value for MV
NAWK Override default value for NAWK
PRINTF Override default value for PRINTF
READLINK Override default value for READLINK
RM Override default value for RM
RMDIR Override default value for RMDIR
SH Override default value for SH
SORT Override default value for SORT
TAIL Override default value for TAIL
TAR Override default value for TAR
TEE Override default value for TEE
TOUCH Override default value for TOUCH
TR Override default value for TR
UNAME Override default value for UNAME
UNIQ Override default value for UNIQ
WC Override default value for WC
WHICH Override default value for WHICH
XARGS Override default value for XARGS
AWK Override default value for AWK
GREP Override default value for GREP
EGREP Override default value for EGREP
FGREP Override default value for FGREP
SED Override default value for SED
CYGPATH Override default value for CYGPATH
DF Override default value for DF
CPIO Override default value for CPIO
NICE Override default value for NICE
PANDOC Override default value for PANDOC
MAKE Override default value for MAKE
UNZIP Override default value for UNZIP
ZIPEXE Override default value for ZIPEXE
LDD Override default value for LDD
READELF Override default value for READELF
DOT Override default value for DOT
HG Override default value for HG
GIT Override default value for GIT
STAT Override default value for STAT
TIME Override default value for TIME
FLOCK Override default value for FLOCK
DTRACE Override default value for DTRACE
PATCH Override default value for PATCH
DSYMUTIL Override default value for DSYMUTIL
MIG Override default value for MIG
XATTR Override default value for XATTR
CODESIGN Override default value for CODESIGN
SETFILE Override default value for SETFILE
ELFEDIT Override default value for ELFEDIT
PKG_CONFIG path to pkg-config utility
JAVA Override default value for JAVA
JAVAC Override default value for JAVAC
JAVADOC Override default value for JAVADOC
JAR Override default value for JAR
JARSIGNER Override default value for JARSIGNER
CC C compiler command
CFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L if you have libraries in a
nonstandard directory
LIBS libraries to pass to the linker, e.g. -l
CPPFLAGS (Objective) C/C++ preprocessor flags, e.g. -I if
you have headers in a nonstandard directory
CXX C++ compiler command
CXXFLAGS C++ compiler flags
CPP C preprocessor
CXXCPP C++ preprocessor
AS Override default value for AS
AR Override default value for AR
LIPO Override default value for LIPO
OTOOL Override default value for OTOOL
INSTALL_NAME_TOOL
Override default value for INSTALL_NAME_TOOL
STRIP Override default value for STRIP
NM Override default value for NM
GNM Override default value for GNM
OBJCOPY Override default value for OBJCOPY
OBJDUMP Override default value for OBJDUMP
CXXFILT Override default value for CXXFILT
BUILD_CC Override default value for BUILD_CC
BUILD_CXX Override default value for BUILD_CXX
BUILD_NM Override default value for BUILD_NM
BUILD_AR Override default value for BUILD_AR
BUILD_OBJCOPY
Override default value for BUILD_OBJCOPY
BUILD_STRIP Override default value for BUILD_STRIP
JTREGEXE Override default value for JTREGEXE
XMKMF Path to xmkmf, Makefile generator for X Window System
FREETYPE_CFLAGS
C compiler flags for FREETYPE, overriding pkg-config
FREETYPE_LIBS
linker flags for FREETYPE, overriding pkg-config
ALSA_CFLAGS C compiler flags for ALSA, overriding pkg-config
ALSA_LIBS linker flags for ALSA, overriding pkg-config
LIBFFI_CFLAGS
C compiler flags for LIBFFI, overriding pkg-config
LIBFFI_LIBS linker flags for LIBFFI, overriding pkg-config
PNG_CFLAGS C compiler flags for PNG, overriding pkg-config
PNG_LIBS linker flags for PNG, overriding pkg-config
LCMS_CFLAGS C compiler flags for LCMS, overriding pkg-config
LCMS_LIBS linker flags for LCMS, overriding pkg-config
ICECC_CMD Override default value for ICECC_CMD
ICECC_CREATE_ENV
Override default value for ICECC_CREATE_ENV
ICECC_WRAPPER
Override default value for ICECC_WRAPPER
CCACHE Override default value for CCACHE
Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.
Report bugs to <[email protected]>.
OpenJDK home page: <http://openjdk.java.net>.
Additional (non-autoconf) OpenJDK Options:
--openjdk-target=TARGET cross-compile with TARGET as target platform
(i.e. the one you will run the resulting binary on).
Equivalent to --host=TARGET --target=TARGET
--build=<current platform>
--debug-configure Run the configure script with additional debug
logging enabled.
The following toolchains are available as arguments to --with-toolchain-type.
Which are valid to use depends on the build platform.
gcc GNU Compiler Collection
clang clang/LLVM
solstudio Oracle Solaris Studio
xlc IBM XL C/C++
microsoft Microsoft Visual Studio
The following JVM features are available as arguments to --with-jvm-features.
Which are valid to use depends on the target platform.
compiler1 compiler2 zero minimal dtrace jvmti jvmci graal vm-structs jni-check services management cmsgc epsilongc g1gc parallelgc serialgc shenandoahgc zgc nmt cds static-build link-time-opt aot jfr
Please be aware that, when cross-compiling, the OpenJDK configure script will
generally use 'target' where autoconf traditionally uses 'host'.
Also note that variables must be passed on the command line. Variables in the
environment will generally be ignored, unlike traditional autoconf scripts.
--with-debug-level=
:设置编译的级别,可选值为release、fastdebug、slowdebug、越往后进行的优化措施就越少,带的调试信息就越多。默认值为release。--enable-debug
:等效与--with-debug-level=fastdebug
。--with-jvm-variants=[]
:编译特定模式(Variants)的HotSpot虚拟机,可以多个模式并存,可选值有server、client、minimal、core、zero、custom、。如编译FastDebug版、仅含Server模式 的HorSpot虚拟机,步骤如下:
执行如下命令,对编译过程中的依赖项、参数、输出目录进行检查和构建,如果编译过程中需要的工具链或者依赖项有缺失,根据命令行提示进行安装。
bash configure --enable-debug --with-jvm-variants=server
如果环境一切正常,会提示如下信息:
Configuration summary:
* Debug level: fastdebug
* HS debug level: fastdebug
* 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 shenandoahgc vm-structs'
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 12-internal+0-adhoc.fcc.jdk12-06222165c35f (12-internal)
Tools summary:
* Boot JDK: java version "12.0.2" 2019-07-16 Java(TM) SE Runtime Environment (build 12.0.2+10) Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing) (at /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home)
* Toolchain: clang (clang/LLVM from Xcode 11.5)
* C Compiler: Version 11.0.3 (at /usr/bin/clang)
* C++ Compiler: Version 11.0.3 (at /usr/bin/clang++)
Build performance summary:
* Cores to use: 12
* Memory limit: 16384 MB
操作成功之后,执行编译命令:
make images
注 意 : 上 述 步 骤 如 果 出 现 异 常 或 者 后 , 再 次 执 行 前 需 要 先 执 行 命 令 清 理 . / b u l i d 编 译 目 录 : \color{red}{注意:上述步骤如果出现异常或者后,再次执行前需要先执行命令清理./bulid编译目录:} 注意:上述步骤如果出现异常或者后,再次执行前需要先执行命令清理./bulid编译目录:
make clean
make dist-clean
正常编译完成后,可在命令行看到成功命令输出:
Finished building target 'images' in configuration 'macosx-x86_64-server-fastdebug'
至此,OpenJDK12编译完成。
命令行移动当前位置至jdk12-06222165c35f/build/macosx-x86_64-server-fastdebug/jdk
目录下,执行./bin/java -version
,可查询编译的JDK版本信息。此目录可打包直接作为完整的JDK进行使用。
openjdk version "12-internal" 2019-03-19
OpenJDK Runtime Environment (fastdebug build 12-internal+0-adhoc.fcc.jdk12-06222165c35f)
OpenJDK 64-Bit Server VM (fastdebug build 12-internal+0-adhoc.fcc.jdk12-06222165c35f, mixed mode)
在笔者编译的过程中,出现过如下的错误:
ERROR: Build failed for target 'images' in configuration 'macosx-x86_64-client-fastdebug' (exit code 2)
Stopping sjavac server
=== Output from failing command(s) repeated here ===
* For target hotspot_variant-client_libjvm_gtest_objs_test_symbolTable.o:
/Users/fcc/Project/JDKSource/jdk12-06222165c35f/test/hotspot/gtest/classfile/test_symbolTable.cpp:62:6: error: explicitly assigning value of variable of type 'TempNewSymbol' to itself [-Werror,-Wself-assign-overloaded]
s1 = s1; // self assignment
~~ ^ ~~
1 error generated.
* All command lines available in /Users/fcc/Project/JDKSource/jdk12-06222165c35f/build/macosx-x86_64-client-fastdebug/make-support/failure-logs.
=== End of repeated output ===
No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: See doc/building.html#troubleshooting for assistance.
make[1]: *** [main] Error 2
make: *** [images] Error 2
根据日志提示找到了dk12-06222165c35f/test/hotspot/gtest/classfile
目录下的test_symbolTable.cpp
文件,跳转第62行,内容如下:
此行我直接将其注释掉了。
保存之后执行清理命令,重新编译。