diff --git a/cmake/app/boilerplate.cmake b/cmake/app/boilerplate.cmake index 2def3a5aeed..23e6a317829 100644 --- a/cmake/app/boilerplate.cmake +++ b/cmake/app/boilerplate.cmake @@ -264,8 +264,22 @@ set(CMAKE_CXX_COMPILER_FORCED 1) include(${ZEPHYR_BASE}/cmake/version.cmake) include(${ZEPHYR_BASE}/cmake/host-tools.cmake) + +# DTS should be close to kconfig because CONFIG_ variables from +# kconfig and dts should be available at the same time. +# +# The DT system uses a C preprocessor for it's code generation needs. +# This creates an awkward chicken-and-egg problem, because we don't +# always know exactly which toolchain the user needs until we know +# more about the target, e.g. after DT and Kconfig. +# +# To resolve this we find "some" C toolchain, configure it generically +# with the minimal amount of configuration needed to have it +# preprocess DT sources, and then, after we have finished processing +# both DT and Kconfig we complete the target-specific configuration, +# and possibly change the toolchain. +include(${ZEPHYR_BASE}/cmake/generic_toolchain.cmake) include(${ZEPHYR_BASE}/cmake/kconfig.cmake) -include(${ZEPHYR_BASE}/cmake/toolchain.cmake) find_package(Git QUIET) if(GIT_FOUND) @@ -294,14 +308,10 @@ else() set(SOC_PATH ${SOC_FAMILY}/${SOC_SERIES}) endif() - -# DTS should be run directly after kconfig because CONFIG_ variables -# from kconfig and dts should be available at the same time. But -# running DTS involves running the preprocessor, so we put it behind -# toolchain. Meaning toolchain.cmake is the only component where -# kconfig and dts variables aren't available at the same time. include(${ZEPHYR_BASE}/cmake/dts.cmake) +include(${ZEPHYR_BASE}/cmake/target_toolchain.cmake) + set(KERNEL_NAME ${CONFIG_KERNEL_BIN_NAME}) set(KERNEL_ELF_NAME ${KERNEL_NAME}.elf) diff --git a/cmake/compiler/clang/generic.cmake b/cmake/compiler/clang/generic.cmake new file mode 100644 index 00000000000..aa75ead07f4 --- /dev/null +++ b/cmake/compiler/clang/generic.cmake @@ -0,0 +1 @@ +find_program(CMAKE_C_COMPILER clang PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) diff --git a/cmake/compiler/clang.cmake b/cmake/compiler/clang/target.cmake similarity index 60% rename from cmake/compiler/clang.cmake rename to cmake/compiler/clang/target.cmake index c04370147e3..afc061541f2 100644 --- a/cmake/compiler/clang.cmake +++ b/cmake/compiler/clang/target.cmake @@ -9,15 +9,15 @@ if(NOT DEFINED NOSYSDEF_CFLAG) set(NOSYSDEF_CFLAG -undef) endif() -find_program(CMAKE_C_COMPILER clang PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) -find_program(CMAKE_CXX_COMPILER clang++ PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) -find_program(CMAKE_AR llvm-ar PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) -find_program(CMAKE_LINKER llvm-link PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) -find_program(CMAKE_NM llvm-nm PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) -find_program(CMAKE_OBJDUMP llvm-objdump PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) -find_program(CMAKE_RANLIB llvm-ranlib PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) -find_program(CMAKE_OBJCOPY objcopy PATH ${TOOLCHAIN_HOME}) -find_program(CMAKE_READELF readelf PATH ${TOOLCHAIN_HOME}) +find_program(CMAKE_C_COMPILER clang PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) +find_program(CMAKE_CXX_COMPILER clang++ PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) +find_program(CMAKE_AR llvm-ar PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) +find_program(CMAKE_LINKER llvm-link PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) +find_program(CMAKE_NM llvm-nm PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) +find_program(CMAKE_OBJDUMP llvm-objdump PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) +find_program(CMAKE_RANLIB llvm-ranlib PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) +find_program(CMAKE_OBJCOPY objcopy PATH ${TOOLCHAIN_HOME}) +find_program(CMAKE_READELF readelf PATH ${TOOLCHAIN_HOME}) foreach(file_name include include-fixed) execute_process( diff --git a/cmake/compiler/gcc/generic.cmake b/cmake/compiler/gcc/generic.cmake new file mode 100644 index 00000000000..7f405bc6829 --- /dev/null +++ b/cmake/compiler/gcc/generic.cmake @@ -0,0 +1,26 @@ +set_ifndef(CC gcc) + +find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}${CC} PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) + +if(CMAKE_C_COMPILER STREQUAL CMAKE_C_COMPILER-NOTFOUND) + message(FATAL_ERROR "Zephyr was unable to find the toolchain. Is the environment misconfigured? +User-configuration: +ZEPHYR_TOOLCHAIN_VARIANT: ${ZEPHYR_TOOLCHAIN_VARIANT} +Internal variables: +CROSS_COMPILE: ${CROSS_COMPILE} +TOOLCHAIN_HOME: ${TOOLCHAIN_HOME} +") +endif() + +execute_process( + COMMAND ${CMAKE_C_COMPILER} --version + RESULT_VARIABLE ret + OUTPUT_QUIET + ERROR_QUIET + ) +if(ret) + message(FATAL_ERROR "Executing the below command failed. Are permissions set correctly? +'${CMAKE_C_COMPILER} --version' +" + ) +endif() diff --git a/cmake/compiler/gcc.cmake b/cmake/compiler/gcc/target.cmake similarity index 88% rename from cmake/compiler/gcc.cmake rename to cmake/compiler/gcc/target.cmake index 5d08a8173d5..3dc2ae0dd05 100644 --- a/cmake/compiler/gcc.cmake +++ b/cmake/compiler/gcc/target.cmake @@ -1,8 +1,8 @@ -set_ifndef(CC gcc) set_ifndef(C++ g++) # Configures CMake for using GCC, this script is re-used by several # GCC-based toolchains + find_program(CMAKE_C_COMPILER ${CROSS_COMPILE}${CC} PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) find_program(CMAKE_OBJCOPY ${CROSS_COMPILE}objcopy PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) find_program(CMAKE_OBJDUMP ${CROSS_COMPILE}objdump PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) @@ -14,29 +14,6 @@ find_program(CMAKE_READELF ${CROSS_COMPILE}readelf PATH ${TOOLCHAIN_HOME} NO_ find_program(CMAKE_GDB ${CROSS_COMPILE}gdb PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) find_program(CMAKE_NM ${CROSS_COMPILE}nm PATH ${TOOLCHAIN_HOME} NO_DEFAULT_PATH) -if(CMAKE_C_COMPILER STREQUAL CMAKE_C_COMPILER-NOTFOUND) - message(FATAL_ERROR "Zephyr was unable to find the toolchain. Is the environment misconfigured? -User-configuration: -ZEPHYR_TOOLCHAIN_VARIANT: ${ZEPHYR_TOOLCHAIN_VARIANT} -Internal variables: -CROSS_COMPILE: ${CROSS_COMPILE} -TOOLCHAIN_HOME: ${TOOLCHAIN_HOME} -") -endif() - -execute_process( - COMMAND ${CMAKE_C_COMPILER} --version - RESULT_VARIABLE ret - OUTPUT_QUIET - ERROR_QUIET - ) -if(ret) - message(FATAL_ERROR "Executing the below command failed. Are permissions set correctly? -'${CMAKE_C_COMPILER} --version' -" - ) -endif() - if(CONFIG_CPLUSPLUS) set(cplusplus_compiler ${CROSS_COMPILE}${C++}) else() diff --git a/cmake/compiler/host-gcc/generic.cmake b/cmake/compiler/host-gcc/generic.cmake new file mode 100644 index 00000000000..0a44a1aa68d --- /dev/null +++ b/cmake/compiler/host-gcc/generic.cmake @@ -0,0 +1,3 @@ +# Configures CMake for using GCC + +find_program(CMAKE_C_COMPILER gcc) diff --git a/cmake/compiler/host-gcc.cmake b/cmake/compiler/host-gcc/target.cmake similarity index 100% rename from cmake/compiler/host-gcc.cmake rename to cmake/compiler/host-gcc/target.cmake diff --git a/cmake/toolchain.cmake b/cmake/generic_toolchain.cmake similarity index 72% rename from cmake/toolchain.cmake rename to cmake/generic_toolchain.cmake index d974f6290f6..e537eea619e 100644 --- a/cmake/toolchain.cmake +++ b/cmake/generic_toolchain.cmake @@ -1,13 +1,3 @@ -set(CMAKE_SYSTEM_NAME Generic) -set(CMAKE_SYSTEM_PROCESSOR ${ARCH}) - -set(BUILD_SHARED_LIBS OFF) - -set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) -set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) -set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) - if(NOT TOOLCHAIN_ROOT) if(DEFINED ENV{TOOLCHAIN_ROOT}) # Support for out-of-tree toolchain @@ -63,23 +53,9 @@ endif() # Configure the toolchain based on what SDK/toolchain is in use. if(NOT (COMPILER STREQUAL "host-gcc")) - include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}.cmake) + include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}/generic.cmake) endif() # Configure the toolchain based on what toolchain technology is used # (gcc, host-gcc etc.) -include(${ZEPHYR_BASE}/cmake/compiler/${COMPILER}.cmake OPTIONAL) - -# Uniquely identify the toolchain wrt. it's capabilities. -# -# What we are looking for, is a signature definition that is defined -# like this: - -# Toolchains with the same signature will always support the same set -# of flags. - -# It is not clear how this signature should be constructed. The -# strategy chosen is to md5sum the CC binary. - -file(MD5 ${CMAKE_C_COMPILER} CMAKE_C_COMPILER_MD5_SUM) -set(TOOLCHAIN_SIGNATURE ${CMAKE_C_COMPILER_MD5_SUM}) +include(${ZEPHYR_BASE}/cmake/compiler/${COMPILER}/generic.cmake OPTIONAL) diff --git a/cmake/target_toolchain.cmake b/cmake/target_toolchain.cmake new file mode 100644 index 00000000000..6abd4fdf668 --- /dev/null +++ b/cmake/target_toolchain.cmake @@ -0,0 +1,37 @@ +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_SYSTEM_PROCESSOR ${ARCH}) + +set(BUILD_SHARED_LIBS OFF) + +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) + +if(NOT (COMPILER STREQUAL "host-gcc")) + include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}/target.cmake) +endif() + +# The 'generic' compiler and the 'target' compiler might be different, +# so we unset the 'generic' one and thereby force the 'target' to +# re-set it. +unset(CMAKE_C_COMPILER) +unset(CMAKE_C_COMPILER CACHE) + +# Configure the toolchain based on what toolchain technology is used +# (gcc, host-gcc etc.) +include(${ZEPHYR_BASE}/cmake/compiler/${COMPILER}/target.cmake OPTIONAL) + +# Uniquely identify the toolchain wrt. it's capabilities. +# +# What we are looking for, is a signature definition that is defined +# like this: + +# Toolchains with the same signature will always support the same set +# of flags. + +# It is not clear how this signature should be constructed. The +# strategy chosen is to md5sum the CC binary. + +file(MD5 ${CMAKE_C_COMPILER} CMAKE_C_COMPILER_MD5_SUM) +set(TOOLCHAIN_SIGNATURE ${CMAKE_C_COMPILER_MD5_SUM}) diff --git a/cmake/toolchain/cross-compile.cmake b/cmake/toolchain/cross-compile/generic.cmake similarity index 100% rename from cmake/toolchain/cross-compile.cmake rename to cmake/toolchain/cross-compile/generic.cmake diff --git a/cmake/toolchain/cross-compile/target.cmake b/cmake/toolchain/cross-compile/target.cmake new file mode 100644 index 00000000000..218d8921e51 --- /dev/null +++ b/cmake/toolchain/cross-compile/target.cmake @@ -0,0 +1 @@ +# This file intentionally left blank. diff --git a/cmake/toolchain/espressif.cmake b/cmake/toolchain/espressif/generic.cmake similarity index 100% rename from cmake/toolchain/espressif.cmake rename to cmake/toolchain/espressif/generic.cmake diff --git a/cmake/toolchain/espressif/target.cmake b/cmake/toolchain/espressif/target.cmake new file mode 100644 index 00000000000..218d8921e51 --- /dev/null +++ b/cmake/toolchain/espressif/target.cmake @@ -0,0 +1 @@ +# This file intentionally left blank. diff --git a/cmake/toolchain/gnuarmemb.cmake b/cmake/toolchain/gnuarmemb/generic.cmake similarity index 100% rename from cmake/toolchain/gnuarmemb.cmake rename to cmake/toolchain/gnuarmemb/generic.cmake diff --git a/cmake/toolchain/gnuarmemb/target.cmake b/cmake/toolchain/gnuarmemb/target.cmake new file mode 100644 index 00000000000..80a6663b9c6 --- /dev/null +++ b/cmake/toolchain/gnuarmemb/target.cmake @@ -0,0 +1 @@ +# Intentionally left blank. diff --git a/cmake/toolchain/issm.cmake b/cmake/toolchain/issm/generic.cmake similarity index 100% rename from cmake/toolchain/issm.cmake rename to cmake/toolchain/issm/generic.cmake diff --git a/cmake/toolchain/issm/target.cmake b/cmake/toolchain/issm/target.cmake new file mode 100644 index 00000000000..80a6663b9c6 --- /dev/null +++ b/cmake/toolchain/issm/target.cmake @@ -0,0 +1 @@ +# Intentionally left blank. diff --git a/cmake/toolchain/llvm.cmake b/cmake/toolchain/llvm/generic.cmake similarity index 100% rename from cmake/toolchain/llvm.cmake rename to cmake/toolchain/llvm/generic.cmake diff --git a/cmake/toolchain/llvm/target.cmake b/cmake/toolchain/llvm/target.cmake new file mode 100644 index 00000000000..80a6663b9c6 --- /dev/null +++ b/cmake/toolchain/llvm/target.cmake @@ -0,0 +1 @@ +# Intentionally left blank. diff --git a/cmake/toolchain/xcc.cmake b/cmake/toolchain/xcc/generic.cmake similarity index 100% rename from cmake/toolchain/xcc.cmake rename to cmake/toolchain/xcc/generic.cmake diff --git a/cmake/toolchain/xcc/target.cmake b/cmake/toolchain/xcc/target.cmake new file mode 100644 index 00000000000..80a6663b9c6 --- /dev/null +++ b/cmake/toolchain/xcc/target.cmake @@ -0,0 +1 @@ +# Intentionally left blank. diff --git a/cmake/toolchain/xtools.cmake b/cmake/toolchain/xtools.cmake deleted file mode 100644 index 2ba10250687..00000000000 --- a/cmake/toolchain/xtools.cmake +++ /dev/null @@ -1,28 +0,0 @@ -set_ifndef(XTOOLS_TOOLCHAIN_PATH "$ENV{XTOOLS_TOOLCHAIN_PATH}") -set( XTOOLS_TOOLCHAIN_PATH ${XTOOLS_TOOLCHAIN_PATH} CACHE PATH "") -assert( XTOOLS_TOOLCHAIN_PATH "XTOOLS_TOOLCHAIN_PATH is not set") - -set(TOOLCHAIN_HOME ${XTOOLS_TOOLCHAIN_PATH}) - -set(COMPILER gcc) - -set(CROSS_COMPILE_TARGET_arm arm-zephyr-eabi) -if("${ARCH}" STREQUAL "x86") - if(CONFIG_X86_IAMCU) - set(CROSS_COMPILE_TARGET_x86 i586-zephyr-elfiamcu) - else() - set(CROSS_COMPILE_TARGET_x86 i586-zephyr-elf) - endif() -endif() -set(CROSS_COMPILE_TARGET_nios2 nios2-zephyr-elf) -set(CROSS_COMPILE_TARGET_riscv32 riscv32-zephyr-elf) -set(CROSS_COMPILE_TARGET_mips mipsel-zephyr-elf) -set(CROSS_COMPILE_TARGET_xtensa xtensa-zephyr-elf) -set(CROSS_COMPILE_TARGET_arc arc-zephyr-elf) - - -set(CROSS_COMPILE_TARGET ${CROSS_COMPILE_TARGET_${ARCH}}) -set(SYSROOT_TARGET ${CROSS_COMPILE_TARGET}) - -set(CROSS_COMPILE ${TOOLCHAIN_HOME}/${CROSS_COMPILE_TARGET}/bin/${CROSS_COMPILE_TARGET}-) -set(SYSROOT_DIR ${TOOLCHAIN_HOME}/${SYSROOT_TARGET}/${SYSROOT_TARGET}) diff --git a/cmake/toolchain/xtools/generic.cmake b/cmake/toolchain/xtools/generic.cmake new file mode 100644 index 00000000000..24373ec9681 --- /dev/null +++ b/cmake/toolchain/xtools/generic.cmake @@ -0,0 +1,23 @@ +set_ifndef(XTOOLS_TOOLCHAIN_PATH "$ENV{XTOOLS_TOOLCHAIN_PATH}") +set( XTOOLS_TOOLCHAIN_PATH ${XTOOLS_TOOLCHAIN_PATH} CACHE PATH "") +assert( XTOOLS_TOOLCHAIN_PATH "XTOOLS_TOOLCHAIN_PATH is not set") + +set(TOOLCHAIN_HOME ${XTOOLS_TOOLCHAIN_PATH}) + +set(COMPILER gcc) + +# Choose one of the toolchains in 'TOOLCHAIN_HOME' at random to use as +# a 'generic' toolchain until we know for sure which toolchain we +# should use. Note that we can't use ARCH to distingush between +# toolchains because choosing between iamcu and non-iamcu is dependent +# on Kconfig, not ARCH. +file(GLOB toolchain_paths ${TOOLCHAIN_HOME}/*) +list(GET toolchain_paths 0 some_toolchain_path) +get_filename_component(some_toolchain "${some_toolchain_path}" NAME) + +set(CROSS_COMPILE_TARGET ${some_toolchain}) + +set(SYSROOT_TARGET ${CROSS_COMPILE_TARGET}) + +set(CROSS_COMPILE ${TOOLCHAIN_HOME}/${CROSS_COMPILE_TARGET}/bin/${CROSS_COMPILE_TARGET}-) +set(SYSROOT_DIR ${TOOLCHAIN_HOME}/${SYSROOT_TARGET}/${SYSROOT_TARGET}) diff --git a/cmake/toolchain/xtools/target.cmake b/cmake/toolchain/xtools/target.cmake new file mode 100644 index 00000000000..b3f6e79b665 --- /dev/null +++ b/cmake/toolchain/xtools/target.cmake @@ -0,0 +1,19 @@ +# Determine if we have an IAMCU toolchain or not. +if(CONFIG_X86_IAMCU) + set(CROSS_COMPILE_TARGET_x86 i586-zephyr-elfiamcu) +else() + set(CROSS_COMPILE_TARGET_x86 i586-zephyr-elf) +endif() + +set(CROSS_COMPILE_TARGET_arm arm-zephyr-eabi) +set(CROSS_COMPILE_TARGET_nios2 nios2-zephyr-elf) +set(CROSS_COMPILE_TARGET_riscv32 riscv32-zephyr-elf) +set(CROSS_COMPILE_TARGET_mips mipsel-zephyr-elf) +set(CROSS_COMPILE_TARGET_xtensa xtensa-zephyr-elf) +set(CROSS_COMPILE_TARGET_arc arc-zephyr-elf) + +set(CROSS_COMPILE_TARGET ${CROSS_COMPILE_TARGET_${ARCH}}) +set(SYSROOT_TARGET ${CROSS_COMPILE_TARGET}) + +set(CROSS_COMPILE ${TOOLCHAIN_HOME}/${CROSS_COMPILE_TARGET}/bin/${CROSS_COMPILE_TARGET}-) +set(SYSROOT_DIR ${TOOLCHAIN_HOME}/${SYSROOT_TARGET}/${SYSROOT_TARGET}) diff --git a/cmake/toolchain/zephyr/generic.cmake b/cmake/toolchain/zephyr/generic.cmake new file mode 100644 index 00000000000..32134769ea5 --- /dev/null +++ b/cmake/toolchain/zephyr/generic.cmake @@ -0,0 +1,7 @@ +set(COMPILER gcc) + +set(CROSS_COMPILE_TARGET i586-${TOOLCHAIN_VENDOR}-elf) +set(SYSROOT_TARGET i586-${TOOLCHAIN_VENDOR}-elf) + +set(CROSS_COMPILE ${TOOLCHAIN_HOME}/usr/bin/${CROSS_COMPILE_TARGET}/${CROSS_COMPILE_TARGET}-) +set(SYSROOT_DIR ${ZEPHYR_SDK_INSTALL_DIR}/sysroots/${SYSROOT_TARGET}/usr) diff --git a/cmake/toolchain/zephyr.cmake b/cmake/toolchain/zephyr/target.cmake similarity index 98% rename from cmake/toolchain/zephyr.cmake rename to cmake/toolchain/zephyr/target.cmake index fb0c5e41344..52fe567d421 100644 --- a/cmake/toolchain/zephyr.cmake +++ b/cmake/toolchain/zephyr/target.cmake @@ -1,5 +1,3 @@ -set(COMPILER gcc) - # These defaults work for some targets like RISC-V set(CROSS_COMPILE_TARGET ${ARCH}-${TOOLCHAIN_VENDOR}-elf) set(SYSROOT_TARGET ${ARCH}-${TOOLCHAIN_VENDOR}-elf)