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