Kristofer Jonsson | 43ce491 | 2020-11-20 09:42:53 +0100 | [diff] [blame] | 1 | # |
Per Åstrand | 9de1b74 | 2022-02-03 19:35:35 +0100 | [diff] [blame] | 2 | # Copyright (c) 2020-2022 Arm Limited. All rights reserved. |
Kristofer Jonsson | 43ce491 | 2020-11-20 09:42:53 +0100 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 19 | # Add .elf to all executables |
| 20 | set(CMAKE_EXECUTABLE_SUFFIX ".elf") |
| 21 | |
| 22 | ############################################################################# |
| 23 | # Link options |
| 24 | ############################################################################# |
| 25 | |
| 26 | function(ethosu_target_link_options target scope) |
| 27 | cmake_parse_arguments(ARG "" "LINK_FILE;ENTRY" "" ${ARGN}) |
| 28 | |
| 29 | # Store the link file in a property to be evaluated by the executable. |
Kristofer Jonsson | ec45155 | 2021-06-04 18:02:59 +0200 | [diff] [blame] | 30 | get_filename_component(LINK_FILE_PATH ${ARG_LINK_FILE} ABSOLUTE) |
| 31 | set_property(GLOBAL PROPERTY ETHOSU_TARGET_LINK_FILE ${LINK_FILE_PATH}) |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 32 | |
| 33 | if (ARG_ENTRY) |
| 34 | target_link_options(${target} ${scope} --entry Reset_Handler) |
| 35 | endif() |
| 36 | endfunction() |
| 37 | |
| 38 | function(ethosu_eval_link_options target) |
| 39 | # Get the link file from the cache |
| 40 | get_property(LINK_FILE GLOBAL PROPERTY ETHOSU_TARGET_LINK_FILE) |
| 41 | |
| 42 | set(prop "$<TARGET_PROPERTY:${target},COMPILE_DEFINITIONS>") |
Kristofer Jonsson | 9317581 | 2022-04-21 19:27:11 +0200 | [diff] [blame] | 43 | set(opt "$<TARGET_PROPERTY:${target},COMPILE_OPTIONS>") |
Kristofer Jonsson | 43ce491 | 2020-11-20 09:42:53 +0100 | [diff] [blame] | 44 | |
| 45 | if (CMAKE_CXX_COMPILER_ID STREQUAL "ARMClang") |
Jonas Ohlsson | f821c61 | 2021-06-04 14:58:34 +0200 | [diff] [blame] | 46 | set(LINK_FILE_EXT scatter) |
Kristofer Jonsson | 43ce491 | 2020-11-20 09:42:53 +0100 | [diff] [blame] | 47 | set(LINK_FILE_OPTION "--scatter") |
Jonas Ohlsson | f821c61 | 2021-06-04 14:58:34 +0200 | [diff] [blame] | 48 | # Note: the -mcpu flag is added to avoid warnings caused when using the default cpu/arch. |
| 49 | set(COMPILER_PREPROCESSOR_OPTIONS -mcpu=${CMAKE_SYSTEM_PROCESSOR} --target=arm-arm-none-eabi -E -x c -P) |
Kristofer Jonsson | 43ce491 | 2020-11-20 09:42:53 +0100 | [diff] [blame] | 50 | elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
Jonas Ohlsson | f821c61 | 2021-06-04 14:58:34 +0200 | [diff] [blame] | 51 | set(LINK_FILE_EXT ld) |
Kristofer Jonsson | 43ce491 | 2020-11-20 09:42:53 +0100 | [diff] [blame] | 52 | set(LINK_FILE_OPTION "-T") |
Jonas Ohlsson | f821c61 | 2021-06-04 14:58:34 +0200 | [diff] [blame] | 53 | set(COMPILER_PREPROCESSOR_OPTIONS -E -x c -P) |
Kristofer Jonsson | 43ce491 | 2020-11-20 09:42:53 +0100 | [diff] [blame] | 54 | endif() |
| 55 | |
Kristofer Jonsson | ec45155 | 2021-06-04 18:02:59 +0200 | [diff] [blame] | 56 | if (EXISTS ${LINK_FILE}) |
| 57 | set(LINK_FILE_IN ${LINK_FILE}) |
| 58 | else() |
| 59 | set(LINK_FILE_IN ${LINK_FILE}.${LINK_FILE_EXT}) |
| 60 | endif() |
| 61 | |
Jonas Ohlsson | f821c61 | 2021-06-04 14:58:34 +0200 | [diff] [blame] | 62 | get_filename_component(LINK_FILE_OUT_BASE ${LINK_FILE} NAME) |
| 63 | set(LINK_FILE_OUT ${CMAKE_CURRENT_BINARY_DIR}/${LINK_FILE_OUT_BASE}-${target}.${LINK_FILE_EXT}) |
| 64 | |
| 65 | add_custom_command( |
| 66 | OUTPUT ${LINK_FILE_OUT} |
| 67 | DEPENDS ${LINK_FILE_IN} |
Jonas Ohlsson | f821c61 | 2021-06-04 14:58:34 +0200 | [diff] [blame] | 68 | COMMAND ${CMAKE_C_COMPILER} ${COMPILER_PREPROCESSOR_OPTIONS} -o ${LINK_FILE_OUT} ${LINK_FILE_IN} |
| 69 | COMMAND_EXPAND_LISTS "-D$<JOIN:${prop},;-D>" |
Kristofer Jonsson | 9317581 | 2022-04-21 19:27:11 +0200 | [diff] [blame] | 70 | COMMAND_EXPAND_LISTS "$<FILTER:${opt},INCLUDE,^(-U|-D)>" |
Jonas Ohlsson | f821c61 | 2021-06-04 14:58:34 +0200 | [diff] [blame] | 71 | COMMAND_EXPAND_LISTS "-I$<JOIN:$<TARGET_PROPERTY:${target},INCLUDE_DIRECTORIES>,;-I>" |
| 72 | COMMENT "Preprocessing and generating linker script" |
| 73 | VERBATIM) |
| 74 | add_custom_target(${target}-linker-script |
| 75 | DEPENDS ${LINK_FILE_OUT} |
| 76 | VERBATIM) |
| 77 | add_dependencies(${target} ${target}-linker-script) |
| 78 | |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 79 | target_link_options(${target} PUBLIC ${LINK_FILE_OPTION} ${LINK_FILE_OUT}) |
Kristofer Jonsson | 43ce491 | 2020-11-20 09:42:53 +0100 | [diff] [blame] | 80 | set_target_properties(${target} PROPERTIES LINK_DEPENDS ${LINK_FILE_OUT}) |
| 81 | endfunction() |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 82 | |
| 83 | ############################################################################# |
| 84 | # Executable |
| 85 | ############################################################################# |
| 86 | |
| 87 | function(ethosu_add_executable target) |
| 88 | cmake_parse_arguments(ARGS "" "TARGET_LIBRARY" "SOURCES;LIBRARIES" ${ARGN}) |
| 89 | add_executable(${target}) |
| 90 | |
| 91 | target_sources(${target} PRIVATE |
| 92 | ${ARGS_SOURCES}) |
| 93 | |
| 94 | if(NOT ARGS_TARGET_LIBRARY) |
| 95 | set(ARGS_TARGET_LIBRARY ethosu_target_init) |
| 96 | endif() |
| 97 | |
| 98 | target_link_libraries(${target} PRIVATE |
| 99 | ${ARGS_TARGET_LIBRARY} ethosu_core ${ARGS_LIBRARIES}) |
| 100 | |
| 101 | ethosu_eval_link_options(${target}) |
Per Åstrand | 0954a8a | 2021-03-26 09:52:07 +0100 | [diff] [blame] | 102 | |
Per Åstrand | 6440bba | 2021-03-29 15:58:16 +0200 | [diff] [blame] | 103 | if (CMAKE_CXX_COMPILER_ID STREQUAL "ARMClang") |
| 104 | target_link_options(${target} PRIVATE --map --list=${target}.map) |
| 105 | elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 106 | target_link_options(${target} PRIVATE -Xlinker -Map=${target}.map) |
| 107 | endif() |
| 108 | |
Per Åstrand | 0954a8a | 2021-03-26 09:52:07 +0100 | [diff] [blame] | 109 | ethosu_add_binaries(${target}) |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 110 | endfunction() |
| 111 | |
| 112 | ############################################################################# |
| 113 | # Test |
| 114 | ############################################################################# |
| 115 | |
Per Åstrand | 9de1b74 | 2022-02-03 19:35:35 +0100 | [diff] [blame] | 116 | set(ETHOSU_CTEST_TARMAC_TRACE OFF CACHE BOOL "Enable to generate tarmac trace files when running ctest") |
| 117 | |
Kristofer Jonsson | f62c3d7 | 2021-01-21 17:39:03 +0100 | [diff] [blame] | 118 | function(ethosu_add_test target) |
| 119 | if(NOT BUILD_TESTING) |
| 120 | return() |
| 121 | endif() |
| 122 | |
| 123 | cmake_parse_arguments(ARGS "" "NAME" "COMMAND" ${ARGN}) |
| 124 | |
| 125 | if (NOT ARGS_COMMAND) |
| 126 | set(ARGS_COMMAND ${ETHOSU_COMMAND_DEFAULT}) |
| 127 | endif() |
| 128 | |
| 129 | if (NOT ARGS_NAME) |
| 130 | set(ARGS_NAME ${target}) |
| 131 | endif() |
| 132 | |
| 133 | add_test(NAME ${ARGS_NAME} |
| 134 | COMMAND ${ARGS_COMMAND} $<TARGET_FILE:${target}>) |
| 135 | endfunction() |
| 136 | |
| 137 | ############################################################################# |
| 138 | # Executable and test |
| 139 | ############################################################################# |
| 140 | |
| 141 | function(ethosu_add_executable_test target) |
| 142 | ethosu_add_executable(${target} ${ARGN}) |
| 143 | ethosu_add_test(${target} ${ARGN}) |
| 144 | endfunction() |
Per Åstrand | 0954a8a | 2021-03-26 09:52:07 +0100 | [diff] [blame] | 145 | |
| 146 | ############################################################################# |
| 147 | # Generate binaries |
| 148 | ############################################################################# |
| 149 | |
Kristofer Jonsson | d387405 | 2021-11-25 13:07:27 +0100 | [diff] [blame] | 150 | set(Python3_FIND_STRATEGY LOCATION) |
| 151 | find_package(Python3 COMPONENTS Interpreter) |
| 152 | |
Per Åstrand | 0954a8a | 2021-03-26 09:52:07 +0100 | [diff] [blame] | 153 | function(ethosu_add_binaries target) |
Kristofer Jonsson | ec45155 | 2021-06-04 18:02:59 +0200 | [diff] [blame] | 154 | set(SCRIPTS_DIR ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../scripts) |
| 155 | |
Per Åstrand | 0954a8a | 2021-03-26 09:52:07 +0100 | [diff] [blame] | 156 | add_custom_command(TARGET ${target} POST_BUILD |
Kristofer Jonsson | d387405 | 2021-11-25 13:07:27 +0100 | [diff] [blame] | 157 | COMMAND ${Python3_EXECUTABLE} ${SCRIPTS_DIR}/generate_binaries.py --output ./fw $<TARGET_FILE:${target}> -d ${target}.d |
Kristofer Jonsson | 8948704 | 2021-11-16 16:26:41 +0100 | [diff] [blame] | 158 | DEPFILE ${CMAKE_CURRENT_BINARY_DIR}/${target}.d |
Per Åstrand | 0954a8a | 2021-03-26 09:52:07 +0100 | [diff] [blame] | 159 | WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
| 160 | COMMENT "Generate binaries for ${target}") |
Kristofer Jonsson | ec45155 | 2021-06-04 18:02:59 +0200 | [diff] [blame] | 161 | endfunction() |
Per Åstrand | 0954a8a | 2021-03-26 09:52:07 +0100 | [diff] [blame] | 162 | |
Kristofer Jonsson | ec45155 | 2021-06-04 18:02:59 +0200 | [diff] [blame] | 163 | ############################################################################# |
| 164 | # Ethos-U NPU configuration |
| 165 | ############################################################################# |
| 166 | |
| 167 | function(ethosu_get_architecture config) |
| 168 | string(TOLOWER ${config} config) |
Kristofer Jonsson | a2f95d4 | 2021-06-30 13:07:35 +0200 | [diff] [blame] | 169 | |
| 170 | if(${config} STREQUAL "none") |
| 171 | set(ETHOSU_ARCH "none" PARENT_SCOPE) |
| 172 | set(ETHOSU_NUM_MACS 0 PARENT_SCOPE) |
| 173 | elseif(${config} MATCHES "^ethos-(u[0-9]+|uz)-([0-9]+$)") |
Kristofer Jonsson | ec45155 | 2021-06-04 18:02:59 +0200 | [diff] [blame] | 174 | set(ETHOSU_ARCH ${CMAKE_MATCH_1} PARENT_SCOPE) |
| 175 | set(ETHOSU_NUM_MACS ${CMAKE_MATCH_2} PARENT_SCOPE) |
| 176 | else() |
| 177 | message(FATAL_ERROR "Unknown NPU config ${config}") |
| 178 | endif() |
Per Åstrand | 0954a8a | 2021-03-26 09:52:07 +0100 | [diff] [blame] | 179 | endfunction() |