Skip to content

Instantly share code, notes, and snippets.

@jorgedfbranco
Last active July 15, 2023 08:56
Show Gist options
  • Save jorgedfbranco/1a6bd30c0736eddca68abb6ceaf77245 to your computer and use it in GitHub Desktop.
Save jorgedfbranco/1a6bd30c0736eddca68abb6ceaf77245 to your computer and use it in GitHub Desktop.

Building the JDK and getting the HotSpot sources loaded into CLion

As I've already found myself more than once going through the steps of building the JDK on my Ubuntu machine and getting the Hotspot's source loaded into CLion, I ended up deciding to assemble this document to help me or others in the future, would the need eventually arise.

Building the JDK should be relatively painless, and when problems arise the tools are in generally very specific about what's wrong. In the worst of scenarios, a simple google search will most likely suffice. But as there are at least some things we should be wary about, I'm writing this simple tutorial down. In case you get stuck or if you need a more thorough guide, I highly recommend https://github.com/openjdk/jdk/blob/master/doc/building.md.

Getting the Hotspot code on CLion should also be straightforward, as long as you understand that due to macros / templates and the way code is separated (shared, os-specific, architecture-specific, etc), the experience will never be as smooth as one would get with Intellij and Java.

So, without further ado, let's start:

Downloading a recent version of the JDK

To build the JDK you will need to have one already installed in your machine. We'll call this the bootstrap JDK. And it should be as recent as possible.

If you want to build JDK X, you will need a JDK X or if that is yet unavailable, JDK X-1.

Cloning the repository

We'll then clone the repo:

git clone https://github.com/openjdk/jdk.git && cd jdk

Downloading all the needed dependencies

The JDK comes with a nice tool called bash configure that will check what dependencies the JDK build might need that you don't have in your computer. You can run it with bash configure but most likely that won't be enough. I always have to run it with my JDK passed in as argument:

bash configure --with-boot-jdk=/home/<username>/bin/jdk-12.0.2 (that's where my bootstrap JDK resides)

After some computations what you'll most likely see is an error message stating that some dependencies are missing. It will also show you what you need to install (and what command to run) to get it done:

configure: error: Could not find fontconfig! You might be able to fix this by running 'sudo apt-get install libfontconfig1-dev'.

So, in this case, running sudo apt-get install libfontconfig1-dev will fix the issue. You should now run bash configure again, and at least for a couple of times more you should hit on another dependency error. Keep installing them until you have all of them.

Beware of something: it may happen that it requires you to download in a single command a bunch of dependencies. Something of the form of sudo apt install a b c. If when executing it, a fails, you should still try to sudo apt install b c, as that may actually be enough to get you going.

Building the JDK

Now, at last, you can build the JDK:

make images

  • If it complains that it doesn't recognize your operating system, make sure that you are checked out on the master branch and not on an old commit, corresponding to an older JDK. The problem is not the older JDK, but that its commits most likely correspond to a time when the current operating system you're running in at its current version still did not exist. So, either make the build out of a relatively recent commit or follow the tip given here: https://stackoverflow.com/questions/14285820/os-not-supported-error-while-building-hotspot.

  • It may also complain about having multiple options, and having to choose one. You can do it through the CONF parameter, as shown here: make images CONF=linux-x86_64-server-release.

The results of your build should be available in: XYZ

Getting the Hotspot's source code loaded into CLion

TODO:

cmake_minimum_required(VERSION 3.7)
project(hotspot)
include_directories(
        src/hotspot/cpu/x86
        src/hotspot/os/linux
        src/hotspot/os/posix
        src/hotspot/os_cpu/linux_x86
        src/hotspot/share
        src/hotspot/share/precompiled
        src/hotspot/share/include
        src/java.base/unix/native/include
        src/java.base/share/native/include
)
file(GLOB_RECURSE SOURCE_FILES "*.cpp" "*.hpp" "*.c" "*.h")
add_executable(hotspot ${SOURCE_FILES})

Building HotSpot

  • Steps to build HotSpot https://github.com/openjdk/jdk/blob/master/doc/building.md

    Beware that to compile the JDK/Hotspot version X you will need to have a boot version of at least X. That is, to compile the JDK 11 you will need at least to compile your shit against JDK 11 or 12. So, the steps in my machine are:

    • git clone https://github.com/openjdk/jdk.git && cd jdk

    • bash configure

      This step will make sure you have the proper dependencies in place. If you miss any dependencies (as you most likely will), the script will show an exception instructing you on which commands to run to download them. Beware that it may happen that it requires you to download in a single command a bunch of dependencies. Something of them form of sudo apt install a b c, and when executing it, a fails. You should still try to sudo apt install b c, as that may actually be enough to get you going.

      Also, you may need to pass an extra flag with the path of a recent JDK (at least as recent as the jdk you're trying to build, that is): bash configure --with-boot-jdk=/home/jorgedfbranco/bin/jdk-12.0.2. Be very careful about not using ~ in your path, as bash configure will not work with it.

    • make images (builds the JDK)

    • make compile-commands will generate the needed files for CLion to cleanly import the project. You can find information @ https://blog.jetbrains.com/clion/2018/08/working-with-makefiles-in-clion-using-compilation-db/ on how to then load that to CLion.

  • Debugging Hotspot with CLion (https://blog.csdn.net/wd2014610/article/details/81703203)

  • CMakeLists.txt for HotSpot to use with CLion (https://gist.github.com/jyukutyo/de3a54eaeb5408b2016785543ba85b2e) This is a nice try but unfortunately I still see most of highlighting red in CLion..

  • make compile-commands will generate the compile_commands needed to import this into CLion: https://twitter.com/shipilev/status/1093088443738013697

Object Layout in Memory

Random articles of possible interest

Chinese Articles

By some weird reason there seems to be a lot of hotspot/jdk information in these chinese websites / blogs:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment