1#
2# Arm SCP/MCP Software
3# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8add_library(arch-arm-m)
9
10#
11# Determine which architecture shall be supported according to
12# the used CPU
13#
14
15if((CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m(33|55)")
16                           OR (CMAKE_SYSTEM_ARCH MATCHES "armv8(\.1)?-m"))
17    target_compile_definitions(arch-arm-m PUBLIC "ARMV8M")
18else()
19    target_compile_definitions(arch-arm-m PUBLIC "ARMV7M")
20endif()
21
22target_include_directories(arch-arm-m
23                           PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
24
25# cmake-lint: disable=E1122
26
27target_sources(
28    arch-arm-m
29    PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/arch_exceptions.c"
30            "${CMAKE_CURRENT_SOURCE_DIR}/src/arch_handlers.c"
31            "${CMAKE_CURRENT_SOURCE_DIR}/src/arch_main.c"
32            "${CMAKE_CURRENT_SOURCE_DIR}/src/arch_nvic.c"
33    PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../src/arch_mm.c")
34
35target_link_libraries(arch-arm-m PUBLIC cmsis::core-m)
36
37
38#
39# Select the standard library, if we have the option.
40#
41
42include(CMakeDependentOption)
43
44cmake_dependent_option(
45    SCP_ENABLE_NEWLIB_NANO "Enable newlib-nano instead of newlib?" TRUE
46    "CMAKE_C_COMPILER_ID STREQUAL GNU OR CMAKE_C_COMPILER_ID STREQUAL Clang"
47    FALSE)
48
49if(SCP_ENABLE_NEWLIB_NANO)
50    if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
51        target_link_options(arch-arm-m PUBLIC "--specs=nano.specs")
52    elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
53        target_link_libraries(arch-arm-m PUBLIC c_nano)
54    endif()
55else()
56    if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
57        target_link_options(arch-arm-m PUBLIC "--specs=nosys.specs")
58    elseif(CMAKE_C_COMPILER_ID STREQUAL "Clang")
59        target_link_libraries(arch-arm-m PUBLIC c)
60    endif()
61endif()
62
63#
64# Force the linker to include any object files containing our implementations of
65# functions that the standard library relies on.
66#
67
68target_link_options(arch-arm-m PUBLIC "LINKER:--undefined=arch_exceptions")
69
70if(CMAKE_C_COMPILER_ID STREQUAL "ARMClang")
71    target_link_options(arch-arm-m
72                        PUBLIC "LINKER:--entry=arch_exception_reset")
73endif()
74
75if(SCP_HAVE_NEWLIB)
76    target_link_options(arch-arm-m PUBLIC "LINKER:--undefined=posix_memalign"
77                                           "LINKER:--undefined=_sbrk")
78endif()
79
80#
81# Preprocess the linker script.
82#
83# CMake provides the `CMAKE_C_CREATE_PREPROCESSED_SOURCE` variable, which
84# describes the command line required to preprocess a C source file. This
85# variable is in a format similar to this (verbatim):
86#
87# <CMAKE_C_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -E <SOURCE> >
88# <PREPROCESSED_SOURCE>
89#
90# We do some processing on this variable to convert these bracket-surrounded
91# names to variables we set. For example, `<DEFINES>` is replaced with
92# `${CPP_DEFINES}`. We then need to do some magic to expand that string out to
93# the value of the actual variable.
94#
95
96include(SCPTargetLinkerScript)
97
98if(CMAKE_C_COMPILER_ID STREQUAL "ARMClang")
99    set(scp_lds "${CMAKE_CURRENT_SOURCE_DIR}/src/arch.scatter.S")
100else()
101    set(scp_lds "${CMAKE_CURRENT_SOURCE_DIR}/src/arch.ld.S")
102endif()
103
104scp_target_linker_script(arch-arm-m "${scp_lds}")
105