Skip to content

Commit

Permalink
toolchain: improved toolchain abstraction for compilers and linker
Browse files Browse the repository at this point in the history
First abstraction completed for the toolchains:
- gcc
- clang

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
  • Loading branch information
tejlmand authored and carlescufi committed Sep 4, 2020
1 parent e917c6c commit c55c64e
Show file tree
Hide file tree
Showing 34 changed files with 590 additions and 730 deletions.
294 changes: 132 additions & 162 deletions CMakeLists.txt

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion arch/arm/core/aarch32/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
zephyr_library()

if (CONFIG_COVERAGE)
toolchain_cc_coverage()
zephyr_compile_options($<TARGET_PROPERTY:compiler,coverage>)
zephyr_link_libraries($<TARGET_PROPERTY:linker,coverage>)
endif ()

zephyr_library_sources(
Expand Down
3 changes: 2 additions & 1 deletion arch/arm/core/aarch64/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
zephyr_library()

if (CONFIG_COVERAGE)
toolchain_cc_coverage()
zephyr_compile_options($<TARGET_PROPERTY:compiler,coverage>)
zephyr_link_libraries($<TARGET_PROPERTY:linker,coverage>)
endif ()

zephyr_library_sources(
Expand Down
11 changes: 7 additions & 4 deletions arch/posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@ zephyr_compile_options(
)

# @Intent: Obtain compiler specific flags for no freestanding compilation
toolchain_cc_no_freestanding_options()
zephyr_compile_options($<TARGET_PROPERTY:compiler,hosted>)

zephyr_include_directories(${BOARD_DIR})

if (CONFIG_COVERAGE)
toolchain_cc_coverage()
zephyr_compile_options($<TARGET_PROPERTY:compiler,coverage>)
zephyr_link_libraries($<TARGET_PROPERTY:linker,coverage>)
endif ()

if (CONFIG_ASAN)
toolchain_cc_asan()
zephyr_compile_options($<TARGET_PROPERTY:compiler,sanitize_address>)
zephyr_link_libraries($<TARGET_PROPERTY:linker,sanitize_address>)
endif ()

if (CONFIG_UBSAN)
toolchain_cc_ubsan()
zephyr_compile_options($<TARGET_PROPERTY:compiler,sanitize_undefined>)
zephyr_link_libraries($<TARGET_PROPERTY:linker,sanitize_undefined>)
endif ()

zephyr_compile_definitions(_POSIX_C_SOURCE=200809 _XOPEN_SOURCE=600 _XOPEN_SOURCE_EXTENDED)
Expand Down
3 changes: 2 additions & 1 deletion arch/x86/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
zephyr_library()

if (CONFIG_COVERAGE)
toolchain_cc_coverage()
zephyr_compile_options($<TARGET_PROPERTY:compiler,coverage>)
zephyr_link_libraries($<TARGET_PROPERTY:linker,coverage>)
endif ()

zephyr_library_sources(cpuhalt.c)
Expand Down
20 changes: 13 additions & 7 deletions cmake/app/boilerplate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,13 @@ alternate .overlay file using this parameter. These settings will override the \
settings in the board's .dts file. Multiple files may be listed, e.g. \
DTC_OVERLAY_FILE=\"dts1.overlay dts2.overlay\"")

# Populate USER_CACHE_DIR with a directory that user applications may
# write cache files to.
if(NOT DEFINED USER_CACHE_DIR)
find_appropriate_cache_directory(USER_CACHE_DIR)
endif()
message(STATUS "Cache files will be written to: ${USER_CACHE_DIR}")

# Prevent CMake from testing the toolchain
set(CMAKE_C_COMPILER_FORCED 1)
set(CMAKE_CXX_COMPILER_FORCED 1)
Expand Down Expand Up @@ -605,6 +612,12 @@ include(${ZEPHYR_BASE}/cmake/target_toolchain.cmake)

project(Zephyr-Kernel VERSION ${PROJECT_VERSION})
enable_language(C CXX ASM)
# The setup / configuration of the toolchain itself and the configuration of
# supported compilation flags are now split, as this allows to use the toolchain
# for generic purposes, for example DTS, and then test the toolchain for
# supported flags at stage two.
# Testing the toolchain flags requires the enable_language() to have been called in CMake.
include(${ZEPHYR_BASE}/cmake/target_toolchain_flags.cmake)

# 'project' sets PROJECT_BINARY_DIR to ${CMAKE_CURRENT_BINARY_DIR},
# but for legacy reasons we need it to be set to
Expand All @@ -624,13 +637,6 @@ set(KERNEL_EXE_NAME ${KERNEL_NAME}.exe)
set(KERNEL_STAT_NAME ${KERNEL_NAME}.stat)
set(KERNEL_STRIP_NAME ${KERNEL_NAME}.strip)

# Populate USER_CACHE_DIR with a directory that user applications may
# write cache files to.
if(NOT DEFINED USER_CACHE_DIR)
find_appropriate_cache_directory(USER_CACHE_DIR)
endif()
message(STATUS "Cache files will be written to: ${USER_CACHE_DIR}")

include(${BOARD_DIR}/board.cmake OPTIONAL)

# If we are using a suitable ethernet driver inside qemu, then these options
Expand Down
103 changes: 103 additions & 0 deletions cmake/compiler/clang/compiler_flags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# First step is to inherit all properties from gcc, as clang is compatible with most flags.
include(${ZEPHYR_BASE}/cmake/compiler/gcc/compiler_flags.cmake)

# Now, let's overwrite the flags that are different in clang.

# No property flag, clang doesn't understand fortify at all
set_compiler_property(PROPERTY security_fortify)

# No property flag, this is used by the native_posix, clang has problems
# compiling the native_posix with -fno-freestanding.
check_set_compiler_property(PROPERTY hosted)

# clang flags for coverage generation
set_property(TARGET compiler PROPERTY coverage --coverage -fno-inline)

#######################################################
# This section covers flags related to warning levels #
#######################################################

# clang option standard warning base in Zephyr
set_compiler_property(PROPERTY warning_base
-Wall
-Wformat
-Wformat-security
-Wno-format-zero-length
-Wno-main
)

check_set_compiler_property(APPEND PROPERTY warning_base -Wno-pointer-sign)

# Prohibit void pointer arithmetic. Illegal in C99
check_set_compiler_property(APPEND PROPERTY warning_base -Wpointer-arith)

# clang options for warning levels 1, 2, 3, when using `-DW=[1|2|3]`
set_compiler_property(PROPERTY warning_dw_1
-Wextra
-Wunused
-Wno-unused-parameter
-Wmissing-declarations
-Wmissing-format-attribute
)
check_set_compiler_property(APPEND PROPERTY warning_dw_1
-Wold-style-definition
-Wmissing-prototypes
-Wmissing-include-dirs
-Wunused-but-set-variable
-Wno-missing-field-initializers
)

set_compiler_property(PROPERTY warning_dw_2
-Waggregate-return
-Wcast-align
-Wdisabled-optimization
-Wnested-externs
-Wshadow
)

check_set_compiler_property(APPEND PROPERTY warning_dw_2
-Wlogical-op
-Wmissing-field-initializers
)

set_compiler_property(PROPERTY warning_dw_3
-Wbad-function-cast
-Wcast-qual
-Wconversion
-Wpacked
-Wpadded
-Wpointer-arith
-Wredundant-decls
-Wswitch-default
)

check_set_compiler_property(APPEND PROPERTY warning_dw_3
-Wpacked-bitfield-compat
-Wvla
)


check_set_compiler_property(PROPERTY warning_extended
#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
-Wno-gnu
# comparison of unsigned expression < 0 is always false
-Wno-tautological-compare
)

set_compiler_property(PROPERTY warning_error_coding_guideline
-Werror=vla
-Wimplicit-fallthrough
-Wconversion
-Woverride-init
)
21 changes: 0 additions & 21 deletions cmake/compiler/clang/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,3 @@ macro(toolchain_cc_nostdinc)
zephyr_compile_options( -nostdinc)
endif()
endmacro()

# Clang and GCC are almost feature+flag compatible, so reuse freestanding gcc
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_security_canaries.cmake)
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_optimizations.cmake)
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_cpp.cmake)
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_asm.cmake)
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_baremetal.cmake)
include(${ZEPHYR_BASE}/cmake/compiler/${COMPILER}/target_warnings.cmake)
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_imacros.cmake)
include(${ZEPHYR_BASE}/cmake/compiler/gcc/target_base.cmake)
include(${ZEPHYR_BASE}/cmake/compiler/${COMPILER}/target_coverage.cmake)
include(${ZEPHYR_BASE}/cmake/compiler/${COMPILER}/target_sanitizers.cmake)

macro(toolchain_cc_security_fortify)
# No op, clang doesn't understand fortify at all
endmacro()

macro(toolchain_cc_no_freestanding_options)
# No op, this is used by the native_posix, clang has problems
# compiling the native_posix with -fno-freestanding.
endmacro()
18 changes: 0 additions & 18 deletions cmake/compiler/clang/target_coverage.cmake

This file was deleted.

15 changes: 0 additions & 15 deletions cmake/compiler/clang/target_sanitizers.cmake

This file was deleted.

135 changes: 0 additions & 135 deletions cmake/compiler/clang/target_warnings.cmake

This file was deleted.

Loading

0 comments on commit c55c64e

Please sign in to comment.