Deepin编译JDK8
<p>编译JDK步骤大致是这样的:</p><ol>
<li>下载源码</li>
<li>configure</li>
<li>make</li>
</ol>
<p>看起来很简单,但是实践的时候总会遇到很多问题。所以我总结了一下自己的踩坑经历。</p>
<h2 id="下载源码">下载源码</h2>
<h3 id="方式一使用mercurial下载">方式一:使用Mercurial下载</h3>
<p>安装Mercurial</p>
<pre><code class="language-bash">sudo apt-get install mercurial
</code></pre>
<p>访问OpenJDK的Mercurial仓库:</p>
<pre><code class="language-bash">https://hg.openjdk.java.net/jdk8u/jdk8u/
</code></pre>
<p>点击左边<code>browser</code>,再点击左边<code>zip</code>,下载回来解压,打开看<code>README-builds.html</code>,有如下说明:</p>
<blockquote>
<h2 id="getting-the-source">Getting the Source</h2>
<p>To get the entire set of OpenJDK Mercurial repositories use the script get_source.sh located in the root repository:</p>
<pre><code class="language-bash">hg clone http://hg.openjdk.java.net/jdk8/jdk8 YourOpenJDK
cd YourOpenJDK
bash ./get_source.sh
</code></pre>
<p>Once you have all the repositories, keep in mind that each repository is its own independent repository.</p>
<p>You can also re-run <code>./get_source.sh </code> anytime to pull over all the latest changesets in all the repositories.</p>
<p>This set of nested repositories has been given the term "forest" and there are various ways to apply the same hg command to each of the repositories.</p>
<p>For example, the script <code>make/scripts/hgforest.sh</code> can be used to repeat the same hg command on every repository, e.g.</p>
<pre><code class="language-bash">cd YourOpenJDK
bash ./make/scripts/hgforest.sh status
</code></pre>
</blockquote>
<p>刚才下载zip包其实是相当于 <code>hg clone</code>的步骤,但是如果单纯下载zip包解压,运行<code>./get_source.sh</code>脚本,会报错:</p>
<pre><code class="language-bash">ERROR: Need initial repository to use this script
</code></pre>
<p>上面报错意思就是,必须要用<code>hg clone</code>初始化仓库后,才能使用<code>get_source.sh</code>命令。因此必须要按照上面说明,使用<code>hg clone</code>命令来初始化jdk的源码仓库:</p>
<pre><code class="language-bash">$ hg clone https://hg.openjdk.java.net/jdk8u/jdk8u/
$ cd jdk8u
$ bash ./get_source.sh
# Repositories:corba jaxp jaxws langtools jdk hotspot nashorn
............
</code></pre>
<p>上面过程结束后,会下载几个模块对应的仓库源码:</p>
<table>
<thead>
<tr>
<th>Repository</th>
<th>Contains</th>
</tr>
</thead>
<tbody>
<tr>
<td>. (root)</td>
<td>common configure and makefile logic</td>
</tr>
<tr>
<td><strong>hotspot</strong></td>
<td>source code and make files for building the OpenJDK Hotspot Virtual Machine</td>
</tr>
<tr>
<td>langtools</td>
<td>source code for the OpenJDK javac and language tools</td>
</tr>
<tr>
<td><strong>jdk</strong></td>
<td>source code and make files for building the OpenJDK runtime libraries and misc files</td>
</tr>
<tr>
<td>jaxp</td>
<td>source code for the OpenJDK JAXP functionality</td>
</tr>
<tr>
<td>jaxws</td>
<td>source code for the OpenJDK JAX-WS functionality</td>
</tr>
<tr>
<td>corba</td>
<td>source code for the OpenJDK Corba functionality</td>
</tr>
<tr>
<td>nashorn</td>
<td>source code for the OpenJDK JavaScript implementation</td>
</tr>
</tbody>
</table>
<p>其中就有 <strong>jvm的源码</strong> 和 <strong>jdk的源码</strong></p>
<p>但是通过<code>get_source.sh</code>脚本下载源码很慢,并不推荐。</p>
<h3 id="方式二打包下载推荐">方式二:打包下载(推荐)</h3>
<p>按下面顺序找到下载链接:</p>
<ol>
<li>
<p>访问:http://openjdk.java.net/</p>
</li>
<li>
<p>点击左边 JDK 8</p>
</li>
<li>
<p>点击文中链接 Linux Distributions</p>
</li>
<li>
<p>在标题“RI Source Code”标题下找到 zip file</p>
</li>
</ol>
<p>下载:</p>
<pre><code class="language-bash">wget https://download.java.net/openjdk/jdk8u41/ri/openjdk-8u41-src-b04-14_jan_2020.zip
$ unzip openjdk-8u41-src-b04-14_jan_2020.zip
# 目录改一下名称
$ mv openjdk openjdk-8u41-src
</code></pre>
<p>最后我是用打包下载方式下载到open jdk8 的源码的,下面讲解设定源码解压后目录在这里:</p>
<pre><code class="language-bash">$ pwd
/home/caibh/source/openjdk-8u41-src
$ ls
ASSEMBLY_EXCEPTIONconfigureget_source.shjaxp jdk LICENSEMakefileREADME test
common corba hotspot jaxwslangtoolsmake nashorn README-builds.htmlTHIRD_PARTY_README
</code></pre>
<h2 id="configure">configure</h2>
<h3 id="尝试configure">尝试configure</h3>
<p>按<code>README-builds.html</code>说明,运行<code>bash ./configure</code>,遇到报错:</p>
<pre><code class="language-bash">configure: error: Could not find all X11 headers (shape.h Xrender.h XTest.h Intrinsic.h). You might be able to fix this by running 'sudo apt-get install libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev'.
configure exiting with result code 1
</code></pre>
<p>按提示安装对应的软件,又报错:</p>
<pre><code class="language-bash">$ sudo apt-get install libX11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev
E: 无法定位软件包 libX11-dev
</code></pre>
<p>把<code>libX11-dev</code>中的<code>X</code>改成小写就可以了:<code>libx11-dev</code>。</p>
<pre><code class="language-bash">$ sudo apt-get install libx11-dev libxext-dev libxrender-dev libxtst-dev libxt-dev
</code></pre>
<p>然后继续运行<code>bash ./configure</code>,遇到报错:</p>
<pre><code class="language-bash">configure: error: Could not find cups! You might be able to fix this by running 'sudo apt-get install libcups2-dev'.
configure exiting with result code 1
</code></pre>
<p>按提示安装:</p>
<pre><code class="language-bash">$ sudo apt-get install libcups2-dev
</code></pre>
<p>继续运行<code>bash ./configure</code>,终于没报错:</p>
<pre><code class="language-bash">====================================================
A new configuration has been successfully created in
/home/caibh/source/jdk8u/build/linux-x86_64-normal-server-release
using default settings.
Configuration summary:
* Debug level: release
* JDK variant: normal
* JVM variants: server
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
Tools summary:
* Boot JDK: java version "1.8.0_261" Java(TM) SE Runtime Environment (build 1.8.0_261-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)(at /home/caibh/app/jdk/8u261)
* Toolchain: gcc (GNU Compiler Collection)
* C Compiler: Version 8.3.0 (at /usr/bin/gcc)
* C++ Compiler: Version 8.3.0 (at /usr/bin/g++)
Build performance summary:
* Cores to use: 10
* Memory limit: 15437 MB
</code></pre>
<h3 id="configure帮助信息">configure帮助信息</h3>
<p>但是上面明显就不对,Boot JDK显示用的是<code>1.8.0_261</code>,编译JDK8应该用JDK7作为Boot JDK,所以还要配置。</p>
<p>通过查看帮助,能看到很多有用的配置:</p>
<pre><code class="language-bash">$ cd ~/source/jdk8u
$ bash ./configure --help
</code></pre>
<p>其中就有一条可以配置 Boot JDK:</p>
<pre><code class="language-bash">--with-boot-jdk path to Boot JDK (used to bootstrap build)
</code></pre>
<p>下面先按<code>README-builds.html</code>文档说明,下载好Boot JDK,以及安装好其他依赖软件,再做一次<code>bash ./configure</code></p>
<h3 id="下载-open-jdk-7">下载 Open JDK 7</h3>
<p>按下面顺序找到下载链接:</p>
<ol>
<li>
<p>访问:http://openjdk.java.net/</p>
</li>
<li>
<p>点击左边 JDK 7</p>
</li>
<li>
<p>点击文中链接 Linux Distributions</p>
</li>
<li>
<p>在标题为“JDK 9 & Later”的段落开头找到链接jdk.java.net,选择 7</p>
</li>
<li>
<p>选中“Accept License Agreement”,然后下载 Linux x64 Binary</p>
</li>
</ol>
<p>下载:</p>
<pre><code class="language-bash">$ cd source
$ wget https://download.java.net/openjdk/jdk7u75/ri/jdk_ri-7u75-b13-linux-x64-18_dec_2014.tar.gz
# 解压,改下名字
$ mv jdk_ri-7u75-b13-linux-x64-18_dec_2014.tar.gz openjdk-7u75-bin.tar.gz
$ tar -xzf jdk7u75.tar.gz
$ mv java-se-7u75-ri/ openjdk-7u75-bin
</code></pre>
<p>其实<code>README-builds.html</code>中还提及要安装一些依赖,安装后再执行<code>bash ./configure</code>,报错应该会少很多:</p>
<ul>
<li>alsa</li>
<li>freetype</li>
<li>cups</li>
<li>xrender</li>
<li>ccache</li>
</ul>
<h3 id="执行configure">执行configure</h3>
<pre><code class="language-bash">bash ./configure --with-boot-jdk=/home/caibh/source/openjdk-7u75-bin
</code></pre>
<h3 id="安装依赖软件">安装依赖软件</h3>
<p>执行<code>bash ./configure</code>后会提示安装ccache,于是我又安装了一下。</p>
<pre><code class="language-bash">$ sudo apt-get install ccache
</code></pre>
<h2 id="make">make</h2>
<h3 id="尝试make">尝试make</h3>
<pre><code class="language-bash">$ make
</code></pre>
<p>报错:</p>
<pre><code class="language-bash">*** This OS is not supported: Linux book 5.10.18-amd64-desktop #1 SMP Mon Mar 1 17:09:41 CST 2021 x86_64 GNU/Linux
make: *** 错误 1
make: *** 错误 2
make: *** 错误 2
make: *** 错误 2
make: *** 错误 2
make: *** 错误 2
</code></pre>
<h3 id="修改支持内核版本">修改支持内核版本</h3>
<p>报错信息显示<code>check_os_version</code>的地方出错了,于是找到报错文件所在行:</p>
<p><em>/home/caibh/source/openjdk-8u41-src/hotspot/make/linux/Makefile:238</em></p>
<pre><code class="language-bash"># We do not want people accidentally building on old systems (e.g. Linux 2.2.x,
# Solaris 2.5.1, 2.6).
# Disable this check by setting DISABLE_HOTSPOT_OS_VERSION_CHECK=ok.
SUPPORTED_OS_VERSION = 2.4% 2.5% 2.6% 3% 5% // 加入5%
OS_VERSION := $(shell uname -r)
EMPTY_IF_NOT_SUPPORTED = $(filter $(SUPPORTED_OS_VERSION),$(OS_VERSION))
check_os_version:
ifeq ($(DISABLE_HOTSPOT_OS_VERSION_CHECK)$(EMPTY_IF_NOT_SUPPORTED),)
$(QUIETLY) >&2 echo "*** This OS is not supported:" `uname -a`; exit 1; // 238行
endif
</code></pre>
<p>修改上面代码,加入5.x内核支持,因为我自己的Linux系统,用的就是5.x的内核。</p>
<h3 id="安装旧版本gcc">安装旧版本gcc</h3>
<p>继续执行<code>make</code>,继续出错,错误的信息很多提示是语法错误,例如:</p>
<pre><code>warning: invalid suffix on literal; C++11 requires a space between literal and string macro
......
warning: ‘int readdir_r(DIR*, dirent*, dirent**)’ is deprecated [-Wdeprecated-declarations]
......
make: *** 错误 1
make: *** 错误 2
make: *** 错误 2
make: *** 错误 2
make: *** 错误 2
make: *** 错误 2
make: *** 错误 2
</code></pre>
<p>经过一番查找资料,判定应该是gcc版本太高导致的,<code>README-builds.html</code>中也提过C/C++的编译器要求版本是 gcc 4.3 以上,而现在我系统的gcc/g++编译器是8.3,太新了,呵呵。</p>
<p>问题又来了,Deepin的源中没有更低版本的gcc和g++,不想通过编译源码方式安装gcc和g++,想通过apt方式安装。办法是将系统源临时改成Ubuntu 18或16的。经测试Ubuntu 18能安装gcc-5,Ubuntu 16能安装 gcc-4.8。</p>
<p>将源换成Ubuntu 18的:</p>
<pre><code class="language-bash">$ cd /etc/apt
$ sudo cp sources.list sources.list.bak
$ sudo vim sources.list
</code></pre>
<p>替换成下面内容:</p>
<pre><code class="language-bash">deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
</code></pre>
<p>然后安装证书,更新:</p>
<pre><code class="language-bash">$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 3B4FE6ACC0B21F32
$ sudo apt update
</code></pre>
<p>最后安装gcc-5</p>
<pre><code class="language-bash">$ sudo apt-get install gcc-5 g++
</code></pre>
<h3 id="配置多个版本的gcc和g">配置多个版本的gcc和g++</h3>
<p>配置多个gcc</p>
<pre><code class="language-bash">sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 100
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 100
</code></pre>
<p>切换gcc版本:</p>
<pre><code class="language-bash">sudo update-alternatives --config gcc
</code></pre>
<p>配置多个g++</p>
<pre><code class="language-bash">sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 100
</code></pre>
<p>切换g++版本:</p>
<pre><code class="language-bash">sudo update-alternatives --config g++
</code></pre>
<h3 id="屏蔽编译警告">屏蔽编译警告</h3>
<p>编译设置默认会将警告当成错误,造成make的时候会中断,于是需要dirty hack一下。</p>
<p>找到<code>hotspot/make/linux/makefiles/gcc.make</code>文件,将<code>WARNINGS_ARE_ERRORS</code>注释掉:</p>
<pre><code class="language-bash">#WARNINGS_ARE_ERRORS = -Werror
</code></pre><br><br>
来源:https://www.cnblogs.com/caibh/p/14726110.html
頁:
[1]