# ##############################################################################
# arch/arm64/src/cmake/clang.cmake
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. The ASF licenses this
# file to you 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
#
# http://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.
#
# ##############################################################################
set(CMAKE_C_COMPILER_FORCED TRUE)
set(CMAKE_CXX_COMPILER_FORCED TRUE)
set(CMAKE_ASM_COMPILER clang)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_PREPROCESSOR clang -E -P -x c)
set(CMAKE_STRIP llvm-strip --strip-unneeded)
set(CMAKE_OBJCOPY llvm-objcopy)
set(CMAKE_OBJDUMP llvm-objdump)
set(CMAKE_LINKER ld.lld)
set(CMAKE_LD ld.lld)
set(CMAKE_AR llvm-ar)
set(CMAKE_NM llvm-nm)
set(CMAKE_RANLIB llvm-ranlib)
add_link_options(-Wl,--entry=__start)
add_link_options(-nostdlib)
set(NO_LTO "-fno-lto")
add_compile_options(--target=aarch64-none-elf)
if(CONFIG_DEBUG_CUSTOMOPT)
add_compile_options(${CONFIG_DEBUG_OPTLEVEL})
elseif(CONFIG_DEBUG_FULLOPT)
add_compile_options(-Os)
endif()
if(NOT CONFIG_DEBUG_NOOPT)
add_compile_options(-fno-strict-aliasing)
endif()
if(CONFIG_FRAME_POINTER)
add_compile_options(-fno-omit-frame-pointer)
else()
add_compile_options(-fomit-frame-pointer)
endif()
if(CONFIG_STACK_CANARIES)
add_compile_options(-fstack-protector-all)
endif()
if(CONFIG_STACK_USAGE)
add_compile_options(-fstack-usage)
endif()
if(CONFIG_STACK_USAGE_WARNING)
add_compile_options(-Wstack-usage=${CONFIG_STACK_USAGE_WARNING})
endif()
if(CONFIG_MM_UBSAN_ALL)
add_compile_options(${CONFIG_MM_UBSAN_OPTION})
endif()
if(CONFIG_MM_UBSAN_TRAP_ON_ERROR)
add_compile_options(-fsanitize-undefined-trap-on-error)
endif()
if(CONFIG_MM_KASAN_INSTRUMENT_ALL)
add_compile_options(-fsanitize=kernel-address)
set(KASAN_PARAM "")
list(APPEND KASAN_PARAM "asan-stack=0")
list(APPEND KASAN_PARAM "asan-instrumentation-with-call-threshold=0")
if(CONFIG_MM_KASAN_GLOBAL)
list(APPEND KASAN_PARAM "asan-globals=1")
else()
list(APPEND KASAN_PARAM "asan-globals=0")
endif()
if(CONFIG_MM_KASAN_DISABLE_READS_CHECK)
list(APPEND KASAN_PARAM "asan-instrument-reads=0")
endif()
if(CONFIG_MM_KASAN_DISABLE_WRITES_CHECK)
list(APPEND KASAN_PARAM "asan-instrument-writes=0")
endif()
set(KASAN_PARAM_STR "")
foreach(param IN LISTS KASAN_PARAM)
string(APPEND KASAN_PARAM_STR "-mllvm -${param} ")
endforeach()
file(WRITE ${CMAKE_BINARY_DIR}/kasan_params.cmd "${KASAN_PARAM_STR}")
add_compile_options("@${CMAKE_BINARY_DIR}/kasan_params.cmd")
endif()
if(CONFIG_ARCH_INSTRUMENT_ALL)
add_compile_options(-finstrument-functions)
endif()
if(CONFIG_COVERAGE_ALL)
add_compile_options(-fprofile-instr-generate -fcoverage-mapping)
endif()
if(CONFIG_PROFILE_ALL)
add_compile_options(-pg)
endif()
if(CONFIG_ARCH_FPU)
add_compile_options(-D_LDBL_EQ_DBL)
endif()
add_compile_options(
-fno-common
-Wall
-Wshadow
-Wundef
-Wno-attributes
-Wno-unknown-pragmas
$<$<COMPILE_LANGUAGE:C>:-Werror>
$<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>)
if(NOT CONFIG_LIBCXXTOOLCHAIN)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-nostdinc++>)
endif()
if(NOT CONFIG_ARCH_TOOLCHAIN_CLANG)
add_compile_options(-Wno-psabi)
endif()
if(CONFIG_CXX_STANDARD)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-std=${CONFIG_CXX_STANDARD}>)
endif()
if(NOT CONFIG_CXX_EXCEPTION)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
$<$<COMPILE_LANGUAGE:CXX>:-fcheck-new>)
endif()
if(NOT CONFIG_CXX_RTTI)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
endif()
add_link_options(-nostdlib)
if(CONFIG_DEBUG_OPT_UNUSED_SECTIONS)
add_link_options(-Wl,--gc-sections)
add_compile_options(-ffunction-sections -fdata-sections)
endif()
if(CONFIG_DEBUG_LINK_MAP)
add_link_options(-Wl,--cref -Wl,-Map=nuttx.map)
endif()
if(CONFIG_DEBUG_SYMBOLS)
add_compile_options(${CONFIG_DEBUG_SYMBOLS_LEVEL})
endif()
if(NOT CONFIG_ARCH_USE_MMU)
add_compile_options(-fno-builtin)
endif()
add_link_options(-no-pie)
if(CONFIG_LIBM_TOOLCHAIN)
add_compile_options(--config=newlib.cfg)
endif()
# override nuttx_find_toolchain_lib
set(NUTTX_FIND_TOOLCHAIN_LIB_DEFINED true)
if(CONFIG_BUILTIN_TOOLCHAIN)
function(nuttx_find_toolchain_lib)
if(ARGN)
execute_process(
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} ${NUTTX_EXTRA_FLAGS}
--print-file-name=${ARGN}
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE extra_lib_path)
nuttx_add_extra_library(${extra_lib_path})
else()
execute_process(
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} ${NUTTX_EXTRA_FLAGS}
--print-libgcc-file-name
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE libgcc_path)
get_filename_component(libgcc_name ${libgcc_path} NAME)
execute_process(
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} ${NUTTX_EXTRA_FLAGS}
--print-file-name=${libgcc_name}
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE libgcc)
nuttx_add_extra_library(${libgcc})
endif()
endfunction()
else()
function(nuttx_find_toolchain_lib)
if(ARGN)
execute_process(
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} ${NUTTX_EXTRA_FLAGS}
--print-file-name=${ARGN}
OUTPUT_STRIP_TRAILING_WHITESPACE
OUTPUT_VARIABLE extra_lib_path)
endif()
nuttx_add_extra_library(${extra_lib_path})
endfunction()
endif()
set(PREPROCESS ${CMAKE_C_COMPILER} ${CMAKE_C_FLAG_ARGS} -E -P -x c)