1#
2# Arm SCP/MCP Software
3# Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8include("${CMAKE_CURRENT_LIST_DIR}/Clang-Base.cmake")
9include("${CMAKE_CURRENT_LIST_DIR}/Generic-Baremetal.cmake")
10
11if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
12    execute_process(
13        COMMAND "which" "${SCP_LLVM_SYSROOT_CC}"
14        OUTPUT_VARIABLE LLVM_SYSROOT_CC_PATH
15        OUTPUT_STRIP_TRAILING_WHITESPACE)
16    execute_process(
17        COMMAND "dirname" "${LLVM_SYSROOT_CC_PATH}"
18        OUTPUT_VARIABLE LLVM_SYSROOT_PATH
19        OUTPUT_STRIP_TRAILING_WHITESPACE)
20    string(APPEND LLVM_SYSROOT_PATH "/../aarch64-none-elf")
21    cmake_path(NORMAL_PATH LLVM_SYSROOT_PATH OUTPUT_VARIABLE LLVM_SYSROOT_PATH)
22    set(gcc-multi-dir "")
23else()
24    execute_process(
25        COMMAND "${SCP_LLVM_SYSROOT_CC}" "-print-sysroot"
26        OUTPUT_VARIABLE LLVM_SYSROOT_PATH
27        OUTPUT_STRIP_TRAILING_WHITESPACE)
28    execute_process(
29        COMMAND "${SCP_LLVM_SYSROOT_CC}" "-print-multi-directory"
30                "-mcpu=${CMAKE_SYSTEM_PROCESSOR}" "-mthumb"
31        OUTPUT_VARIABLE gcc-multi-dir
32        OUTPUT_STRIP_TRAILING_WHITESPACE)
33endif()
34
35foreach(language IN ITEMS ASM C CXX)
36
37    if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
38        string(APPEND CMAKE_${language}_FLAGS_INIT
39               "-mstrict-align -fno-builtin -DAARCH64 -D__ASSEMBLY__ ")
40        string(APPEND CMAKE_${language}_FLAGS_INIT
41               "-I\"${LLVM_SYSROOT_PATH}/include\" ")
42    endif()
43
44    if(CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m(3|7|33|55)")
45        set(BUILD_TARGET "-mcpu=${CMAKE_SYSTEM_PROCESSOR} -mthumb ")
46        string(APPEND CMAKE_${language}_FLAGS_INIT "${BUILD_TARGET}")
47        if(CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m7")
48            set(CLANG_BUILTINS_ARCH "armv7em")
49        elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m33")
50            set(CLANG_BUILTINS_ARCH "armv8m.main")
51        elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m3")
52            set(CLANG_BUILTINS_ARCH "armv7m")
53        elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "cortex-m55")
54            set(CLANG_BUILTINS_ARCH "armv8.1m.main")
55        endif()
56    endif()
57
58    string(APPEND CMAKE_${language}_FLAGS_INIT
59           "--target=${CMAKE_${language}_COMPILER_TARGET} ")
60
61    string(APPEND CMAKE_${language}_FLAGS_INIT
62           "--sysroot=${LLVM_SYSROOT_PATH} ")
63    string(APPEND CMAKE_${language}_FLAGS_INIT "-ffunction-sections ")
64    string(APPEND CMAKE_${language}_FLAGS_INIT "-fdata-sections ")
65    string(APPEND CMAKE_${language}_FLAGS_INIT "-fshort-enums ")
66    string(APPEND CMAKE_${language}_FLAGS_INIT "-Wall -Werror -Wextra ")
67    string(APPEND CMAKE_${language}_FLAGS_INIT
68           "-Wno-error=deprecated-declarations ")
69    string(APPEND CMAKE_${language}_FLAGS_INIT "-Wno-unused-parameter ")
70    string(APPEND CMAKE_${language}_FLAGS_INIT
71           "-Wno-missing-field-initializers ")
72
73    string(APPEND CMAKE_${language}_FLAGS_DEBUG_INIT "-Og")
74endforeach()
75
76string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT "-Wl,--gc-sections ")
77string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT "-I\"${LLVM_SYSROOT_PATH}/include\" ")
78
79string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT
80       "-L${LLVM_SYSROOT_PATH}/lib/${gcc-multi-dir} ")
81if(NOT CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
82    string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT "-fuse-ld=lld ")
83    string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT
84           "-nostdlib -lclang_rt.builtins-${CLANG_BUILTINS_ARCH} ")
85    foreach(crtobj IN ITEMS crti crtbegin crt0 crtend crtn)
86        execute_process(
87            COMMAND "${SCP_LLVM_SYSROOT_CC}" "--print-file-name=${crtobj}.o"
88                    "-mcpu=${CMAKE_SYSTEM_PROCESSOR}" "-mthumb"
89            OUTPUT_VARIABLE gcc-${crtobj}
90            OUTPUT_STRIP_TRAILING_WHITESPACE)
91        cmake_path(NORMAL_PATH gcc-${crtobj} OUTPUT_VARIABLE gcc-${crtobj})
92        string(APPEND CMAKE_EXE_LINKER_FLAGS_INIT "${gcc-${crtobj}} ")
93    endforeach()
94endif()
95