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