Mac下编译openJDK

Mac下编译openJDK

最近阅读《深入理解Java虚拟机:JVM高级特性与最佳实践》时,按照书上的教程打算编译一个JDK。由于书中使用的版本是openJdk7u4年代比较久远,而我的osxSierra,真是各种坑。所以在此建议:如果需要编译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
-------------------------
    

参考文章

  • 第一章 Mac os下编译openJDK 7
  • OS X El Capitan에서 OpenJDK7 빌드하기

你可能感兴趣的:(Mac下编译openJDK)