Using CMake to Generate Eclipse Projects
Tags: cmake, eclipse, toolchains, embedded
Published: 2018-11-10 01:43:17

---
title: Using CMake to Generate Eclipse Projects
published: true
description: 
series: AlphaAutomata
tags: cmake, eclipse, toolchains, embedded
---

# The Thing

As of an [issue]-closing [commit] today, [Alpha RTOS] has a build process I'm happy to use to develop it with.

Using a CMake [project script] with an associated [toolchain script], I am now able to run compilation of the Alpha kernel in the following environments:

- Xilinx SDK on Windows
- Ubuntu (in the Windows Subsystem for Linux flavor)

I'm sad that Visual Studio 2017 is not on that list, but for some reason CMake refuses to discover the Microsoft compiler.

# The Background

In my [first ever post], I summarized [Alpha Automata]'s origins, its status at the time, and where I want to develop it into.

Since this project involves VHDL modules, a lot of my development will occur on a Xilinx Zynq SoC. My current priority is to get the [Alpha RTOS] kernel running, for several reasons:

- I want to deepen my knowledge of real-time operating systems.
- I want to design and implement a somewhat complex, highly modular system that I can be proud of. None of the other [Alpha Automata] components are nearly as complex as an RTOS.
- A [C++Now 2018 talk] gave me, for the first time, a good example of how to use CMake toolchain files. This inspired me to lay the groundwork for using CI/CD in [Alpha Automata].
- Having a working RTOS makes writing and running test code snippets for every other part of [Alpha Automata] easier.

The last two bullet points in that list led me to create CMake scripts for [Alpha RTOS] now, instead of just using projects manually set up in Xilinx SDK and leaving CMake for later.

# The CMaking Of

The [C++Now 2018 talk] showed CMake examples for compiling bare-metal executable images outside of any IDEs. For [Alpha RTOS], I want to compile a bare-metal static library in Xilinx SDK. Compiling a static library using Xilinx SDK allows me to skip the tedium of studying the Zynq memory map to create a linker script and trying to figure out how to compile Xilinx libraries that initialize Zynq-specific registers. Using Xilinx SDK forces me to learn how to generate Eclipse projects using CMake.

I welcomed the chance to learn how to generate Eclipse projects using CMake. When I was in [Wisconsin Robotics], we created a [packet library] built using CMake to target Linux and Windows. I wanted the embedded target build system (we used Code Composer Studio at the time) to also use CMake, but we never found the time to make that happen.

The [first version] of the CMake scripts for [Alpha RTOS] closely mirror the examples from the [C++Now 2018 talk]. This was my first attempt at writing a cross-compiling CMake script, and I only wanted to get it to work at all. The [first version] of these scripts could run compilations targeting the host system, and cross-compile on Linux. However, I couldn't generate an eclipse project using the Eclipse CDT generators provided by CMake. Even if I gave CMake the proper paths to the ARM-target compilers, the CMake Eclipse CDT generators kept making this complaint:

    CMake Error: CMake was unable to find a build program corresponding to "Unix
    Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a
    different build tool.

After some reading and experimentation, I figured out that I needed to point CMake to a proper `make` executable. After some more digging around, I found out that Xilinx distributes a bunch of GNU tools, including `make` and `gcc`, with Xilinx SDK. Armed with this new knowledge, I [made improvements] to the [Alpha RTOS] CMake scripts. They toolchain script now makes use of the tools distributed by Xilinx when given a Xilinx SDK version and the Xilinx SDK installation directory.

This generated Eclipse project files that I could import into Xilinx SDK, but for some weird reason, the project wouldn't compile. The compiler kept complaining about not being able to find any of the `#include` files. I scoured the Internet for answers, but found none. Finally, I took the compile command from the SDK console and tried playing around with it in PowerShell. It took me less than ten minutes to figure out what hours of Google searching couldn't tell me: the GCC version distributed by Xilinx doesn't process `-I` include paths that have `..` in them.

Running `gcc` with this flag works:

    -IC:/SVC/AlphaAutomata/AlphaRTOS/api

Running `gcc` with this flag fails:

    -IC:/SVC/AlphaAutomata/AlphaRTOS/build/../api

My CMake script set parent directories using `set` and the `..` special directory:

```cmake
set(PARENT_DIR ${CURRENT_DIR}/..)
```

Using `get_filename_component` fixed my problem:

```cmake
get_filename_component(PARENT_DIR ${CURRENT_DIR} DIRECTORY)
```

With this fix in the [latest version], I can now run compilation using a Xilinx SDK project generated using CMake.

# The State of Alpha

Since last month, not much has changed. I've defined the [Alpha RTOS] API and internal architecture some more, but the kernel is nowhere near a usable state. Now that the CMake scripts are working, I hope to make faster progress.

# Onwards and Upwards

I will continue working on [Alpha RTOS] to get the kernel running and solidify the API.

I made a promise to post on Dev every Wednesday, but the last time I posted was a month ago. I can't stick to a weekly schedule because of work and other life pressures, but I will try really hard to post every two weeks.

[commit]: https://github.com/AlphaAutomata/AlphaRTOS/commit/0a0165c3c98175a44c7848c156de1f5678cc3e18
[Alpha RTOS]: https://github.com/AlphaAutomata/AlphaRTOS
[issue]: https://github.com/AlphaAutomata/AlphaRTOS/issues/6
[project script]: https://github.com/AlphaAutomata/AlphaRTOS/blob/master/build/CMakeLists.txt
[toolchain script]: https://github.com/AlphaAutomata/AlphaRTOS/blob/master/build/toolchain.cmake
[first ever post]: https://dev.to/zhu48/alphaautomata---2018-10-03-471f
[Alpha Automata]: https://github.com/AlphaAutomata
[C++Now 2018 talk]: https://www.youtube.com/watch?v=c9Xt6Me3mJ4&t=2342s
[Wisconsin Robotics]: https://github.com/WisconsinRobotics
[packet library]: https://github.com/WisconsinRobotics/BadgerCommandLibrary
[first version]: https://github.com/AlphaAutomata/AlphaRTOS/commit/98c2bcadda4a50ea58bc27492a7510c52723986c#diff-58b29f322932c0c2e13d8ead532526bb
[made improvements]: https://github.com/AlphaAutomata/AlphaRTOS/commit/036c626aecb7e75f12e8ee96129ba7c6394552ae#diff-58b29f322932c0c2e13d8ead532526bb