Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ST Mbed targets to be CMake buildsystem targets #14199

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 28 additions & 42 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
cmake_minimum_required(VERSION 3.19.0 FATAL_ERROR)

include(${MBED_CONFIG_PATH}/mbed_config.cmake)
include(tools/cmake/set_linker_script.cmake)

add_library(mbed-core INTERFACE)

Expand All @@ -23,7 +24,6 @@ target_link_libraries(mbed-baremetal
INTERFACE
mbed-core
)

# Validate selected C library type
# The C library type selected has to match the library that the target can support
if(${MBED_C_LIB} STREQUAL "small")
Expand All @@ -35,7 +35,7 @@ if(${MBED_C_LIB} STREQUAL "small")
" we are using the standard C library instead."
)
set(MBED_C_LIB "std" CACHE STRING "")
endif()
endif()
endif()
elseif(NOT ${MBED_C_LIB} IN_LIST MBED_TARGET_SUPPORTED_C_LIBS)
message(FATAL_ERROR
Expand Down Expand Up @@ -67,7 +67,7 @@ target_compile_definitions(mbed-core

# Add compile definitions for backward compatibility with the toolchain
# supported. New source files should instead check for __GNUC__ and __clang__
# for the GCC_ARM and ARM toolchains respectively.
# for the GCC_ARM and ARM toolchains respectively.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how this spaces got in... Jamie proposed to have formatter for CMake, we should review that one to avoid these in future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used https://github.com/cheshirekow/cmake_format before and it worked well. There are pre-commit hooks available, so we could add it to CI quite easily. It's very easy to miss trailing whitespace if your editor isn't set up correctly to flag it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the PR adding format: #14011 ,if you have a moment, please review

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

besides that, I can make a PR to add .editorconfig -- it's almost a standard and it's super useful for this kind of case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

besides that, I can make a PR to add .editorconfig -- it's almost a standard and it's super useful for this kind of case.

That might be useful.

if(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
target_compile_definitions(mbed-core
INTERFACE
Expand All @@ -87,6 +87,15 @@ target_include_directories(mbed-core
${CMAKE_CURRENT_SOURCE_DIR}
)

# We need to generate a "response file" to pass to the C preprocessor because of path length
# limitations on Windows. We set the response file and bind the path to a global property here.
# We query this global property when we set the linker script for the MBED_TARGET being built.
#
# TODO: Remove this and find a more idiomatic way of passing compile definitions to CPP without
# using global properties.
mbed_generate_options_for_linker(${APP_TARGET} LINKER_PREPROCESS_DEFINITIONS)
set_property(GLOBAL PROPERTY COMPILE_DEFS_RESPONSE_FILE ${LINKER_PREPROCESS_DEFINITIONS})

# These targets are made visible here so their source files which
# are spread in different directories can be referenced and can be linked against
# by libraries that depend on them.
Expand All @@ -111,6 +120,22 @@ add_subdirectory(features EXCLUDE_FROM_ALL)
add_subdirectory(cmsis/CMSIS_5/CMSIS/RTOS2 EXCLUDE_FROM_ALL)
add_subdirectory(cmsis/device/rtos EXCLUDE_FROM_ALL)

# This is a temporary workaround to prevent the build from failing for MBED_TARGETS that
# haven't been converted to build system targets yet.
# The refactored MBED_TARGETS set the linker script and forward it to the build system as a
# usage requirement. The 'old' mechanism was to set the linker script on the top level mbed-core
# target. This was needed because MBED_TARGETS were not registered as buildsystem targets,
# preventing CMake from working its usage requirements magic and forcing us to set the linker
# script globally.
#
# TODO: Remove when all MBED_TARGETS have been converted to build system targets.
hugueskamba marked this conversation as resolved.
Show resolved Hide resolved
if(TARGET ${MBED_TARGET})
target_link_libraries(mbed-core INTERFACE ${MBED_TARGET})
else()
get_property(LINKER_SCRIPT GLOBAL PROPERTY MBED_TARGET_LINKER_FILE)
mbed_set_linker_script(mbed-core ${LINKER_SCRIPT})
endif()

#
# Configures the application
#
Expand All @@ -123,45 +148,6 @@ function(mbed_configure_app_target target)
)
endfunction()

#
# Specifies linker script used for linking `target`.
#
function(mbed_set_mbed_target_linker_script target)
get_property(mbed_target_linker_script GLOBAL PROPERTY MBED_TARGET_LINKER_FILE)
mbed_generate_options_for_linker(${target} _linker_preprocess_definitions)
if(MBED_TOOLCHAIN STREQUAL "GCC_ARM")
set(CMAKE_PRE_BUILD_COMMAND
COMMAND "arm-none-eabi-cpp" ${_linker_preprocess_definitions} -x assembler-with-cpp -E -Wp,-P
${mbed_target_linker_script} -o ${CMAKE_BINARY_DIR}/${target}.link_script.ld

WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
BYPRODUCTS "${CMAKE_BINARY_DIR}/${target}.link_script.ld"
)
target_link_options(mbed-core
INTERFACE
"-T" "${CMAKE_BINARY_DIR}/${target}.link_script.ld"
"-Wl,-Map=${CMAKE_BINARY_DIR}/${target}${CMAKE_EXECUTABLE_SUFFIX}.map"
)
elseif(MBED_TOOLCHAIN STREQUAL "ARM")
set(CMAKE_PRE_BUILD_COMMAND COMMAND "")
target_link_options(mbed-core
INTERFACE
"--scatter=${mbed_target_linker_script}"
"--predefine=${_linker_preprocess_definitions}"
"--map"
)
endif()
add_custom_command(
TARGET
${target}
PRE_LINK
${CMAKE_PRE_BUILD_COMMAND}
COMMENT
"Link line:"
VERBATIM
)
endfunction()

#
# Converts output file of `target` to binary file and to Intel HEX file.
#
Expand Down
1 change: 1 addition & 0 deletions targets/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
include(../tools/cmake/set_linker_script.cmake)

if("Ambiq_Micro" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_Ambiq_Micro)
Expand Down
49 changes: 18 additions & 31 deletions targets/TARGET_STM/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

if("STM32F0" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32F0)
elseif("STM32F1" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32F1)
elseif("STM32F2" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32F2)
elseif("STM32F3" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32F3)
elseif("STM32F4" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32F4)
elseif("STM32F7" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32F7)
elseif("STM32G0" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32G0)
elseif("STM32G4" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32G4)
elseif("STM32H7" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32H7)
elseif("STM32L0" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32L0)
elseif("STM32L1" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32L1)
elseif("STM32L4" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32L4)
elseif("STM32L5" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32L5)
elseif("STM32WB" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32WB)
endif()
add_subdirectory(TARGET_STM32F0 EXCLUDE_FROM_ALL)
Patater marked this conversation as resolved.
Show resolved Hide resolved
add_subdirectory(TARGET_STM32F1 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32F2 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32F3 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32F4 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32F7 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32G0 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32G4 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32H7 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32L0 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32L1 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32L4 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32L5 EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32WB EXCLUDE_FROM_ALL)

target_include_directories(mbed-core
add_library(STM INTERFACE)

target_include_directories(STM
INTERFACE
.
)

target_sources(mbed-core
target_sources(STM
INTERFACE
USBPhy_STM32.cpp
analogin_api.c
Expand Down
22 changes: 10 additions & 12 deletions targets/TARGET_STM/TARGET_STM32F0/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

if("STM32F091xC" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32F091xC)
elseif("STM32F072xB" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32F072xB)
elseif("STM32F070xB" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32F070xB)
elseif("STM32F030x8" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_STM32F030x8)
endif()
add_subdirectory(TARGET_STM32F091xC EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32F072xB EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32F070xB EXCLUDE_FROM_ALL)
add_subdirectory(TARGET_STM32F030x8 EXCLUDE_FROM_ALL)
add_subdirectory(STM32Cube_FW EXCLUDE_FROM_ALL)

add_subdirectory(STM32Cube_FW)
add_library(STM32F0 INTERFACE)

target_include_directories(mbed-core
target_include_directories(STM32F0
INTERFACE
.
)

target_sources(mbed-core
target_sources(STM32F0
INTERFACE
analogin_device.c
analogout_device.c
Expand All @@ -29,3 +25,5 @@ target_sources(mbed-core
serial_device.c
spi_api.c
)

target_link_libraries(STM32F0 INTERFACE STM STM32F0Cube_FW)
6 changes: 4 additions & 2 deletions targets/TARGET_STM/TARGET_STM32F0/STM32Cube_FW/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

target_sources(mbed-core
add_library(STM32F0Cube_FW INTERFACE)
Patater marked this conversation as resolved.
Show resolved Hide resolved

target_sources(STM32F0Cube_FW
INTERFACE
STM32F0xx_HAL_Driver/Legacy/stm32f0xx_hal_can_legacy.c
STM32F0xx_HAL_Driver/stm32f0xx_hal.c
Expand Down Expand Up @@ -66,7 +68,7 @@ target_sources(mbed-core
system_stm32f0xx.c
)

target_include_directories(mbed-core
target_include_directories(STM32F0Cube_FW
INTERFACE
.
CMSIS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
set(LINKER_FILE TOOLCHAIN_ARM/stm32f030x8.sct)
endif()

set_property(GLOBAL PROPERTY MBED_TARGET_LINKER_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_FILE})
add_library(STM32F030x8 INTERFACE)

target_sources(mbed-core
target_sources(STM32F030x8
INTERFACE
${STARTUP_FILE}
)

target_include_directories(mbed-core
target_include_directories(STM32F030x8
INTERFACE
.
)

mbed_set_linker_script(STM32F030x8 ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_FILE})

target_link_libraries(STM32F030x8 INTERFACE STM32F0)
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

if("NUCLEO_F070RB" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_NUCLEO_F070RB)
endif()
add_subdirectory(TARGET_NUCLEO_F070RB EXCLUDE_FROM_ALL)

if(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
set(STARTUP_FILE TOOLCHAIN_GCC_ARM/startup_stm32f070xb.S)
Expand All @@ -13,15 +11,19 @@ elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
set(LINKER_FILE TOOLCHAIN_ARM/stm32f070xb.sct)
endif()

set_property(GLOBAL PROPERTY MBED_TARGET_LINKER_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_FILE})
add_library(STM32F070xB INTERFACE)

target_sources(mbed-core
target_sources(STM32F070xB
INTERFACE
system_clock.c
${STARTUP_FILE}
)

target_include_directories(mbed-core
target_include_directories(STM32F070xB
INTERFACE
.
)

mbed_set_linker_script(STM32F070xB ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_FILE})

target_link_libraries(STM32F070xB INTERFACE STM32F0)
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

target_sources(mbed-core
add_library(NUCLEO_F070RB INTERFACE)

target_sources(NUCLEO_F070RB
INTERFACE
PeripheralPins.c
)

target_include_directories(mbed-core
target_include_directories(NUCLEO_F070RB
INTERFACE
.
)

target_link_libraries(NUCLEO_F070RB INTERFACE STM32F070xB)
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

if("NUCLEO_F072RB" IN_LIST MBED_TARGET_LABELS)
add_subdirectory(TARGET_NUCLEO_F072RB)
endif()
add_subdirectory(TARGET_NUCLEO_F072RB EXCLUDE_FROM_ALL)

if(${MBED_TOOLCHAIN} STREQUAL "GCC_ARM")
set(STARTUP_FILE TOOLCHAIN_GCC_ARM/startup_stm32f072xb.S)
Expand All @@ -13,15 +11,19 @@ elseif(${MBED_TOOLCHAIN} STREQUAL "ARM")
set(LINKER_FILE TOOLCHAIN_ARM/stm32f072xb.sct)
endif()

set_property(GLOBAL PROPERTY MBED_TARGET_LINKER_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_FILE})
add_library(STM32F072xB INTERFACE)

target_sources(mbed-core
target_sources(STM32F072xB
INTERFACE
system_clock.c
${STARTUP_FILE}
)

target_include_directories(mbed-core
target_include_directories(STM32F072xB
INTERFACE
.
)

mbed_set_linker_script(STM32F072xB ${CMAKE_CURRENT_SOURCE_DIR}/${LINKER_FILE})

target_link_libraries(STM32F072xB INTERFACE STM32F0)
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

target_sources(mbed-core
add_library(NUCLEO_F072RB INTERFACE)

target_sources(NUCLEO_F072RB
INTERFACE
PeripheralPins.c
)

target_include_directories(mbed-core
target_include_directories(NUCLEO_F072RB
INTERFACE
.
)

target_link_libraries(NUCLEO_F072RB INTERFACE STM32F072xB)
Loading