Skip to content

Commit

Permalink
Merge pull request #8 from rmspacefish/develop
Browse files Browse the repository at this point in the history
Use proper CMake toolchain file now
  • Loading branch information
robamu authored Dec 31, 2020
2 parents 46d4298 + 50e73fe commit a2bd929
Show file tree
Hide file tree
Showing 13 changed files with 545 additions and 738 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.project
.idea
!.idea/runConfigurations
45 changes: 45 additions & 0 deletions BuildType.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function(set_build_type)

message(STATUS "Used build generator: ${CMAKE_GENERATOR}")

# Set a default build type if none was specified
set(DEFAULT_BUILD_TYPE "RelWithDebInfo")
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
set(DEFAULT_BUILD_TYPE "Debug")
endif()

if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS
"Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified."
)
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE
STRING "Choose the type of build." FORCE
)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Release" "MinSizeRel" "RelWithDebInfo"
)
endif()

if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
message(STATUS
"Building Debug application with flags: ${CMAKE_C_FLAGS_DEBUG}"
)
elseif(${CMAKE_BUILD_TYPE} MATCHES "RelWithDebInfo")
message(STATUS
"Building Release (Debug) application with "
"flags: ${CMAKE_C_FLAGS_RELWITHDEBINFO}"
)
elseif(${CMAKE_BUILD_TYPE} MATCHES "MinSizeRel")
message(STATUS
"Building Release (Size) application with "
"flags: ${CMAKE_C_FLAGS_MINSIZEREL}"
)
else()
message(STATUS
"Building Release (Speed) application with "
"flags: ${CMAKE_C_FLAGS_RELEASE}"
)
endif()

endfunction()
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 2-Clause License

Copyright (c) 2020, Spacefish
Copyright (c) 2020, Robin Mueller
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
61 changes: 33 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# RTEMS CMake Build Support

This repository contains the first version of a possible RTEMS CMake build support. The intention is to provide most CMake configuration to perform cross-compiling of RTEMS applications and provide a decent starting point for developers which would like to build their RTEMS application with CMake. The support has been written as generic as possible.
This repository contains the first version of a possible RTEMS CMake build support. The intention is to provide most CMake configuration to perform cross-compiling of RTEMS applications and provide a decent starting point for developers which would like to build their RTEMS application with CMake. The support has been written as generic as possible and only required a few lines of code is the application `CMakeLists.txt` file and some necessary variables set to determine compiler information.

This is still a prototype. Simple applications have been tested, but it has not been attempted to link an additional library for an application yet.
It is assumed that the RTEMS tools and the BSPs have already been built. If you are a beginner and this is not the case, it is recommended to have a look at [this demo](https://github.com/rmspacefish/rtems-demo) or the [QuickStart](https://docs.rtems.org/branches/master/user/start/index.html) to get started with RTEMS.

The usual way to set up a cross-compile project in CMake is to specify a CMake toolchain file. The CMake RTEMS support uses a combination of the supplied RTEMS BSP,
RTEMS version, RTEMS prefix and the `pkgconfig` utility to set up the RTEMS environment properly so that application developers can focus on their application.

This is still a prototype. Simple applications have been tested, but no larger projects have been compiled with this build support yet.
The compilation of simple applications was tested on Windows 10 and Ubuntu 20.04.
Improvements, suggestions and pull requests are welcome :-)

## How to use

Expand All @@ -22,61 +29,59 @@ set(RTEMS_CONFIG_DIR
)
```

It is also recommended to add the following lines before the `project()` call in
the application `CMakeLists.txt`:
We need to prepare some internal environmental variables for the CMake toolchain file and we also need to process information like the supplied `RTEMS_BSP` and `RTEMS_PREFIX` for the PKG config utility.
Add the following lines of code before the `project` call in your `CMakeLists.txt`
to call `rtems_pre_project_config` and pass `RTEMS_PREFIX` and `RTEMS_BSP` into the function call.

```sh
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_C_COMPILER_WORKS 1)
set(CMAKE_CXX_COMPILER_WORKS 1)
set(CMAKE_CROSSCOMPILING 1)
include(${RTEMS_CONFIG_DIR}/RTEMSPreProjectConfig.cmake)
rtems_pre_project_config(${RTEMS_PREFIX} ${RTEMS_BSP})
```

This will disable the compiler checks for the standard C/C++ compiler.


If this repository was cloned inside the application root, the path can be
set to `${CMAKE_CURRENT_SOURCE_DIRECTORY}/rtems-cmake`.
Now the CMake environment is prepared for the toolchan file. We set the `CMAKE_TOOLCHAIN_FILE` to CMake can set up the compilers and required RTEMS flags properly in the `project` call:

After that, include the general configuration file with the following line:
Add the following lines of code before the `project` call:

```sh
include("${RTEMS_CONFIG_DIR}/RTEMSConfig.cmake")
set(CMAKE_TOOLCHAIN_FILE ${RTEMS_CONFIG_DIR}/RTEMSToolchain.cmake)
```

And then call the configuration function:
Finally, you can add the following lines to call the post-project configuration,
passing the target name into the function call

```sh
rtems_general_config(<TargetName> <RTEMS Prefix> <RTEMS BSP>)
include("${RTEMS_CONFIG_DIR}/RTEMSPostProjectConfig.cmake")
rtems_post_project_config(${TARGET_NAME})
```

This function will call the the `rtems_generic_config` function internally to set up the cross-compiler, using the provided RTEMS prefix and the BSP name,
and the RTEMS BSP (e.g. sparc/erc32) to this function.
This is not mandatory yet, but is useful for additional debug information (if `RTEMS_VERBOSE` is set to `TRUE`) and might become useful in the future if some additional target specific properties need to be set for RTEMS.

It is recommended to either hardcode mandatory values like the prefix and BSP path in the application `CMakeLists.txt` (especially when using the CMake GUI) or to supply them via command line and write scripts to ease this process.

After that, it will call the the function `rtems_hw_config` which will assign necessary compiler and linker flags to the provided target.

## Optional configuration of the CMake support

The RTEMS CMake build support can be configured either by passing configuration options prepended with `-D` to the build command or by setting these build variables in the application `CMakeLists.txt` before calling the build support. Following options are available

- `RTEMS_VERSION`: Can be specified manually. If this is not specified, the CMake build support will attempt to extract the version number from the RTEMS prefix (last letter of path). This variable needs to be valid for the RTEMS support to work!
- `RTEMS_VERBOSE`: Enable additional diagnostic output.
- `RTEMS_SCAN_PKG_CONFIG`: The RTEMS CMake support will try to read the pkgconfig files to extract compile and link flag options.
- `RTEMS_TOOLS`: Can be specified if the RTEMS tools folder. Can be different from the prefix but will be equal to the prefix for most users.
- `RTEMS_PATH`: Folder containing the RTEMS installation (BSPs). Can be different from the prefix but will be equal to the prefix for most users.

## CMake build configuration helper

A small python script is provided in the build support to allow easier configuration of the CMake build systems when using RTEMS. Call `cmake_build_config.py --help` to get
some information how to configure a build. Python 3 has to be installed to use this script.

## Extending the build system support

It is possible to read the pkfconfig files now, so extending the manual build configuration might not be necessary in the future.

Extending the build support is relatively easy:

Extract the necessary compiler and linker flags for the RTEMS build from the pkgconfig file
for the specific BSP. This file will generally be located inside the `lib/pkgconfig` folder of the RTEMS tools folder. Add these flags in the `RTEMSHardware.cmake` file for your specific BSP.
If this becomes necessary after all, follow these steps:

When building with CMake, `-v` can be added to verify the flags.
Extract the necessary compiler and linker flags for the RTEMS build from the pkgconfig file for the specific BSP. This file will generally be located inside the `lib/pkgconfig` folder of the RTEMS tools folder. Add these flags manually before the `project` file call (see `RTEMSToolchain.cmake` for examples) for your specific BSP.

## Example
## Examples

See https://github.com/rmspacefish/rtems-demo/tree/master/applications/hello for an example. This is the Hello World project taken from the RTEMS quick start guide,
but compiled using RTEMS. The repository also contains instructions on how to build the RTEMS tools if required and all specific steps to build with CMake.
but compiled using RTEMS. The repository also contains instructions on how to build the RTEMS tools if required and all specific steps to build with CMake and a blinky example for the STM32H743ZI-Nucleo board.
135 changes: 135 additions & 0 deletions RTEMSCompilerConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
function(rtems_compiler_config RTEMS_PREFIX RTEMS_BSP)

message(STATUS "Setting up and checking RTEMS cross compile configuration..")


set(RTEMS_PREFIX ${RTEMS_PREFIX} CACHE FILEPATH "RTEMS prefix")
set(RTEMS_BSP ${RTEMS_BSP} CACHE STRING "RTEMS BSP")

if(NOT DEFINED RTEMS_VERSION)
if(NOT DEFINED ENV{RTEMS_VERSION})
message(WARNING
"No RTEMS verson specified via argument or in"
" environment variables!"
)
else()
set(RTEMS_VERSION $ENV{RTEMS_VERSION})
set(RTEMS_VERSION ${RTEMS_VERSION} CACHE STRING "RTEMS version")
endif()
endif()

if(NOT RTEMS_VERSION)
message(STATUS "No RTEMS_VERSION supplied.")
message(STATUS "Autodetermining version from tools path ${RTEMS_TOOLS} ..")
string(REGEX MATCH [0-9]+$ RTEMS_VERSION "${RTEMS_TOOLS}")
message(STATUS "Version ${RTEMS_VERSION} found")
set(RTEMS_VERSION ${RTEMS_VERSION} CACHE STRING "RTEMS version")
endif()

set(RTEMS_INSTALL
${CMAKE_INSTALL_PREFIX}
CACHE FILEPATH "RTEMS install destination"
)

if(NOT RTEMS)
if(NOT $ENV{RTEMS})
message(STATUS
"RTEMS path was not specified and was set to RTEMS prefix."
)
else()
set(RTEMS $ENV{RTEMS})
endif()

set(RTEMS ${RTEMS_PREFIX} CACHE FILEPATH "RTEMS folder")
endif()

set(RTEMS ${RTEMS} CACHE FILEPATH "RTEMS folder")

if(NOT RTEMS_TOOLS)
if(NOT $ENV{RTEMS})
message(STATUS
"RTEMS toolchain path was not specified and was set to RTEMS prefix."
)
else()
set(RTEMS_TOOLS $ENV{RTEMS_TOOLS})
endif()

set(RTEMS_TOOLS ${RTEMS_PREFIX} CACHE FILEPATH "RTEMS tools folder")
endif()



if(NOT ENV{RTEMS_ARCH_VERSION_NAME})
string(REPLACE "/" ";" RTEMS_BSP_LIST_SEPARATED ${RTEMS_BSP})
list(LENGTH RTEMS_BSP_LIST_SEPARATED BSP_LIST_SIZE)

if(NOT ${BSP_LIST_SIZE} EQUAL 2)
message(FATAL_ERROR
"Supplied RTEMS_BSP variable invalid. "
"Make sure to provide a slash separated string"
)
endif()

list(GET RTEMS_BSP_LIST_SEPARATED 0 RTEMS_ARCH_NAME)
list(GET RTEMS_BSP_LIST_SEPARATED 1 RTEMS_BSP_NAME)

set(RTEMS_ARCH_VERSION_NAME
"${RTEMS_ARCH_NAME}-rtems${RTEMS_VERSION}"
CACHE STRING
"Architecture-BSP-Version Identifier"
)

endif()

set(RTEMS_ARCH_LIB_PATH "${RTEMS}/${RTEMS_ARCH_VERSION_NAME}/lib")
set(RTEMS_TOOLS_LIB_PATH "${RTEMS_TOOLS}/lib")

set(RTEMS_BSP_PATH "${RTEMS}/${RTEMS_ARCH_VERSION_NAME}/${RTEMS_BSP_NAME}")
if(NOT IS_DIRECTORY ${RTEMS_BSP_PATH})
message(STATUS
"Supplied or autodetermined BSP path "
"${RTEMS_BSP_PATH} is invalid!"
)
message(FATAL_ERROR
"Please check the BSP path or make sure "
"the BSP is installed."
)
endif()

set(RTEMS_BSP_LIB_PATH "${RTEMS_BSP_PATH}/lib")
if(NOT IS_DIRECTORY "${RTEMS_BSP_LIB_PATH}")
message(FATAL_ERROR
"RTEMS BSP lib folder not found at "
"${RTEMS_BSP_LIB_PATH}"
)
endif()

set(RTEMS_BSP_INC_PATH "${RTEMS_BSP_LIB_PATH}/include")
if(NOT IS_DIRECTORY "${RTEMS_BSP_INC_PATH}")
message(FATAL_ERROR
"RTEMS BSP include folder not found at "
"${RTEMS_BSP_INC_PATH}"
)
endif()

# Variables set in the cache so they can be used everywhere.
set(RTEMS_ARCH_NAME ${RTEMS_ARCH_NAME} CACHE FILEPATH "Architecture name")
set(RTEMS_BSP_NAME ${RTEMS_BSP_NAME} CACHE FILEPATH "BSP name")
set(RTEMS_TOOLS_LIB_PATH ${RTEMS_TOOLS_LIB_PATH}
CACHE FILEPATH "Tools library path"
)
set(RTEMS_BSP_LIB_PATH ${RTEMS_BSP_LIB_PATH} CACHE FILEPATH "BSP library path")
set(RTEMS_BSP_INC_PATH ${RTEMS_BSP_INC_PATH} CACHE FILEPATH "BSP include path")
set(RTEMS_ARCH_LIB_PATH ${RTEMS_ARCH_LIB_PATH}
CACHE FILEPATH "Architecture library path"
)
set(RTEMS_ARCH_VERSION_NAME ${RTEMS_ARCH_VERSION_NAME}
CACHE FILEPATH "Unique architecture-version identifier"
)

list(APPEND CMAKE_PREFIX_PATH ${RTEMS_BSP_LIB_PATH})
list(APPEND CMAKE_PREFIX_PATH ${RTEMS_BSP_INC_PATH})
list(APPEND CMAKE_PREFIX_PATH ${RTEMS_ARCH_LIB_PATH})
list(APPEND CMAKE_PREFIX_PATH ${RTEMS_TOOLS_LIB_PATH})

endfunction()
76 changes: 0 additions & 76 deletions RTEMSConfig.cmake

This file was deleted.

Loading

0 comments on commit a2bd929

Please sign in to comment.