1#
2# Copyright 2020, Data61, CSIRO (ABN 41 687 119 230)
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7function(check_arch_clang)
8    if("${KernelSel4Arch}" STREQUAL "ia32")
9        string(
10            REGEX
11                MATCH
12                "^x86_64"
13                correct_triple
14                ${TRIPLE}
15        )
16    elseif("${KernelSel4Arch}" STREQUAL "x86_64")
17        string(
18            REGEX
19                MATCH
20                "^x86_64"
21                correct_triple
22                ${TRIPLE}
23        )
24    elseif("${KernelSel4Arch}" STREQUAL "aarch32" OR "${KernelSel4Arch}" STREQUAL "arm_hyp")
25        string(
26            REGEX
27                MATCH
28                "^arm"
29                correct_triple
30                ${TRIPLE}
31        )
32    elseif("${KernelSel4Arch}" STREQUAL "aarch64")
33        string(
34            REGEX
35                MATCH
36                "^aarch64"
37                correct_triple
38                ${TRIPLE}
39        )
40    elseif("${KernelSel4Arch}" STREQUAL "riscv32")
41        string(
42            REGEX
43                MATCH
44                "^riscv(32|64)"
45                correct_triple
46                ${TRIPLE}
47        )
48    elseif("${KernelSel4Arch}" STREQUAL "riscv64")
49        string(
50            REGEX
51                MATCH
52                "^riscv64"
53                correct_triple
54                ${TRIPLE}
55        )
56    else()
57        message(SEND_ERROR "KernelSel4Arch is not set to a valid arch")
58    endif()
59
60    if(NOT correct_triple)
61        message(SEND_ERROR "Clang Triple: ${TRIPLE} isn't for seL4_arch: ${KernelSel4Arch}")
62    endif()
63
64endfunction()
65
66function(check_arch_gcc)
67    if("${KernelSel4Arch}" STREQUAL "ia32")
68        set(compiler_variable "defined(__i386)")
69    elseif("${KernelSel4Arch}" STREQUAL "x86_64")
70        set(compiler_variable "defined(__x86_64)")
71    elseif("${KernelSel4Arch}" STREQUAL "aarch32" OR "${KernelSel4Arch}" STREQUAL "arm_hyp")
72        if(KernelArchArmV7a OR KernelArchArmV7ve)
73            set(compiler_variable "defined(__ARM_ARCH_7A__)")
74        elseif(KernelArchArmV6)
75            set(compiler_variable "defined(__ARM_ARCH_6__)")
76        elseif(KernelArchArmV8a)
77            set(compiler_variable "defined(__ARM_ARCH_8A__)")
78        endif()
79    elseif("${KernelSel4Arch}" STREQUAL "aarch64")
80        set(compiler_variable "defined(__aarch64__)")
81    elseif("${KernelSel4Arch}" STREQUAL "riscv32")
82        set(compiler_variable "__riscv_xlen == 32")
83    elseif("${KernelSel4Arch}" STREQUAL "riscv64")
84        set(compiler_variable "__riscv_xlen == 64")
85    else()
86        message(SEND_ERROR "KernelSel4Arch is not set to a valid arch")
87    endif()
88
89    set(arch_test "
90#if ${compiler_variable}
91    int main() {return 0;}
92#else
93#error Invalid arch
94#endif
95    ")
96
97    check_c_source_compiles("${arch_test}" compiler_arch_test)
98
99    if(NOT compiler_arch_test)
100        message(SEND_ERROR "Compiler: ${CMAKE_C_COMPILER} isn't for seL4_arch: ${KernelSel4Arch}")
101    endif()
102
103endfunction()
104
105function(check_arch_compiler)
106    if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
107        check_arch_clang()
108    else()
109        check_arch_gcc()
110    endif()
111endfunction()
112