JVM之编译OpenJDK

学习JVM时看到书里讲到自己编译OpenJDK。记录一下过程

  • Mac系统版本:High Sierra 10.13.6
  • 源码版本:jdk8u-dev

一、准备源码

我是从官网下载网站的,openJDK源码是用mercurial进行管理的,所以首先使用homebrew安装mercurial

brew install mercurial

安装完成之后,再运行命令克隆jdk源码,之后通过运行脚本get_source.sh获取所有的源代码

hg clone http://hg.openjdk.java.net/jdk8u/jdk8u-dev/
cd jdk8u-dev
sh ./get_source.sh 

在获取源码的过程中可能会遇到一些问题,如下所示。我遇到的问题主要是网络的问题,后来通过切换网络,使用手机热点解决了。

abort: stream ended unexpectedly (got 1596 bytes, expected 10164)

二、设置环境变量

在编译开始之前需要设置一些变量。这里与书中所讲的有一些不一样,只因为我用的是Mac平台,所以编译器之类的可能会有一些不一样。可以将下面的内容拷贝建立一个脚本运行。

这里的环境变量设置采用了博客https://www.jianshu.com/p/d9a1e1072f37中的设置

# 语言选项
export LANG=C
# Mac平台,C编译器不再是GCC,是clang
export CC=clang
# 跳过clang的一些严格的语法检查,不然会将N多的警告作为Error
export COMPILER_WARNINGS_FATAL=false
# 链接时使用的参数
export LFLAGS='-Xlinker -lstdc++'
# 是否使用clang
export USE_CLANG=true
# 使用64位数据模型
export LP64=1
# 告诉编译平台是64位,不然会按32位来编译
export ARCH_DATA_MODEL=64
# 允许自动下载依赖
export ALLOW_DOWNLOADS=true
# 并行编译的线程数,编译时间长,为了不影响其他工作,我选择为2
export HOTSPOT_BUILD_JOBS=2
# 是否跳过与先前版本的比较
export SKIP_COMPARE_IMAGES=true
# 是否使用预编译头文件,加快编译速度
export USE_PRECOMPILED_HEADER=true
# 是否使用增量编译
export INCREMENTAL_BUILD=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
# 避开javaws和浏览器Java插件之类的部分的build
export BUILD_DEPLOY=false
export BUILD_INSTALL=false
unset JAVA_HOME

三、编译

设置好环境变量之后,就可以开始编译了,首先运行configure,这个脚本会调用很多其他的脚本进行配置并检查编译环境。

bash ./configure

运行成功后的显示如下:

A new configuration has been successfully created in
/Users/zhengshuangxi/mercurial/temp/build/macosx-x86_64-normal-server-release
using default settings.

Configuration summary:
* Debug level:    release
* JDK variant:    normal
* JVM variants:   server
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64

Tools summary:
* Boot JDK:       java version "1.8.0_212" Java(TM) SE Runtime Environment (build 1.8.0_212-b10) Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)  (at /Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home)
* Toolchain:      gcc (GNU Compiler Collection)
* C Compiler:     Version Apple LLVM version 10.0.0 (clang-1000.11.45.5) Target: x86_64-apple-darwin17.7.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin (at /usr/bin/clang)
* C++ Compiler:   Version Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 10.0.0 (clang-1000.11.45.5) Target: x86_64-apple-darwin17.7.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin (at /usr/bin/g++)

Build performance summary:
* Cores to use:   2
* Memory limit:   8192 MB

也有可能会遇到一些问题,这里我遇到的问题有:

  • Xcode版本不符合,需要Xcode 4
  • 无法找到gcc编译器

具体解决办法在本文最后提到。如果没有遇到问题就可以进一步编译了。

直接使用make all命令进行编译

make all

编译成功之后显示如下:

## Finished docs (build time 00:02:31)

----- Build times -------
Start 2019-06-21 16:43:05
End   2019-06-21 16:57:48
00:00:27 corba
00:00:55 demos
00:02:31 docs
00:03:43 hotspot
00:01:34 images
00:00:15 jaxp
00:00:24 jaxws
00:04:10 jdk
00:00:31 langtools
00:00:12 nashorn
00:14:43 TOTAL
-------------------------
Finished building OpenJDK for target 'all'

在编译过程中还可能出现一些问题,目前我只遇到这一个问题:

  • 参数错误“-std=gnu++98”,具体解决办法在本文最后提到。

四、运行

编译完成之后,在build文件夹中会生成一个名为macosx-x86_64-normal-server-release的文件夹,这里就是我们编译生成的结果。在这个文件夹中jdk就是OpenJDK编译成功的产物。

我们可以通过如下命令运行虚拟机,查看版本号,如果没什么问题终端会显示如下信息:

cd ./build/macosx-x86_64-normal-server-release/jdk/bin
./java -version
openjdk version "1.8.0-internal"
OpenJDK Runtime Environment (build 1.8.0-internal-zhengshuangxi_2019_06_21_17_25-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00, mixed mode)

运行之后我又遇到一个问题,在运行过程中会报错,提示 libjvm.dylib 动态链接库相关的错误。具体解决在本文最后。

五、遇到的问题及解决办法

5.1、运行configure之后,提示Xcode版本问题

checking for xcodebuild... /usr/bin/xcodebuild
configure: error: Xcode 4 is required to build JDK 8, the version found was 10.1. Use --with-xcode-path to specify the location of Xcode 4 or make Xcode 4 active by using xcode-select.
configure exiting with result code 1

我们只需要将文件 jdk8u-dev/common/autoconf/generated-configure.sh 进行一些修改,找到以下代码并进行注释,代码大约在26780行左右。注释之后就不会提示版本问题了

    # Fail-fast: verify we're building on Xcode 4, we cannot build with Xcode 5 or later
    XCODE_VERSION=`$XCODEBUILD -version | grep '^Xcode ' | sed 's/Xcode //'`
    XC_VERSION_PARTS=( ${XCODE_VERSION//./ } )
    if test ! "${XC_VERSION_PARTS[0]}" = "4"; then
      as_fn_error $? "Xcode 4 is required to build JDK 8, the version found was $XCODE_VERSION. Use --with-xcode-path to specify the location of Xcode 4 or make Xcode 4 active by using xcode-select." "$LINENO" 5
    fi

5.2、gcc编译器找不到

configure: Will use user supplied compiler CC=clang
checking for clang... /usr/bin/clang
checking resolved symbolic links for CC... no symlink
configure: The C compiler (located as /usr/bin/clang) does not seem to be the required gcc compiler.
configure: The result from running with --version was: ""
configure: error: A gcc compiler is required. Try setting --with-tools-dir.
configure exiting with result code 1

解决办法也是在generated-configure.sh文件中注释掉以下的条件代码。注释的地方一共有两处:

# 第一处代码,27936行左右
elif test "x$TOOLCHAIN_TYPE" = xgcc; then # gcc --version output typically looks like # gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1 # Copyright (C) 2013 Free Software Foundation, Inc. # This is free software; see the source for copying conditions. There is NO # warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. COMPILER_VERSION_OUTPUT=`$COMPILER --version 2>&1` # Check that this is likely to be GCC. $ECHO "$COMPILER_VERSION_OUTPUT" | $GREP "Free Software Foundation" > /dev/null # 条件语句注释掉 # if test $? -ne 0; then # { $as_echo "$as_me:${as_lineno-$LINENO}: The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler." >&5 # $as_echo "$as_me: The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler." >&6;} # { $as_echo "$as_me:${as_lineno-$LINENO}: The result from running with --version was: \"$COMPILER_VERSION\"" >&5 # $as_echo "$as_me: The result from running with --version was: \"$COMPILER_VERSION\"" >&6;} # as_fn_error $? "A $TOOLCHAIN_TYPE compiler is required. Try setting --with-tools-dir." "$LINENO" 5 # fi
# 第二处代码,29677行左右
  elif test  "x$TOOLCHAIN_TYPE" = xgcc; then
    # gcc --version output typically looks like
    #     gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
    #     Copyright (C) 2013 Free Software Foundation, Inc.
    #     This is free software; see the source for copying conditions.  There is NO
    #     warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    COMPILER_VERSION_OUTPUT=`$COMPILER --version 2>&1`
    # Check that this is likely to be GCC.
    $ECHO "$COMPILER_VERSION_OUTPUT" | $GREP "Free Software Foundation" > /dev/null
# 条件语句注释掉
#     if test $? -ne 0; then
#       { $as_echo "$as_me:${as_lineno-$LINENO}: The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler." >&5
# $as_echo "$as_me: The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required $TOOLCHAIN_TYPE compiler." >&6;}
#       { $as_echo "$as_me:${as_lineno-$LINENO}: The result from running with --version was: \"$COMPILER_VERSION\"" >&5
# $as_echo "$as_me: The result from running with --version was: \"$COMPILER_VERSION\"" >&6;}
#       as_fn_error $? "A $TOOLCHAIN_TYPE compiler is required. Try setting --with-tools-dir." "$LINENO" 5
#     fi

注释完之后,重新运行configure

5.3、make过程中遇到参数错误“-std=gnu++98”

Making signal interposition lib...
error: invalid argument '-std=gnu++98' not allowed with 'C'
make[6]: *** [libjsig.dylib] Error 1
make[6]: *** Waiting for unfinished jobs....
make[5]: *** [the_vm] Error 2
make[4]: *** [product] Error 2
make[3]: *** [generic_build2] Error 2
make[2]: *** [product] Error 2
make[1]: *** [/Users/zhengshuangxi/mercurial/temp/build/macosx-x86_64-normal-server-release/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2

generated-configure.sh文件中找到参数“-std=gnu++98”,将代码注释如下:

      LDFLAGS_JDK="$LDFLAGS_JDK -Wl,-z,relro"
      LEGACY_EXTRA_LDFLAGS="$LEGACY_EXTRA_LDFLAGS -Wl,-z,relro"
    fi
    此处注释掉
    #CXXSTD_CXXFLAG="-std=gnu++98"

之后重新运行configure,再执行make all

5.4、编译时遇到 lstdc++ 库无法找到

ld: library not found for -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[6]: *** [libsaproc.dylib] Error 1
make[6]: *** Waiting for unfinished jobs....
make[5]: *** [the_vm] Error 2
make[4]: *** [product] Error 2
make[3]: *** [generic_build2] Error 2
make[2]: *** [product] Error 2
make[1]: *** [/Users/zhengshuangxi/mercurial/temp/build/macosx-x86_64-normal-server-release/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2

这个原因是Xcode升级到10以后就没有包含lstdc++库了,解决办法是将/usr/lib中的库文件拷贝到Xcode相应的文件夹中,并设置相关的软连接。

sudo cp /usr/lib/libstdc++.6.0.9.dylib /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/
cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib/
sudo ln -s libstdc++.6.0.9.dylib libstdc++.6.dylib
sudo ln -s libstdc++.6.dylib libstdc++.dylib

然后重新运行make all命令进行编译。

5.5、编译完成之后,运行虚拟机时出现错误

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGILL (0x4) at pc=0x000000010fe801bf, pid=48116, tid=0x0000000000002203
#
# JRE version: OpenJDK Runtime Environment (8.0) (build 1.8.0-internal-zhengshuangxi_2019_06_21_16_42-b00)
# Java VM: OpenJDK 64-Bit Server VM (25.71-b00 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.dylib+0x4801bf]  PerfDataManager::destroy()+0xab
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/zhengshuangxi/mercurial/temp/build/macosx-x86_64-normal-server-release/jdk/bin/hs_err_pid48116.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#

[error occurred during error reporting , id 0x4]

Abort trap: 6

根据这个错误提示,我们可以知道错误发生在动态链接库 libjvm.dylib中,根据提示,错误代码时PerDataManager::destroy()函数中。因此我们需要对这个函数进行修改,即文件 hotspot/src/share/vm/runtime/perfData.cpp,在这个文件中将代码"delete p"注释掉。

问题的解决办法是在博客https://www.jianshu.com/p/34c8a8c37169中看到的。

// 大约在287行
void PerfDataManager::destroy() {

  if (_all == NULL)
    // destroy already called, or initialization never happened
    return;

  for (int index = 0; index < _all->length(); index++) {
    PerfData* p = _all->at(index);
    // 将delete p注释掉
    //delete p;
  }

  delete(_all);
  delete(_sampled);
  delete(_constants);

  _all = NULL;
  _sampled = NULL;
  _constants = NULL;
}

然后重新运行configure以及make all进行编译。之后就不会出现问题了。

<style></style> <style></style> <style></style>

原文地址:https://www.cnblogs.com/zhengshuangxi/p/11063938.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


在有效期内的苹果开发者账号(类型为个人或者公司账号)。还有一种情况,就是你的Apple ID被添加到公司开发者账号团队里面,这样也是可以的,但是需要叫管理员给你开通相应的账号权限,如下截图:这里可能有些同学会问,苹果开发者账号是什么?如何申请?那么可以看看我的上一篇文章:iOS苹果开发者账号(公司账号)申请流程详解能正常编译打包的iOS工程项目(都不能正常编译谈何出包上架
Appuploader官网--IOS ipa上传发布工具,证书制作工具跨平台版,windows,linux,mac系统都可用 (applicationloader.net)第一步:注册苹果开发者账号,访问以下网址,按照提示注册即可,因为不需要支付688认证苹果开发者,所以没什么好讲的。证书部分:主要是通过工具生成.p12证书文件,后面这个证书要导入mac系统。描述文件:这个文件主要包含了证书,公钥,设备信息等,具体可以百度了解详情。第三步:使用xcode打包导出ipa文件,供其他人内测。..........
苹果在9月13号凌晨(北京时间)发布 iOS 16,该系统的设备可能会因为各种原因,导致功能不可用和UI错乱等问题,我们需要做好适配iOS 16。
计算机图形学--OpenGL递归实现光线追踪
Xcode 14打出来的包在低版本系统运行时会崩溃,报错信息是Library not loaded: /usr/lib/swift/libswiftCoreGraphics.dylib,在苹果开发者论坛搜索报错信息,可以看到会闪退的最高版本是iOS12.1(不敢肯定,毕竟我没测过,不过肯定低于iOS 12.4
iOS16手机开启开发者模式 "developer mode disable"Pod工程中的Bundle target签名报错
【计算机图形学】【实验报告】DDA画线算法、Bresenham中点画线算法、多边形填充算法(附代码)
iOS 16 满载全新的个性化功能、更具深度的智能技术,以及更多无缝的沟通与共享方式,让 iPhone 的体验更进一步。13、隐私权限增强,如通过 UIDevice 获取设备名称时,无法获取用户的信息,只能获取设备对应的名称。
3、回到苹果开发者中心,如下图,点击certificates,点蓝色小加号,就可以开始创建证书,创建证书的时候无论测试还是上传app store打包,都要选ios distribution app store and adhoc,不要选apple和develpment类型的证书。2、如下图,点左边的profiles菜单,点击蓝色加号,创建描述文件,创建过程中会要求我们选择描述文件的类型,假如你想发布app到app store,则选择app store,假如你想真机测试,则选择ad hoc类型。
需要:Unity,IOS平台模块,Xcode,IOS_SDk,MAC电脑,Iphone手机
最近下载安装 xcode 并解压安装,遇到一些问题误以为是错误,记录在此。从百度和谷歌上搜了很多帖子,发现并没有靠谱的 xcode 国内的镜像,这里提供一个可以跳转到官网的下载方式。xcode 不同版本的列表,下载时注意不同 macOs 不同的系统和 Xcode 版本的对应关系。如 要求 及其以上版本。https://xcodereleases.com/注意点击后会跳到官方下载地址,需要登录苹果账号。xcode 文件很大,通常要 10G 以上,下载速度很慢。可以考虑使用 Free Downlo
使用苹果登录作为第三方登录
missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun解决方法
Xcode14 正式版编译报错' does not contain bitcode.You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. file 'xxx' for architecture arm64解决方案
那应该和升级Xcode 14有关系。但是官方还没有给出解决方案。应该后续会有兼容的cocoapods 新版本。
项目中需要用到Xcode将C++代码输出的Mac版本的DLL文件即DYLIB文件,并能够使用C#代码调用。Unity与 DLL文件 ☀️| 怎样使用VC++生成一个DLL文件并调用!这次来看一下在Mac使用Xcode生成的方法吧!本文介绍了在Mac中怎样使用 Xcode项目使用C++生成 .dylib文件的方法。相对于在Windows中使用AndroidStudio创建动态链接库的方法来说,使用Xcode创建.dylib文件确实简单了很多。