From 72edc4e15ff61410555073bcad626014461598b3 Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Wed, 2 May 2018 00:09:31 -0500 Subject: [PATCH] clang/llvm: add initial configuration file for clang Add an LLVM backend and a clang toolchain variant to support building with llvm coming with popular Linux distributions. This has been tested with X86 boards: - quark_d2000_crb - quark_se_c1000_devboard/Arduino 101 Use: export ZEPHYR_TOOLCHAIN_VARIANT=clang Signed-off-by: Anas Nashif --- CMakeLists.txt | 10 +++++++++- arch/x86/core/CMakeLists.txt | 6 +++--- cmake/compiler/llvm.cmake | 22 ++++++++++++++++++++++ cmake/toolchain/clang.cmake | 24 ++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 cmake/compiler/llvm.cmake create mode 100644 cmake/toolchain/clang.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c9eb627ff..a6028d20d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -230,9 +230,17 @@ zephyr_compile_options(${CONFIG_COMPILER_OPT_AS_LIST}) # TODO: Include arch compiler options at this point. -# TODO: This Clang check is broken if(CMAKE_C_COMPILER_ID STREQUAL "Clang") zephyr_cc_option( + #FIXME: need to fix all of those + -Wno-sometimes-uninitialized + -Wno-shift-overflow + -Wno-missing-braces + -Wno-self-assign + -Wno-address-of-packed-member + -Wno-unused-function + -Wno-initializer-overrides + -Wno-section -Wno-unknown-warning-option -Wno-unused-variable -Wno-format-invalid-specifier diff --git a/arch/x86/core/CMakeLists.txt b/arch/x86/core/CMakeLists.txt index 2b9e6ce72a..ed0cd5ce96 100644 --- a/arch/x86/core/CMakeLists.txt +++ b/arch/x86/core/CMakeLists.txt @@ -1,12 +1,12 @@ zephyr_library() -if (COMPILER STREQUAL "clang") +if (CMAKE_LANG_COMPILER_ID STREQUAL "Clang") # We rely on GAS for assembling, so don't use the integrated assembler zephyr_compile_options_ifndef(CONFIG_X86_IAMCU $<$:-no-integrated-as>) +elseif(CMAKE_LANG_COMPILER_ID STREQUAL "GNU") + zephyr_compile_options($<$:-Wa,--divide>) endif() -zephyr_compile_options($<$:-Wa,--divide>) - zephyr_library_sources( cache.c cache_s.S diff --git a/cmake/compiler/llvm.cmake b/cmake/compiler/llvm.cmake new file mode 100644 index 0000000000..5548353f9b --- /dev/null +++ b/cmake/compiler/llvm.cmake @@ -0,0 +1,22 @@ +# Configuration for host installed llvm +# + +set(NOSTDINC "") + +foreach(file_name include include-fixed) + execute_process( + COMMAND ${CMAKE_C_COMPILER} --print-file-name=${file_name} + OUTPUT_VARIABLE _OUTPUT + ) + string(REGEX REPLACE "\n" "" _OUTPUT ${_OUTPUT}) + + list(APPEND NOSTDINC ${_OUTPUT}) +endforeach() + +foreach(isystem_include_dir ${NOSTDINC}) + list(APPEND isystem_include_flags -isystem ${isystem_include_dir}) +endforeach() + +set(CMAKE_REQUIRED_FLAGS -nostartfiles -nostdlib ${isystem_include_flags} -Wl,--unresolved-symbols=ignore-in-object-files) +string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") + diff --git a/cmake/toolchain/clang.cmake b/cmake/toolchain/clang.cmake new file mode 100644 index 0000000000..740b547cb8 --- /dev/null +++ b/cmake/toolchain/clang.cmake @@ -0,0 +1,24 @@ +set(CLANG_ROOT $ENV{CLANG_ROOT_DIR}) +set_ifndef(CLANG_ROOT /usr) +set(COMPILER clang) + +if(CONFIG_ARM) +set(triple arm-none-eabi) +set(CMAKE_EXE_LINKER_FLAGS_INIT "--specs=nosys.specs") +elseif(CONFIG_X86) +set(triple i686-pc-none-elf) +endif() + +set(CMAKE_C_COMPILER ${CLANG_ROOT}/bin/clang) +set(CMAKE_C_COMPILER_TARGET ${triple}) +set(CMAKE_ASM_COMPILER_TARGET ${triple}) +set(CMAKE_CXX_COMPILER ${CLANG_ROOT}/bin/clang++) +set(CMAKE_CXX_COMPILER_TARGET ${triple}) +set(CMAKE_AR "${CLANG_ROOT}/bin/llvm-ar" CACHE INTERNAL " " FORCE) +set(CMAKE_LINKER "${CLANG_ROOT}/bin/llvm-link" CACHE INTERNAL " " FORCE) +SET(CMAKE_NM "${CLANG_ROOT}/bin/llvm-nm" CACHE INTERNAL " " FORCE) +SET(CMAKE_OBJDUMP "${CLANG_ROOT}/bin/llvm-objdump" CACHE INTERNAL " " FORCE) +SET(CMAKE_RANLIB "${CLANG_ROOT}/bin/llvm-ranlib" CACHE INTERNAL " " FORCE) +set(CMAKE_OBJCOPY objcopy CACHE INTERNAL " " FORCE) +set(CMAKE_READELF readelf CACHE INTERNAL " " FORCE) +