blob: 686488b83b7c905d0883784bbe4cbb7aaf5b9d2f [file] [log] [blame]
Kristofer Jonsson43ce4912020-11-20 09:42:53 +01001#
2# Copyright (c) 2020 Arm Limited. All rights reserved.
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 Jonssonf62c3d72021-01-21 17:39:03 +010019# Add .elf to all executables
20set(CMAKE_EXECUTABLE_SUFFIX ".elf")
21
22#############################################################################
23# Link options
24#############################################################################
25
26function(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.
30 set_property(GLOBAL PROPERTY ETHOSU_TARGET_LINK_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${ARG_LINK_FILE})
31
32 if (ARG_ENTRY)
33 target_link_options(${target} ${scope} --entry Reset_Handler)
34 endif()
35endfunction()
36
37function(ethosu_eval_link_options target)
38 # Get the link file from the cache
39 get_property(LINK_FILE GLOBAL PROPERTY ETHOSU_TARGET_LINK_FILE)
40
41 set(prop "$<TARGET_PROPERTY:${target},COMPILE_DEFINITIONS>")
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010042
43 if (CMAKE_CXX_COMPILER_ID STREQUAL "ARMClang")
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010044 set(LINK_FILE_OUT ${LINK_FILE}.scatter)
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010045 set(LINK_FILE_OPTION "--scatter")
46
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010047 target_link_options(${target} PUBLIC
48 --predefine=\"-D$<JOIN:${prop},\" ;--predefine=\"-D>\")
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010049 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010050 set(LINK_FILE_IN ${LINK_FILE}.ld)
51 get_filename_component(LINK_FILE_OUT_BASE ${LINK_FILE} NAME)
52 set(LINK_FILE_OUT ${CMAKE_CURRENT_BINARY_DIR}/${LINK_FILE_OUT_BASE}-${target}.ld)
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010053 set(LINK_FILE_OPTION "-T")
54
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010055 add_custom_command(
56 OUTPUT ${LINK_FILE_OUT}
57 DEPENDS ${LINK_FILE_IN}
58 BYPRODUCTS ${LINK_FILE_OUT}
59 COMMAND ${CMAKE_C_COMPILER} -E -x c -P -o ${LINK_FILE_OUT} ${LINK_FILE_IN}
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010060 COMMAND_EXPAND_LISTS "-D$<JOIN:${prop},;-D>"
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010061 COMMENT "Preprocessing and generating linker script"
62 VERBATIM)
63 add_custom_target(${target}-linker-script
64 DEPENDS ${LINK_FILE_OUT}
65 VERBATIM)
66 add_dependencies(${target} ${target}-linker-script)
67 endif()
68
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010069 target_link_options(${target} PUBLIC ${LINK_FILE_OPTION} ${LINK_FILE_OUT})
Kristofer Jonsson43ce4912020-11-20 09:42:53 +010070 set_target_properties(${target} PROPERTIES LINK_DEPENDS ${LINK_FILE_OUT})
71endfunction()
Kristofer Jonssonf62c3d72021-01-21 17:39:03 +010072
73#############################################################################
74# Executable
75#############################################################################
76
77function(ethosu_add_executable target)
78 cmake_parse_arguments(ARGS "" "TARGET_LIBRARY" "SOURCES;LIBRARIES" ${ARGN})
79 add_executable(${target})
80
81 target_sources(${target} PRIVATE
82 ${ARGS_SOURCES})
83
84 if(NOT ARGS_TARGET_LIBRARY)
85 set(ARGS_TARGET_LIBRARY ethosu_target_init)
86 endif()
87
88 target_link_libraries(${target} PRIVATE
89 ${ARGS_TARGET_LIBRARY} ethosu_core ${ARGS_LIBRARIES})
90
91 ethosu_eval_link_options(${target})
92endfunction()
93
94#############################################################################
95# Test
96#############################################################################
97
98function(ethosu_add_test target)
99 if(NOT BUILD_TESTING)
100 return()
101 endif()
102
103 cmake_parse_arguments(ARGS "" "NAME" "COMMAND" ${ARGN})
104
105 if (NOT ARGS_COMMAND)
106 set(ARGS_COMMAND ${ETHOSU_COMMAND_DEFAULT})
107 endif()
108
109 if (NOT ARGS_NAME)
110 set(ARGS_NAME ${target})
111 endif()
112
113 add_test(NAME ${ARGS_NAME}
114 COMMAND ${ARGS_COMMAND} $<TARGET_FILE:${target}>)
115endfunction()
116
117#############################################################################
118# Executable and test
119#############################################################################
120
121function(ethosu_add_executable_test target)
122 ethosu_add_executable(${target} ${ARGN})
123 ethosu_add_test(${target} ${ARGN})
124endfunction()