#chiroito ’s blog

Java を中心とした趣味の技術について

macOS に OpenJDK のビルド環境を構築

環境

以下の環境で試しました。

  • macOS Mojave バージョン 10.14.6
  • Xcode Version 11.3.1 (11C504)

環境の準備

環境を準備するには以下の3つが必要です。

  • Xcode
  • autoconf
  • Boot用JDK

Xcode は App Store からインストールしました。 デフォルトの設定ではbash configure時に以下のエラーが出るので開発者ディレクトリを変更します。

$ bash configure
Runnable configure script is not present
Generating runnable configure script at /Users/cito/develop/jdk/build/.configure-support/generated-configure.sh
Using autoconf at /usr/local/bin/autoconf [autoconf (GNU Autoconf) 2.69]
configure: Configuration created at Wed Mar 11 13:35:20 JST 2020.
checking for basename... /usr/bin/basename
(略)
checking which variants of the JVM to build... server
checking for sdk name... 
configure: error: No xcodebuild tool and no system framework headers found, use --with-sysroot or --with-sdk-name to provide a path to a valid SDK
/Users/cito/develop/jdk/build/.configure-support/generated-configure.sh: line 82: 5: Bad file descriptor
configure exiting with result code 1

開発者ディレクトリはxcode-selectコマンドに-sオプションを付けて変更します。-pオプションを付けることで今設定されているパスを確認できます。

$ xcode-select -p
/Library/Developer/CommandLineTools

$  sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

$ xcode-select -p
/Applications/Xcode.app/Contents/Developer

次に、Autoconf をインストールします。これはbrewコマンドでインストールします。

$ brew install autoconf
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
(略)
==> Caveats
Emacs Lisp files have been installed to:
  /usr/local/share/emacs/site-lisp/autoconf
==> Summary
🍺  /usr/local/Cellar/autoconf/2.69: 71 files, 3.0MB

最後に Boot 用JDKをインストールします。これはリリースされている最新メジャーバージョンのJDKが必要です。

$ brew install java
==> Downloading https://homebrew.bintray.com/bottles/openjdk-13.0.2+8_2.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/60/6034ec5a0927803eae37a5e85b6c6efadb930527827b45ecc593e25a9750061c?__gda__=exp=1583902642
######################################################################## 100.0%
==> Pouring openjdk-13.0.2+8_2.mojave.bottle.tar.gz
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

openjdk is keg-only, which means it was not symlinked into /usr/local,
because it shadows the macOS `java` wrapper.

If you need to have openjdk first in your PATH run:
  echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.bash_profile

For compilers to find openjdk you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk/include"

==> Summary
🍺  /usr/local/Cellar/openjdk/13.0.2+8_2: 631 files, 314.6MB

インストールできたことを確認しましょう

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    13, x86_64: "OpenJDK 13"    /Library/Java/JavaVirtualMachines/openjdk-13.jdk/Contents/Home
    1.8.0_221, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/openjdk-13.jdk/Contents/Home

ビルド

ビルドはbash configureしてからmakeします。

$ bash configure
configure: Configuration created at Wed Mar 11 14:12:40 JST 2020.
checking for basename... /usr/bin/basename
(略)
====================================================
The existing configuration has been successfully updated in
/Users/cito/develop/jdk/build/macosx-x86_64-server-release
using default settings.

Configuration summary:
* Debug level:    release
* HS debug level: product
* JVM variants:   server
* JVM features:   server: 'aot cds compiler1 compiler2 dtrace epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services shenandoahgc vm-structs zgc' 
* OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
* Version string: 15-internal+0-adhoc.cito.jdk (15-internal)

Tools summary:
* Boot JDK:       openjdk version "13" 2019-09-17 OpenJDK Runtime Environment (build 13+33) OpenJDK 64-Bit Server VM (build 13+33, mixed mode, sharing)  (at /Library/Java/JavaVirtualMachines/openjdk-13.jdk/Contents/Home)
* Toolchain:      clang (clang/LLVM from Xcode 11.3.1)
* C Compiler:     Version 11.0.0 (at /usr/bin/clang)
* C++ Compiler:   Version 11.0.0 (at /usr/bin/clang++)

Build performance summary:
* Cores to use:   8
* Memory limit:   16384 MB

WARNING: The result of this configuration has overridden an older
configuration. You *should* run 'make clean' to make sure you get a
proper build. Failure to do so might result in strange build problems.

makeしてみましょう。

make images
Building target 'images' in configuration 'macosx-x86_64-server-release'
Compiling 8 files for BUILD_TOOLS_LANGTOOLS
Parsing 2 properties into enum-like class for jdk.compiler
Compiling 10 properties into resource bundles for jdk.javadoc
Compiling 19 properties into resource bundles for jdk.compiler
Compiling 12 properties into resource bundles for jdk.jdeps
(略)
Creating jdk image
WARNING: Using incubator modules: jdk.incubator.foreign, jdk.incubator.jpackage
Creating CDS archive for jdk image
Finished building target 'images' in configuration 'macosx-x86_64-server-release'

確認

ビルドしたものを確認してみます。

build/macosx-x86_64-server-release/jdk/bin/java -version
openjdk version "15-internal" 2020-09-15
OpenJDK Runtime Environment (build 15-internal+0-adhoc.cito.jdk)
OpenJDK 64-Bit Server VM (build 15-internal+0-adhoc.cito.jdk, mixed mode)