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# When targeting a host based target default to the host system's toolchain,
74# unless the user has selected to build with llvm (which is also valid for hosts builds)
75# or they are clearly trying to cross-compile a native simulator 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") AND
79     (NOT ("${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "cross-compile" AND DEFINED NATIVE_TARGET_HOST)))
80    set(ZEPHYR_TOOLCHAIN_VARIANT "host")
81  endif()
82endif()
83
84# Prevent CMake from testing the toolchain
85set(CMAKE_C_COMPILER_FORCED   1)
86set(CMAKE_CXX_COMPILER_FORCED 1)
87
88if(NOT TOOLCHAIN_ROOT)
89  if(DEFINED ENV{TOOLCHAIN_ROOT})
90    # Support for out-of-tree toolchain
91    set(TOOLCHAIN_ROOT $ENV{TOOLCHAIN_ROOT})
92  else()
93    # Default toolchain cmake file
94    set(TOOLCHAIN_ROOT ${ZEPHYR_BASE})
95  endif()
96endif()
97zephyr_file(APPLICATION_ROOT TOOLCHAIN_ROOT)
98
99# Host-tools don't unconditionally set TOOLCHAIN_HOME anymore,
100# but in case Zephyr's SDK toolchain is used, set TOOLCHAIN_HOME
101if("${ZEPHYR_TOOLCHAIN_VARIANT}" STREQUAL "zephyr")
102  set(TOOLCHAIN_HOME ${HOST_TOOLS_HOME})
103endif()
104
105set(TOOLCHAIN_ROOT ${TOOLCHAIN_ROOT} CACHE STRING "Zephyr toolchain root" FORCE)
106assert(TOOLCHAIN_ROOT "Zephyr toolchain root path invalid: please set the TOOLCHAIN_ROOT-variable")
107
108# Set cached ZEPHYR_TOOLCHAIN_VARIANT.
109set(ZEPHYR_TOOLCHAIN_VARIANT ${ZEPHYR_TOOLCHAIN_VARIANT} CACHE STRING "Zephyr toolchain variant")
110
111# Configure the toolchain based on what SDK/toolchain is in use.
112include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}/generic.cmake)
113
114# Configure the toolchain based on what toolchain technology is used
115# (gcc, host-gcc etc.)
116include(${TOOLCHAIN_ROOT}/cmake/compiler/${COMPILER}/generic.cmake OPTIONAL)
117include(${TOOLCHAIN_ROOT}/cmake/linker/${LINKER}/generic.cmake OPTIONAL)
118include(${TOOLCHAIN_ROOT}/cmake/bintools/${BINTOOLS}/generic.cmake OPTIONAL)
119
120# Optional folder for toolchains with may provide a Kconfig file for capabilities settings.
121set_ifndef(TOOLCHAIN_KCONFIG_DIR ${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT})
122
123set(HostTools_FOUND TRUE)
124set(HOSTTOOLS_FOUND TRUE)
125build_info(toolchain name VALUE ${ZEPHYR_TOOLCHAIN_VARIANT})
126string(TOUPPER ${ZEPHYR_TOOLCHAIN_VARIANT} zephyr_toolchain_variant_upper)
127build_info(toolchain path PATH "${${zephyr_toolchain_variant_upper}_TOOLCHAIN_PATH}")
128