Skip to content

Commit 4c64aef

Browse files
author
Donatien Garnier
authored
Merge pull request #1424 from 0xc0170/cmake-targets-update
CMake: update porting guide after target refactor
2 parents ddb78ae + 5467908 commit 4c64aef

File tree

1 file changed

+69
-32
lines changed

1 file changed

+69
-32
lines changed

docs/porting/target/cmake.md

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,83 @@ Note: All the CMake variables above are generated by mbed-tools and their values
1515

1616
## Mbed targets CMake input file structure
1717

18+
As we are still supporting Mbed CLI 1, we have maintained the directory naming scheme that use prefixes (`FEATURE_`, `COMPONENT_`, `TARGET_`).
19+
20+
Every Mbed CMake target should be prefixed with `mbed-`. Mbed boards listed in `targets.json` are translated in CMake to add the prefix and replace `_` with `-`. For example, the Mbed board `CYSBSYSKIT_01` is represented by the CMake target `mbed-cysbsyskit-01`.
21+
1822
### Vendor selection
1923

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.
24+
Vendor selection is handled in [`targets/CMakeLists.txt`](https://github.com/ARMmbed/mbed-os/blob/master/targets/CMakeLists.txt). A subdirectory is added to the build if one of the CMake targets it contains is referenced.
2125
e.g
2226

2327
```
24-
if("Cypress" IN_LIST MBED_TARGET_LABELS)
25-
add_subdirectory(TARGET_Cypress)
26-
endif()
28+
add_subdirectory(TARGET_Cypress EXCLUDE_FROM_ALL)
2729
```
28-
Add an `elseif` conditional statement to add a vendor directory.
2930

3031
### MCU family targets
3132

3233
`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.
3334
For example, the content of `targets/TARGET_Cypress/TARGET_PSOC6` can be listed as follows:
3435

3536
```
36-
if("SCL" IN_LIST MBED_TARGET_LABELS)
37-
add_subdirectory(COMPONENT_SCL EXCLUDE_FROM_ALL)
38-
endif()
37+
add_subdirectory(COMPONENT_SCL EXCLUDE_FROM_ALL)
3938
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()
4439
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)
40+
add_subdirectory(COMPONENT_WHD EXCLUDE_FROM_ALL)
41+
add_subdirectory(common/COMPONENT_WHD EXCLUDE_FROM_ALL)
42+
43+
add_subdirectory(TARGET_CY8CKIT064B0S2_4343W EXCLUDE_FROM_ALL)
44+
add_subdirectory(TARGET_CY8CKIT_062S2_43012 EXCLUDE_FROM_ALL)
4945
...
50-
endif()
5146
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
47+
add_library(mbed-psoc6 INTERFACE)
48+
49+
# Add the include directories accessible from this directory that are specific to this MCU family or Mbed target
50+
target_include_directories(mbed-psoc6
5451
INTERFACE
5552
...
5653
)
5754
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
55+
# Add the source files accessible from this directory that are specific to this MCU family or Mbed target
56+
target_sources(mbed-psoc6
6057
INTERFACE
6158
...
6259
)
6360
```
6461

65-
## Add target's CMakeLists.txt
62+
The board CMake targets should link to `mbed-psoc6`:
63+
64+
```
65+
add_library(mbed-cysbsyskit-01 INTERFACE)
66+
67+
target_include_directories(mbed-cysbsyskit-01
68+
INTERFACE
69+
...
70+
)
71+
72+
target_sources(mbed-cysbsyskit-01
73+
INTERFACE
74+
...
75+
)
76+
# Other libraries that the Mbed board depends on
77+
target_link_libraries(mbed-cysbsyskit-01
78+
INTERFACE
79+
mbed-cat1a
80+
mbed-cysbsyskit-01-cm4
81+
mbed-cysbsyskit-01-bsp-design-modus
82+
mbed-psoc6
83+
mbed-cy-psoc6-scl
84+
)
85+
86+
target_compile_definitions(mbed-cysbsyskit-01
87+
INTERFACE
88+
"CY8C624AFNI_S2D43F"
89+
)
90+
91+
mbed_post_build_psoc6_merge_hex("CYSBSYSKIT_01")
92+
```
93+
94+
## Add Mbed board CMake input source file
6695

6796
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.
6897

@@ -74,17 +103,15 @@ A whole directory can be added to the build using `add_subdirectory()` if it is
74103
See an example below:
75104

76105
```
77-
if("<VENDOR_MCU_VARIANT>" IN_LIST MBED_TARGET_LABELS)
78-
add_subdirectory(TARGET_<VENDOR_MCU_VARIANT>)
79-
endif()
106+
add_subdirectory(TARGET_<VENDOR_MCU_VARIANT> EXCLUDE_FROM_ALL)
80107
81-
target_include_directories(mbed-core
108+
target_include_directories(mbed-<vendor-mcu-variant>
82109
INTERFACE
83110
.
84111
subdirectory
85112
)
86113
87-
target_sources(mbed-core
114+
target_sources(mbed-<vendor-mcu-variant>
88115
INTERFACE
89116
file1.c
90117
@@ -97,17 +124,27 @@ Sources are listed using CMake's `target_sources()` function and added to the `m
97124

98125
### Linker script file
99126

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}`.
127+
A function `mbed_set_linker_script` must be invoked for each linker file. The 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}`.
101128
e.g
102129

103130
```
131+
add_library(mbed-cysbsyskit-01-cm4 INTERFACE)
132+
104133
if(${MBED_TOOLCHAIN} STREQUAL "ARM")
105-
set(LINKER_FILE relative/path/to/TOOLCHAIN_ARM_STD/linker_file.sct)
134+
set(LINKER_FILE_CM4 device/COMPONENT_CM4/TOOLCHAIN_ARM/cy8c6xxa_cm4_dual.sct)
135+
set(STARTUP_FILE_CM4 device/COMPONENT_CM4/TOOLCHAIN_ARM/startup_psoc6_02_cm4.S)
106136
elseif(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
107-
set(LINKER_FILE relative/path/to/TOOLCHAIN_GCC_ARM/linker_file.ld)
137+
set(LINKER_FILE_CM4 device/COMPONENT_CM4/TOOLCHAIN_GCC_ARM/cy8c6xxa_cm4_dual.ld)
138+
set(STARTUP_FILE_CM4 device/COMPONENT_CM4/TOOLCHAIN_GCC_ARM/startup_psoc6_02_cm4.S)
108139
endif()
109140
110-
set_property(GLOBAL PROPERTY MBED_TARGET_LINKER_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_FILE})
141+
target_sources(mbed-cysbsyskit-01-cm4
142+
INTERFACE
143+
device/COMPONENT_CM4/system_psoc6_cm4.c
144+
${STARTUP_FILE_CM4}
145+
)
146+
147+
mbed_set_linker_script(mbed-cysbsyskit-01-cm4 ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_FILE_CM4})
111148
```
112149

113150
#### ARMClang linker file
@@ -121,7 +158,7 @@ Pre-compiled libraries and object files are listed using CMake's `target_link_li
121158
e.g
122159

123160
```
124-
target_link_libraries(mbed-core
161+
target_link_libraries(mbed-cysbsyskit-01-cm4
125162
INTERFACE
126163
${CMAKE_CURRENT_SOURCE_DIR}/libprecompiled.ar
127164
${CMAKE_CURRENT_SOURCE_DIR}/file_object.o

0 commit comments

Comments
 (0)