Hi everyone,
After days of figuring out how CMake works, I try to write a CMakeList.txt myself, and decide to put my progress on this community. Right now, using CLion with the CMakeList can compile the hex and binary file successfully. I think the first version is not tiny, but it's easy to read, modify and transplant in your project. Hopefully, we can make the project together, and more clear for developers to build their own cross-compiling platform.
0 Before we start. Environment.
My OS is Ubuntu 18.04 LTS, which is installed in a Dell XPS laptop. Make sure you have downloaded the ARM compiling toolchain and set the environment variable.
The ARM Toolchain I used is: gcc-arm-none-eabi-6-2017-q2-update.
Also, my GCC and G++ versions in Ubuntu 18.04 have been downgraded to 4.8.5. It seems that we don't have a chance to use it in this project.
Download the latest version of CLion of Jetbrain. And activate the product.
1 The first step. Setting Up.
After the system is ready, we are going to the first step. Download the nRF52 SDK from Nordic website. The SDK version I used is nRF5_SDK_15.1.0. Then extract the compressed file to a folder, like nRF5SDK in your home directory.
2 The CMake List
CLion is a brilliant product for developers to build their product on a cross-compiling platform, a lot of features of CLion make your developing work faster and easier, yeah, the IAR and Keil are not smart yet, but anyway, those two products are easier for you to compile and download, they are excellent products.
CLion uses CMake to help you create the correct compiling file, the Makefile, then build and link all the output files to the target file you want, like the hex file and binary file we needed in the embedded system development. The first thing we are going to do, is to write a feasible CMakeLists.txt, which is used by CMake tools in CLion.
In CLion, choose File -> Open, open the SDK folder you have extracted. The entire project is imported now.
Then, create a new empty 'CMakeLists.txt' in the SDK root directory. CMakeLists.txt is like your code file, can be an entire file or many separated files. I choose a multiple layer structure to manage every sub cmakelist files, that makes the compiling structure more clearly. In the root CMakeList.txt file, we need to set some variables.
# CMakeList must have a minimum required version in the root file cmake_minimum_required(VERSION 3.6) # Set C/C++ standard set(CMAKE_C_STANDARD 99) set(CMAKE_CXX_STANDARD 98) # Indicate the toolchain path in your system set(ARM_TOOLCHAIN_PATH /usr/local/gcc-arm-none-eabi-6-2017-q2-update/bin) set(ARM_TOOLCHAIN_PREFIX arm-none-eabi-) # Call the sub cmakelist file add_subdirectory("examples/ble_peripheral/ble_app_hrs/pca10056/s140/armgcc")
That is, easy to write, right? Now we enter the complicated part. Let go deeper into the example you want to build. I choose ble_peripheral -> ble_app_hrs. Since we use armgcc compiler, we go to that folder in the ble_app_hrs project, a Makefile is there. Figuring out the makefile is helpful for us to comprehend what the compiler is doing when we compile the project. Let's open the makefile, and I will talk about it in some phrases. The makefile usually consists of these parts:
Variables.
Directories. Include directories, source files directories, and library directories. This part is same as you adding source files and include files in Keil or IAR.
Flags. C flags, assembler flags, and linker flags. This part is same as you setting the flags in Keil and IAR.
Definitions. The pre-definitions we usually fill in the IDE.
Customized commands. Like clean the build directory, flash the hex or bin into the DK.
As we quickly learned the structure of a Makefile, we can follow the pattern and write the same thing in CMake language. The goal of CMake is simple, helps you generate the Makefile. Create a new 'CMakeLists.txt' in the current folder, under the ble_app_hrs/pca10056/s140/armgcc, this is the example project I choose.
The file you can fetch from my Git repo, it is not hard to understand with the comments. You only need some basic CMake commands. After the CMakeLists.txt file is finished, you can build the project in CLion. Enjoy!
The git repo: github.com/.../nRF52xCLion.git
Issues: In the ble_app_hrs project, after adding the micro_ecc_lib_nrf52.a in linking stage, the linker will report a error. I am not sure about the reason. I think the project doesn't use the micro-ecc, so the library cannot be added.