win10&Cygwin編譯調試OpenJDK11(精簡)


win10&Cygwin編譯調試OpenJDK11(精簡)

博主機器8G,嘗試過虛擬機,編譯成功了,調試實在時卡到爆,迫不得已使用本地編譯

大家也可以嘗試WSL2, 應該比虛擬機性能好些

windos 下編譯jdk 坑較多,如果機器性能較好,建議使用Ubuntu

請嚴格按照下面教程來,特別是各個版本號,不同的版本目錄結構不一樣,比如visual studio,如果自己具備基本的shell編程能力,百度無法解決時,可以自行閱讀相關shell文件

比如:build\.configure-support\generated-configure.sh

編譯

  1. 下載openjdk11 源代碼, 同時安裝jdk10

    OpenJDK 11 源代碼:

    https://github.com/openjdk/jdk,版本:jkd 11.27 切換tag下載, 解壓

    建議先閱讀源碼目錄下doc/building.html編譯文檔, 形成一個良好的學習習慣

    JDK10(需要登錄): 作為編譯引導jdk

    https://download.oracle.com/otn/java/jdk/10.0.2+13/, 安裝后可以不用設置環境變量

  2. 安裝cygwin 及其依賴包

    http://www.cygwin.com/

    下載當前最新版本: 3.3.3

    安裝過程中會提示你設置安裝目錄以及選擇鏡像

    安裝相關依賴項:

    切換 setup-x86_64.exe 文件所在目錄, 輸入命令:

    ./setup-x86_64 -q -P autoconf -P make -P unzip -P zip
    
  3. 下載安裝visual studio 2017

    https://my.visualstudio.com/Downloads?q=visual studio 2017&wt.mc_id=omsftvscom~older-downloads

    下載需要登錄微軟賬號

    我下載的這個版本:

    image-20220105210451890

    安裝目錄不要有空格,否則在configure命令過程中會出現找不到路徑

    啟動后會提示你需要安裝的內容,我這里選擇了兩個, 感覺C++的桌面開發 可以省略

    image-20220107210810362

​ 如果安裝過程中目錄忘記了更改,等待安裝完成后,也可以將Visual studio里面的2017目錄復制一份出來, 由於我之前並沒有更改目錄,因此導致了很多錯誤

image-20220107211226968

​ 完成后查看目錄, 正常情況下面目錄會有這些文件

image-20220107212431184

  1. 配置環境

    • 在開始菜單中找到cygwin64 terminal, 啟動

    • 切換到jdk11 源代碼的目錄

      • cd /cygdriver/e/xxxx/openjdk11 (/cygdriver 后面的路徑為磁盤號/路徑….)
    • 輸入下面命令

    # --with-boot-jdk: 參數表示jdk10 的路徑, (\ )表示空格轉義
    # --with-tools-dir: 指定visual studio C的庫文件位置, 這里使用轉義路徑會出問題
    ./configure --with-target-bits=64 --enable-debug --with-jvm-variants=server --with-boot-jdk=/cygdrive/e/Program\ Files/java/jdk-10.0.2 --with-tools-dir=/cygdrive/e/2017/Community/VC/Auxiliary --disable-warnings-as-errors
    

    成功的話會提示下面日志信息:

    image-20220107212710205

  2. 上面無錯誤,進行編譯

    輸入:

    make images

    這里會報錯,解決見最后

  3. 等待編譯完成即可

    完成后日志如下:

    image-20220107214109420

  4. 驗證JDK

    • 切換目錄:CMD進入即可
    build\windows-x86_64-normal-server-fastdebug\jdk\bin
    
    • 輸入java -version

      有internal 字樣就說明是自己編譯生成的

    image-20220107215012735

調試

可以使用Clion,VS Code,甚至GDB,這里介紹使用Clion 2021.1調試

  1. 使用Clion 打開openjdk的目錄

    image-20220107215241543

  2. 打開后會提示你使用CMAKE、MakeList工程,直接選擇CMAKE 即可

  3. 導入源碼后會自動對文件建立索引

  4. 設置C編譯器、調試器, 一定得使用visual studio 的,不能使用自己下載的TDM,或則MingW,因為我們的jdk是用visual studio 中的庫 編譯的

    image-20220107215540039

    可以看看這里是否正確

    image-20220107215719092

  5. 配置啟動

    image-20220107220301857

  6. 斷點開始Debug

    打兩個斷點:

    程序啟動入口:

    image-20220107220447043

​ 創建線程入口

image-20220107220502373

啟動調試

結果如下:

image-20220107220702633

遇到的問題

configure執行問題

下面所列問題都是由於路徑問題導致

Visual Studio installation not recognized

VS 路徑的問題

image-20220101225842847

image-20220101232244224

查看配置文件,發現路徑是vc/bin/vavars64.bat, 而我們的vavars64.bat 路徑為vc/auxiliary/build/vavars64.bat, 修改即可

image-20220102080319406

image-20220102114907603

'E:/Program' is not recognized as an internal or external command

還是路徑找不到,😥, 空格符的問題,這里可以使用轉義解決, 使用 \ (一個反斜杠+空格) 代替空格

image-20220102133158461

image-20220102133552752

No such file or directory

依然是路徑問題,上面已經轉義為什么還是報錯

image-20220102162812132

在報錯的地方路徑寫死

image-20220102163057574

image-20220102163134352

找不到microsoft compiler , 貌似是之前--with-tools-dir 參數(…/Community/VC/Auxiliary)少填了后面的Auxiliary導致

C compiler cannot create executables

checking whether the C compiler works... no
configure: error: in `/cygdrive/f/opensource/openjdk-jdk-11-27':
configure: error: C compiler cannot create executables

image-20220102184301995

看下config.log 有沒有特別的錯誤信息

image-20220103112818335

路徑問題,/cygdriver/e/program, 嘗試解決一波

查看generated-configure.sh文件,找到相關內容輸出地方

image-20220103112423028

向上翻,找到可疑對象,ac_link_default

image-20220103112520260

這里eval 表示的執行這個變量,在上面可以看到該變量的值為:

$CC $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS

eval意思就是執行這些變量,打印一個信息看看結果,得出下面內容

image-20220103113014156

還是路徑問題,不忍了,更改visual studio 路徑,直接將program files (x86)/microsoft visual studio/2017 移動到根目錄

image-20220103113233821

執行新命令

./configure --with-target-bits=64 --enable-debug --with-jvm-variants=server --with-boot-jdk=/cygdrive/e/Program\ Files/java/jdk-10.0.2 --with-tools-dir=/cygdrive/e/2017/Community/VC/Auxiliary

成功了,浪費這么多時間😭

image-20220103113525689

make執行問題

make images命令執行錯誤后,重新執行命令前,先執行make clean,在執行make images

_the.BUILD_TOOLS_LANGTOOLS_batch

building target 'images' in configuration 'windows-x86_64-normal-server-fastdebug'
make[3]: *** No rule to make target '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/buildtools/langtools_tools_classes/_the.BUILD_TOOLS_LANGTOOLS.vardeps', needed by '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/buildtools/langtools_tools_classes/_the.BUILD_TOOLS_LANGTOOLS_batch'.  Stop.
make[3]: *** Waiting for unfinished jobs....

方案1: 由於默認按照的make為4.3,可以降低make版本為4.2

方案2:修改make/common/MakeBase.gmk, 參考https://blog.csdn.net/qq_50795908/article/details/118827283

如果配置更改錯誤,make images 可能會報下面錯, 重新改下就可以了

make[3]: *** No rule to make target '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/buildtools/interim_langtools_modules/java.compiler.interim/_the.BUILD_java.compiler.interim.vardeps', needed by '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/buildtools/interim_langtools_modules/java.compiler.interim/_the.BUILD_java.compiler.interim_batch'.  Stop.
make[3]: *** Waiting for unfinished jobs....
make[2]: *** [make/Main.gmk:76: interim-langtools] Error 2
make[2]: *** Waiting for unfinished jobs....
make[3]: *** No rule to make target '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/hotspot/variant-server/libjvm/objs/jvm.comp.vardeps', needed by '/cygdrive/f/opensource/openjdk-jdk-11-27/build/windows-x86_64-normal-server-fastdebug/hotspot/variant-server/libjvm/objs/BUILD_LIBJVM_pch.obj'.  Stop.

hotspot/gtest/utilities/test_json.

直接刪除test_json.cpp

* For target hotspot_variant-server_libjvm_gtest_objs_test_json.obj:
test_json.cpp
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(357): error C2143: syntax error: missing ')' before ']'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(355): error C2660: 'JSON_GTest::test': function does not take 1 arguments
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(49): note: see declaration of 'JSON_GTest::test'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(357): error C2143: syntax error: missing ';' before ']'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(357): error C2059: syntax error: ']'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(357): error C2017: illegal escape sequence
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(357): error C2059: syntax error: ')'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(363): error C2143: syntax error: missing ')' before ']'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(361): error C2660: 'JSON_GTest::test': function does not take 1 arguments
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(49): note: see declaration of 'JSON_GTest::test'
f:/opensource/openjdk-jdk-11-27/test/hotspot/gtest/utilities/test_json.cpp(363): error C2143: syntax error: missing ';' before ']'

flags/jvmFlagRangeList

jvmFlagRangeList.cpp
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/runtime/flags/jvmFlagRangeList.cpp(341): error C2220: warning treated as error - no 'object' file generated
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/runtime/flags/jvmFlagRangeList.cpp(341): warning C4305: 'argument': truncation from 'const intx' to 'double'

看到這個字樣:warning treated as error

重新執行configure: 加上--disable-warnings-as-errors參數即可解決

The file contains a character that cannot be represented in the current code page

f:/opensource/openjdk-jdk-11-27/src/hotspot/share/compiler/methodMatcher.cpp(291): warning C4474: 'sscanf' : too many arguments passed for format string
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/compiler/methodMatcher.cpp(291): note: placeholders and their parameters expect 0 variadic arguments, but 2 were provided
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/runtime/objectMonitor.cpp(1): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
f:/opensource/openjdk-jdk-11-27/src/hotspot/share/runtime/objectMonitor.cpp(1063): error C2065: 'Knob_QMode': undeclared identifier

這個問題是由於編碼問題導致,我這里報錯是因為我之前將objectMonitor 添加 了中文注釋導致的,將源文件替換過來即可

參考資料:

https://www.freesion.com/article/755291723/

https://blog.csdn.net/wqlinloveruby/article/details/80409031

https://blog.csdn.net/qq_50795908/article/details/118827283

https://blog.csdn.net/quantum7/article/details/100084314

….


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM