1#
2# Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6
7cmake_minimum_required(VERSION 3.7.2)
8
9# File for helpers that are very specific to the kernel
10
11function(gen_invocation_header)
12    cmake_parse_arguments(PARSE_ARGV 0 "GEN" "LIBSEL4;ARCH;SEL4ARCH" "OUTPUT;XML" "")
13    if(NOT "${GEN_UNPARSED_ARGUMENTS}" STREQUAL "")
14        message(FATAL_ERROR "Unknown arguments to gen_invocation_header: ${GEN_UNPARSED_ARGUMENTS}")
15    endif()
16    # Ensure only one of arch or sel4arch
17    if(GEN_ARCH AND GEN_SEL4ARCH)
18        message(FATAL_ERROR "Can only specify one of ARCH or SEL4ARCH")
19    endif()
20    # OUTPUT and XML are required
21    if(("${GEN_OUTPUT}" STREQUAL "") OR ("${GEN_XML}" STREQUAL ""))
22        message(FATAL_ERROR "OUTPUT and XML must both be specified")
23    endif()
24    set(arch_setting "")
25    if(GEN_ARCH)
26        set(arch_setting "--arch")
27    elseif(GEN_SEL4ARCH)
28        set(arch_setting "--sel4_arch")
29    endif()
30    set(libsel4_setting "")
31    if(GEN_LIBSEL4)
32        set(libsel4_setting "--libsel4")
33    endif()
34    # Turn the input xml into an absolute path as we build commands that may be
35    # be run with a working directory that is not the current source directory
36    get_absolute_source_or_binary(xml_absolute "${GEN_XML}")
37    add_custom_command(
38        OUTPUT "${GEN_OUTPUT}"
39        COMMAND rm -f "${GEN_OUTPUT}"
40        COMMAND
41            "${PYTHON3}" "${INVOCATION_ID_GEN_PATH}"
42            --xml "${xml_absolute}" ${libsel4_setting} ${arch_setting}
43            --dest "${GEN_OUTPUT}"
44        DEPENDS "${xml_absolute}" "${INVOCATION_ID_GEN_PATH}"
45        COMMENT "Generate invocation header ${GEN_OUTPUT}"
46    )
47endfunction(gen_invocation_header)
48
49# Adds files to the global sources list, but only if the supplied dependencies are met.
50# A dependency lists can be specified with DEP and CFILES are added to c_sources whilst
51# ASMFILES are added to asm_sources. An PREFIX can be given as path to prefix to each
52# C and ASM file given
53function(add_sources)
54    cmake_parse_arguments(PARSE_ARGV 0 "ADD" "" "DEP;PREFIX" "CFILES;ASMFILES")
55    if(NOT "${ADD_UNPARSED_ARGUMENTS}" STREQUAL "")
56        message(FATAL_ERROR "Unknown arguments to add_c_sources: ${ADD_UNPARSED_ARGUMENTS}")
57    endif()
58    # Need to prefix files with the CMAKE_CURRENT_SOURCE_DIR as we use these
59    # in custom commands whose working directory is not the source directory
60    # Also need to ensure that if an additional prefix wasn't specified by the
61    # caller, that we don't add an additional /, as this will screw up file sorting
62    if(NOT "${ADD_PREFIX}" STREQUAL "")
63        set(ADD_PREFIX "${ADD_PREFIX}/")
64    endif()
65    set(ADD_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/${ADD_PREFIX}")
66    foreach(file IN LISTS ADD_CFILES)
67        list(APPEND new_c_sources "${ADD_PREFIX}${file}")
68    endforeach()
69    foreach(file IN LISTS ADD_ASMFILES)
70        list(APPEND new_asm_sources "${ADD_PREFIX}${file}")
71    endforeach()
72    list_append_if(c_sources "${ADD_DEP}" ${new_c_sources})
73    list_append_if(asm_sources "${ADD_DEP}" ${new_asm_sources})
74endfunction(add_sources)
75
76# If the dependencies list in the 'dep' argument is true then a bf file is added
77# to the bf_declarations list. The format of file+prefix+path is used in order to
78# separate where the bf file is located in the source, versus where it will get
79# generated.
80function(add_bf_source_old dep file prefix path)
81    list_append_if(
82        bf_declarations "${dep}" "${CMAKE_CURRENT_SOURCE_DIR}/${prefix}/${path}/${file}:${path}"
83    )
84endfunction(add_bf_source_old)
85