windows环境下编译OpenJDK12

14 篇文章 0 订阅
1 篇文章 0 订阅

环境:Windows11

目录:

1、下载OpenJDK12源码

下载地址:

https://hg.openjdk.org/jdk/jdk12

Untitled.png

点击zip下载到本地。

解压到本地。

Tip:注意本地路径中最好不要包含中文或空格。

2、阅读一遍doc/building.html

如果只是想构建JDK,又没耐心看完那么多内容,只要看 TL;DR (Instructions for the Impatient) 部分的内容就可以了 😃

构建步骤主要为以下5步:

1、获取源码(http://hg.openjdk.java.net/jdk/jdk)

2、运行配置(bash configure)

3、运行make(make images)

4、验证是否构建成功(./build/*/images/jdk/bin/java -version)

5、运行基本测试(make run-test-tier1)

3、安装CYGWIN

CYGWIN是一个用于在windows上模拟UNIX或LINUX环境的软件,在其上可使用GNU工具集在Windows上进行嵌入式系统开发。

之所以要下载这个软件,是因为后面编译OpenJDK的时候,需要用GNU Make来执行Makefile文件。

下载地址:
Cygwin

安装教程:

Windows环境运行Linux命令——Cygwin安装_难拳的博客-CSDN博客

4、安装Visual Studio 2022(C++编译器)

Untitled 1.png

官网下载地址:https://visualstudio.microsoft.com/zh-hans/vs/

5、配置依赖

打开控制台,输入 bash ,进入Bourne Again Shell环境:

Untitled 2.png

进入OpenJDK12源码解压后的路径,输入 bash configure:

Untitled 3.png

编译FastDebug版、仅含Server模式的HotSpot虚拟机:

bash configure --enable-debug --with-jvm-variants=server

Tip:如果之前执行过配置命令,需要先清除一下目录:make dist-clean

配置踩坑合集

报错1:

Untitled 4.png

configure: Your path contains Windows tools (C:\Windows\system32) before your unix (cygwin or msys) tools. configure: This will not work. Please correct and make sure /usr/bin (or similar) is first in path. configure: error: Cannot continue configure exiting with result code 1

解决方案1:

bash --login -i 进入shell环境

报错2:

configure: Could not find a valid Boot JDK.
configure: error: Cannot continue
configure: This might be fixed by explicitly setting --with-boot-jdk
configure exiting with result code 1

解决方案2:

下载JDK11:https://www.oracle.com/java/technologies/downloads/#java11

安装完成后,执行:

bash configure --enable-debug --with-jvm-variants=server --with-boot-jdk=/cygdrive/d/jdk11

指定boot jdk路径:-with-boot-jdk=/cygdrive/d/jdk11

报错3:

configure: Cannot locate a valid Visual Studio installation, checking current environment
checking for Visual Studio variables... not found
configure: Cannot locate a valid Visual Studio or Windows SDK installation on disk,
configure: nor is this script run from a Visual Studio command prompt.
configure: Try setting --with-tools-dir to the VC/bin directory within the VS installation
configure: or run "bash.exe -l" from a VS command prompt and then run configure from there.
configure: error: Cannot continue
configure exiting with result code 1

解决方案3:

bash configure --enable-debug --with-jvm-variants=server --with-boot-jdk=/cygdrive/d/jdk11 --with-tools-dir=/cygdrive/d/software/vs2019/VC/Auxiliary/Build

指定:-with-tools-dir=/cygdrive/d/software/vs2019/VC/Auxiliary/Build

同时注释掉OpenJDK12源码中make/autoconf/toolchain_windows.m4文件中的这句:

#VS_ENV_CMD=""

以及make/autoconf/toolchain.m4中的:

#AC_MSG_ERROR([A $TOOLCHAIN_TYPE compiler is required. Try setting --with-tools-dir.])

报错4:

configure: Rewriting OBJDUMP to "/usr/bin/objdump"
configure: error: Target CPU mismatch. We are building for x86_64 but CL is for "□□"; expected "x64".
configure exiting with result code 1

解决方案4:

在make/autoconf/toolchain.m4中找到Target CPU mismatch对应的AC_MSG_ERROR,改成AC_MSG_NOTICE。

报错5:

configure: Rewriting CYGWIN_VC_INSTALL_DIR to "/cygdrive/d/software/vs2019/VC"
ls: cannot access '/cygdrive/d/software/vs2019/VC/Redist/MSVC/*/x64/Microsoft.VC141.CRT/vcruntime140.dll': No such file or directory
configure: Found vcruntime140.dll at /cygdrive/d/jdk11/bin/vcruntime140.dll using well-known location in Boot JDK
checking found vcruntime140.dll architecture... ok
checking for vcruntime140.dll... /cygdrive/d/jdk11/bin/vcruntime140.dll
configure: Rewriting CYGWIN_VC_INSTALL_DIR to "/cygdrive/d/software/vs2019/VC"
ls: cannot access '/cygdrive/d/software/vs2019/VC/Redist/MSVC/*/x64/Microsoft.VC141.CRT/msvcp140.dll': No such file or directory
configure: Found msvcp140.dll at /cygdrive/d/jdk11/bin/msvcp140.dll using well-known location in Boot JDK
checking found msvcp140.dll architecture... ok
checking for msvcp140.dll... /cygdrive/d/jdk11/bin/msvcp140.dll
checking for UCRT DLL dir... configure: Rewriting CYGWIN_WINDOWSSDKDIR to "/cygdrive/c/progra~2/wi3cf2~1/10"
no
configure: error: Could not find any dlls in /cygdrive/c/progra~2/wi3cf2~1/10/Redist/ucrt/DLLs/x64
configure exiting with result code 1

解决方案5:

toolchain_window.m4文件中的

UCRT_DLL_DIR="$CYGWIN_WINDOWSSDKDIR/Redist/ucrt/DLLs/$dll_subdir"

中间加上对应的版本号:

UCRT_DLL_DIR="$CYGWIN_WINDOWSSDKDIR/Redist/10.0.19041.0(版本号,在c/progra~2/wi3cf2~1/10/Redist下)/ucrt/DLLs/$dll_subdir"

再次执行:

bash configure --enable-debug --with-jvm-variants=server --with-boot-jdk=/cygdrive/d/jdk11 --with-tools-dir=/cygdrive/d/software/vs2019/VC/Auxiliary/Build

终于配置成功:

Untitled 5.png

6、编译

先执行make clean清除旧配置

Untitled 6.png

执行make images编译:

编译踩坑合集

报错1:

Untitled 7.png

解决1:

摸进e/jdk12-06222165c35f/build/windows-x86_64-server-fastdebug/make-support/failure-logs的错误日志里看看:

Untitled 8.png

这个test_json.cpp文件里有语法错误,是文件编码的问题,把文件编码改为utf-8-bom格式。

再次执行make clean和make images:

报错2:

Untitled 9.png

解决2:

看到test_json.cpp的357和363等行里都有一个小雪人字符:

Untitled 10.png

把所有雪人字符都替换成普通字母,再次执行make clean和make images:

报错3:

Untitled 11.png

解决3:

找到methodMatcher.cpp,在240行后加上这三句:

Untitled 12.png

    #pragma warning(disable: 4819)
    #pragma warning(disable: 4778)
    #pragma warning(disable: 4474)

报错4:

Untitled 13.png

error C2039: "Do_Not_Use_calloc_Use_safe_Calloc_Instead": 不是 "`global namespace'" 的成员
error C2873: “Do_Not_Use_calloc_Use_safe_Calloc_Instead”: 符号不能用在 using 声明中
error C2039: "Do_Not_Use_malloc_Use_safe_Malloc_Instead": 不是 "`global namespace'" 的成员
error C2873: “Do_Not_Use_malloc_Use_safe_Malloc_Instead”: 符号不能用在 using 声明中
error C2039: "Do_Not_Use_realloc_Use_safe_Realloc_Instead": 不是 "`global namespace'" 的成员
error C2873: “Do_Not_Use_realloc_Use_safe_Realloc_Instead”: 符号不能用在 using 声明中

解决4:

找到src\java.desktop\windows\native\libawt\windows\alloc.h,将文件格式改为utf-8-bom。

并将87行开始的下面这三句注释掉:

#define malloc Do_Not_Use_malloc_Use_safe_Malloc_Instead
#define calloc Do_Not_Use_calloc_Use_safe_Calloc_Instead
#define realloc Do_Not_Use_realloc_Use_safe_Realloc_Instead

报错5:

Building target 'images' in configuration 'windows-x86_64-server-fastdebug'
Warning: No SCM configuration present and no .src-rev
make[3]: *** No rule to make target '/cygdrive/e/jdk12/build/windows-x86_64-server-fastdebug/buildtools/langtools_tools_classes/_the.BUILD_TOOLS_LANGTOOLS.vardeps', needed by '/cygdrive/e/jdk12/build/windows-x86_64-server-fastdebug/buildtools/langtools_tools_classes/_the.BUILD_TOOLS_LANGTOOLS_batch'.  Stop.
make[3]: *** Waiting for unfinished jobs....
make[3]: *** No rule to make target '/cygdrive/e/jdk12/build/windows-x86_64-server-fastdebug/make-support/vardeps/make/ReleaseFile.gmk/create-info-file.vardeps', needed by '/cygdrive/e/jdk12/build/windows-x86_64-server-fastdebug/jdk/release'.  Stop.
make[2]: *** [make/Main.gmk:369: release-file] Error 2
make[2]: *** Waiting for unfinished jobs....
make[2]: *** [make/Main.gmk:73: buildtools-langtools] Error 2
make[3]: *** No rule to make target '/cygdrive/e/jdk12/build/windows-x86_64-server-fastdebug/make-support/vardeps/make/ModuleWrapper.gmk/java.base/ORDERED_CFG_VARIANTS.vardeps', needed by '/cygdrive/e/jdk12/build/windows-x86_64-server-fastdebug/support/modules_libs/java.base/jvm.cfg'.  Stop.
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [make/Main.gmk:162: java.base-copy] Error 2

解决5:

参见 windows 平台编译openjdk12 - 乐途 - 博客园

make -v,检查一下Cygwin里make的版本,我的版本不知道什么时候改成了4.4,在Cygwin里卸载掉就可以了。

解决完所有报错后,执行 make images

编译成功

Untitled 15.png

7、安装IDE:CLion

1)CLion安装教程:

https://www.kdocs.cn/l/cvdh8F64PEfQ?openfrom=docs

2)打开OpenJDK项目:选择Open Project

Untitled 16.png

3)CLion配置Cygwin(Cygwin安装教程见上一章上机实验):

Setting>Toolchains

Untitled 17.png

4)CLion配置编译参数:

Setting>Custom Build Targets

Untitled 18.png

点击Build和Clean右侧的…配置对应的External Tools:

Untitled 19.png

  • CONF=windows-x86_64-server-fastdebug(在jdk编译后的build目录下)

Untitled 20.png

Untitled 21.png

Untitled 22.png

AddConfiguration>Custom Build Application

Untitled 23.png

运行:

Untitled 24.png

正确输出版本号,测试成功:

Untitled 25.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值