Skip to content

Commit a2bd929

Browse files
authored
Merge pull request #8 from rmspacefish/develop
Use proper CMake toolchain file now
2 parents 46d4298 + 50e73fe commit a2bd929

13 files changed

+545
-738
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.project
2+
.idea
3+
!.idea/runConfigurations

BuildType.cmake

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function(set_build_type)
2+
3+
message(STATUS "Used build generator: ${CMAKE_GENERATOR}")
4+
5+
# Set a default build type if none was specified
6+
set(DEFAULT_BUILD_TYPE "RelWithDebInfo")
7+
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
8+
set(DEFAULT_BUILD_TYPE "Debug")
9+
endif()
10+
11+
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
12+
message(STATUS
13+
"Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified."
14+
)
15+
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE
16+
STRING "Choose the type of build." FORCE
17+
)
18+
# Set the possible values of build type for cmake-gui
19+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
20+
"Debug" "Release" "MinSizeRel" "RelWithDebInfo"
21+
)
22+
endif()
23+
24+
if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
25+
message(STATUS
26+
"Building Debug application with flags: ${CMAKE_C_FLAGS_DEBUG}"
27+
)
28+
elseif(${CMAKE_BUILD_TYPE} MATCHES "RelWithDebInfo")
29+
message(STATUS
30+
"Building Release (Debug) application with "
31+
"flags: ${CMAKE_C_FLAGS_RELWITHDEBINFO}"
32+
)
33+
elseif(${CMAKE_BUILD_TYPE} MATCHES "MinSizeRel")
34+
message(STATUS
35+
"Building Release (Size) application with "
36+
"flags: ${CMAKE_C_FLAGS_MINSIZEREL}"
37+
)
38+
else()
39+
message(STATUS
40+
"Building Release (Speed) application with "
41+
"flags: ${CMAKE_C_FLAGS_RELEASE}"
42+
)
43+
endif()
44+
45+
endfunction()

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
BSD 2-Clause License
22

3-
Copyright (c) 2020, Spacefish
3+
Copyright (c) 2020, Robin Mueller
44
All rights reserved.
55

66
Redistribution and use in source and binary forms, with or without

README.md

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
# RTEMS CMake Build Support
22

3-
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.
3+
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.
44

5-
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.
5+
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.
6+
7+
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,
8+
RTEMS version, RTEMS prefix and the `pkgconfig` utility to set up the RTEMS environment properly so that application developers can focus on their application.
9+
10+
This is still a prototype. Simple applications have been tested, but no larger projects have been compiled with this build support yet.
11+
The compilation of simple applications was tested on Windows 10 and Ubuntu 20.04.
12+
Improvements, suggestions and pull requests are welcome :-)
613

714
## How to use
815

@@ -22,61 +29,59 @@ set(RTEMS_CONFIG_DIR
2229
)
2330
```
2431

25-
It is also recommended to add the following lines before the `project()` call in
26-
the application `CMakeLists.txt`:
32+
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.
33+
Add the following lines of code before the `project` call in your `CMakeLists.txt`
34+
to call `rtems_pre_project_config` and pass `RTEMS_PREFIX` and `RTEMS_BSP` into the function call.
2735

2836
```sh
29-
set(CMAKE_SYSTEM_NAME Generic)
30-
set(CMAKE_C_COMPILER_WORKS 1)
31-
set(CMAKE_CXX_COMPILER_WORKS 1)
32-
set(CMAKE_CROSSCOMPILING 1)
37+
include(${RTEMS_CONFIG_DIR}/RTEMSPreProjectConfig.cmake)
38+
rtems_pre_project_config(${RTEMS_PREFIX} ${RTEMS_BSP})
3339
```
3440

35-
This will disable the compiler checks for the standard C/C++ compiler.
36-
37-
38-
If this repository was cloned inside the application root, the path can be
39-
set to `${CMAKE_CURRENT_SOURCE_DIRECTORY}/rtems-cmake`.
41+
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:
4042

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

4345
```sh
44-
include("${RTEMS_CONFIG_DIR}/RTEMSConfig.cmake")
46+
set(CMAKE_TOOLCHAIN_FILE ${RTEMS_CONFIG_DIR}/RTEMSToolchain.cmake)
4547
```
4648

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

4952
```sh
50-
rtems_general_config(<TargetName> <RTEMS Prefix> <RTEMS BSP>)
53+
include("${RTEMS_CONFIG_DIR}/RTEMSPostProjectConfig.cmake")
54+
rtems_post_project_config(${TARGET_NAME})
5155
```
5256

53-
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,
54-
and the RTEMS BSP (e.g. sparc/erc32) to this function.
57+
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.
58+
59+
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.
5560

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

5862
## Optional configuration of the CMake support
5963

6064
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
6165

6266
- `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!
6367
- `RTEMS_VERBOSE`: Enable additional diagnostic output.
64-
- `RTEMS_SCAN_PKG_CONFIG`: The RTEMS CMake support will try to read the pkgconfig files to extract compile and link flag options.
6568
- `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.
6669
- `RTEMS_PATH`: Folder containing the RTEMS installation (BSPs). Can be different from the prefix but will be equal to the prefix for most users.
6770

71+
## CMake build configuration helper
72+
73+
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
74+
some information how to configure a build. Python 3 has to be installed to use this script.
75+
6876
## Extending the build system support
6977

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

72-
Extending the build support is relatively easy:
73-
74-
Extract the necessary compiler and linker flags for the RTEMS build from the pkgconfig file
75-
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.
80+
If this becomes necessary after all, follow these steps:
7681

77-
When building with CMake, `-v` can be added to verify the flags.
82+
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.
7883

79-
## Example
84+
## Examples
8085

8186
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,
82-
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.
87+
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.

RTEMSCompilerConfig.cmake

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
function(rtems_compiler_config RTEMS_PREFIX RTEMS_BSP)
2+
3+
message(STATUS "Setting up and checking RTEMS cross compile configuration..")
4+
5+
6+
set(RTEMS_PREFIX ${RTEMS_PREFIX} CACHE FILEPATH "RTEMS prefix")
7+
set(RTEMS_BSP ${RTEMS_BSP} CACHE STRING "RTEMS BSP")
8+
9+
if(NOT DEFINED RTEMS_VERSION)
10+
if(NOT DEFINED ENV{RTEMS_VERSION})
11+
message(WARNING
12+
"No RTEMS verson specified via argument or in"
13+
" environment variables!"
14+
)
15+
else()
16+
set(RTEMS_VERSION $ENV{RTEMS_VERSION})
17+
set(RTEMS_VERSION ${RTEMS_VERSION} CACHE STRING "RTEMS version")
18+
endif()
19+
endif()
20+
21+
if(NOT RTEMS_VERSION)
22+
message(STATUS "No RTEMS_VERSION supplied.")
23+
message(STATUS "Autodetermining version from tools path ${RTEMS_TOOLS} ..")
24+
string(REGEX MATCH [0-9]+$ RTEMS_VERSION "${RTEMS_TOOLS}")
25+
message(STATUS "Version ${RTEMS_VERSION} found")
26+
set(RTEMS_VERSION ${RTEMS_VERSION} CACHE STRING "RTEMS version")
27+
endif()
28+
29+
set(RTEMS_INSTALL
30+
${CMAKE_INSTALL_PREFIX}
31+
CACHE FILEPATH "RTEMS install destination"
32+
)
33+
34+
if(NOT RTEMS)
35+
if(NOT $ENV{RTEMS})
36+
message(STATUS
37+
"RTEMS path was not specified and was set to RTEMS prefix."
38+
)
39+
else()
40+
set(RTEMS $ENV{RTEMS})
41+
endif()
42+
43+
set(RTEMS ${RTEMS_PREFIX} CACHE FILEPATH "RTEMS folder")
44+
endif()
45+
46+
set(RTEMS ${RTEMS} CACHE FILEPATH "RTEMS folder")
47+
48+
if(NOT RTEMS_TOOLS)
49+
if(NOT $ENV{RTEMS})
50+
message(STATUS
51+
"RTEMS toolchain path was not specified and was set to RTEMS prefix."
52+
)
53+
else()
54+
set(RTEMS_TOOLS $ENV{RTEMS_TOOLS})
55+
endif()
56+
57+
set(RTEMS_TOOLS ${RTEMS_PREFIX} CACHE FILEPATH "RTEMS tools folder")
58+
endif()
59+
60+
61+
62+
if(NOT ENV{RTEMS_ARCH_VERSION_NAME})
63+
string(REPLACE "/" ";" RTEMS_BSP_LIST_SEPARATED ${RTEMS_BSP})
64+
list(LENGTH RTEMS_BSP_LIST_SEPARATED BSP_LIST_SIZE)
65+
66+
if(NOT ${BSP_LIST_SIZE} EQUAL 2)
67+
message(FATAL_ERROR
68+
"Supplied RTEMS_BSP variable invalid. "
69+
"Make sure to provide a slash separated string"
70+
)
71+
endif()
72+
73+
list(GET RTEMS_BSP_LIST_SEPARATED 0 RTEMS_ARCH_NAME)
74+
list(GET RTEMS_BSP_LIST_SEPARATED 1 RTEMS_BSP_NAME)
75+
76+
set(RTEMS_ARCH_VERSION_NAME
77+
"${RTEMS_ARCH_NAME}-rtems${RTEMS_VERSION}"
78+
CACHE STRING
79+
"Architecture-BSP-Version Identifier"
80+
)
81+
82+
endif()
83+
84+
set(RTEMS_ARCH_LIB_PATH "${RTEMS}/${RTEMS_ARCH_VERSION_NAME}/lib")
85+
set(RTEMS_TOOLS_LIB_PATH "${RTEMS_TOOLS}/lib")
86+
87+
set(RTEMS_BSP_PATH "${RTEMS}/${RTEMS_ARCH_VERSION_NAME}/${RTEMS_BSP_NAME}")
88+
if(NOT IS_DIRECTORY ${RTEMS_BSP_PATH})
89+
message(STATUS
90+
"Supplied or autodetermined BSP path "
91+
"${RTEMS_BSP_PATH} is invalid!"
92+
)
93+
message(FATAL_ERROR
94+
"Please check the BSP path or make sure "
95+
"the BSP is installed."
96+
)
97+
endif()
98+
99+
set(RTEMS_BSP_LIB_PATH "${RTEMS_BSP_PATH}/lib")
100+
if(NOT IS_DIRECTORY "${RTEMS_BSP_LIB_PATH}")
101+
message(FATAL_ERROR
102+
"RTEMS BSP lib folder not found at "
103+
"${RTEMS_BSP_LIB_PATH}"
104+
)
105+
endif()
106+
107+
set(RTEMS_BSP_INC_PATH "${RTEMS_BSP_LIB_PATH}/include")
108+
if(NOT IS_DIRECTORY "${RTEMS_BSP_INC_PATH}")
109+
message(FATAL_ERROR
110+
"RTEMS BSP include folder not found at "
111+
"${RTEMS_BSP_INC_PATH}"
112+
)
113+
endif()
114+
115+
# Variables set in the cache so they can be used everywhere.
116+
set(RTEMS_ARCH_NAME ${RTEMS_ARCH_NAME} CACHE FILEPATH "Architecture name")
117+
set(RTEMS_BSP_NAME ${RTEMS_BSP_NAME} CACHE FILEPATH "BSP name")
118+
set(RTEMS_TOOLS_LIB_PATH ${RTEMS_TOOLS_LIB_PATH}
119+
CACHE FILEPATH "Tools library path"
120+
)
121+
set(RTEMS_BSP_LIB_PATH ${RTEMS_BSP_LIB_PATH} CACHE FILEPATH "BSP library path")
122+
set(RTEMS_BSP_INC_PATH ${RTEMS_BSP_INC_PATH} CACHE FILEPATH "BSP include path")
123+
set(RTEMS_ARCH_LIB_PATH ${RTEMS_ARCH_LIB_PATH}
124+
CACHE FILEPATH "Architecture library path"
125+
)
126+
set(RTEMS_ARCH_VERSION_NAME ${RTEMS_ARCH_VERSION_NAME}
127+
CACHE FILEPATH "Unique architecture-version identifier"
128+
)
129+
130+
list(APPEND CMAKE_PREFIX_PATH ${RTEMS_BSP_LIB_PATH})
131+
list(APPEND CMAKE_PREFIX_PATH ${RTEMS_BSP_INC_PATH})
132+
list(APPEND CMAKE_PREFIX_PATH ${RTEMS_ARCH_LIB_PATH})
133+
list(APPEND CMAKE_PREFIX_PATH ${RTEMS_TOOLS_LIB_PATH})
134+
135+
endfunction()

RTEMSConfig.cmake

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)