Windows 11,cygwin + Visual Studio 2017编译OpenJDK12

背景

因为最近在看《深入理解Java虚拟机 JVM高级特性与最佳实践》,书上介绍了jdk编译的方法,所以进行实践。

目标

编译在Windows 11上运行的x64版本OpenJDK 12。

环境准备

系统

我的系统是Windows 11,源码中的build.html中对Windows的说明是高于Windows XP的应该都能构建。

Windows XP is not a supported platform, but all newer Windows should be able to build the JDK.

OpenJDK 12源码

选择以下任一方法,

  1. 使用Mercurial clone源码,
hg clone http://hg.openjdk.java.net/jdk/jdk
  1. 下载源码包。

本文用的是OpenJDK12+33/OpenJDK12-ga,下载的是tar.gz包

md5sum  : 760bdd74283e89940f850beb9897bb78
sha1sum : d04d49b05d3152697321b7f98156172233f63b14

下载完成后,建议对比一下md5和sha1摘要,对比主要是为了检查文件是否完整,如果文件不完整,后面执行构建的时候,可能会提示"No rule to make target"的错误[1]。

Boot JDK

(Open)JDK 11或12

强烈不建议把JDK放到包含空格的路径下,可以省去一些奇奇怪怪的麻烦。

也不建议放在包含中文的路径下,但因为我没有验证过中文路径,所以不确定会不会出现编码相关的问题。

工具链

我使用的是Visual Studio 2017 Community + cygwin 3.3.3-1

同上,强烈不建议把这些程序放到包含空格的路径下,可以省去一些奇奇怪怪的麻烦。

也不建议放在包含中文的路径下,但因为我没有验证过中文路径,所以不确定会不会出现编码相关的问题。

Visual Studio 2017

可以在微软官网下了Visual Studio的安装器,安装Visual Studio 2017 Community。
Windows 11,cygwin + Visual Studio 2017编译OpenJDK12_第1张图片

我看到其他博客提到要装英文版本[2,3],但其实中文版本也是可以的,不过需要修改构建的脚本(我因为很早之前就装了VS,所以就没有装回英文版)。

Cygwin

可以在Cygwin官网下载安装器,安装的时候选择autoconf、make、zip和unzip 4个包。

我所选择的版本是,

autoconf 15-1
make 4.2.1-2
zip 3.0-12
unzip 6.0-17

版本不需要选择太新,因为OpenJDK 12已经停止维护了,太新的工具可能不兼容源码的构建脚本。比如,make建议选择4.2.x版本的,4.3版本的对本文所使用的OpenJDK12源码构建有不兼容[4]

安装完后启动Cygwin,程序自动初始化,

Copying skeleton files.
These files are for the users to personalise their cygwin experience.

They will never be overwritten nor automatically updated.

'./.bashrc' -> '/home/frank//.bashrc'
'./.bash_profile' -> '/home/frank//.bash_profile'
'./.inputrc' -> '/home/frank//.inputrc'
'./.profile' -> '/home/frank//.profile'

执行cygcheck -d -d来查看安装的包。

$ cygcheck -c -d
Cygwin Package Information
Package              Version
_autorebase          001091-1
alternatives         1.3.30c-10
autoconf             15-1
autoconf2.1          2.13-12
autoconf2.5          2.69-5
autoconf2.7          2.71-2
base-cygwin          3.8-1
base-files           4.3-3
bash                 4.4.12-3
bzip2                1.0.8-1
ca-certificates      2021.2.52-1
coreutils            8.26-2
crypto-policies      20190218-1
cygutils             1.4.16-7
cygwin               3.3.3-1
dash                 0.5.11.5-1
diffutils            3.8-1
editrights           1.03-1
file                 5.41-2
findutils            4.8.0-1
gawk                 5.1.1-1
getent               2.18.90-4
grep                 3.7-2
groff                1.22.4-1
gzip                 1.11-1
hostname             3.13-1
info                 6.8-2
ipc-utils            1.0-2
less                 590-1
libargp              20110921-3
libattr1             2.4.48-2
libblkid1            2.33.1-2
libbz2_1             1.0.8-1
libcrypt0            2.1-1
libcrypt2            4.4.20-1
libdb5.3             5.3.28-2
libfdisk1            2.33.1-2
libffi6              3.2.1-2
libgc1               8.0.6-1
libgcc1              11.2.0-1
libgdbm6             1.18.1-1
libgdbm_compat4      1.18.1-1
libgmp10             6.2.1-2
libguile2.0_22       2.0.14-3
libguile2.2_1        2.2.7-1
libiconv2            1.16-2
libintl8             0.21-1
libltdl7             2.4.6-8
liblz4_1             1.7.5-1
liblzma5             5.2.4-1
libmpfr6             4.1.0-2
libncursesw10        6.1-1.20190727
libp11-kit0          0.23.20-1
libpcre1             8.45-1
libpcre2_8_0         10.39-1
libpipeline1         1.5.3-1
libpopt-common       1.18-1
libpopt0             1.18-1
libreadline7         8.1-2
libsigsegv2          2.10-2
libsmartcols1        2.33.1-2
libssl1.1            1.1.1l-2
libstdc++6           11.2.0-1
libtasn1_6           4.14-1
libunistring2        0.9.10-1
libuuid1             2.33.1-2
login                1.13-1
m4                   1.4.19-1
make                 4.2.1-2
man-db               2.9.4-2.1
mintty               3.5.2-1
ncurses              6.1-1.20190727
openssl              1.1.1l-2
p11-kit              0.23.20-1
p11-kit-trust        0.23.20-1
perl                 5.32.1-2
perl_autorebase      5.32.1-2
perl_base            5.32.1-2
rebase               4.5.0-1
run                  1.3.4-2
sed                  4.8-1
tar                  1.34-1
terminfo             6.1-1.20190727
terminfo-extra       6.1-1.20190727
tzcode               2021e-1
tzdata               2021e-1
unzip                6.0-17
util-linux           2.33.1-2
vim-minimal          8.2.3755-1
which                2.20-2
xz                   5.2.4-1
zip                  3.0-12
zlib0                1.2.11-1
zstd                 1.5.1-1

配置

如果是下载tar.gz源码包,则找一个目录解压,

$ tar xzf openjdk12-b67884871b5f.tar.gz
$ ls
jdk-b67884871b5f  openjdk12-b67884871b5f.tar.gz

这个版本的构建脚本有点问题,通过–with-tools-dir来配置Visual Studio的路径不起作用,因为脚本设置的相关变量被清除了[5],所以需要修改jdk-b67884871b5f\make\autoconf\toolchain_windows.m4文件(非官方做法,我只是注释,官方是删掉[6],不过我只是想说明下,实际上没有区别,可以忽略。这个问题是在OpenJDK12+33之后才改的(通过比对时间发现的),但OpenJDK12已经停止维护了,非LTS)。

# 修改前,205行
  VS_ENV_CMD=""

# 修改后,205行
  #VS_ENV_CMD=""

如果安装的Visual Studio是中文的,具体一点,找到cl.exe,然后执行,检查输出的是否包含中文,

$ /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.16.27045 版
版权所有(C) Microsoft Corporation。保留所有权利。

用法: cl [ 选项... ] 文件名... [ /link 链接选项... ]

如果输出包含中文,跟上面代码片段相似,那么就需要修改jdk-b67884871b5f\make\autoconf\toolchain.m4文件(2处),原因是构建脚本判断编译器是否存在,是根据cl.exe等命令的输出做正则判断的,正则表达式匹配的是英文版本,所以上面代码片段的输出无法被默认的表达式识别。

# 修改前,435行
    $ECHO "$COMPILER_VERSION_OUTPUT" | $GREP "Microsoft.*Compiler" > /dev/null

# 修改后,435行
    $ECHO "$COMPILER_VERSION_OUTPUT" | $GREP "Microsoft.*编译器" > /dev/null

# 修改前,1008-1012行
    elif test "x$OPENJDK_TARGET_CPU" = "xx86_64"; then
      if test "x$COMPILER_CPU_TEST" != "xx64"; then
        AC_MSG_ERROR([Target CPU mismatch. We are building for $OPENJDK_TARGET_CPU but CL is for "$COMPILER_CPU_TEST"; expected "x64".])
      fi
    fi

# 修改后,1008-1015行,我没有加上x64的判断,只是让脚本知道本地有编译器,语义有变化
    elif test "x$OPENJDK_TARGET_CPU" = "xx86_64"; then
      if test "x$COMPILER_CPU_TEST" != "xx64"; then
        $ECHO "$CC_VERSION_OUTPUT" | $GREP "Microsoft.*编译器" > /dev/null
        if test $? -ne 0; then
          AC_MSG_ERROR([Target CPU mismatch. We are building for $OPENJDK_TARGET_CPU but CL is for "$COMPILER_CPU_TEST"; expected "x64".])
        fi
      fi
    fi

2022.02.13更新 begin

补充一点,如果要编译32位的版本,则可以不做上面1008-1012行的改动,而是修改稍微上面点的1005-1007行,如下,

# 修改前,1005-1007行
      if test "x$COMPILER_CPU_TEST" != "x80x86" -a "x$COMPILER_CPU_TEST" != "xx86"; then
        AC_MSG_ERROR([Target CPU mismatch. We are building for $OPENJDK_TARGET_CPU but CL is for "$COMPILER_CPU_TEST"; expected "80x86" or "x86".])
      fi

# 修改后,1005-1010行,我没有加上x86的判断,只是让脚本知道本地有编译器,语义有变化
      if test "x$COMPILER_CPU_TEST" != "x80x86" -a "x$COMPILER_CPU_TEST" != "xx86"; then
        $ECHO "$CC_VERSION_OUTPUT" | $GREP "Microsoft.*编译器" > /dev/null
        if test $? -ne 0; then
          AC_MSG_ERROR([Target CPU mismatch. We are building for $OPENJDK_TARGET_CPU but CL is for "$COMPILER_CPU_TEST"; expected "80x86" or "x86".])
        fi
      fi

2022.02.13更新 end

执行下面命令开始配置,

$ ./configure --enable-debug --disable-warnings-as-errors --with-version-pre=frankcrcBuild --with-boot-jdk=/cygdrive/d/Programs/Java/jdk-11.0.11_windows-x64_bin/jdk-11.0.11/ --with-tools-dir=/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Auxiliary/Build/
参数 说明
–enable-debug 表示fastdebug版本,可以调试。— 2022.02.13更新 begin { 如果要编译32位的版本,设置这个参数会有问题[9]。} end —
–disable-warnings-as-errors 表示禁用把警告当做错误的特性,如果不关闭,编译过程中会出错并退出,跟编译器相关。
–with-version-pre 表示自定义版本信息,我这里指定了frankcrcBuild,后面执行java -version的时候,就会显示出openjdk version "12-frankcrcBuild" 2019-03-19
–with-boot-jdk 表示Boot JDk路径,指定到java的上层目录,即bin文件夹所在目录。
–with-tools-dir 表示Visual Studio路径,指定到vcvars64.bat所在目录,因为脚本会检查这个路径的../..../../..,vcvars64.bat所在路径能够匹配第二个情况
2022.02.13更新
–with-target-bits 目标编译jdk地址位数,默认根据机器推断,如果编32位的,就填32

这是我本地执行的结果,执行了15分钟左右,可以参考,

$ ./configure --enable-debug --disable-warnings-as-errors --with-version-pre=frankcrcBuild --with-boot-jdk=/cygdrive/d/Programs/Java/jdk-11.0.11_windows-x64_bin/jdk-11.0.11/ --with-tools-dir=/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Auxiliary/Build/
Runnable configure script is not present
Generating runnable configure script at /cygdrive/f/programs/tmp/jdk-b67884871b5f/build/.configure-support/generated-configure.sh
Using autoconf at /usr/bin/autoconf [autoconf (GNU Autoconf) 2.71]
stdin:222: warning: AC_CHECK_HEADERS($HEADERS_TO_CHECK): you should use literals
/mnt/share/cygpkgs/autoconf2.7/autoconf2.7.noarch/src/autoconf-2.71/lib/autoconf/headers.m4:217: AC_CHECK_HEADERS is expanded from...
/cygdrive/f/programs/tmp/jdk-b67884871b5f/make/autoconf/libraries.m4:93: LIB_SETUP_LIBRARIES is expanded from...
stdin:222: the top level
configure: Configuration created at Sun Jan 23 14:56:04 CST 2022.
checking for basename... /usr/bin/basename
...
checking for cygpath... /usr/bin/cygpath
checking for df... /usr/bin/df
checking for cpio... no
checking for bsdcpio... no
checking for nice... /usr/bin/nice
checking for pandoc... no
checking build system type... x86_64-unknown-cygwin
checking host system type... x86_64-unknown-cygwin
checking target system type... x86_64-unknown-cygwin
checking openjdk-build os-cpu... windows-x86_64
checking openjdk-target os-cpu... windows-x86_64
checking compilation type... native
checking cygwin release... 3.3.3(0.341/5/3)
checking cygwin root directory as unix-style path... /cygdrive/e/Programs/cygwin64
checking what kind of 'find' is first on the PATH... unix style
checking for top-level directory... /cygdrive/f/programs/tmp/jdk-b67884871b5f
checking if custom source is suppressed (openjdk-only)... no
checking which debug level to use... fastdebug
checking which variants of the JVM to build... server
checking for sysroot...
checking for toolchain path... /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Auxiliary/Build/
checking for extra path...
checking where to store configuration... in default location
checking what configuration name to use... windows-x86_64-server-fastdebug
checking for apt-get... no
checking for yum... no
checking for brew... no
checking for port... no
checking for pkgutil... no
checking for pkgadd... no
checking for gmake... no
checking for make... /usr/bin/make
configure: Testing potential make at /usr/bin/make, found using make in PATH
configure: Using GNU make at /usr/bin/make (version: GNU Make 4.2.1)
checking if make --output-sync is supported... yes
checking for output-sync value... none
checking if find supports -delete... yes
checking what type of tar was found... gnu
checking that grep (/usr/bin/grep) -Fx handles empty lines in the pattern list correctly... yes
checking for unzip... /usr/bin/unzip
checking for zip... /usr/bin/zip
checking for ldd... /usr/bin/ldd
checking for greadelf... no
checking for readelf... no
checking for dot... no
checking for hg... no
checking for git... /cygdrive/d/Program Files/Git/cmd/git
checking for stat... /usr/bin/stat
checking for time... no
checking for flock... /usr/bin/flock
checking for dtrace... no
checking for gpatch... no
checking for patch... no
checking bash version... 4.4.12
checking if bash supports pipefail... yes
checking if bash supports errexit (-e)... yes
checking for pkg-config... no
checking for default LOG value...
checking headless only... no
checking for graphviz dot... no, cannot generate full docs
checking for pandoc... no, cannot generate full docs
checking full docs... no, missing dependencies
checking for cacerts file... default
checking for jni library path... default
checking if packaged modules are kept... yes (default)
checking for version string... 12-frankcrcBuild+0-adhoc.frank.jdk-b67884871b5f
configure: Found potential Boot JDK using configure arguments
checking for Boot JDK... /cygdrive/d/Programs/Java/jdk-11.0.11_windows-x64_bin/jdk-11.0.11/
checking Boot JDK version... java version "11.0.11" 2021-04-20 LTS  Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194)  Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode)
checking for java in Boot JDK... ok
checking for javac in Boot JDK... ok
checking for javadoc in Boot JDK... ok
checking for jar in Boot JDK... ok
checking for jarsigner in Boot JDK... ok
checking if Boot JDK is 32 or 64 bits... 64
checking for local Boot JDK Class Data Sharing (CDS)... no, creation failed
checking for Build JDK... yes, will use output dir
configure: Using default toolchain microsoft (Microsoft Visual Studio)
checking for link... /usr/bin/link
checking if the first found link.exe is actually the Cygwin link tool... yes
configure: Found Visual Studio installation at /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Auxiliary/Build//../.. using --with-tools-dir
configure: Warning: None of vc/bin/amd64/vcvars64.bat vc/bin/x86_amd64/vcvarsx86_amd64.bat             vc/auxiliary/build/vcvarsx86_amd64.bat vc/auxiliary/build/vcvars64.bat were found, Visual Studio installation not recognized. Ignoring
configure: Found Visual Studio installation at /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Auxiliary/Build//../../.. using --with-tools-dir
configure: Found Microsoft Visual Studio 2017
configure: Rewriting VS_ENV_CMD to "/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/vc/auxiliary/build/vcvarsx86_amd64.bat"
configure: Trying to extract Visual Studio environment variables
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.43
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86_x64'
configure: Setting extracted environment variables
checking for Visual Studio variables... ok
configure: Rewriting ipath to "/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/ATLMFC/include"
configure: Rewriting ipath to "/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/include"
configure: Rewriting ipath to "/cygdrive/c/progra~2/wi3cf2~1/10/include/100183~1.0/ucrt"
configure: Rewriting ipath to "/cygdrive/c/progra~2/wi3cf2~1/10/include/100183~1.0/shared"
configure: Rewriting ipath to "/cygdrive/c/progra~2/wi3cf2~1/10/include/100183~1.0/um"
configure: Rewriting ipath to "/cygdrive/c/progra~2/wi3cf2~1/10/include/100183~1.0/winrt"
configure: Rewriting ipath to "/cygdrive/c/progra~2/wi3cf2~1/10/include/100183~1.0/cppwinrt"
configure: Rewriting libpath to "/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/ATLMFC/lib/x64"
configure: Rewriting libpath to "/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/lib/x64"
configure: Rewriting libpath to "/cygdrive/c/progra~2/wi3cf2~1/10/lib/100183~1.0/ucrt/x64"
configure: Rewriting libpath to "/cygdrive/c/progra~2/wi3cf2~1/10/lib/100183~1.0/um/x64"
checking for cl... no
checking for cl... /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl
checking resolved symbolic links for CC... no symlink
configure: Using microsoft C compiler version 用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.16.27045 版 [用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.16.27045 版]
checking whether the C compiler works... yes
checking for C compiler default output file name... conftest.exe
checking for suffix of executables... .exe
checking whether we are cross compiling... no
checking for suffix of object files... obj
checking whether the compiler supports GNU C... no
checking whether /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl accepts -g... yes
checking for /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl option to enable C11 features... unsupported
checking for /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl option to enable C99 features... unsupported
checking for /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl option to enable C89 features... unsupported
checking for cl... no
checking for cl... /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl
checking resolved symbolic links for CXX... no symlink
configure: Using microsoft C++ compiler version 用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.16.27045 版 [用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.16.27045 版]
checking whether the compiler supports GNU C++... no
checking whether /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl accepts -g... yes
checking for /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl option to enable C++11 features... none needed
configure: WARNING: You are using microsoft older than 16.00.30319.01. This is not a supported configuration.
checking how to run the C preprocessor... /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl -E
checking how to run the C++ preprocessor... /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl -E
checking for link... link
configure: Rewriting LD to "/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/link"
checking if the found link.exe is actually the Visual Studio linker... yes
configure: Using microsoft linker version 14.16.27045.0 [Microsoft (R) Incremental Linker Version 14.16.27045.0]
checking for lib... lib
configure: Rewriting AR to "/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/lib"
checking for mt... mt
configure: Rewriting MT to "/cygdrive/c/progra~2/wi3cf2~1/10/bin/100183~1.0/x86/mt"
checking for rc... rc
configure: Rewriting RC to "/cygdrive/c/progra~2/wi3cf2~1/10/bin/100183~1.0/x86/rc"
checking for dumpbin... dumpbin
configure: Rewriting DUMPBIN to "/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/dumpbin"
checking for msbuild.exe... msbuild.exe
checking for gobjdump... no
checking for objdump... no
checking for jtreg... no
checking for jtreg test harness... no, not found
checking for jmh (Java Microbenchmark Harness)... no, disabled
checking for jib... no
checking if the C compiler supports "-m64"... yes
checking if the C++ compiler supports "-m64"... yes
checking if both compilers support "-m64"... yes
checking for stdio.h... yes
...
checking for target address size... 64 bits
checking whether byte ordering is bigendian... no
checking if native warnings are errors... no (explicitly set)
checking for library containing clock_gettime... no
checking what type of native debug symbols to use... external
checking for dtrace tool... not found, cannot build dtrace
checking for sys/sdt.h... no
checking if dtrace should be built... no, missing dependencies
checking if Hotspot gtest unit tests should be built... yes
checking if fixpath can be created... yes
checking if fixpath.exe works... yes
configure: Rewriting CYGWIN_VC_INSTALL_DIR to "/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC"
POSSIBLE_MSVC_DLL /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Redist/MSVC/14.16.27012/x64/Microsoft.VC141.CRT/vcruntime140.dll
configure: Found vcruntime140.dll at /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Redist/MSVC/14.16.27012/x64/Microsoft.VC141.CRT/vcruntime140.dll using well-known location in VCINSTALLDIR
checking found vcruntime140.dll architecture... ok
checking for vcruntime140.dll... /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Redist/MSVC/14.16.27012/x64/Microsoft.VC141.CRT/vcruntime140.dll
configure: Rewriting CYGWIN_VC_INSTALL_DIR to "/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC"
POSSIBLE_MSVC_DLL /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Redist/MSVC/14.16.27012/x64/Microsoft.VC141.CRT/msvcp140.dll
configure: Found msvcp140.dll at /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Redist/MSVC/14.16.27012/x64/Microsoft.VC141.CRT/msvcp140.dll using well-known location in VCINSTALLDIR
checking found msvcp140.dll architecture... ok
checking for msvcp140.dll... /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Redist/MSVC/14.16.27012/x64/Microsoft.VC141.CRT/msvcp140.dll
checking for UCRT DLL dir... configure: Rewriting CYGWIN_WINDOWSSDKDIR to "/cygdrive/c/progra~2/wi3cf2~1/10"
/cygdrive/c/progra~2/wi3cf2~1/10/Redist/ucrt/DLLs/x64
Using freetype: bundled
checking for which libjpeg to use... bundled
checking for which giflib to use... bundled
checking for PNG... checking for which libpng to use... bundled
checking for compress in -lz... no
checking for which zlib to use... bundled
checking for which lcms to use... bundled
checking for dlopen in -ldl... no
checking if shenandoah can be built... yes
checking if zgc can be built... no, platform not supported
checking if jvmci module jdk.internal.vm.ci should be built... yes
checking if graal module jdk.internal.vm.compiler should be built... yes
checking if aot should be enabled... yes
checking if cds should be enabled... yes
checking if elliptic curve crypto implementation is present... yes
checking if jtreg failure handler should be built... no, missing jtreg
checking if the CDS classlist generation should be enabled... yes
checking if any translations should be excluded... no
checking if static man pages should be copied... yes
checking if a default CDS archive should be generated... yes
checking for number of cores... 12
checking for memory size... 16271 MB
checking for appropriate number of jobs to run in parallel... 12
checking flags for boot jdk java command ...  -Duser.language=en -Duser.country=US -Xshare:auto
checking flags for boot jdk java command for big workloads...  -Xms64M -Xmx1600M -XX:ThreadStackSize=1536
checking flags for bootcycle boot jdk java command for big workloads... -Xms64M -Xmx1600M -XX:ThreadStackSize=1536
checking flags for boot jdk java command for small workloads...  -XX:+UseSerialGC -Xms32M -Xmx512M -XX:TieredStopAtLevel=1
checking whether to use sjavac... no
checking whether to use javac server... yes
checking If precompiled header is enabled... yes
checking is ccache enabled... no
checking if build directory is on local disk... yes
checking JVM features for JVM variant 'server'... "aot cds cmsgc compiler1 compiler2 epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services shenandoahgc vm-structs"
configure: creating /cygdrive/f/programs/tmp/jdk-b67884871b5f/build/windows-x86_64-server-fastdebug/configure-support/config.status
config.status: creating /cygdrive/f/programs/tmp/jdk-b67884871b5f/build/windows-x86_64-server-fastdebug/spec.gmk
config.status: creating /cygdrive/f/programs/tmp/jdk-b67884871b5f/build/windows-x86_64-server-fastdebug/bootcycle-spec.gmk
config.status: creating /cygdrive/f/programs/tmp/jdk-b67884871b5f/build/windows-x86_64-server-fastdebug/buildjdk-spec.gmk
config.status: creating /cygdrive/f/programs/tmp/jdk-b67884871b5f/build/windows-x86_64-server-fastdebug/compare.sh
config.status: creating /cygdrive/f/programs/tmp/jdk-b67884871b5f/build/windows-x86_64-server-fastdebug/Makefile

====================================================
A new configuration has been successfully created in
/cygdrive/f/programs/tmp/jdk-b67884871b5f/build/windows-x86_64-server-fastdebug
using configure arguments '--enable-debug --disable-warnings-as-errors --with-version-pre=frankcrcBuild --with-boot-jdk=/cygdrive/d/Programs/Java/jdk-11.0.11_windows-x64_bin/jdk-11.0.11/ --with-tools-dir=/cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Auxiliary/Build/'.

Configuration summary:
* Debug level:    fastdebug
* HS debug level: fastdebug
* JVM variants:   server
* JVM features:   server: 'aot cds cmsgc compiler1 compiler2 epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services shenandoahgc vm-structs'
* OpenJDK target: OS: windows, CPU architecture: x86, address length: 64
* Version string: 12-frankcrcBuild+0-adhoc.frank.jdk-b67884871b5f (12-frankcrcBuild)

Tools summary:
* Environment:    cygwin version 3.3.3(0.341/5/3) (root at /cygdrive/e/Programs/cygwin64)
* Boot JDK:       java version "11.0.11" 2021-04-20 LTS  Java(TM) SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194)  Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode)   (at /cygdrive/d/Programs/Java/jdk-11.0.11_windows-x64_bin/jdk-11.0.11/)
* Toolchain:      microsoft (Microsoft Visual Studio 2017)
* C Compiler:     Version 用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.16.27045 版 (at /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl)
* C++ Compiler:   Version 用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.16.27045 版 (at /cygdrive/e/Programs/Microsoft/VisualStudio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/HostX86/x64/cl)

Build performance summary:
* Cores to use:   12
* Memory limit:   16271 MB

The following warnings were produced. Repeated here for convenience:
WARNING: You are using microsoft older than 16.00.30319.01. This is not a supported configuration.

构建

首先要修改一个文件[7,8],jdk-b67884871b5f\test\hotspot\gtest\utilities\test_json.cpp,文件是UTF-8编码,里面带有非ASCII字符集的符号,363-373行,可能是linux和windows的差异(编译器对编码处理的差异),Visual Studio编译器处理这样的文件,需要把UTF-8改成UTF-8 BOM,可以在Visual Studio里面,执行 文件->高级保存选项(如果你的Visual Studio里面找不到这个入口,则是默认没有添加到文件菜单下,可以搜索下怎么把命令加上,在自定菜单功能中),

Windows 11,cygwin + Visual Studio 2017编译OpenJDK12_第2张图片
将文件另存为UTF-8(带签名),
Windows 11,cygwin + Visual Studio 2017编译OpenJDK12_第3张图片
当然,也可以使用其他文本编辑工具,如VSCode,Sublime等,或者把357行和下面几行的奇怪的字符修改为其他的字符。

2022.02.13更新 begin

如果要构建32位的jdk,同时要使用--enalbe-debug参数,即要调试,那么还需要修改jdk12-b67884871b5f\src\java.desktop\share\native\common\awt\debug\debug_trace.h,做如下修改[9],

// 修改前,51行
typedef void (*DTRACE_PRINT_CALLBACK)(const char * file, int line, int argc, const char * fmt, va_list arglist);

// 修改后,51行
typedef void (JNICALL *DTRACE_PRINT_CALLBACK)(const char * file, int line, int argc, const char * fmt, va_list arglist);

但需要注意,做完这个改动,也仅仅是能构建通过,无法正常运行,如java -version都有异常,如果真的想要调试,还得继续分析错误原因,我没有再继续分析,而是关掉了--enable-debug参数。

修改完成后,执行make,等待编译完成,我本地编了4个半小时(我第二次构建(构建32位版本)的时候关掉了360,然后从configure开始到make结束,只花费了不到半小时,所以构建64位版本的时间可能也用不了4小时),

原 begin
执行make,等待编译完成,我本地编了4个半小时,
原 end

2022.02.13更新 end

make

顺利的话,前后能看到这些,

Building target 'default (exploded-image)' in configuration 'windows-x86_64-server-fastdebug'
Warning: No SCM configuration present and no .src-rev
...
Compiling 3009 files for java.base
...
Compiling 117 files for java.compiler
...
Compiling 4 files for BUILD_JIGSAW_TOOLS
Stopping sjavac server
Finished building target 'default (exploded-image)' in configuration 'windows-x86_64-server-fastdebug'

构建完成后的文件在jdk-b67884871b5f\build\windows-x86_64-server-fastdebug\jdk目录下,执行java.exe -version,就可以看到成果了。

$ build/windows-x86_64-server-fastdebug/jdk/bin/java.exe -version
openjdk version "12-frankcrcBuild" 2019-03-19
OpenJDK Runtime Environment (fastdebug build 12-frankcrcBuild+0-adhoc.frank.jdk-b67884871b5f)
OpenJDK 64-Bit Server VM (fastdebug build 12-frankcrcBuild+0-adhoc.frank.jdk-b67884871b5f, mixed mode)

经验总结

  1. 检查源码是否完整。

  2. 建议构建的时候,关闭安全软件,部分命令在执行的时候,安全软件可能会提示拦截,如果人不在屏幕旁边的话,可能一不注意,就被自动拒绝,导致过程中断。如果遇到一些文件复制失败,没有权限的,dll失效的问题,可以排除下安全软件的干扰。

  3. 强烈不建议把Boot JDK、Visual Studio等工具装在带有空格的路径下,比如检查Boot JDK相关特性的时候,jdk-b67884871b5f\make\autoconf\boot-jdk.m4的474行,或者jdk12-b67884871b5f\build\.configure-support\generated-configure.sh的64907-64919行,$JAVA是没有使用双引号引用的,如果路径存在空格,在执行java命令的时候,就会提示命令不存在。又比如在脚本在配置Visual Studio相关环境的时候,jdk-b67884871b5f\make\autoconf\toolchain_windows.m4的373-403行,或者jdk12-b67884871b5f\build\.configure-support\generated-configure.sh的36737-36762行,脚本会取Visutal Studio的脚本执行,执行的命令是没有进行缩写,或unix路径转换的,也没有使用双引号来引用路径,所以执行export命令的时候,变量值就不完整。

  4. configure过程中,把m4文件会被编译成sh脚本,即jdk12-b67884871b5f\build\.configure-support\generated-configure.sh,如果遇到文中没有提到的问题,可以在这个文件中查找相关关键字,做一些修改。

  5. 直接修改generated-configure.sh,重新执行./conigure ...命令,不会重新生成generated-configure.sh,但是修改m4后再执行./configure ...,会重新生成generated-configure.sh,所以修改的时候需要注意,以免generated-configure.sh重新生成导致修改丢失。

  6. make过程中,我遇到了"模块中不允许使用未命名的包"错误,应该是在使用Boot JDK构建新JDK模块的时候出的问题,可以根据提示,找到对应的java文件(我本地提示的是java文件),看看文件是否完整。我遇到的错误是jdk-b67884871b5f\build\windows-x86_64-server-fastdebug\support\gensrc\java.base\java\nio\channels\NonReadableChannelException.java文件不完整导致的,文件是一个构建过程中自动生成的文件,文件头部的注释少了*/结尾,不知道具体的原因。我的解决方法是,把jdk-b67884871b5f\build\windows-x86_64-server-fastdebug\support\gensrc\java.base目录删了,然后再次make,目录和下面的文件会重新生成。

  7. 可以阅读下源码中的jdk12-b67884871b5f\doc\building.html文件。

参考

  • [1] Ubuntu 编译 openJDK 9 失败,tecton的回答
  • [2] windows10编译openjdk详细步骤与经验总结
  • [3] Win10下编译OpenJDK8
  • [4] 编译jdk
  • [5] Visual Studio install found through --with-tools-dir value is discarded
  • [6] jdk-updates/jdk11u-dev: f137acb74e2f
  • [7] OpenJDK 12 + Visual Studio 2019编译过程
  • [8] win10上构建并调试openjdk 11
  • [9] compiling openJdk 11 on windows 7 32bits fail

你可能感兴趣的:(windows,visual,studio,java,OpenJDK12)