1# SPDX-License-Identifier: Apache-2.0
2
3# FindTargetTools module for locating a set of tools to use on the host but
4# targeting a remote target for Zephyr development.
5#
6# This module will lookup following target tools for Zephyr development:
7# +---------------------------------------------------------------+
8# | Tool               | Required |  Notes:                       |
9# +---------------------------------------------------------------+
10# | Target C-compiler  | Yes      |                               |
11# | Target Assembler   | Yes      |                               |
12# | Target linker      | Yes      |                               |
13# +---------------------------------------------------------------+
14#
15# The module defines the following variables:
16#
17# 'CMAKE_C_COMPILER'
18# Path to target C compiler.
19# Set to 'CMAKE_C_COMPILER-NOTFOUND' if no C compiler was found.
20#
21# 'TargetTools_FOUND', 'TARGETTOOLS_FOUND'
22# True if all required host tools were found.
23
24find_package(HostTools)
25
26if(TargetTools_FOUND)
27  return()
28endif()
29
30# Prevent CMake from testing the toolchain
31set(CMAKE_C_COMPILER_FORCED   1)
32set(CMAKE_CXX_COMPILER_FORCED 1)
33
34# https://cmake.org/cmake/help/latest/variable/CMAKE_SYSTEM_NAME.html:
35#   The name of the operating system for which CMake is to build.
36#
37# https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/CrossCompiling:
38#   CMAKE_SYSTEM_NAME : this one is mandatory, it is the name of the target
39#   system, i.e. the same as CMAKE_SYSTEM_NAME would have if CMake would run
40#   on the target system.  Typical examples are "Linux" and "Windows". This
41#   variable is used for constructing the file names of the platform files
42#   like Linux.cmake or Windows-gcc.cmake. If your target is an embedded
43#   system without OS set CMAKE_SYSTEM_NAME to "Generic".
44set(CMAKE_SYSTEM_NAME Generic)
45
46# https://cmake.org/cmake/help/latest/variable/CMAKE_SYSTEM_PROCESSOR.html:
47#   The name of the CPU CMake is building for.
48#
49# https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/CrossCompiling:
50#   CMAKE_SYSTEM_PROCESSOR : optional, processor (or hardware) of the
51#   target system. This variable is not used very much except for one
52#   purpose, it is used to load a
53#   CMAKE_SYSTEM_NAME-compiler-CMAKE_SYSTEM_PROCESSOR.cmake file,
54#   which can be used to modify settings like compiler flags etc. for
55#   the target
56set(CMAKE_SYSTEM_PROCESSOR ${ARCH})
57
58# https://cmake.org/cmake/help/latest/variable/CMAKE_SYSTEM_VERSION.html:
59#   When the CMAKE_SYSTEM_NAME variable is set explicitly to enable cross
60#   compiling then the value of CMAKE_SYSTEM_VERSION must also be set
61#   explicitly to specify the target system version.
62set(CMAKE_SYSTEM_VERSION ${PROJECT_VERSION})
63
64# https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_BYTE_ORDER.html
65#   Byte order of <LANG> compiler target architecture, if known.
66#
67# Zephyr Kconfig defines BIG_ENDIAN according to arch, SoC, Board, so propagate
68# this setting to allow users to read the standard CMake variable or use
69# 'test_big_endian()' function.
70if(CONFIG_BIG_ENDIAN)
71  set(CMAKE_C_BYTE_ORDER   BIG_ENDIAN)
72  set(CMAKE_CXX_BYTE_ORDER BIG_ENDIAN)
73else()
74  set(CMAKE_C_BYTE_ORDER   LITTLE_ENDIAN)
75  set(CMAKE_CXX_BYTE_ORDER LITTLE_ENDIAN)
76endif()
77
78# Do not build dynamically loadable libraries by default
79set(BUILD_SHARED_LIBS OFF)
80
81# Custom targets for compiler and linker flags.
82add_custom_target(asm)
83add_custom_target(compiler)
84add_custom_target(compiler-cpp)
85add_custom_target(linker)
86
87if(NOT (COMPILER STREQUAL "host-gcc"))
88  include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}/target.cmake)
89endif()
90
91# The 'generic' compiler and the 'target' compiler might be different,
92# so we unset the 'generic' one and thereby force the 'target' to
93# re-set it.
94unset(CMAKE_C_COMPILER)
95unset(CMAKE_C_COMPILER CACHE)
96
97# A toolchain consist of a compiler and a linker.
98# In Zephyr, toolchains require a port under cmake/toolchain/.
99# Each toolchain port must set COMPILER and LINKER.
100# E.g. toolchain/llvm may pick {clang, ld} or {clang, lld}.
101add_custom_target(bintools)
102
103include(${TOOLCHAIN_ROOT}/cmake/compiler/${COMPILER}/target.cmake OPTIONAL)
104include(${TOOLCHAIN_ROOT}/cmake/linker/${LINKER}/target.cmake OPTIONAL)
105include(${ZEPHYR_BASE}/cmake/bintools/bintools_template.cmake)
106include(${TOOLCHAIN_ROOT}/cmake/bintools/${BINTOOLS}/target.cmake OPTIONAL)
107
108include(${TOOLCHAIN_ROOT}/cmake/linker/target_template.cmake)
109
110set(TargetTools_FOUND TRUE)
111set(TARGETTOOLS_FOUND TRUE)
112