| # |
| # Copyright (c) 2020-2021 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. |
| # |
| |
| # Add .elf to all executables |
| set(CMAKE_EXECUTABLE_SUFFIX ".elf") |
| |
| ############################################################################# |
| # Link options |
| ############################################################################# |
| |
| function(ethosu_target_link_options target scope) |
| cmake_parse_arguments(ARG "" "LINK_FILE;ENTRY" "" ${ARGN}) |
| |
| # Store the link file in a property to be evaluated by the executable. |
| set_property(GLOBAL PROPERTY ETHOSU_TARGET_LINK_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${ARG_LINK_FILE}) |
| |
| if (ARG_ENTRY) |
| target_link_options(${target} ${scope} --entry Reset_Handler) |
| endif() |
| endfunction() |
| |
| function(ethosu_eval_link_options target) |
| # Get the link file from the cache |
| get_property(LINK_FILE GLOBAL PROPERTY ETHOSU_TARGET_LINK_FILE) |
| |
| set(prop "$<TARGET_PROPERTY:${target},COMPILE_DEFINITIONS>") |
| |
| if (CMAKE_CXX_COMPILER_ID STREQUAL "ARMClang") |
| set(LINK_FILE_OUT ${LINK_FILE}.scatter) |
| set(LINK_FILE_OPTION "--scatter") |
| |
| target_link_options(${target} PUBLIC |
| --predefine=\"-D$<JOIN:${prop},\" ;--predefine=\"-D>\") |
| elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| set(LINK_FILE_IN ${LINK_FILE}.ld) |
| get_filename_component(LINK_FILE_OUT_BASE ${LINK_FILE} NAME) |
| set(LINK_FILE_OUT ${CMAKE_CURRENT_BINARY_DIR}/${LINK_FILE_OUT_BASE}-${target}.ld) |
| set(LINK_FILE_OPTION "-T") |
| |
| add_custom_command( |
| OUTPUT ${LINK_FILE_OUT} |
| DEPENDS ${LINK_FILE_IN} |
| BYPRODUCTS ${LINK_FILE_OUT} |
| COMMAND ${CMAKE_C_COMPILER} -E -x c -P -o ${LINK_FILE_OUT} ${LINK_FILE_IN} |
| COMMAND_EXPAND_LISTS "-D$<JOIN:${prop},;-D>" |
| COMMENT "Preprocessing and generating linker script" |
| VERBATIM) |
| add_custom_target(${target}-linker-script |
| DEPENDS ${LINK_FILE_OUT} |
| VERBATIM) |
| add_dependencies(${target} ${target}-linker-script) |
| endif() |
| |
| target_link_options(${target} PUBLIC ${LINK_FILE_OPTION} ${LINK_FILE_OUT}) |
| set_target_properties(${target} PROPERTIES LINK_DEPENDS ${LINK_FILE_OUT}) |
| endfunction() |
| |
| ############################################################################# |
| # Executable |
| ############################################################################# |
| |
| function(ethosu_add_executable target) |
| cmake_parse_arguments(ARGS "" "TARGET_LIBRARY" "SOURCES;LIBRARIES" ${ARGN}) |
| add_executable(${target}) |
| |
| target_sources(${target} PRIVATE |
| ${ARGS_SOURCES}) |
| |
| if(NOT ARGS_TARGET_LIBRARY) |
| set(ARGS_TARGET_LIBRARY ethosu_target_init) |
| endif() |
| |
| target_link_libraries(${target} PRIVATE |
| ${ARGS_TARGET_LIBRARY} ethosu_core ${ARGS_LIBRARIES}) |
| |
| ethosu_eval_link_options(${target}) |
| |
| if (CMAKE_CXX_COMPILER_ID STREQUAL "ARMClang") |
| target_link_options(${target} PRIVATE --map --list=${target}.map) |
| elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| target_link_options(${target} PRIVATE -Xlinker -Map=${target}.map) |
| endif() |
| |
| ethosu_add_binaries(${target}) |
| endfunction() |
| |
| ############################################################################# |
| # Test |
| ############################################################################# |
| |
| function(ethosu_add_test target) |
| if(NOT BUILD_TESTING) |
| return() |
| endif() |
| |
| cmake_parse_arguments(ARGS "" "NAME" "COMMAND" ${ARGN}) |
| |
| if (NOT ARGS_COMMAND) |
| set(ARGS_COMMAND ${ETHOSU_COMMAND_DEFAULT}) |
| endif() |
| |
| if (NOT ARGS_NAME) |
| set(ARGS_NAME ${target}) |
| endif() |
| |
| add_test(NAME ${ARGS_NAME} |
| COMMAND ${ARGS_COMMAND} $<TARGET_FILE:${target}>) |
| endfunction() |
| |
| ############################################################################# |
| # Executable and test |
| ############################################################################# |
| |
| function(ethosu_add_executable_test target) |
| ethosu_add_executable(${target} ${ARGN}) |
| ethosu_add_test(${target} ${ARGN}) |
| endfunction() |
| |
| ############################################################################# |
| # Generate binaries |
| ############################################################################# |
| |
| function(ethosu_add_binaries target) |
| add_custom_command(TARGET ${target} POST_BUILD |
| COMMAND ${SCRIPTS_DIR}/generate_binaries.py --output ./fw $<TARGET_FILE:${target}> |
| BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/fw/* |
| WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} |
| COMMENT "Generate binaries for ${target}") |
| |
| endfunction() |