Skip to content

Commit 2f3463c

Browse files
author
Donatien Garnier
authored
Merge pull request #1381 from 0xc0170/cmake-targets-porting
CMake targets porting
2 parents 1486a4e + b0c74cf commit 2f3463c

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

docs/porting/target/cmake.md

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# CMake target porting
2+
3+
See below instructions to add support for Mbed targets to build with CMake using mbed-tools.
4+
5+
Important variables provided for Mbed target:
6+
7+
- `MBED_TARGET_LABELS`: a list of labels added to a given Mbed target coming from the Mbed target default configuration file `targets.json`.
8+
- `MBED_TARGET_DEFINITIONS`: a list of macro definitions added to a given Mbed target coming from the Mbed target default configuration file `targets.json`.
9+
10+
- `MBED_CONFIG_DEFINITIONS`: a list of macro definitions added to a given Mbed target coming from various Mbed library configuration files.
11+
- `MBED_TARGET_SUPPORTED_C_LIBS`: a list of C library types supported by a given Mbed target coming from the Mbed target default configuration file `targets.json`.
12+
- `MBED_TARGET_SUPPORTED_APPLICATION_PROFILES` a list of application profiles supported by a given Mbed target coming from the Mbed target default configuration file `targets.json`.
13+
14+
Note: All the CMake variables above are generated by mbed-tools and their values can be overridden using an application configuration file (mbed_app.json) in the same manner.
15+
16+
## Mbed targets CMake input file structure
17+
18+
### Vendor selection
19+
20+
Vendor selection is handled in [`targets/CMakeLists.txt`](https://github.com/ARMmbed/mbed-os/blob/master/targets/CMakeLists.txt) using a conditional statement to verify that one of the labels used by a given Mbed target matches a given partner directory. The matching directory is added to directories to build.
21+
e.g
22+
23+
```
24+
if("Cypress" IN_LIST MBED_TARGET_LABELS)
25+
add_subdirectory(TARGET_Cypress)
26+
endif()
27+
```
28+
Add an `elseif` conditional statement to add a vendor directory.
29+
30+
### MCU family targets
31+
32+
`targets/TARGET_<VENDOR_NAME>` usually contains different MCU or Mbed target families. The structure of the vendor directory is up to the vendor but it is preferable that it has logical separations.
33+
For example, the content of `targets/TARGET_Cypress/TARGET_PSOC6` can be listed as follows:
34+
35+
```
36+
if("SCL" IN_LIST MBED_TARGET_LABELS)
37+
add_subdirectory(COMPONENT_SCL EXCLUDE_FROM_ALL)
38+
endif()
39+
40+
if("WHD" IN_LIST MBED_TARGET_LABELS)
41+
add_subdirectory(COMPONENT_WHD EXCLUDE_FROM_ALL)
42+
add_subdirectory(common/COMPONENT_WHD EXCLUDE_FROM_ALL)
43+
endif()
44+
45+
if("CY8CKIT064B0S2_4343W" IN_LIST MBED_TARGET_LABELS)
46+
add_subdirectory(TARGET_CY8CKIT064B0S2_4343W)
47+
elseif("CY8CKIT_062S2_43012" IN_LIST MBED_TARGET_LABELS)
48+
add_subdirectory(TARGET_CY8CKIT_062S2_43012)
49+
...
50+
endif()
51+
52+
# Add the include directories accessible from this directory that are not specific to an MCU family or Mbed target
53+
target_include_directories(mbed-core
54+
INTERFACE
55+
...
56+
)
57+
58+
# Add the source files accessible from this directory that are not specific to an MCU family or Mbed target
59+
target_sources(mbed-core
60+
INTERFACE
61+
...
62+
)
63+
```
64+
65+
## Add target's CMakeLists.txt
66+
67+
Add `CMakeLists.txt` files to the top level directory of a given vendor directory. List all files found in the directory in this CMake input source file, adding additional CMake input source file if it is MCU or Mbed target specific and has a great number of files which will make the top level `CMakeLists.txt` too complex. Think when you decide to create functions in a computer software code to remove complexity.
68+
69+
See [`targets/TARGET_Cypress/CMakeLists.txt`](https://github.com/ARMmbed/mbed-os/blob/master/targets/TARGET_Cypress/CMakeLists.txt) for examples.
70+
71+
### Add your sources and includes
72+
73+
A whole directory can be added to the build using `add_subdirectory()` if it is specific to an MCU family (or Mbed target) and has a complex structure containing more directories and files. Otherwise simply list the files contained in the directory.
74+
See an example below:
75+
76+
```
77+
if("<VENDOR_MCU_VARIANT>" IN_LIST MBED_TARGET_LABELS)
78+
add_subdirectory(TARGET_<VENDOR_MCU_VARIANT>)
79+
endif()
80+
81+
target_include_directories(mbed-core
82+
INTERFACE
83+
.
84+
subdirectory
85+
)
86+
87+
target_sources(mbed-core
88+
INTERFACE
89+
file1.c
90+
91+
subdirectory/file2.c
92+
)
93+
```
94+
95+
Sources are listed using CMake's `target_sources()` function and added to the `mbed-core` CMake target as shown above. Header files are not explicitly listed, the directory where they can be found is listed instead. This is also shown above.
96+
97+
98+
### Linker script file
99+
100+
A global CMake property named `MBED_TARGET_LINKER_FILE` must be set for each linker file. This linker file must be listed with its absolute path, this is achieved by adding the sub-path obtained by the CMake variable `${CMAKE_CURRENT_SOURCE_DIR}`.
101+
e.g
102+
103+
```
104+
if(${MBED_TOOLCHAIN} STREQUAL "ARM")
105+
set(LINKER_FILE relative/path/to/TOOLCHAIN_ARM_STD/linker_file.sct)
106+
elseif(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
107+
set(LINKER_FILE relative/path/to/TOOLCHAIN_GCC_ARM/linker_file.ld)
108+
endif()
109+
110+
set_property(GLOBAL PROPERTY MBED_TARGET_LINKER_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_FILE})
111+
```
112+
113+
#### ARMClang linker file
114+
115+
The shebang in the ARM toolchain linker file, also known as scatter file, needs to update the interpreter program from `armcc` to `armclang` and pass it different optional arguments.
116+
Replace `armcc -E` with `#! armclang -E --target=arm-arm-none-eabi -x c -mcpu=<CORE_TYPE_FLAG>` where <CORE_TYPE_FLAG> is the MCU core type for a given Mbed target. Once you have determined which MCU core your Mbed target is based on, head to [tools/cmake/cores/](https://github.com/ARMmbed/mbed-os/tree/master/tools/cmake/cores) and open the CMake module your Mbed target is based on and look what the `-mcpu` is set to.
117+
118+
### Adding pre-compiled target libraries
119+
120+
Pre-compiled libraries and object files are listed using CMake's `target_link_libraries()` function with an absolute path of the file added.
121+
e.g
122+
123+
```
124+
target_link_libraries(mbed-core
125+
INTERFACE
126+
${CMAKE_CURRENT_SOURCE_DIR}/libprecompiled.ar
127+
${CMAKE_CURRENT_SOURCE_DIR}/file_object.o
128+
)
129+
```

0 commit comments

Comments
 (0)