Compiling & Debugging MariaDB(and MySQL) in Eclipse from scratch – Part 2: “Compile in Eclipse”

Section 2: “COMPILE MARIADB IN ECLIPSE”

2.1 Download and prepare sources folder

We will need a directory to use as our playground, if you create the user yoda in Section 1:

$ su - yoda
$ mkdir -p ~/playground

Download latest MariaDB 10 sources tar.gz and copy the archive into the above directory, you can latest sources from:

https://downloads.mariadb.org/

For Example, if you wanted to download MariaDB 10.0.11:

https://downloads.mariadb.org/interstitial/mariadb-10.0.11/source/mariadb-10.0.11.tar.gz/from/http://mariadb.mirror.nucleus.be/

Now you have to copy the source tarball into our working location and extract the archive:

$ cd <location of source tarball>
$ cp mariadb-10.0.11.tar.gz ~/playground
$ cd ~/playground
$ tar -zxvf mariadb-10.0.11.tar.gz

We have here one first choice to make:

  1. Work on the same source folder for building
  2. Keep the source folder clean(and reusable) and build on a separate directory

Even if it takes slightly more work at the beginning 2. is usually preferred, and we will use 2.

We will work on a sibling directory, adding a ‘-eclipse’ to the working directory:

$ mkdir mariadb-10.0.11-eclipse
$ cd mariadb-10.0.11-eclipse

2.2 Prepare project for compilation and make a simple test

We will now prepare the project with cmake specifying the sibling source directory, and two parameters more that will be explained:

$ cmake -DBUILD_CONFIG=mysql_release -DCMAKE_BUILD_TYPE=debug ../mariadb-10.0.11

Parameters explanation:

#1: -DBUILD_CONFIG=mysql_release
Use the same configuration of the official mysql/mariadb standard builds.

#2: -DCMAKE_BUILD_TYPE=debug
We want a debug build to help debugging our instance. It will generate debugging information and include them in the compiled files.

#3: ../mariadb-10.0.11
Folder containing our source distribution.

At this point want to test if our system can compile properly, and we can do this at command line simply with:

$ make

This can take anywehere between 5 to 60 minutes depending on your hardware, if everything is ok no Error messages will be output.

Note: Do not proceed further if you can’t compile at command line, it will not work in Eclipse either.

2.3 Prepare the project for compilation under Eclipse

Note: Eclipse lacks an official cmake plugin, but preparing the project for Eclipse with cmake on command line is very straightforward.

To prepare the project for Eclipse we add two additional parameters, passing a total of 5 parameters to cmake:

#1: -DBUILD_CONFIG=mysql_release
Use the same configuration of the official mysql/mariadb standard builds.

#2: -DCMAKE_BUILD_TYPE=debug
We want a debug build to help debugging our instance. It will generate debugging information and include them in the compiled files.

#3: ../mariadb-10.0.11
Folder containing our source distribution.

Two extra parameters for Eclipse:

#4: -G"Eclipse CDT4 - Unix Makefiles"
We are telling to generate Makefiles specifically for Eclipse (don’t know the details, and it works also without this flag)

#5: -DECLIPSE_CDT4_GENERATE_SOURCE_PROJECT=TRUE
Since Eclipse can’t browse files outside the root folder of the project AND we are using a sibling folder for building, we have rather hard time in browsing the sources in Eclipse IDE to add breakpoints and debug.
This parameter will include sources in the project treeview that will contain pointers to the sibling source folder and files.

$ cmake -DBUILD_CONFIG=mysql_release -DCMAKE_BUILD_TYPE=debug -G"Eclipse CDT4 - Unix Makefiles" -DECLIPSE_CDT4_GENERATE_SOURCE_PROJECT=TRUE ../mariadb-10.0.11

Once this is done with no errors the project is ready to be imported into Eclipse.

NOTE: In cmake versions 2.8.7 and above the flag -DECLIPSE_CDT4_GENERATE_SOURCE_PROJECT=TRUE has been replaced by -DCMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT (Thanks Pól!).

2.4 Compile the project in Eclipse:

Start Eclipse CDT and select from menu "File-->New-->Makefile Project with existing code" in the popup window you need to provide three information:

  1. A name for the project (e.g.: MariaDB10-test1)
  2. The source folder
  3. The toolchain used (for the Eclipse indexer)

For Point 1:

Specify any name, e.g. MariaDB10-test1

For Point 2:

Browse and provide the mariadb-10.0.11-eclipse folder location created at 2.1.

This folder contains the makefile project created with cmake and, as said before, it is separated from the source folder, so that we can do several tests without impacting the original source folder.
The project ‘knows’ where to find the sources (which, if you followed the instructions in this guide, will be a sibling folder), and thanks to ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT=TRUE flag given to cmake, Eclipse will show the source folder and files, as it were contained in the project folder (it is a link), the node should be named ‘MySQL’, you can distinguish a linked resource by a tiny arrow on the yellow folder icon.

For Point 3:

In the “Toolchain for indexer setting” listbox you should use the toolchain in use on your system, Linux GCC should be fine in most cases. This setting will be used for things like keyword autocomplete.

Click on Finish and go back to the newly created project MariaDB10-test1.

Now you can already try to build it by:
Right Click on Project "MariaDB10-test1--> Build Project"

If all is good it will start building and it could take several minutes, watch progress % and possible errors in the Eclipse log console in the lower part of the screen, it should finally show something like:

[99%] ...... [100%] build target my_safe_process ... Build Finished (took....)

Note: Sometimes Eclipse does seems not picking up a change immediately and strange things can happen like a context menu voice greyed-out. In such cases you can try doing a: Project–> Refresh.

If all is good you can now proceed to Part 3 or read again Part 1.
This is the End of Part 2.