This is the 27th day of my participation in the August Genwen Challenge.More challenges in August

One, foreword

Recently, I encountered many problems in the process of in-depth research on Java lock upgrade. These questions all kinds of articles from the Internet didn’t find the right answer (online mostly said more conceptual, and further details the key failed to clear, the subsequent I will pack a special article from the source code level analysis Java lock escalation, please look), and had only their download JDK source for research.

Because to study the source code has to be compiled, debugging and other operations. As I was not familiar with C/C++, I encountered many problems, consulted many articles and made a lot of effort in the whole process. This article will compile and debug OpenJdk8. I hope it will be helpful to the students in need.

Two, environmental preparation

1. Environment description

My current compilation environment information is as follows:

Operating system: Mac OS 10.14.6

Xcode version: 10.3

Command_Line_Tools version: macOS 10.14 for Xcode 10.3

Its version: 1.8, address: hg.openjdk.java.net/jdk8u/jdk8u

Other software required: Mercurial, ccache, FreeType

PS:

1. Pay attention to the Mac OS, Xcode, and Command_Line_Tools versions. Therefore, it is recommended that you use the same version number as the author if you want to compile and debug completely with this article.

2. The Xcode version corresponds to the Command_Line_Tools version.

2. Software installation

A. Install Xcode

Since my Mac OS version is not the minimum 10.15, I cannot download the Xcode version directly from the App Store to install it (it will tell me that it cannot be installed). If you can download it on your MAC, you can just install it from the App Store. Here’s how to install Xcode without downloading it from the App Store.

First we visit apple’s website (need apple to login) : developer.apple.com/download/mo… There will be historical versions of all kinds of software. Let’s just search Xcode and find version 10.3 to download. Install DMG directly after downloading. The installation process is relatively simple and will not be described here.

B. Install Command_Line_Tools

It is said that the new version of Xcode does not automatically install Command_Line_Tools after installation. So we need to install it ourselves. The installation method is the same as that of Xcode. You can download and install it directly from the official website.

If you already have Command_Line_Tools, but to ensure that the version number is consistent with Xcode, you can delete the local Command_Line_Tools. Delete method is simple, direct execution sudo rm – rf/Library/Developer/CommandLineTools can.

C. Signed for the Licence of Xcode

The newly installed Xcode needs to sign for its licence. Sudo xcodebuild-license is a unique way to do this: enter sudo xcodebuild-license on the command line (root permission required). And then a lot of text will pop up, the licence content. Just keep pressing TAB to the end of the file, type Agree, and press Enter. If the license is not approved here, an error will be reported when xcode is used later.

D. Install mercurial, CCache, and FreeType

This installation is also relatively simple, directly from the command line to execute the following command.

brew install mercurial

brew install ccache

brew install freetype

3. Compilation process

1. Download the source code

Oneself to create a new directory, then execute: hg clone hg.openjdk.java.net/jdk8u/jdk8u… In jdk8u, run sh get_source.sh. This step is the place to actually download the source code, this process may take a bit longer, depending on your network speed.

2. Configuration process

A. Modify the configuration file

Code after the download is complete, the need for a common configuration file/autoconf/generated – configure. Sh. Otherwise there will be a lot of problems in the compilation process.

Xcode4 is required to build JDK 8. Xcode4 is required to build JDK 8

Invalid argument ‘-std=gnu++98’ not allowed with ‘C’ : CXXSTD_CXXFLAG=”-std=gnu++98″

Error: A GCC compiler is required: Find all code logic that looks like the following and comment it out

Fatal error: ‘iostream’ file not found fatal error: ‘iostream’ file not found fatal error: ‘iostream’ file not found

NEW_INCLUDE=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1

export CFLAGS=-I
N E W I N C L U D E   e x p o r t C X X F L A G S = I NEW_INCLUDE\ export CXXFLAGS=-I
NEW_INCLUDE

Library not found for -lstDC ++

PS: question solution source address: blog.csdn.net/quantum7/ar…

Git Clone github.com/quantum6/xc…

Then go to the tool and run sh install.sh

Then run the following command to set the environment variables:

NEW_LIB=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib

export LDFLAGS=”-L
N E W L I B   e x p o r t L I B R A R Y P A T H = {NEW_LIB}”\ export LIBRARY_PATH=
NEW_LIB:$LIBRARY_PATH

Clang of settlement: error: unknown argument: ‘- FPCH – deps’ problem: to find the following documents the hotspot/make/BSD/makefiles/GCC. Make and comment out the following parts:

Ld: Symbol (s) not found for architecture X86_64

The error log is as follows:

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

Solutions: Find files JDK/SRC/macosx/native/sun/osxapp/ThreadUtilities m, Change the inline void attachCurrentThread(void** env) to static inline void attachCurrentThread(void** env) as shown in the following figure:

Fixed crashes after compilation in slowdebug mode: JVM crashes after execution of Java-version after compilation in slowdebug mode.

Find files hotspot/SRC/share/vm/runtime/perfMemory CPP file. Comment out the following:

B. Configuration engineering

After you modify the configuration file in the above manner, you can perform the configuration operation. The specific method is very simple, in the JDk8u directory to run the following command.

sh configure –with-freetype-include=/usr/local/include/freetype2 –with-freetype-lib=/usr/local/lib/ –disable-zip-debug-info –disable-debug-symbols –with-debug-level=slowdebug –with-target-bits=64 –with-jvm-variants=server

If you follow this document with all of your tool versions and all of your operations, you will not have any errors if you go this far. The following logs are generated after the configuration is successful:

Install compiledB

This step is missing in many online tutorials. In fact, the reason for this step operation is to solve the problem that many header files can not be found after compiling and importing Clion. That is, in the compiler, the header folder is red. Installing compiledB seems to be possible by modifying header files, but I tried. I don’t think so. Eventually you have to install CompiledB, which is easy.

Installation requirements: Python3 + is required. If Python is 2+, python3 can be installed by using the brew install python3 command.

Then need to install the PIP, installation instructions: curl the bootstrap. Pypa. IO/get – PIP. Py | python3

PIP install compiledb can be used to install compiledb

Compile the source code

1. Setting environment variables

There are also a few common environment variables that need to be set before you start compiling:

Export LANG=C export CC=clang C compiler is no longer GCC but clang export COMPILER_WARNINGS_FATAL=false # Skip clang some strict syntax check # Allow automatic downloads dependency export ALLOW_DOWNLOADS=true export LFLAGS=’ -xlinker -LC+ + -lstDC ++’ To match the number of CPU cores, export HOTSPOT_BUILD_JOBS=4 export ALT_PARALLEL_COMPILE_JOBS=4 export SKIP_COMPARE_IMAGES=true export USE_PRECOMPILED_HEADER=true # Export BUILD_LANGTOOLS=true #export BUILD_JAXP=false #export BUILD_JAXWS=false #export BUILD_CORBA=false export BUILD_HOTSPOT=true export BUILD_JDK=true BUILD_DEPLOY=false BUILD_INSTALL=false unset JAVA_HOME unset CLASSPATH

2. Compilation process

Now that the configuration is complete, it’s time to compile OpenJdk. Make all in jdk8u directory (see below).

This process takes a long time. If the following log is displayed, the compilation is successful.

There may be a large number of No such file or directory warnings at the end after compilation. Don’t worry about that, just look ahead to the log and see the compile success log as described above.

Finally, we verify whether the JDK compiled by ourselves is available through the command, we look at the Java version number through the command, that is, execute the command: Java -version, as follows, if the normal printing of the publication number, that is, the compilation is completely OK.

3. Summary of other problems

During the entire installation process, many problems were encountered. Normally speaking, if you install the above installation steps will not appear these problems. But for your convenience, here is a summary of some other errors not directly mentioned.

/objs/java_objs/java.diz’: No such file or directory /objs/java_objs/ java.dsym ‘: No such file or directory

–with-native debug-symbols=external –with-native debug-symbols=external –with-native debug-symbols=external –with-native debug-symbols=external –with-native debug-symbols=external –with-native debug-symbols=external –with-native debug-symbols=external –with-native debug-symbols=external Specific can see a bug below. Github.com/AdoptOpenJD… .

The solution is to turn off the debug message. –disable-zip-debug-info –disable-debug-symbols –disable-debug-symbols –disable-zip-debug-info –disable-debug-symbols

configure: error: Could not find freetype! Question:

The problem is that we can’t find the freeType we installed in configure. Configure configure to set path to freeType –with-freetype-include=/usr/local/include/freetype2 –with-freetype-lib=/usr/local/lib/

PS: Now you can understand why the configure directive requires some parameters.

sh configure  –with-freetype-include=/usr/local/include/freetype2 –with-freetype-lib=/usr/local/lib/ –disable-zip-debug-info –disable-debug-symbols

In the next article, we will introduce how to debug compiled source code using Clion on MAC.

Five, references,

www.jianshu.com/p/d9a1e1072…

Blog.csdn.net/davi_qiang/…

www.jianshu.com/p/d9a1e1072…

www.cnblogs.com/yuhangwang/…

www.jianshu.com/p/ee7e91766…

Blog.csdn.net/y3over/arti…

Blog.csdn.net/quantum7/ar…

www.jianshu.com/p/0fc877be4…

Sixth, practices

If you have any questions or comments about this article, please add lifeofCoder.