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(NOT "${ARCH}" STREQUAL "posix") 22 include(${ZEPHYR_BASE}/cmake/gcc-m-cpu.cmake) 23 include(${ZEPHYR_BASE}/cmake/gcc-m-fpu.cmake) 24 25 if("${ARCH}" STREQUAL "arm") 26 list(APPEND TOOLCHAIN_C_FLAGS 27 -fshort-enums 28 ) 29 list(APPEND TOOLCHAIN_LD_FLAGS 30 -fshort-enums 31 ) 32 33 include(${ZEPHYR_BASE}/cmake/compiler/clang/target_arm.cmake) 34 elseif("${ARCH}" STREQUAL "arm64") 35 include(${ZEPHYR_BASE}/cmake/compiler/clang/target_arm64.cmake) 36 elseif("${ARCH}" STREQUAL "riscv") 37 include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_riscv.cmake) 38 endif() 39 40 if(DEFINED CMAKE_C_COMPILER_TARGET) 41 set(clang_target_flag "--target=${CMAKE_C_COMPILER_TARGET}") 42 endif() 43 44 foreach(file_name include/stddef.h) 45 execute_process( 46 COMMAND ${CMAKE_C_COMPILER} --print-file-name=${file_name} 47 OUTPUT_VARIABLE _OUTPUT 48 ) 49 get_filename_component(_OUTPUT "${_OUTPUT}" DIRECTORY) 50 string(REGEX REPLACE "\n" "" _OUTPUT ${_OUTPUT}) 51 52 list(APPEND NOSTDINC ${_OUTPUT}) 53 endforeach() 54 55 foreach(isystem_include_dir ${NOSTDINC}) 56 list(APPEND isystem_include_flags -isystem "\"${isystem_include_dir}\"") 57 endforeach() 58 59 if(CONFIG_X86) 60 if(CONFIG_64BIT) 61 list(APPEND TOOLCHAIN_C_FLAGS "-m64") 62 else() 63 list(APPEND TOOLCHAIN_C_FLAGS "-m32") 64 endif() 65 endif() 66 67 # LLVM will use a default sysroot for selection of the C library. The default 68 # C library sysroot was defined at built time of clang/LLVM. 69 # 70 # For example, LLVM for Arm comes pre-built with Picolibc, and thus no flags 71 # are required for selecting Picolibc. 72 # 73 # Other clang/LLVM distributions may come with other pre-built C libraries. 74 # clang/LLVM supports using an alternative C library, either by direct linking, 75 # or by specifying '--sysroot <path>'. 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 # This libgcc code is partially duplicated in compiler/*/target.cmake 104 execute_process( 105 COMMAND ${CMAKE_C_COMPILER} ${clang_target_flag} ${TOOLCHAIN_C_FLAGS} 106 --print-libgcc-file-name 107 OUTPUT_VARIABLE RTLIB_FILE_NAME 108 OUTPUT_STRIP_TRAILING_WHITESPACE 109 ) 110 111 get_filename_component(RTLIB_DIR ${RTLIB_FILE_NAME} DIRECTORY) 112 get_filename_component(RTLIB_NAME_WITH_PREFIX ${RTLIB_FILE_NAME} NAME_WLE) 113 string(REPLACE lib "" RTLIB_NAME ${RTLIB_NAME_WITH_PREFIX}) 114 115 set_property(TARGET linker PROPERTY lib_include_dir "-L${RTLIB_DIR}") 116 set_property(TARGET linker PROPERTY rt_library "-l${RTLIB_NAME}") 117 118 list(APPEND CMAKE_REQUIRED_FLAGS -nostartfiles -nostdlib ${isystem_include_flags}) 119 string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") 120 121endif() 122