OpenJDK8 Windows編譯 原 薦

前言

Open JDK 是開源的JDK。通過對開源JDK的編譯調試,我們可以深入瞭解JVM的運行原理。

以下是我的環境信息:

操作系統: Microsoft Windows 7 Professional x64 SP1

安裝組件:

1、Windows Unix Toolkit>  CYGWIN x64,放在E:\hub\cygwin64目錄下

2、C/C++ Compiler  > Visual Studio 2010 Professional (x86) - DVD (English),包括WindowsSDK 7.1 X64 

3、Bootstrap JDK > Oracle jdk1.7.0_80 x64,放在E:\sdkUnity\java\jdk1.7.0_80目錄下 

4、freetype ,使用 Visual Studio 2010 編譯 x64 的freetype.dll文件,放在E:\hub\freetype目錄下 

 

一、準備編譯環境

1、安裝CYGWIN

官網:http://www.cygwin.com/ 選擇x64位,即setup-x86_64.exe下載

下載完後,開始安裝

安裝過程選擇鏡像:

接下來是定製需要安裝的工具,在默認的基礎上,我們加裝如下工具:

Binary Name   Category        Package        Description  
================================================================  
ar.exe        Devel            binutils      The GNU assembler, linker and binary utilities  

make.exe      Devel            make          The GNU version of the 'make' utility built for CYGWIN

m4.exe        Interpreters     m4            GNU implementation of the traditional Unix macro processor  

cpio.exe      Utils            cpio          A program to manage archives of files  

zip.exe       Archive          zip           Package and compress (archive) files  

unzip.exe     Archive          unzip         Extract compressed files in a ZIP archive  

free.exe      System           procps        Display amount of free and used memory in the system  
================================================================ 

(注: 此表來自OpenJDK8u的源代碼根目錄中的README-builds.html) 

在輸入框中建議Binary Name不帶 exe搜索,按照建議的Category,對照Description選擇正確的組件。如果沒有,按Package搜索。

以下是當前CYGWIN最新版本的搜索:

安裝完,我們有一個圖標在桌面:

 

2、安裝VS2010

Visual Studio 2010 Professional (x86) - DVD (English)

ed2k://|file|en_visual_studio_2010_professional_x86_dvd_509727.iso|2351316992|D395B6381AC129209EE80D9BED4BA5F1|/

主要需要C++ x64位編譯器模塊,如下圖:

然後安裝

WindowsSDK 7.1 X64(GRMSDKX_EN_DVD.iso)

http://www.microsoft.com/en-us/download/details.aspx?id=8442

https://download.microsoft.com/download/F/1/0/F10113F5-B750-4969-A255-274341AC6BCE/GRMSDKX_EN_DVD.iso

A problem occurred while installing selected Windows SDK components.

 

Installation of the "Microsoft Windows SDK for Windows 7" product has reported the following error: Please refer to Samples\Setup\HTML\ConfigDetails.htm document for further information.

 

Please attempt to resolve the problem and then start Windows SDK setup again. If you continue to have problems with this issue, please visit the SDK team support page at http://go.microsoft.com/fwlink/?LinkId=130245.

 

Click the View Log button to review the installation log.

 

To exit, click Finish.

解決辦法:  卸載Visual C++ 2010 Redistributable

 

3、安裝JDK7

在Oracle官網下載JDK7安裝即可。

 

4、下載並安裝(編譯)feetype

由於OpenJDK中的swing與JConsole需要使用freetype的字體渲染功能,因此需要首先對其進行編譯。

首先到下載freetype:https://www.freetype.org/

我們可以下載已經編譯好的lib和dll直接使用,而不需要自己編譯:

https://github.com/ubawurinna/freetype-windows-binaries

下載完後解壓爲E:\hub\freetype目錄,把win64目錄改爲名稱lib,如下所示:

 

二、獲取OpenJDK源碼

OpenJDK的源碼有兩種下載方式:

Source

Repository 

鏈接:http://download.java.net/openjdk/jdk8/

以上是GA版本,我們這次構建的是包括fix bugs的Update Releases(http://openjdk.java.net/projects/jdk8u/),當然你也可以構建GA版本。

採用Mercurial進行版本管理下載 ,爲了獲取最新版本的源碼,我們需要下載TortoiseHg。

Windows環境下,則可以到bitbucket官方進行下載和安裝:https://tortoisehg.bitbucket.io/download/index.html

安裝完注意配置TortoiseHg的hg.exe所在目錄環境變量到PATH。

在一個目錄(建議爲英文路徑)中右鍵選擇 TortoiseHg>Clone...,克隆OpenJDK源碼:

源填入OpenJDK源碼的路徑:http://hg.openjdk.java.net/jdk8u/jdk8u

點擊“克隆”,等待完成。

源碼下載完成後,我們可以看到OpenJDK的工作臺日誌了。

源碼中有一個README-builds.html 是構建OpenJDK的詳細參考文檔。本編的教程基本都會參考其中內容。

需要運行sh get_source.sh才能獲取完整的代碼。

使用Cygwin64 Terminal(桌面圖標)(注意,Cygwin中硬盤路徑爲/cygdrive/盤符/路徑)

cd  /cygdrive/e/hub/openjdk/jdk8u
bash get_source.sh

等待完成。(本機網絡很不好,我在阿里雲服務器上下載再解壓到本機 o(╯□╰)o)

我的百度雲盤OpenJDK8源碼:鏈接:https://pan.baidu.com/s/1ggEAGp1 密碼:svh1

 

三、編譯OpenJDK版本

1、configure

到目前爲止,我們已經完成了VS2010的安裝、Cygwin及需要用到的工具的安裝、freetype的編譯生成、OpenJDK8的源碼獲取。

在本機,上述各組件的爲止如下:

1)Cygwin安裝到E:\hub\cygwin64,其下有Cygwin.bat腳本用於設置環境,或者你可以使用Cygwin64 Terminal(桌面圖標)

2)freetype庫的路徑爲E:\hub\freetype,其下有include、lib文件夾

3)有Oracle JDK7在 E:\sdkUnity\java\jdk1.7.0_80
4)下載的OpenJDK源碼路徑爲E:\hub\openjdk\jdk8u

 

首先進行配置。開啓一個Cygwin的bash窗口,進入OpenJDK源碼路徑(注意,Cygwin中硬盤路徑爲/cygdrive/盤符/路徑),按我的配置,則爲:

cd  /cygdrive/e/hub/openjdk/jdk8u

開始配置:

bash ./configure --with-freetype=/cygdrive/e/hub/freetype --with-boot-jdk=/cygdrive/e/sdkUnity/java/jdk1.7.0_80 -with-target-bits=64 --with-jvm-variants=server --with-debug-level=release

以上的參數簡單作一些說明: (其他可以參考README-builds.html)

--with-freetype=/cygdrive/e/hub/freetype : 指定feetype的位置,必選;
--with-boot-jdk=/cygdrive/e/sdkUnity/java/jdk1.7.0_80:啓動jdk的路徑,必選;
--with-target-bits=64 :指定生成64位jdk,必選;

--with-jvm-variants=server : 編譯JVM的模式,有server, client, kernel, zero and zeroshark 五種 ,默認server模式;
--with-debug-level=release:編譯時debug的級別,有release, fastdebug(可調試的JDK), slowdebug 三種級別; 

成功。

----------------

接下來,爲可選操作,修改版本信息,在spec.gmk中可以找到相關定義,並根據自身需要修改。

# Different version strings generated from the above information.
JDK_VERSION:=1.8.0_162
RUNTIME_NAME=$(PRODUCT_NAME) $(PRODUCT_SUFFIX)
COOKED_JDK_UPDATE_VERSION:=0
COOKED_BUILD_NUMBER:=0
# These variables need to be generated here so that MILESTONE and
# JDK_BUILD_NUMBER can be overridden on the make command line.
# ifeq ($(MILESTONE), fcs)
# RELEASE=$(JDK_VERSION)$(BUILD_VARIANT_RELEASE)
# else
#  RELEASE=$(JDK_VERSION)-$(MILESTONE)$(BUILD_VARIANT_RELEASE)
# endif
RELEASE=$(JDK_VERSION)$(BUILD_VARIANT_RELEASE)

# ifneq ($(USER_RELEASE_SUFFIX), )
#  FULL_VERSION=$(RELEASE)-$(USER_RELEASE_SUFFIX)-$(JDK_BUILD_NUMBER)
# else
#  FULL_VERSION=$(RELEASE)-$(JDK_BUILD_NUMBER)
# endif
FULL_VERSION=$(RELEASE)-$(JDK_BUILD_NUMBER)
JRE_RELEASE_VERSION:=$(FULL_VERSION)

可能錯誤:cygwin 版本檢測異常

checking cygwin release... 2.10.0(0.325/5/3)

configure: Your cygwin is too old. You are running 2.10.0(0.325/5/3), but at least cygwin 1.7 is required. Please upgrade.

configure: error: Cannot continue

configure exiting with result code 1

如果cygwin安裝的是2.X的版本則在編譯的過程中會報cygwin的版本太老,這是檢測腳本的一個bug,該腳本在common/autoconf目錄下使用文本編輯器打開generated-configure.sh搜索Your cygwin is too old,關注7220行原來是1.7這裏我根據自己的cygwin版本改成不是1.7以下的判斷。

 CYGWIN_VERSION_OLD=`$ECHO $CYGWIN_VERSION | $GREP -e '^1\.0-6'`
    if test "x$CYGWIN_VERSION_OLD" != x; then
      { $as_echo "$as_me:${as_lineno-$LINENO}: Your cygwin is too old. You are running $CYGWIN_VERSION, but at least cygwin 1.7 is required. Please upgrade." >&5
$as_echo "$as_me: Your cygwin is too old. You are running $CYGWIN_VERSION, but at least cygwin 1.7 is required. Please upgrade." >&6;}

 

2、make

執行編譯:

make all

其他,如果只需要JVM客戶端,不需要其他東西,可以:(其他可以參考README-builds.html

make images JOBS=4

其中參數JOBS指定並行構建編譯的線程數,建議爲機器的CPU核數。

如果有多個配置,如:

No CONF given, but more than one configuration found in /cygdrive/e/hub/openjdk/jdk8u//build.

Available configurations:

* windows-x86_64-normal-server-release

* windows-x86_64-normal-server-slowdebug

Please retry building with CONF=<config pattern> (or SPEC=<specfile>)

Makefile:55: *** Cannot continue。 停止。

則執行:

make images CONF=windows-x86_64-normal-server-fastdebug

如下圖:

編譯耗時可能比較長(我的機器30分鐘),請耐心等待。

編譯完的結果保存在openjdk\build\windows-x86_64-normal-server-fastdebug中。

目錄下images/j2sdk-image 爲構建完成的JDK;

目錄下images/j2re-image 爲構建完成的JRE。

測試一下:

java --version

可能錯誤:LNK1123: failure during conversion to COFF

LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt

NMAKE : fatal error U1077: 'C:\progra~2\micros~2.0\vc\bin\amd64\link.exe' : return code '0x463'

Stop.

NMAKE : fatal error U1077: 'cd' : return code '0x2'

Stop.

NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\amd64\nmake.EXE"' : return code '0x2'

Stop.

make[3]: *** [Makefile:217:generic_build2] 錯誤 2

make[2]: *** [Makefile:167:debug] 錯誤 2

make[1]: *** [HotspotWrapper.gmk:45:/cygdrive/e/hub/openjdk/jdk8u/build/windows-x86_64-normal-server-release/hotspot/_hotspot.timestamp] 錯誤 2

make: *** [/cygdrive/e/hub/openjdk/jdk8u//make/Main.gmk:109:hotspot-only] 錯誤 2

參考:

http://stackoverflow.com/questions/10888391/link-fatal-error-lnk1123-failure-during-conversion-to-coff-file-invalid-or-c

因爲是cvtres.exe版本錯誤導致的結果,所以凡是能使VS鏈接器找到正確的cvtres.exe版本的方法都可以解決該問題。或者使VS鏈接器不生成COFF的方法都可以。

解決方法:

  當前系統中存在兩個cvtres.exe文件,版本不同。讓VS2010使用.NET 4.5的cvtres.exe程序。

具體步驟:

重命名cvtres.exe爲cvtres.exe.bak:(vs2010安裝的位置)C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\cvtres.exe

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\cvtres.exe

 

這樣C:\Windows\Microsoft.NET\Framework\v4.0.30319 (.NET 4.5)中的cvtres.exe文件就可以被VS2010使用。

可能錯誤:_the.rt.jar.contents  CreateJars.gmk 格式異常

Updating images/src.zip

make[2]: *** [CreateJars.gmk:268:/cygdrive/e/hub/openjdk/jdk8u/build/windows-x86_64-normal-server-release/images/lib/_the.rt.jar.contents] 錯誤 1

make[2]: *** 正在等待未完成的任務....

make[1]: *** [BuildJdk.gmk:101:images] 錯誤 2

make: *** [/cygdrive/e/hub/openjdk/jdk8u//make/Main.gmk:136:images-only] 錯誤 2

解決方法:

用vi打開/jdk/make目錄下的CreateJars.gmk

cd  /cygdrive/e/hub/openjdk/jdk8u/jdk/make
vi CreateJars.gmk

定位到268行,相距不遠處有兩個$$換行符,將其轉換爲Windows下的換行符。

在VI下,可輸入268gg。

將光標定位到兩個$$之前,按i切換到insert模式後,按Ctrl + V, Ctrl + M,即可打出^M。

完成後按esc退出編輯模式,然後按:進入命令模式,輸入wq保存並退出。

重新生成。 make dist-clean 然後重新make

可能錯誤: make clean

## Starting jdk

”,由“/cygdrive/e/hub/openjdk/jdk8u/build/windows-x86_64-normal-server-release/jdk/objs/libverify/check_code.obj” 需求。 停止。

make[1]: *** [BuildJdk.gmk:70:libs-only] 錯誤 2

make: *** [/cygdrive/e/hub/openjdk/jdk8u//make/Main.gmk:116:jdk-only] 錯誤 2

 

解決方法:重新生成。 make clean 然後重新 make

 

by 斯武丶風晴  https://my.oschina.net/langxSpirit

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章