aboutsummaryrefslogtreecommitdiff
path: root/cmsis.cmake
blob: 9322240f30ad01962df10da631ec557690494946 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#
# Copyright (c) 2019-2020 Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the License); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Extract the CPU number from the system processor
string(REGEX MATCH "^cortex-m([0-9]+)$" CPU_NUMBER ${CMAKE_SYSTEM_PROCESSOR})
if(NOT CPU_NUMBER)
    message(FATAL_ERROR "System processor '${CMAKE_SYSTEM_PROCESSOR}' not supported. Should be cortex-m<nr>.")
endif()
string(REGEX REPLACE "^cortex-m([0-9]+)$" "\\1" CPU_NUMBER ${CMAKE_SYSTEM_PROCESSOR})

set(ARM_CPU "ARMCM${CPU_NUMBER}")

# Set CPU specific features
if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "cortex-m33")
    set(ARM_FEATURES "_DSP_FP")
elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "cortex-m4")
    set(ARM_FEATURES "_FP")
elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "cortex-m7")
    set(ARM_FEATURES "_DP")
else()
    set(ARM_FEATURES "")
endif()

# CMSIS core
add_library(cmsis_core INTERFACE)
target_include_directories(cmsis_core INTERFACE ${CMSIS_PATH}/CMSIS/Core/Include)

# CMSIS device
add_library(cmsis_device INTERFACE)
target_include_directories(cmsis_device INTERFACE ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Include)
if (TRUSTZONE_BUILD)
    # Use Cortex-M Secure Extension when compiling
    target_compile_options(cmsis_device INTERFACE -mcmse)
    target_compile_definitions(cmsis_device INTERFACE TRUSTZONE_BUILD)
    if (TRUSTZONE_SIDE STREQUAL secure)
        target_compile_definitions(cmsis_device INTERFACE TRUSTZONE_SECURE)
    else()
        target_compile_definitions(cmsis_device INTERFACE TRUSTZONE_NONSECURE)
    endif()
endif()
target_compile_options(cmsis_device INTERFACE -include${ARM_CPU}${ARM_FEATURES}.h)
target_link_libraries(cmsis_device INTERFACE cmsis_core)

# CMSIS startup
add_library(cmsis_startup STATIC)
if (TRUSTZONE_BUILD)
    if (TRUSTZONE_SIDE STREQUAL secure)
        target_sources(cmsis_startup PRIVATE
            ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/startup_${ARM_CPU}.c
            ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/system_${ARM_CPU}.c)
        # Bring in the partion header
        target_include_directories(cmsis_startup PRIVATE ${TRUSTZONE_PARTITION_DIRECTORY})
    elseif(TRUSTZONE_SIDE STREQUAL nonsecure)
        target_sources(cmsis_startup PRIVATE
            ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/startup_${ARM_CPU}.c)
    endif()
else()
    target_sources(cmsis_startup PRIVATE
        ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/startup_${ARM_CPU}.c
        ${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/system_${ARM_CPU}.c)
endif()

set_source_files_properties(${CMSIS_PATH}/Device/ARM/${ARM_CPU}/Source/startup_${ARM_CPU}.c PROPERTIES COMPILE_FLAGS
    -Wno-redundant-decls)

target_compile_definitions(cmsis_startup PRIVATE ${ARM_CPU}${ARM_FEATURES})
target_link_libraries(cmsis_startup PRIVATE cmsis_device)

# Install libraries
install(TARGETS cmsis_startup LIBRARY DESTINATION "lib")