Mac下編譯openJDK
最近閱讀《深入理解Java虛擬機:JVM高級特性與最佳實踐》時,按照書上的教程打算編譯一個JDK。由于書中使用的版本是openJdk7u4年代比較久遠,而我的osx是Sierra,真是各種坑。所以在此建議:如果需要編譯openJdk那么盡量選用最新版本的openJdk。盡量使用linux進行編譯,因為一是新版的OSX加入Rootless機制,二十XCODE等一系列基礎組件(因為編譯要用c/c++)與之前的版本環境不太一樣,所以會遇到很多問題。如果確實需要使用OSX編譯那請看如下教程。
準備工作
下載與安裝
- 升級Xcode到最新版,(目前版本8.3.2)
- 安裝XQuartz
- 安裝ant(如果是openJdk7對應最低版本是ant-1.7.1 ant版本也不能太高,因為需要最新JDK的支持) 下載
- 安裝JDK 1.7.0_u4(jdk版本盡量低于openJDK版本,否則坑太多 下載
- 下載cups 下載
- 下載openJDK 下載
執行命令構建環境
- 關閉Rootless
- 重啟 Mac 并按住 Command+R,進入恢復模式
- 打開終端 Terminal
- csrutil disable (重新開啟:csrutil enable)
-
編譯cups
$ tar -zxvf cups-2.1.4-source.tar.gz $ cd cups-2.1.4-source $ ./configure --prefix=/usr/local/cups $ sudo make $ sudo make install
-
添加軟鏈
為了防止openJDK找不不到庫,所以需要加個軟鏈鏈接到他們歷史上曾經存在位置
//ant 必須在關閉**rootless**后執行,否則提示權限不夠 $ sudo ln -s ${your_ant_home}/apache-ant-1.9.7/bin/ant /usr/bin/ant //安裝 Command Line Tools $ xcode-select --install //llvm $ sudo ln -s /usr/bin/llvm-gcc /Applications/Xcode.app/Contents/Developer/usr/bin/llvm-gcc $ sudo ln -s /usr/bin/llvm-g++ /Applications/Xcode.app/Contents/Developer/usr/bin/llvm-g++ //XQuartz $ sudo ln -s /usr/X11/include/X11 /usr/include/X11 $ sudo ln -s /usr/X11/include/freetype2/freetype/ /usr/X11/include/freetype
-
編寫配置文件
$ cd ${youre_openJDK_source_code_HOME}/openJdk/ $ vi buildConfig 在 buildCofig添加如下內容: #to build openJDK # 設定編譯預約 export LANG=C export LC_ALL=C export CC=clang export LFLAGS='-Xlinker -lstdc++' export USE_CLANG=true export LP64=1 export ARCH_DATE_MODEL=64 # 設置為增量編譯,因為問題可能比較多,節約編譯時間 export INCREMENTAL_BUILD=true export COMPILER_WARNINGS_FATAL=false export ALLOW_DOWNLOADS=true export HOTSPOT_BUILD_JOBS=8 export ALT_PARALLEL_COMPILE_JOBS=8 export SKIP_COMPARE_IMAGES=true export USE_PRECOMPILED_HEADER=true export BUILD_LANGTOOLS=true export BUILD_JAXP=true export BUILD_JAXWS=true export BUILD_CORBA=true export BUILD_HOTSPOT=true export BUILD_JDK=true export SKIP_DEBUG_BUILD=true export SKIP_FASTDEBUG_BUILD=false export DEBUG_NAME=debug export SHOW_ALL_WARNINGS=false export BUILD_DEPLOY=false export BUILD_INSTALL=false #三方的庫 export ALT_BOOTDIR=/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home export ALT_CUPS_HEADERS_PATH='${your_cups_home}/cups/include' #這里如果做了軟鏈可以不加 export ANT_HOME=/usr/local/ant-1.7.1 export FREETYPE_LIB_PATH=/usr/X11/lib export FREETYPE_HEADERS_PATH=/usr/X11/include export ALT_FREETYPE_LIB_PATH=/usr/local/Cellar/freetype/2.6.3/lib export ALT_FREETYPE_HEADERS_PATH=/usr/local/Cellar/freetype/2.6.3/include export ALT_OUTPUTDIR=/Users/menzhongxin/Downloads/openjdkBuild export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/ export MILESTONE=internal export BUILD_NUMBER=b25 unset JAVA_HOME unset CLASSPATH export _JAVA_OPTIONS=-Dfile.encoding=ASCII #make sanity make 2>&1 | tee $ALT_OUTPUTDIR/build.log
開始編譯
編譯之前可以先讀一下這篇文章,里邊提供了很多錯誤的解決方法
http://www.voidcn.com/blog/j754379117/article/p-6347581.html
我在編譯時遇到了文中提到的問題 4,5, 7
另外還有一個文中沒有提到的問題如下:
```
Undefined symbols for architecture x86_64:
"_attachCurrentThread", referenced from:
+[ThreadUtilities getJNIEnv] in ThreadUtilities.o
+[ThreadUtilities getJNIEnvUncached] in ThreadUtilities.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
```
解決方法:
```
修改 **jdk/src/macosx/native/sun/osxapp/ThreadUtilities.m ** 文件的38行
將
inline void attachCurrentThread(void** env) {
修改為:
static inline void attachCurrentThread(void** env) {
```
開始編譯
```
$ cd ${your_open_source_code_jdk_home}/
$ source configBuild
```
編譯成功后結果顯示
>>>Finished making images @ Wed Apr 26 22:40:00 CST 2017 ...
########################################################################
##### Leaving jdk for target(s) sanity all docs images #####
########################################################################
##### Build time 00:06:57 jdk for target(s) sanity all docs images #####
########################################################################
#-- Build times ----------
Target debug_build
Start 2017-04-26 22:32:33
End 2017-04-26 22:40:00
00:00:11 corba
00:00:09 hotspot
00:00:02 jaxp
00:00:06 jaxws
00:06:57 jdk
00:00:02 langtools
00:07:27 TOTAL
-------------------------