1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (c) 2022, Nordic Semiconductor ASA
4
5# FindHostTools module for locating a set of tools to use on the host for
6# Zephyr development.
7#
8# This module will lookup the following tools for Zephyr development:
9# +---------------------------------------------------------------+
10# | Tool               | Required |  Notes:                       |
11# +---------------------------------------------------------------+
12# | Generic C-compiler | Yes      |  Pre-processing of devicetree |
13# | Zephyr-sdk         |          |                               |
14# | gperf              |          |                               |
15# | openocd            |          |                               |
16# | bossac             |          |                               |
17# | imgtool            |          |                               |
18# +---------------------------------------------------------------+
19#
20# The module defines the following variables:
21#
22# 'CMAKE_C_COMPILER'
23# Path to C compiler.
24# Set to 'CMAKE_C_COMPILER-NOTFOUND' if no C compiler was found.
25#
26# 'GPERF'
27# Path to gperf.
28# Set to 'GPERF-NOTFOUND' if gperf was not found.
29#
30# 'OPENOCD'
31# Path to openocd.
32# Set to 'OPENOCD-NOTFOUND' if openocd was not found.
33#
34# 'BOSSAC'
35# Path to bossac.
36# Set to 'BOSSAC-NOTFOUND' if bossac was not found.
37#
38# 'IMGTOOL'
39# Path to imgtool.
40# Set to 'IMGTOOL-NOTFOUND' if imgtool was not found.
41#
42# 'HostTools_FOUND', 'HOSTTOOLS_FOUND'
43# True if all required host tools were found.
44
45include(extensions)
46
47if(HostTools_FOUND)
48  return()
49endif()
50
51find_package(Deprecated COMPONENTS CROSS_COMPILE)
52
53find_package(Zephyr-sdk 0.16)
54
55# gperf is an optional dependency
56find_program(GPERF gperf)
57
58# openocd is an optional dependency
59find_program(OPENOCD openocd)
60
61# bossac is an optional dependency
62find_program(BOSSAC bossac)
63
64# imgtool is an optional dependency (prefer the version that is in the mcuboot repository, if
65# present and a user has not specified a different version)
66zephyr_get(IMGTOOL SYSBUILD LOCAL)
67find_program(IMGTOOL imgtool.py HINTS ${ZEPHYR_MCUBOOT_MODULE_DIR}/scripts/ NAMES imgtool NAMES_PER_DIR)
68
69# winpty is an optional dependency
70find_program(PTY_INTERFACE winpty)
71if("${PTY_INTERFACE}" STREQUAL "PTY_INTERFACE-NOTFOUND")
72  set(PTY_INTERFACE "")
73endif()
74
75# Default to the host system's toolchain if we are targeting a host based target
76if((${BOARD_DIR} MATCHES "boards\/native") OR ("${ARCH}" STREQUAL "posix")
77   OR ("${BOARD}" STREQUAL "unit_testing"))
78  if(NOT "${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "llvm")
79    set(ZEPHYR_TOOLCHAIN_VARIANT "host")
80  endif()
81endif()
82
83# Prevent CMake from testing the toolchain
84set(CMAKE_C_COMPILER_FORCED   1)
85set(CMAKE_CXX_COMPILER_FORCED 1)
86
87if(NOT TOOLCHAIN_ROOT)
88  if(DEFINED ENV{TOOLCHAIN_ROOT})
89    # Support for out-of-tree toolchain
90    set(TOOLCHAIN_ROOT $ENV{TOOLCHAIN_ROOT})
91  else()
92    # Default toolchain cmake file
93    set(TOOLCHAIN_ROOT ${ZEPHYR_BASE})
94  endif()
95endif()
96zephyr_file(APPLICATION_ROOT TOOLCHAIN_ROOT)
97
98# Host-tools don't unconditionally set TOOLCHAIN_HOME anymore,
99# but in case Zephyr's SDK toolchain is used, set TOOLCHAIN_HOME
100if("${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "zephyr")
101  set(TOOLCHAIN_HOME ${HOST_TOOLS_HOME})
102endif()
103
104set(TOOLCHAIN_ROOT ${TOOLCHAIN_ROOT} CACHE STRING "Zephyr toolchain root" FORCE)
105assert(TOOLCHAIN_ROOT "Zephyr toolchain root path invalid: please set the TOOLCHAIN_ROOT-variable")
106
107# Set cached ZEPHYR_TOOLCHAIN_VARIANT.
108set(ZEPHYR_TOOLCHAIN_VARIANT ${ZEPHYR_TOOLCHAIN_VARIANT} CACHE STRING "Zephyr toolchain variant")
109
110# Configure the toolchain based on what SDK/toolchain is in use.
111include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}/generic.cmake)
112
113# Configure the toolchain based on what toolchain technology is used
114# (gcc, host-gcc etc.)
115include(${TOOLCHAIN_ROOT}/cmake/compiler/${COMPILER}/generic.cmake OPTIONAL)
116include(${TOOLCHAIN_ROOT}/cmake/linker/${LINKER}/generic.cmake OPTIONAL)
117include(${TOOLCHAIN_ROOT}/cmake/bintools/${BINTOOLS}/generic.cmake OPTIONAL)
118
119# Optional folder for toolchains with may provide a Kconfig file for capabilities settings.
120set_ifndef(TOOLCHAIN_KCONFIG_DIR ${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT})
121
122set(HostTools_FOUND TRUE)
123set(HOSTTOOLS_FOUND TRUE)
124build_info(toolchain name VALUE ${ZEPHYR_TOOLCHAIN_VARIANT})
125string(TOUPPER ${ZEPHYR_TOOLCHAIN_VARIANT} zephyr_toolchain_variant_upper)
126build_info(toolchain path PATH "${${zephyr_toolchain_variant_upper}_TOOLCHAIN_PATH}")
127