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(Zephyr-sdk 0.16)
52
53# gperf is an optional dependency
54find_program(GPERF gperf)
55
56# openocd is an optional dependency
57find_program(OPENOCD openocd)
58
59# bossac is an optional dependency
60find_program(BOSSAC bossac)
61
62# imgtool is an optional dependency (prefer the version that is in the mcuboot repository, if
63# present and a user has not specified a different version)
64zephyr_get(IMGTOOL SYSBUILD LOCAL)
65find_program(IMGTOOL imgtool.py HINTS ${ZEPHYR_MCUBOOT_MODULE_DIR}/scripts/ NAMES imgtool NAMES_PER_DIR)
66
67# winpty is an optional dependency
68find_program(PTY_INTERFACE winpty)
69if("${PTY_INTERFACE}" STREQUAL "PTY_INTERFACE-NOTFOUND")
70  set(PTY_INTERFACE "")
71endif()
72
73# Default to the host system's toolchain if we are targeting a host based target
74if((${BOARD_DIR} MATCHES "boards\/native") OR ("${ARCH}" STREQUAL "posix")
75   OR ("${BOARD}" STREQUAL "unit_testing"))
76  if(NOT "${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "llvm")
77    set(ZEPHYR_TOOLCHAIN_VARIANT "host")
78  endif()
79endif()
80
81# Prevent CMake from testing the toolchain
82set(CMAKE_C_COMPILER_FORCED   1)
83set(CMAKE_CXX_COMPILER_FORCED 1)
84
85if(NOT TOOLCHAIN_ROOT)
86  if(DEFINED ENV{TOOLCHAIN_ROOT})
87    # Support for out-of-tree toolchain
88    set(TOOLCHAIN_ROOT $ENV{TOOLCHAIN_ROOT})
89  else()
90    # Default toolchain cmake file
91    set(TOOLCHAIN_ROOT ${ZEPHYR_BASE})
92  endif()
93endif()
94zephyr_file(APPLICATION_ROOT TOOLCHAIN_ROOT)
95
96# Host-tools don't unconditionally set TOOLCHAIN_HOME anymore,
97# but in case Zephyr's SDK toolchain is used, set TOOLCHAIN_HOME
98if("${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "zephyr")
99  set(TOOLCHAIN_HOME ${HOST_TOOLS_HOME})
100endif()
101
102set(TOOLCHAIN_ROOT ${TOOLCHAIN_ROOT} CACHE STRING "Zephyr toolchain root" FORCE)
103assert(TOOLCHAIN_ROOT "Zephyr toolchain root path invalid: please set the TOOLCHAIN_ROOT-variable")
104
105# Set cached ZEPHYR_TOOLCHAIN_VARIANT.
106set(ZEPHYR_TOOLCHAIN_VARIANT ${ZEPHYR_TOOLCHAIN_VARIANT} CACHE STRING "Zephyr toolchain variant")
107
108# Configure the toolchain based on what SDK/toolchain is in use.
109include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}/generic.cmake)
110
111# Configure the toolchain based on what toolchain technology is used
112# (gcc, host-gcc etc.)
113include(${TOOLCHAIN_ROOT}/cmake/compiler/${COMPILER}/generic.cmake OPTIONAL)
114include(${TOOLCHAIN_ROOT}/cmake/linker/${LINKER}/generic.cmake OPTIONAL)
115include(${TOOLCHAIN_ROOT}/cmake/bintools/${BINTOOLS}/generic.cmake OPTIONAL)
116
117# Optional folder for toolchains with may provide a Kconfig file for capabilities settings.
118set_ifndef(TOOLCHAIN_KCONFIG_DIR ${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT})
119
120set(HostTools_FOUND TRUE)
121set(HOSTTOOLS_FOUND TRUE)
122build_info(toolchain name VALUE ${ZEPHYR_TOOLCHAIN_VARIANT})
123string(TOUPPER ${ZEPHYR_TOOLCHAIN_VARIANT} zephyr_toolchain_variant_upper)
124build_info(toolchain path PATH "${${zephyr_toolchain_variant_upper}_TOOLCHAIN_PATH}")
125