Since the MAC High Sierra system has undergone major changes, I wrote a blog post about compiling OpenJDK8 with MAC High Sierra.

Preparation stage

  • MAC OS Version10.13.3
  • Since the official openJDK source code is not managed with Git, the source code is storedhttp://hg.openjdk.java.net/First you need to install the version management tool Mercurial, which you can use directly on your MACbrewInstall it.
brew install mercurial
Copy the code
  • Download the appropriate version of the source code, the compiled version I’m using isjdk8u-dev.clone source code
hg clone http://hg.openjdk.java.net/jdk8u/jdk8u-dev/
Copy the code

After cloning, use the script to get the latest source code

cd jdk8u-dev
sh ./get_source.sh # This step takes a long time depending on the network environment
Copy the code

The structure after downloading is as follows, including the codes of each module

- ~/hg/jdk8u-dev ll total 632-rw-r --r-- 1 xx staff 1.5K Nov 11 18:02 ASSEMBLY_EXCEPTION -rw-r--r-- 1 xx staff 19K Nov 11 18:02 license-RW-r --r-- 1 xx staff 6.1K Nov 11 18:02 makefile-rw-r --r-- 1 xx staff 1.5K Nov 11 18:02 README -rw-r--r-- 1 xx staff 126K Nov 11 18:02 README-builds.html -rw-r--r-- 1 xx staff 142K Nov 11 18:02 THIRD_PARTY_README Drwxr-xr-x 5 xx staff 160B Nov 11 21:55 build DRwxr-xr-x 6 XX staff 192B Nov 11 18:02 common-RW-r --r-- 1 xx staff 1.2k Configure drwxr-xr-x 12 xx staff 384B Nov 11 18:02 CORba-RW-r --r-- 1 xx staff 3.0K Nov 11 18:02 configure drwxr-xr-x 12 XX staff 384B Nov 11 18:02 CORba-RW-r --r-- 1 xx staff 3.0K Nov 11 18:02 get_source.sh drwxr-xr-x 17 xx staff 544B Nov 11 21:51 hotspot drwxr-xr-x 16 xx staff 512B Nov 11 18:03 jaxp drwxr-xr-x 16 xx staff 512B Nov 11 18:03 jaxws drwxr-xr-x 13 xx staff 416B Nov 11 18:14 jdk drwxr-xr-x 13 xx staff 416B Nov 11 18:04 langtools drwxr-xr-x 12 xx staff 384B Nov 11 18:02 make drwxr-xr-x 20 xx staff 640B Nov 11 18:07 nashorn drwxr-xr-x 4 xx staff 128B Nov 11 18:02test
Copy the code
  • Of course, you also need to prepare some necessary tools, such as installing the latest versionXcode, there areg++.gccEnvironment, installationfreetype, using the commandbrew install freetypeTo install
~/hg/jdk8u-dev gcc -v Configured with: -- prefix = / Applications/Xcode. App/Contents/Developer/usr - with GXX - include - dir = / usr/include/c + + / 2 Apple LLVM version 9.0.0 (clang-900.0.39.2) Target: x86_64-apple-Darwin17.4.0 Thread Model: POSIx InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin ~/hg/jdk8u-dev g++ -v Configured with: -- prefix = / Applications/Xcode. App/Contents/Developer/usr - with GXX - include - dir = / usr/include/c + + / 2 Apple LLVM version 9.0.0 (clang-900.0.39.2) Target: x86_64-apple-Darwin17.4.0 Thread Model: POSIx InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/binCopy the code

Compilation phase

  • Setting environment Variables
Set language options, mandatory
export LANG=C
# Mac platform, C compiler is not GCC, is clang
export CC=clang
# Skip some of clang's strict syntax checks, otherwise N warnings will be treated as errors
export COMPILER_WARNINGS_FATAL=false
# parameters to use when linking
export LFLAGS='-Xlinker -lstdc++'
# Whether to use clang
export USE_CLANG=true
# Use 64-bit data model
export LP64=1
Tell the compiler it is 64-bit, otherwise it will compile at 32-bit
export ARCH_DATA_MODEL=64
Allow automatic download dependencies
export ALLOW_DOWNLOADS=true
# Number of concurrent compilation threads, long compilation time, in order not to affect other work, I choose 2
export HOTSPOT_BUILD_JOBS=2
Whether to skip the comparison with the previous version
export SKIP_COMPARE_IMAGES=true
# Whether to use precompiled header files to speed up compilation
export USE_PRECOMPILED_HEADER=true
Whether to use incremental compilation
export INCREMENTAL_BUILD=true
# Compile content
export BUILD_LANGTOOLS=true
export BUILD_JAXP=true
export BUILD_JAXWS=true
export BUILD_CORBA=true
export BUILD_HOTSPOT=true
export BUILD_JDK=true
# compiled version
export SKIP_DEBUG_BUILD=true
export SKIP_FASTDEBUG_BUILD=false
export DEBUG_NAME=debug
# Avoid parts of builds like Javaws and browser Java plug-ins
export BUILD_DEPLOY=false
export BUILD_INSTALL=false
unset JAVA_HOME
Copy the code
  • Make sure you have an existing Java environment locally, usingjava -versionTo view the
java version "1.7.0 _80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
Copy the code
  • startconfigure, the configure command I use here is as follows in case it cannot be foundfreetypeThe path of the
bash ./configure  --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/
Copy the code

1. Unsurprisingly, there will be a few mistakes. Xcode version does not comply with the error:

. checking flagsfor boot jdk java command. checking flagsfor boot jdk java command for big workloads...  -Xms64M -Xmx1600M -XX:ThreadStackSize=1536 -XX:PermSize=32m -XX:MaxPermSize=160m
checking flags for boot jdk java command for small workloads...  -XX:+UseSerialGC -Xms32M -Xmx512M
checking for jtreg... no
checking Determining if we need to set DEVELOPER_DIR... no
checking for xcodebuild... /usr/bin/xcodebuild
configure: error: Xcode 4 is required to build JDK 8, the version found was 9.2. 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
Copy the code

The solution: find the configure file and open the vim common/autoconf/generated – configure. Sh, find a place to judge version, will this period all commented out.

# 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
Copy the code

The GCC compiler cannot be found.

. checkingforxcodebuild... /usr/bin/xcodebuild checking Determining Xcode SDK path... / Applications/Xcode. App/Contents/Developer/Platforms/MacOSX platform/Developer/SDKs/MacOSX10.13 SDK checkingfor clang... /usr/local/opt/llvm@4/bin/clang
configure: Resolving CC (as /usr/local/opt/llvm@4/bin/clang) failed, using /usr/local/opt/llvm@4/bin/clang directly.
checking resolved symbolic links for CC... /usr/local/ Cellar/LLVM @ 4/4.0.1 / bin/clang - 4.0 checkingif CC is disguised ccache... no, keeping CC
configure: The C compiler (located as /usr/local/Cellar/llvm@4/4.0.1/bin/clang-4.0) does not seem to be the required GCC compiler.
configure: The result from running with --version was: "Clang version 4.0.1 (tags/RELEASE_401 / final)"
configure: error: GCC compiler is required. Try setting --with-tools-dir.
configure exiting with result code 1
Copy the code

Solution: or just find files common/autoconf/generated – configure. Sh, there is two check, find and comment out the validation logic.

 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 GCC compiler."5 > &$as_echo "$as_me: The $COMPILER_NAME compiler (located as $COMPILER) does not seem to be the required GCC compiler."> & 6; } {$as_echo "$as_me:${as_lineno-$LINENO}: The result from running with --version was: \"$COMPILER_VERSION_TEST\ ""5 > &$as_echo "$as_me: The result from running with --version was: \"$COMPILER_VERSION_TEST\ ""> & 6; } as_fn_error $?"GCC compiler is required. Try setting --with-tools-dir." "$LINENO" 5
    fi
Copy the code

Then continue compiling.

  • The configuration is successful. If nothing happens, the configuration should be successful and the following information is displayed.
====================================================
A new configuration has been successfully created in
/Users/lican/hg_bak/jdk8u-dev/build/macosx-x86_64-normal-server-release
using configure arguments '--with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/'.

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.7.0 _80"Java(TM) SE Runtime Environment (Build 1.7.0_80-b15) Java HotSpot(TM) 64-bit Server VM (build 24.80-b11, Mixed mode) (at/Library/Java/JavaVirtualMachines jdk1.7.0 _80. JDK/Contents/Home) * C Compiler: Clang Version (tags/RELEASE_401/final) Version 4.0.1 (at /usr/local/Cellar/llvm@4/4.0.1/bin/clang-4.0) * C++ Compiler: version Configured with: -- prefix = / Applications/Xcode. App/Contents/Developer/usr - with GXX - include - dir = / usr/include/c + + / 2 (at/usr/bin/g + +) Build performance summary: * Cores to use: 2 * Memorylimit:   7883 MB
* ccache status:  not installed (consider installing)

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
Copy the code
  • startmake allI’m actually going to start compiling and then I’m going to get a bunch of errors one, pointer to zero
Compiling /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/opto/library_call.cpp
/Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/opto/library_call.cpp:3829:20: warning: shifting a negative signed value is undefined [-Wshift-negative-value] << Klass::_lh_array_tag_shift) ^ /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/opto/lcm.cpp:52:34:  error: ordered comparison between pointer and zero ('address' (aka 'unsigned char *') and 'int')
  if (Universe::narrow_oop_base()>0) { // Implies UseCompressedOops.
      ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
1 error generated.
make[6]: *** [lcm.o] Error 1
make[6]: *** Waiting for unfinished jobs....
1 warning generated.
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/lican/hg_bak/jdk8u-dev/build/macosx-x86_64-normal-server-release/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2
Copy the code

Change the place for comparison to Universe::narrow_oop_base()! = NULL, save, continue compiling 2, a similar error may occur in the near future

Compiling /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/services/virtualMemoryTracker.cpp
Compiling /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/runtime/virtualspace.cpp
/Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/runtime/virtualspace.cpp:345:13: error: ordered comparison between pointer and zero ('char *' and 'int')
  if (base()>0) {
      ~~~~~~^~
1 error generated.
make[6]: *** [virtualspace.o] 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/lican/hg_bak/jdk8u-dev/build/macosx-x86_64-normal-server-release/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2
Copy the code

The solution is similar to the above, replace with if (base()! Invalid argument ‘-std=gnu++98’ not allowed with ‘C/ObjC’ invalid argument ‘-std=gnu++98’ not allowed with ‘C/ObjC’

Compiling /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/utilities/xmlstream.cpp
Compiling /Users/lican/hg_bak/jdk8u-dev/hotspot/src/share/vm/utilities/yieldingWorkgroup.cpp
Making signal interposition lib...
Making SA debugger back-end...
error: invalid argument '-std=gnu++98' not allowed with 'C/ObjC'
make[6]: *** [libsaproc.dylib] Error 1
make[6]: *** Waiting for unfinished jobs....
warning: (x86_64) /var/folders/s3/bzyrc_3d56gfl4w34ccx62_00000gn/T/jsig-d50157.o unable to open object file: No such file or directory
warning: no debug symbols in executable (-arch x86_64)
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/lican/hg_bak/jdk8u-dev/build/macosx-x86_64-normal-server-release/hotspot/_hotspot.timestamp] Error 2
make: *** [hotspot-only] Error 2
Copy the code

Solution: even with guess, from the beginning we use the configure file (common/autoconf/generated – configure. Sh) found the relevant configuration

  case $COMPILER_NAME in
    gcc )
      COMMON_CCXXFLAGS_JDK="$COMMON_CCXXFLAGS $COMMON_CCXXFLAGS_JDK -W -Wall -Wno-unused -Wno-parentheses \
      -pipe \
      -D_GNU_SOURCE -D_REENTRANT -D_LARGEFILE64_SOURCE"
      CXXSTD_CXXFLAG="-std=gnu++98"
Copy the code

Comment out CXXSTD_CXXFLAG=”-std=gnu++98″, then make clean and restart configure, i.e

bash ./configure  --with-freetype-include=/usr/local/include/freetype2 --with-freetype-lib=/usr/local/lib/
Copy the code
  • Oh, no other errors have been found

Compile the complete

If the compilation is complete, the progress and time of each compilation will be displayed, which will take more than 10 minutes

----- Build times -------
Start 2018-03-01 15:28:52
End   2018-03-01 15:45:10
00:00:47 corba
00:01:10 demos
00:04:16 docs
00:00:29 hotspot
00:02:12 images
00:00:25 jaxp
00:00:38 jaxws
00:05:55 jdk
00:00:00 langtools
00:00:25 nashorn
00:16:18 TOTAL
-------------------------
Finished building OpenJDK for target 'all'
Copy the code

# # to verify

cd build/macosx-x86_64-normal-server-release/jdk/bin && ./java -version

openjdk version "1.8.0 comes with - internal"OpenJDK Runtime Environment (build 1.8.0-internal-lican_2018_03_01_15_28-b00)  mixed mode)Copy the code