1# SPDX-License-Identifier: Apache-2.0 2 3# Configuration for host installed clang 4# 5 6set(NOSTDINC "") 7 8# Note that NOSYSDEF_CFLAG may be an empty string, and 9# set_ifndef() does not work with empty string. 10if(NOT DEFINED NOSYSDEF_CFLAG) 11 set(NOSYSDEF_CFLAG -undef) 12endif() 13 14if(DEFINED TOOLCHAIN_HOME) 15 set(find_program_clang_args PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) 16endif() 17 18find_program(CMAKE_C_COMPILER clang ${find_program_clang_args}) 19find_program(CMAKE_CXX_COMPILER clang++ ${find_program_clang_args}) 20 21if(SYSROOT_DIR) 22 # The toolchain has specified a sysroot dir, pass it to the compiler 23 list(APPEND TOOLCHAIN_C_FLAGS "--sysroot=${SYSROOT_DIR}") 24 list(APPEND TOOLCHAIN_LD_FLAGS "--sysroot=${SYSROOT_DIR}") 25endif() 26 27if(NOT "${ARCH}" STREQUAL "posix") 28 include(${ZEPHYR_BASE}/cmake/gcc-m-cpu.cmake) 29 include(${ZEPHYR_BASE}/cmake/gcc-m-fpu.cmake) 30 31 if("${ARCH}" STREQUAL "arm") 32 include(${ZEPHYR_BASE}/cmake/compiler/clang/target_arm.cmake) 33 elseif("${ARCH}" STREQUAL "arm64") 34 include(${ZEPHYR_BASE}/cmake/compiler/clang/target_arm64.cmake) 35 elseif("${ARCH}" STREQUAL "riscv") 36 include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_riscv.cmake) 37 endif() 38 39 if(DEFINED CMAKE_C_COMPILER_TARGET) 40 set(clang_target_flag "--target=${CMAKE_C_COMPILER_TARGET}") 41 endif() 42 43 foreach(file_name include/stddef.h) 44 execute_process( 45 COMMAND ${CMAKE_C_COMPILER} --print-file-name=${file_name} 46 OUTPUT_VARIABLE _OUTPUT 47 ) 48 get_filename_component(_OUTPUT "${_OUTPUT}" DIRECTORY) 49 string(REGEX REPLACE "\n" "" _OUTPUT ${_OUTPUT}) 50 51 list(APPEND NOSTDINC ${_OUTPUT}) 52 endforeach() 53 54 foreach(isystem_include_dir ${NOSTDINC}) 55 list(APPEND isystem_include_flags -isystem "\"${isystem_include_dir}\"") 56 endforeach() 57 58 if(CONFIG_X86) 59 if(CONFIG_64BIT) 60 list(APPEND TOOLCHAIN_C_FLAGS "-m64") 61 else() 62 list(APPEND TOOLCHAIN_C_FLAGS "-m32") 63 endif() 64 endif() 65 66 # LLVM will use a default sysroot for selection of the C library. The default 67 # C library sysroot was defined at built time of clang/LLVM. 68 # 69 # For example, LLVM for Arm comes pre-built with Picolibc, and thus no flags 70 # are required for selecting Picolibc. 71 # 72 # Other clang/LLVM distributions may come with other pre-built C libraries. 73 # clang/LLVM supports using an alternative C library, either by direct linking, 74 # or by specifying '--sysroot <path>'. Sysroot can also be passed using the 75 # CMake variable SYSROOT_DIR which will append the '--sysroot <path>' flags. 76 # 77 # LLVM for Arm provides a 'newlib.cfg' file for newlib C selection. 78 # Let us support this principle by looking for a dedicated 'newlib.cfg' or 79 # 'picolibc.cfg' and specify '--config <spec>.cfg' if such a file is found. 80 # If no cfg-file matching the chosen C implementation, then we assume that the 81 # chosen C implementation is identical to the default C library used be the 82 # toolchain. 83 if(CONFIG_NEWLIB_LIBC) 84 file(GLOB_RECURSE newlib_cfg ${LLVM_TOOLCHAIN_PATH}/newlib.cfg) 85 if(newlib_cfg) 86 list(GET newlib_cfg 0 newlib_cfg) 87 set_linker_property(PROPERTY c_library "--config=${newlib_cfg};-lc") 88 list(APPEND CMAKE_REQUIRED_FLAGS --config=${newlib_cfg}) 89 list(APPEND TOOLCHAIN_C_FLAGS --config=${newlib_cfg}) 90 endif() 91 endif() 92 93 if(CONFIG_PICOLIBC) 94 file(GLOB_RECURSE picolibc_cfg ${LLVM_TOOLCHAIN_PATH}/picolibc.cfg) 95 if(picolibc_cfg) 96 list(GET picolibc_cfg 0 picolibc_cfg) 97 set_linker_property(PROPERTY c_library "--config=${picolibc_cfg};-lc") 98 list(APPEND CMAKE_REQUIRED_FLAGS --config=${picolibc_cfg}) 99 list(APPEND TOOLCHAIN_C_FLAGS --config=${picolibc_cfg}) 100 endif() 101 endif() 102 103 list(APPEND CMAKE_REQUIRED_FLAGS -nostartfiles -nostdlib ${isystem_include_flags}) 104 string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") 105 106endif() 107