1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (c) 2022, Nordic Semiconductor ASA
4
5# FindDeprecated module provides a single location for deprecated CMake build code.
6# Whenever CMake code is deprecated it should be moved to this module and
7# corresponding COMPONENTS should be created with name identifying the deprecated code.
8#
9# This makes it easier to maintain deprecated code and cleanup such code when it
10# has been deprecated for two releases.
11#
12# Example:
13# CMakeList.txt contains deprecated code, like:
14# if(DEPRECATED_VAR)
15#   deprecated()
16# endif()
17#
18# such code can easily be around for a long time, so therefore such code should
19# be moved to this module and can then be loaded as:
20# FindDeprecated.cmake
21# if(<deprecated_name> IN_LIST Deprecated_FIND_COMPONENTS)
22#   # This code has been deprecated after Zephyr x.y
23#   if(DEPRECATED_VAR)
24#     deprecated()
25#   endif()
26# endif()
27#
28# and then in the original CMakeLists.txt, this code is inserted instead:
29# find_package(Deprecated COMPONENTS <deprecated_name>)
30#
31# The module defines the following variables:
32#
33# 'Deprecated_FOUND', 'DEPRECATED_FOUND'
34# True if the Deprecated component was found and loaded.
35
36if("${Deprecated_FIND_COMPONENTS}" STREQUAL "")
37  message(WARNING "find_package(Deprecated) missing required COMPONENTS keyword")
38endif()
39
40if("CROSS_COMPILE" IN_LIST Deprecated_FIND_COMPONENTS)
41  list(REMOVE_ITEM Deprecated_FIND_COMPONENTS CROSS_COMPILE)
42  # This code was deprecated after Zephyr v3.1.0
43  if(NOT DEFINED ZEPHYR_TOOLCHAIN_VARIANT)
44    set(ZEPHYR_TOOLCHAIN_VARIANT $ENV{ZEPHYR_TOOLCHAIN_VARIANT})
45  endif()
46
47  if(NOT ZEPHYR_TOOLCHAIN_VARIANT AND
48     (CROSS_COMPILE OR (DEFINED ENV{CROSS_COMPILE})))
49      set(ZEPHYR_TOOLCHAIN_VARIANT cross-compile CACHE STRING "Zephyr toolchain variant" FORCE)
50      message(DEPRECATION  "Setting CROSS_COMPILE without setting ZEPHYR_TOOLCHAIN_VARIANT is deprecated."
51                           "Please set ZEPHYR_TOOLCHAIN_VARIANT to 'cross-compile'"
52      )
53  endif()
54endif()
55
56if("XTOOLS" IN_LIST Deprecated_FIND_COMPONENTS)
57  list(REMOVE_ITEM Deprecated_FIND_COMPONENTS XTOOLS)
58  # This code was deprecated after Zephyr v3.3.0
59  # When removing support for `xtools`, remember to also remove:
60  # cmake/toolchain/xtools (folder with files)
61  # doc/develop/toolchains/crosstool_ng.rst and update the index.rst file.
62  message(DEPRECATION "XTOOLS toolchain variant is deprecated. "
63                      "Please set ZEPHYR_TOOLCHAIN_VARIANT to 'zephyr'")
64endif()
65
66if("SPARSE" IN_LIST Deprecated_FIND_COMPONENTS)
67  list(REMOVE_ITEM Deprecated_FIND_COMPONENTS SPARSE)
68  # This code was deprecated after Zephyr v3.2.0
69  if(SPARSE)
70    message(DEPRECATION
71        "Setting SPARSE='${SPARSE}' is deprecated. "
72        "Please set ZEPHYR_SCA_VARIANT to 'sparse'"
73    )
74    if("${SPARSE}" STREQUAL "y")
75      set_ifndef(ZEPHYR_SCA_VARIANT sparse)
76    endif()
77  endif()
78endif()
79
80if("SOURCES" IN_LIST Deprecated_FIND_COMPONENTS)
81  list(REMOVE_ITEM Deprecated_FIND_COMPONENTS SOURCES)
82  if(SOURCES)
83    message(DEPRECATION
84        "Setting SOURCES prior to calling find_package() for unit tests is deprecated."
85        " To add sources after find_package() use:\n"
86        "    target_sources(testbinary PRIVATE <source-file.c>)")
87  endif()
88endif()
89
90if("PYTHON_PREFER" IN_LIST Deprecated_FIND_COMPONENTS)
91  # This code was deprecated after Zephyr v3.4.0
92  list(REMOVE_ITEM Deprecated_FIND_COMPONENTS PYTHON_PREFER)
93  if(DEFINED PYTHON_PREFER)
94    message(DEPRECATION "'PYTHON_PREFER' variable is deprecated. Please use "
95                        "Python3_EXECUTABLE instead.")
96    if(NOT DEFINED Python3_EXECUTABLE)
97      set(Python3_EXECUTABLE ${PYTHON_PREFER})
98    endif()
99  endif()
100endif()
101
102if("toolchain_ld_base" IN_LIST Deprecated_FIND_COMPONENTS)
103  # This code was deprecated after Zephyr v4.0.0
104  list(REMOVE_ITEM Deprecated_FIND_COMPONENTS toolchain_ld_base)
105
106  if(COMMAND toolchain_ld_base)
107    message(DEPRECATION
108        "The macro/function 'toolchain_ld_base' is deprecated. "
109        "Please use '${LINKER}/linker_flags.cmake' and define the appropriate "
110        "linker flags as properties instead. "
111        "See '${ZEPHYR_BASE}/cmake/linker/linker_flags_template.cmake' for "
112        "known linker properties."
113    )
114    toolchain_ld_base()
115  endif()
116endif()
117
118if("toolchain_ld_baremetal" IN_LIST Deprecated_FIND_COMPONENTS)
119  # This code was deprecated after Zephyr v4.0.0
120  list(REMOVE_ITEM Deprecated_FIND_COMPONENTS toolchain_ld_baremetal)
121
122  if(COMMAND toolchain_ld_baremetal)
123    message(DEPRECATION
124        "The macro/function 'toolchain_ld_baremetal' is deprecated. "
125        "Please use '${LINKER}/linker_flags.cmake' and define the appropriate "
126        "linker flags as properties instead. "
127        "See '${ZEPHYR_BASE}/cmake/linker/linker_flags_template.cmake' for "
128        "known linker properties."
129    )
130    toolchain_ld_baremetal()
131  endif()
132endif()
133
134if("toolchain_ld_cpp" IN_LIST Deprecated_FIND_COMPONENTS)
135  # This code was deprecated after Zephyr v4.0.0
136  list(REMOVE_ITEM Deprecated_FIND_COMPONENTS toolchain_ld_cpp)
137
138  if(COMMAND toolchain_ld_cpp)
139    message(DEPRECATION
140        "The macro/function 'toolchain_ld_cpp' is deprecated. "
141        "Please use '${LINKER}/linker_flags.cmake' and define the appropriate "
142        "linker flags as properties instead. "
143        "See '${ZEPHYR_BASE}/cmake/linker/linker_flags_template.cmake' for "
144        "known linker properties."
145    )
146    toolchain_ld_cpp()
147  endif()
148endif()
149
150if(NOT "${Deprecated_FIND_COMPONENTS}" STREQUAL "")
151  message(STATUS "The following deprecated component(s) could not be found: "
152                 "${Deprecated_FIND_COMPONENTS}")
153endif()
154
155if("SEARCHED_LINKER_SCRIPT" IN_LIST Deprecated_FIND_COMPONENTS)
156  # This code was deprecated after Zephyr v3.5.0
157  list(REMOVE_ITEM Deprecated_FIND_COMPONENTS SEARCHED_LINKER_SCRIPT)
158
159  # Try a board specific linker file
160  set(LINKER_SCRIPT ${BOARD_DIR}/linker.ld)
161  if(NOT EXISTS ${LINKER_SCRIPT})
162    # If not available, try an SoC specific linker file
163    set(LINKER_SCRIPT ${SOC_FULL_DIR}/linker.ld)
164  endif()
165  message(DEPRECATION
166      "Pre-defined `linker.ld` script is deprecated. Please set "
167      "BOARD_LINKER_SCRIPT or SOC_LINKER_SCRIPT to point to ${LINKER_SCRIPT} "
168      "or one of the Zephyr provided common linker scripts for the ${ARCH} "
169      "architecture."
170  )
171endif()
172
173set(Deprecated_FOUND True)
174set(DEPRECATED_FOUND True)
175