aboutsummaryrefslogtreecommitdiff
path: root/cmake/helpers.cmake
blob: 389deb9cf810799226023465c9e358ea9489bd98 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#
# 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})
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()