Original text: zhuanlan.zhihu.com/p/54134602
One, foreword
For Java-based developers, mastery of the JVM virtual machine is a necessary threshold to become an advanced developer. In this article, we will share how to compile the OpenJDK source code on MAC.
Digression, I think: want to become a technical expert, familiar with official documents and master the source code analysis ability is absolutely necessary.
There are a lot of articles on how to compile the openJDK source code, but it is very difficult to pass the process because the authors of such articles do not fully consider the environment.
The best way to compile it is to read the official documentation.
Get the openJDK source code
The openJDK source code is managed using Mercurial and can be cloned locally using HG (similar to Git)
Hg clone hg.openjdk.java.net/jdk9/jdk9 jdk9 / / don’t forget to install mercuial oh
Although this can obtain the source code, but I personally tested the next, it is very difficult, always network disconnect, should be related to the wall.
Here’s another way to get the source code: JDK. I’m going to pick the version, I’m going to pick 9.
Click the link below to download the source code:
Prepare for environmental dependency
Homwbrew is a package management tool that can be used to install and compile the required dependencies. If you have installed homwbrew, you can install it by command:
The/usr/bin/ruby – e “$(curl – fsSL raw.githubusercontent.com/Homebrew/in…). “
Install dependencies:
Brew install ccache // speed up compilation
Brew install freetype // font engine that will be relied on during compilation
Four, compile,
Compilation itself is not complicated, but the most problematic part.
Everyone compiled the operating system version ah, compiler version is not the same, I compiled success does not necessarily you can compile success, so everyone in the compilation, or to the official compilation document mainly. The documentation is in the openJDK source code that you downloaded. The diagram below:
1. Compile environment
My MAC OS version is 10.14.2 and the xcode installed through the App Store is Xcode10, which failed to compile the OpenJDK source code. The official documentation recommends 10.12.5 (Sierra), using XCode 8.3.2.
Since it was too troublesome to downgrade the operating system version, I uninstalled Xcode10 from the App Store and downloaded Xcode8.3.2 from the official website. Tragically, 8.3.2 on 10.14.2 didn’t work at all.
So I downloaded Xcode9.2 again with a try-and-try attitude, but luckily everything worked.
Here is the official link to download Xcode: Sign In – Apple
2, compile,
Compile configuration, we can call the following command to set up the compile, and this script will also check your current environment to see if it meets.
. / configure — with target – bits = 64 – with – freetype = / usr/local/Cellar/freetype 2.9.1 – enable ccache –with-jvm-variants=server,client –with-boot-jdk-jvmargs=”-Xlint:deprecation -Xlint:unchecked” –disable-warnings-as-errors –with-debug-level=slowdebug 2>&1 | tee configure_mac_x64.log
Remember to point –with-freetype to your own Freetype installation path.
Your environment is fine if you print the following:
A new configuration has been successfully created in /Users/yourname/jdk9/build/macosx-x86_64-normal-serverANDclient-slowdebug using configure arguments ‘- with – target – bits = 64 – with – freetype = / usr/local/Cellar/freetype 2.9.1 – enable ccache –with-jvm-variants=server,client –with-boot-jdk-jvmargs=’-Xlint:deprecation -Xlint:unchecked’ –disable-warnings-as-errors –with-debug-level=slowdebug’.
Configuration summary:
- Debug level: slowdebug
- HS debug level: debug
- JDK variant: normal
- JVM variants: server client
- OpenJDK target: OS: macosx, CPU architecture: x86, address length: 64
- Version string: 9-internal+0-adhoc.daiyongzhi.jdk9 (9-internal)
Tools summary:
- Boot JDK: Java version “1.8.0_101” Java(TM) SE Runtime Environment (build 1.8.0_101-B13) Java HotSpot(TM) 64-bit Server VM (build 25.101 – bl3, mixed mode) (at/Library/Java/JavaVirtualMachines jdk1.8.0 _101. JDK/Contents/Home)
- Toolchain: Clang (Clang /LLVM from Xcode 9.2)
- C Compiler: Version 9.0.0 (at /usr/bin/clang)
- C++ Compiler: Version 9.0.0 (at /usr/bin/clang++)
Build performance summary:
- Cores to use: 8
- Memory limit: 16384 MB
- Ccache status: Active (3.5)
NOTE: You have requested to build more than one version of the JVM, which will result in longer build times.
Now you’re ready to compile
export LANG=C
make all LOG=debug 2>&1 | tee make_mac_x64.log
If all goes well, the compile is successful if you see this result:
In the openJDK9 source code, however, there are still several issues that cause compilation to fail.
There are several questions:
/hotspot/src/share/vm/memory/virtualspace.cpp:584:14: error: ordered comparison between pointer and zero (‘char *’ and ‘int’)
/hotspot/src/share/vm/opto/loopPredicate.cpp:903:73: error: ordered comparison between pointer and zero (‘const TypeInt *’ and ‘int’)
/hotspot/src/share/vm/opto/lcm.cpp:42:35: error: ordered comparison between pointer and zero (‘address’ (aka ‘unsigned char *’) and ‘int’)
Google for a solution: Xcode 9.0 (9A235) Problems
3. Verify
Verification is easier. My compilation results are in this directory: openJDK /build/ macosx-x86_64-normal-serverandclient-slowDebug.
Go to openJDK /build/ macosx-x86_64-normal-serverandclient-slowdebug/JDK /bin and run./ Java -version.
Five, debugging,
With the compilation out of the way, debugging is easy.
1. Write Java programs
Let’s start by trying to write a program using the JDK we compiled ourselves. Note that the JDK in the images directory is used in the compile directory. For example, my directory is build/ macosx-x86_64-normal-serverandclient-slowdebug /images/ JDK. Build/macosx-x86_64-normal-serverandclient-slowdebug/JDK
Open the development environment, set the JDK to the JDK directory compiled above, and write a program, such as I wrote (to debug gc log printing).
public class Main { public static int size_1m = 1024*1024; public static List<byte[]> list = new ArrayList<>();
public static void main(String[] args) throws Exception { System.out.println(“Hello World!” ); for(int i=0; i<10; i++){ System.out.println(i+1); list.add(new byte[size_1m]); Thread.sleep(1000L); }}}
The VM parameters are as follows:
-XX:NewSize=6m -XX:SurvivorRatio=4 -XX:MaxNewSize=6m -Xlog:gc*:stdout:time,level,tags -XX:+UseParallelGC
You can run the above code directly (if the compilation is ok, the above code will work on the compiled JDK)
Debug the JVM by running a Java program
(1) import CLion
I use CLion here, import hotspot source code into the development environment, the import directory is in the OpenJDK directory hotspot.
CLion scans your directory to generate a cmakelists.txt file for you, but it generates an incorrect include_directories, causing many source includedirectories to miss the directories. The generated include_directories can be deleted entirely:
include_directories(./src/share/vm) include_directories(./src/cpu/x86/vm) Include_directories (. / SRC/share/vm/precompiled) include_directories (. / SRC/share/vm/utilities) so that c + + programs include is not an error.
② Configuration operation
Add an Application to CLion’s Edit Configurations, as shown:
The Executable is your compiler to generate the Java command, such as my catalog is the build/macosx x86_64 – normal – serverANDclient – slowdebug/images/JDK/bin/Java.
The Program arguments are the arguments that you use to start your Java Program. If you use IDEA to start your Java Program, IDEA will print the following command:
/Users/yourname/jvm/openjdk/build/macosx-x86_64-normal-serverANDclient-slowdebug/images/jdk/bin/java -XX:NewSize=6m -XX:SurvivorRatio=4 -XX:MaxNewSize=6m -Xlog:gc*:stdout:time,level,tags -XX:+UseParallelGC -Dfile.encoding=UTF-8 -classpath /Users/daiyongzhi/IdeaProjects/mytest_projects/test_myjdk9/out/production/test_myjdk9 Main Just copy the arguments to Program Arguments after the Java command. For example, mine:
Under CLion, however, we can disable the source code and debug the virtual machine source code by running Java programs.
Six, summarized
This article does not cover the compilation details in great detail, because it is required in the documentation, no amount of what I have written is as comprehensive and accurate as the official documentation.
Must be based on official documents, other articles can only refer to.