boilerplate.cmake 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. # SPDX-License-Identifier: Apache-2.0
  2. # This file must be included into the toplevel CMakeLists.txt file of
  3. # Zephyr applications.
  4. # Zephyr CMake package automatically includes this file when CMake function
  5. # find_package() is used.
  6. #
  7. # To ensure this file is loaded in a Zephyr application it must start with
  8. # one of those lines:
  9. #
  10. # find_package(Zephyr)
  11. # find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
  12. #
  13. # The `REQUIRED HINTS $ENV{ZEPHYR_BASE}` variant is required for any application
  14. # inside the Zephyr repository.
  15. #
  16. # It exists to reduce boilerplate code that Zephyr expects to be in
  17. # application CMakeLists.txt code.
  18. # CMake version 3.13.1 is the real minimum supported version.
  19. #
  20. # Unfortunately CMake requires the toplevel CMakeLists.txt file to
  21. # define the required version, not even invoking it from an included
  22. # file, like boilerplate.cmake, is sufficient. It is however permitted
  23. # to have multiple invocations of cmake_minimum_required.
  24. #
  25. # Under these restraints we use a second 'cmake_minimum_required'
  26. # invocation in every toplevel CMakeLists.txt.
  27. cmake_minimum_required(VERSION 3.20.0)
  28. # CMP0002: "Logical target names must be globally unique"
  29. cmake_policy(SET CMP0002 NEW)
  30. # Use the old CMake behaviour until we are updating the CMake 3.20 as minimum
  31. # required. This ensure that CMake >=3.20 will be consistent with older CMakes.
  32. # CMP0116: Ninja generators transform DEPFILE s from add_custom_command().
  33. if(${CMAKE_VERSION} VERSION_GREATER_EQUAL 3.20)
  34. cmake_policy(SET CMP0116 OLD)
  35. endif()
  36. define_property(GLOBAL PROPERTY ZEPHYR_LIBS
  37. BRIEF_DOCS "Global list of all Zephyr CMake libs that should be linked in"
  38. FULL_DOCS "Global list of all Zephyr CMake libs that should be linked in.
  39. zephyr_library() appends libs to this list.")
  40. set_property(GLOBAL PROPERTY ZEPHYR_LIBS "")
  41. define_property(GLOBAL PROPERTY ZEPHYR_INTERFACE_LIBS
  42. BRIEF_DOCS "Global list of all Zephyr interface libs that should be linked in."
  43. FULL_DOCS "Global list of all Zephyr interface libs that should be linked in.
  44. zephyr_interface_library_named() appends libs to this list.")
  45. set_property(GLOBAL PROPERTY ZEPHYR_INTERFACE_LIBS "")
  46. define_property(GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES
  47. BRIEF_DOCS "Object files that are generated after Zephyr has been linked once."
  48. FULL_DOCS "\
  49. Object files that are generated after Zephyr has been linked once.\
  50. May include mmu tables, etc."
  51. )
  52. set_property(GLOBAL PROPERTY GENERATED_KERNEL_OBJECT_FILES "")
  53. define_property(GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES
  54. BRIEF_DOCS "Source files that are generated after Zephyr has been linked once."
  55. FULL_DOCS "\
  56. Source files that are generated after Zephyr has been linked once.\
  57. May include isr_tables.c etc."
  58. )
  59. set_property(GLOBAL PROPERTY GENERATED_KERNEL_SOURCE_FILES "")
  60. set(APPLICATION_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE PATH "Application Source Directory")
  61. set(APPLICATION_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} CACHE PATH "Application Binary Directory")
  62. set(__build_dir ${CMAKE_CURRENT_BINARY_DIR}/zephyr)
  63. set(PROJECT_BINARY_DIR ${__build_dir})
  64. message(STATUS "Application: ${APPLICATION_SOURCE_DIR}")
  65. add_custom_target(code_data_relocation_target)
  66. # The zephyr/runners.yaml file in the build directory is used to
  67. # configure the scripts/west_commands/runners Python package used
  68. # by 'west flash', 'west debug', etc.
  69. #
  70. # This is a helper target for setting property:value pairs related to
  71. # this file:
  72. #
  73. # Property Description
  74. # -------------- --------------------------------------------------
  75. # bin_file "zephyr.bin" file for flashing
  76. # hex_file "zephyr.hex" file for flashing
  77. # elf_file "zephyr.elf" file for flashing or debugging
  78. # yaml_contents generated contents of runners.yaml
  79. #
  80. # Note: there are quotes around "zephyr.bin" etc. because the actual
  81. # paths can be changed, e.g. to flash signed versions of these files
  82. # for consumption by bootloaders such as MCUboot.
  83. #
  84. # See cmake/flash/CMakeLists.txt for more details.
  85. add_custom_target(runners_yaml_props_target)
  86. # CMake's 'project' concept has proven to not be very useful for Zephyr
  87. # due in part to how Zephyr is organized and in part to it not fitting well
  88. # with cross compilation.
  89. # Zephyr therefore tries to rely as little as possible on project()
  90. # and its associated variables, e.g. PROJECT_SOURCE_DIR.
  91. # It is recommended to always use ZEPHYR_BASE instead of PROJECT_SOURCE_DIR
  92. # when trying to reference ENV${ZEPHYR_BASE}.
  93. set(ENV_ZEPHYR_BASE $ENV{ZEPHYR_BASE})
  94. # This add support for old style boilerplate include.
  95. if((NOT DEFINED ZEPHYR_BASE) AND (DEFINED ENV_ZEPHYR_BASE))
  96. set(ZEPHYR_BASE ${ENV_ZEPHYR_BASE} CACHE PATH "Zephyr base")
  97. endif()
  98. find_package(ZephyrBuildConfiguration
  99. QUIET NO_POLICY_SCOPE
  100. NAMES ZephyrBuild
  101. PATHS ${ZEPHYR_BASE}/../*
  102. NO_CMAKE_PATH
  103. NO_CMAKE_ENVIRONMENT_PATH
  104. NO_SYSTEM_ENVIRONMENT_PATH
  105. NO_CMAKE_PACKAGE_REGISTRY
  106. NO_CMAKE_SYSTEM_PATH
  107. NO_CMAKE_SYSTEM_PACKAGE_REGISTRY
  108. )
  109. # Note any later project() resets PROJECT_SOURCE_DIR
  110. file(TO_CMAKE_PATH "${ZEPHYR_BASE}" PROJECT_SOURCE_DIR)
  111. set(ZEPHYR_BINARY_DIR ${PROJECT_BINARY_DIR})
  112. set(AUTOCONF_H ${__build_dir}/include/generated/autoconf.h)
  113. # Re-configure (Re-execute all CMakeLists.txt code) when autoconf.h changes
  114. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${AUTOCONF_H})
  115. #
  116. # Import more CMake functions and macros
  117. #
  118. include(CheckCCompilerFlag)
  119. include(CheckCXXCompilerFlag)
  120. include(${ZEPHYR_BASE}/cmake/extensions.cmake)
  121. include(${ZEPHYR_BASE}/cmake/git.cmake)
  122. include(${ZEPHYR_BASE}/cmake/version.cmake) # depends on hex.cmake
  123. #
  124. # Find tools
  125. #
  126. include(${ZEPHYR_BASE}/cmake/python.cmake)
  127. include(${ZEPHYR_BASE}/cmake/west.cmake)
  128. include(${ZEPHYR_BASE}/cmake/ccache.cmake)
  129. if(ZEPHYR_EXTRA_MODULES)
  130. # ZEPHYR_EXTRA_MODULES has either been specified on the cmake CLI or is
  131. # already in the CMakeCache.txt. This has precedence over the environment
  132. # variable ZEPHYR_EXTRA_MODULES
  133. elseif(DEFINED ENV{ZEPHYR_EXTRA_MODULES})
  134. set(ZEPHYR_EXTRA_MODULES $ENV{ZEPHYR_EXTRA_MODULES})
  135. endif()
  136. # 'MODULE_EXT_ROOT' is a prioritized list of directories where module glue code
  137. # may be found. It always includes ${ZEPHYR_BASE} at the lowest priority.
  138. # For module roots, later entries may overrule module settings already defined
  139. # by processed module roots, hence first in list means lowest priority.
  140. zephyr_file(APPLICATION_ROOT MODULE_EXT_ROOT)
  141. list(INSERT MODULE_EXT_ROOT 0 ${ZEPHYR_BASE})
  142. #
  143. # Find Zephyr modules.
  144. # Those may contain additional DTS, BOARD, SOC, ARCH ROOTs.
  145. # Also create the Kconfig binary dir for generated Kconf files.
  146. #
  147. set(KCONFIG_BINARY_DIR ${CMAKE_BINARY_DIR}/Kconfig)
  148. file(MAKE_DIRECTORY ${KCONFIG_BINARY_DIR})
  149. include(${ZEPHYR_BASE}/cmake/zephyr_module.cmake)
  150. if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
  151. message(FATAL_ERROR "Source directory equals build directory.\
  152. In-source builds are not supported.\
  153. Please specify a build directory, e.g. cmake -Bbuild -H.")
  154. endif()
  155. add_custom_target(
  156. pristine
  157. COMMAND ${CMAKE_COMMAND} -DBINARY_DIR=${APPLICATION_BINARY_DIR}
  158. -DSOURCE_DIR=${APPLICATION_SOURCE_DIR}
  159. -P ${ZEPHYR_BASE}/cmake/pristine.cmake
  160. # Equivalent to rm -rf build/*
  161. )
  162. # Dummy add to generate files.
  163. zephyr_linker_sources(SECTIONS)
  164. # 'BOARD_ROOT' is a prioritized list of directories where boards may
  165. # be found. It always includes ${ZEPHYR_BASE} at the lowest priority.
  166. zephyr_file(APPLICATION_ROOT BOARD_ROOT)
  167. list(APPEND BOARD_ROOT ${ZEPHYR_BASE})
  168. # 'SOC_ROOT' is a prioritized list of directories where socs may be
  169. # found. It always includes ${ZEPHYR_BASE}/soc at the lowest priority.
  170. zephyr_file(APPLICATION_ROOT SOC_ROOT)
  171. list(APPEND SOC_ROOT ${ZEPHYR_BASE})
  172. # 'ARCH_ROOT' is a prioritized list of directories where archs may be
  173. # found. It always includes ${ZEPHYR_BASE} at the lowest priority.
  174. zephyr_file(APPLICATION_ROOT ARCH_ROOT)
  175. list(APPEND ARCH_ROOT ${ZEPHYR_BASE})
  176. # Check that BOARD has been provided, and that it has not changed.
  177. zephyr_check_cache(BOARD REQUIRED)
  178. string(FIND "${BOARD}" "@" REVISION_SEPARATOR_INDEX)
  179. if(NOT (REVISION_SEPARATOR_INDEX EQUAL -1))
  180. math(EXPR BOARD_REVISION_INDEX "${REVISION_SEPARATOR_INDEX} + 1")
  181. string(SUBSTRING ${BOARD} ${BOARD_REVISION_INDEX} -1 BOARD_REVISION)
  182. string(SUBSTRING ${BOARD} 0 ${REVISION_SEPARATOR_INDEX} BOARD)
  183. endif()
  184. set(BOARD_MESSAGE "Board: ${BOARD}")
  185. if(DEFINED ENV{ZEPHYR_BOARD_ALIASES})
  186. include($ENV{ZEPHYR_BOARD_ALIASES})
  187. if(${BOARD}_BOARD_ALIAS)
  188. set(BOARD_ALIAS ${BOARD} CACHE STRING "Board alias, provided by user")
  189. set(BOARD ${${BOARD}_BOARD_ALIAS})
  190. message(STATUS "Aliased BOARD=${BOARD_ALIAS} changed to ${BOARD}")
  191. endif()
  192. endif()
  193. include(${ZEPHYR_BASE}/boards/deprecated.cmake)
  194. if(${BOARD}_DEPRECATED)
  195. set(BOARD_DEPRECATED ${BOARD} CACHE STRING "Deprecated board name, provided by user")
  196. set(BOARD ${${BOARD}_DEPRECATED})
  197. message(WARNING "Deprecated BOARD=${BOARD_DEPRECATED} name specified, board automatically changed to: ${BOARD}.")
  198. endif()
  199. zephyr_boilerplate_watch(BOARD)
  200. foreach(root ${BOARD_ROOT})
  201. # Check that the board root looks reasonable.
  202. if(NOT IS_DIRECTORY "${root}/boards")
  203. message(WARNING "BOARD_ROOT element without a 'boards' subdirectory:
  204. ${root}
  205. Hints:
  206. - if your board directory is '/foo/bar/boards/<ARCH>/my_board' then add '/foo/bar' to BOARD_ROOT, not the entire board directory
  207. - if in doubt, use absolute paths")
  208. endif()
  209. # NB: find_path will return immediately if the output variable is
  210. # already set
  211. if (BOARD_ALIAS)
  212. find_path(BOARD_HIDDEN_DIR
  213. NAMES ${BOARD_ALIAS}_defconfig
  214. PATHS ${root}/boards/*/*
  215. NO_DEFAULT_PATH
  216. )
  217. if(BOARD_HIDDEN_DIR)
  218. message("Board alias ${BOARD_ALIAS} is hiding the real board of same name")
  219. endif()
  220. endif()
  221. find_path(BOARD_DIR
  222. NAMES ${BOARD}_defconfig
  223. PATHS ${root}/boards/*/*
  224. NO_DEFAULT_PATH
  225. )
  226. if(BOARD_DIR AND NOT (${root} STREQUAL ${ZEPHYR_BASE}))
  227. set(USING_OUT_OF_TREE_BOARD 1)
  228. endif()
  229. endforeach()
  230. if(EXISTS ${BOARD_DIR}/revision.cmake)
  231. # Board provides revision handling.
  232. include(${BOARD_DIR}/revision.cmake)
  233. elseif(BOARD_REVISION)
  234. message(WARNING "Board revision ${BOARD_REVISION} specified for ${BOARD}, \
  235. but board has no revision so revision will be ignored.")
  236. endif()
  237. if(DEFINED BOARD_REVISION)
  238. set(BOARD_MESSAGE "${BOARD_MESSAGE}, Revision: ${BOARD_REVISION}")
  239. if(DEFINED ACTIVE_BOARD_REVISION)
  240. set(BOARD_MESSAGE "${BOARD_MESSAGE} (Active: ${ACTIVE_BOARD_REVISION})")
  241. set(BOARD_REVISION ${ACTIVE_BOARD_REVISION})
  242. endif()
  243. string(REPLACE "." "_" BOARD_REVISION_STRING ${BOARD_REVISION})
  244. endif()
  245. # Check that SHIELD has not changed.
  246. zephyr_check_cache(SHIELD WATCH)
  247. if(SHIELD)
  248. set(BOARD_MESSAGE "${BOARD_MESSAGE}, Shield(s): ${SHIELD}")
  249. endif()
  250. message(STATUS "${BOARD_MESSAGE}")
  251. if(DEFINED SHIELD)
  252. string(REPLACE " " ";" SHIELD_AS_LIST "${SHIELD}")
  253. endif()
  254. # SHIELD-NOTFOUND is a real CMake list, from which valid shields can be popped.
  255. # After processing all shields, only invalid shields will be left in this list.
  256. set(SHIELD-NOTFOUND ${SHIELD_AS_LIST})
  257. # Use BOARD to search for a '_defconfig' file.
  258. # e.g. zephyr/boards/arm/96b_carbon_nrf51/96b_carbon_nrf51_defconfig.
  259. # When found, use that path to infer the ARCH we are building for.
  260. foreach(root ${BOARD_ROOT})
  261. set(shield_dir ${root}/boards/shields)
  262. # Match the Kconfig.shield files in the shield directories to make sure we are
  263. # finding shields, e.g. x_nucleo_iks01a1/Kconfig.shield
  264. file(GLOB_RECURSE shields_refs_list ${shield_dir}/*/Kconfig.shield)
  265. # The above gives a list like
  266. # x_nucleo_iks01a1/Kconfig.shield;x_nucleo_iks01a2/Kconfig.shield
  267. # we construct a list of shield names by extracting the folder and find
  268. # and overlay files in there. Each overlay corresponds to a shield.
  269. # We obtain the shield name by removing the overlay extension.
  270. unset(SHIELD_LIST)
  271. foreach(shields_refs ${shields_refs_list})
  272. get_filename_component(shield_path ${shields_refs} DIRECTORY)
  273. file(GLOB shield_overlays RELATIVE ${shield_path} ${shield_path}/*.overlay)
  274. foreach(overlay ${shield_overlays})
  275. get_filename_component(shield ${overlay} NAME_WE)
  276. list(APPEND SHIELD_LIST ${shield})
  277. set(SHIELD_DIR_${shield} ${shield_path})
  278. endforeach()
  279. endforeach()
  280. if(DEFINED SHIELD)
  281. foreach(s ${SHIELD_AS_LIST})
  282. if(NOT ${s} IN_LIST SHIELD_LIST)
  283. continue()
  284. endif()
  285. list(REMOVE_ITEM SHIELD-NOTFOUND ${s})
  286. # if shield config flag is on, add shield overlay to the shield overlays
  287. # list and dts_fixup file to the shield fixup file
  288. list(APPEND
  289. shield_dts_files
  290. ${SHIELD_DIR_${s}}/${s}.overlay
  291. )
  292. list(APPEND
  293. shield_dts_fixups
  294. ${SHIELD_DIR_${s}}/dts_fixup.h
  295. )
  296. list(APPEND
  297. SHIELD_DIRS
  298. ${SHIELD_DIR_${s}}
  299. )
  300. # search for shield/shield.conf file
  301. if(EXISTS ${SHIELD_DIR_${s}}/${s}.conf)
  302. # add shield.conf to the shield config list
  303. list(APPEND
  304. shield_conf_files
  305. ${SHIELD_DIR_${s}}/${s}.conf
  306. )
  307. endif()
  308. zephyr_file(CONF_FILES ${SHIELD_DIR_${s}}/boards
  309. DTS shield_dts_files
  310. KCONF shield_conf_files
  311. )
  312. zephyr_file(CONF_FILES ${SHIELD_DIR_${s}}/boards/${s}
  313. DTS shield_dts_files
  314. KCONF shield_conf_files
  315. )
  316. endforeach()
  317. endif()
  318. endforeach()
  319. if(NOT BOARD_DIR)
  320. message("No board named '${BOARD}' found.
  321. Please choose one of the following boards:
  322. ")
  323. execute_process(
  324. COMMAND
  325. ${CMAKE_COMMAND}
  326. -DZEPHYR_BASE=${ZEPHYR_BASE}
  327. -DBOARD_ROOT=${BOARD_ROOT}
  328. -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
  329. -P ${ZEPHYR_BASE}/cmake/boards.cmake
  330. )
  331. unset(CACHED_BOARD CACHE)
  332. message(FATAL_ERROR "Invalid BOARD; see above.")
  333. endif()
  334. if(DEFINED SHIELD AND NOT (SHIELD-NOTFOUND STREQUAL ""))
  335. foreach (s ${SHIELD-NOTFOUND})
  336. message("No shield named '${s}' found")
  337. endforeach()
  338. message("Please choose from among the following shields:")
  339. string(REPLACE ";" "\\;" SHIELD_LIST_ESCAPED "${SHIELD_LIST}")
  340. execute_process(
  341. COMMAND
  342. ${CMAKE_COMMAND}
  343. -DZEPHYR_BASE=${ZEPHYR_BASE}
  344. -DSHIELD_LIST=${SHIELD_LIST_ESCAPED}
  345. -P ${ZEPHYR_BASE}/cmake/shields.cmake
  346. )
  347. unset(CACHED_SHIELD CACHE)
  348. message(FATAL_ERROR "Invalid SHIELD; see above.")
  349. endif()
  350. get_filename_component(BOARD_ARCH_DIR ${BOARD_DIR} DIRECTORY)
  351. get_filename_component(ARCH ${BOARD_ARCH_DIR} NAME)
  352. foreach(root ${ARCH_ROOT})
  353. if(EXISTS ${root}/arch/${ARCH}/CMakeLists.txt)
  354. set(ARCH_DIR ${root}/arch)
  355. break()
  356. endif()
  357. endforeach()
  358. if(NOT ARCH_DIR)
  359. message(FATAL_ERROR "Could not find ARCH=${ARCH} for BOARD=${BOARD}, \
  360. please check your installation. ARCH roots searched: \n\
  361. ${ARCH_ROOT}")
  362. endif()
  363. if(DEFINED CONF_FILE)
  364. # This ensures that CACHE{CONF_FILE} will be set correctly to current scope
  365. # variable CONF_FILE. An already current scope variable will stay the same.
  366. set(CONF_FILE ${CONF_FILE})
  367. # CONF_FILE has either been specified on the cmake CLI or is already
  368. # in the CMakeCache.txt. This has precedence over the environment
  369. # variable CONF_FILE and the default prj.conf
  370. # In order to support a `prj_<name>.conf pattern for auto inclusion of board
  371. # overlays, then we must first ensure only a single conf file is provided.
  372. string(REPLACE " " ";" CONF_FILE_AS_LIST "${CONF_FILE}")
  373. list(LENGTH CONF_FILE_AS_LIST CONF_FILE_LENGTH)
  374. if(${CONF_FILE_LENGTH} EQUAL 1)
  375. # Need the file name to look for match.
  376. # Need path in order to check if it is absolute.
  377. get_filename_component(CONF_FILE_NAME ${CONF_FILE} NAME)
  378. get_filename_component(CONF_FILE_DIR ${CONF_FILE} DIRECTORY)
  379. if(${CONF_FILE_NAME} MATCHES "prj_(.*).conf")
  380. set(CONF_FILE_BUILD_TYPE ${CMAKE_MATCH_1})
  381. set(CONF_FILE_INCLUDE_FRAGMENTS true)
  382. if(NOT IS_ABSOLUTE ${CONF_FILE_DIR})
  383. set(CONF_FILE_DIR ${APPLICATION_SOURCE_DIR}/${CONF_FILE_DIR})
  384. endif()
  385. endif()
  386. endif()
  387. elseif(CACHED_CONF_FILE)
  388. # Cached conf file is present.
  389. # That value has precedence over anything else than a new
  390. # `cmake -DCONF_FILE=<file>` invocation.
  391. set(CONF_FILE ${CACHED_CONF_FILE})
  392. elseif(DEFINED ENV{CONF_FILE})
  393. set(CONF_FILE $ENV{CONF_FILE})
  394. elseif(EXISTS ${APPLICATION_SOURCE_DIR}/prj_${BOARD}.conf)
  395. set(CONF_FILE ${APPLICATION_SOURCE_DIR}/prj_${BOARD}.conf)
  396. elseif(EXISTS ${APPLICATION_SOURCE_DIR}/prj.conf)
  397. set(CONF_FILE ${APPLICATION_SOURCE_DIR}/prj.conf)
  398. set(CONF_FILE_INCLUDE_FRAGMENTS true)
  399. endif()
  400. if(CONF_FILE_INCLUDE_FRAGMENTS)
  401. if(NOT CONF_FILE_DIR)
  402. set(CONF_FILE_DIR ${APPLICATION_SOURCE_DIR})
  403. endif()
  404. zephyr_file(CONF_FILES ${CONF_FILE_DIR}/boards KCONF CONF_FILE BUILD ${CONF_FILE_BUILD_TYPE})
  405. endif()
  406. set(CACHED_CONF_FILE ${CONF_FILE} CACHE STRING "If desired, you can build the application using\
  407. the configuration settings specified in an alternate .conf file using this parameter. \
  408. These settings will override the settings in the application’s .config file or its default .conf file.\
  409. Multiple files may be listed, e.g. CONF_FILE=\"prj1.confi;prj2.conf\" \
  410. The CACHED_CONF_FILE is internal Zephyr variable used between CMake runs. \
  411. To change CONF_FILE, use the CONF_FILE variable.")
  412. unset(CONF_FILE CACHE)
  413. zephyr_file(CONF_FILES ${APPLICATION_SOURCE_DIR}/boards DTS APP_BOARD_DTS)
  414. # The CONF_FILE variable is now set to its final value.
  415. zephyr_boilerplate_watch(CONF_FILE)
  416. if(DTC_OVERLAY_FILE)
  417. # DTC_OVERLAY_FILE has either been specified on the cmake CLI or is already
  418. # in the CMakeCache.txt.
  419. elseif(APP_BOARD_DTS)
  420. set(DTC_OVERLAY_FILE ${APP_BOARD_DTS})
  421. elseif(EXISTS ${APPLICATION_SOURCE_DIR}/${BOARD}.overlay)
  422. set(DTC_OVERLAY_FILE ${APPLICATION_SOURCE_DIR}/${BOARD}.overlay)
  423. elseif(EXISTS ${APPLICATION_SOURCE_DIR}/app.overlay)
  424. set(DTC_OVERLAY_FILE ${APPLICATION_SOURCE_DIR}/app.overlay)
  425. endif()
  426. set(DTC_OVERLAY_FILE ${DTC_OVERLAY_FILE} CACHE STRING "If desired, you can \
  427. build the application using the DT configuration settings specified in an \
  428. alternate .overlay file using this parameter. These settings will override the \
  429. settings in the board's .dts file. Multiple files may be listed, e.g. \
  430. DTC_OVERLAY_FILE=\"dts1.overlay dts2.overlay\"")
  431. # Populate USER_CACHE_DIR with a directory that user applications may
  432. # write cache files to.
  433. if(NOT DEFINED USER_CACHE_DIR)
  434. find_appropriate_cache_directory(USER_CACHE_DIR)
  435. endif()
  436. message(STATUS "Cache files will be written to: ${USER_CACHE_DIR}")
  437. # Prevent CMake from testing the toolchain
  438. set(CMAKE_C_COMPILER_FORCED 1)
  439. set(CMAKE_CXX_COMPILER_FORCED 1)
  440. include(${ZEPHYR_BASE}/cmake/verify-toolchain.cmake)
  441. include(${ZEPHYR_BASE}/cmake/host-tools.cmake)
  442. # Include board specific device-tree flags before parsing.
  443. include(${BOARD_DIR}/pre_dt_board.cmake OPTIONAL)
  444. # The DTC_OVERLAY_FILE variable is now set to its final value.
  445. zephyr_boilerplate_watch(DTC_OVERLAY_FILE)
  446. # DTS should be close to kconfig because CONFIG_ variables from
  447. # kconfig and dts should be available at the same time.
  448. #
  449. # The DT system uses a C preprocessor for it's code generation needs.
  450. # This creates an awkward chicken-and-egg problem, because we don't
  451. # always know exactly which toolchain the user needs until we know
  452. # more about the target, e.g. after DT and Kconfig.
  453. #
  454. # To resolve this we find "some" C toolchain, configure it generically
  455. # with the minimal amount of configuration needed to have it
  456. # preprocess DT sources, and then, after we have finished processing
  457. # both DT and Kconfig we complete the target-specific configuration,
  458. # and possibly change the toolchain.
  459. include(${ZEPHYR_BASE}/cmake/generic_toolchain.cmake)
  460. include(${ZEPHYR_BASE}/cmake/dts.cmake)
  461. include(${ZEPHYR_BASE}/cmake/kconfig.cmake)
  462. set(SOC_NAME ${CONFIG_SOC})
  463. set(SOC_SERIES ${CONFIG_SOC_SERIES})
  464. set(SOC_TOOLCHAIN_NAME ${CONFIG_SOC_TOOLCHAIN_NAME})
  465. set(SOC_FAMILY ${CONFIG_SOC_FAMILY})
  466. if("${SOC_SERIES}" STREQUAL "")
  467. set(SOC_PATH ${SOC_NAME})
  468. else()
  469. set(SOC_PATH ${SOC_FAMILY}/${SOC_SERIES})
  470. endif()
  471. # Use SOC to search for a 'CMakeLists.txt' file.
  472. # e.g. zephyr/soc/xtense/intel_apl_adsp/CMakeLists.txt.
  473. foreach(root ${SOC_ROOT})
  474. # Check that the root looks reasonable.
  475. if(NOT IS_DIRECTORY "${root}/soc")
  476. message(WARNING "SOC_ROOT element without a 'soc' subdirectory:
  477. ${root}
  478. Hints:
  479. - if your SoC family directory is '/foo/bar/soc/<ARCH>/my_soc_family', then add '/foo/bar' to SOC_ROOT, not the entire SoC family path
  480. - if in doubt, use absolute paths")
  481. endif()
  482. if(EXISTS ${root}/soc/${ARCH}/${SOC_PATH})
  483. set(SOC_DIR ${root}/soc)
  484. break()
  485. endif()
  486. endforeach()
  487. if(NOT SOC_DIR)
  488. message(FATAL_ERROR "Could not find SOC=${SOC_NAME} for BOARD=${BOARD}, \
  489. please check your installation. SOC roots searched: \n\
  490. ${SOC_ROOT}")
  491. endif()
  492. include(${ZEPHYR_BASE}/cmake/target_toolchain.cmake)
  493. project(Zephyr-Kernel VERSION ${PROJECT_VERSION})
  494. # Add .S file extension suffix into CMAKE_ASM_SOURCE_FILE_EXTENSIONS,
  495. # because clang from OneApi can't recongnize them as asm files on
  496. # windows now.
  497. list(APPEND CMAKE_ASM_SOURCE_FILE_EXTENSIONS "S")
  498. enable_language(C CXX ASM)
  499. # The setup / configuration of the toolchain itself and the configuration of
  500. # supported compilation flags are now split, as this allows to use the toolchain
  501. # for generic purposes, for example DTS, and then test the toolchain for
  502. # supported flags at stage two.
  503. # Testing the toolchain flags requires the enable_language() to have been called in CMake.
  504. include(${ZEPHYR_BASE}/cmake/target_toolchain_flags.cmake)
  505. # 'project' sets PROJECT_BINARY_DIR to ${CMAKE_CURRENT_BINARY_DIR},
  506. # but for legacy reasons we need it to be set to
  507. # ${CMAKE_CURRENT_BINARY_DIR}/zephyr
  508. set(PROJECT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/zephyr)
  509. set(PROJECT_SOURCE_DIR ${ZEPHYR_BASE})
  510. set(KERNEL_NAME ${CONFIG_KERNEL_BIN_NAME})
  511. set(KERNEL_ELF_NAME ${KERNEL_NAME}.elf)
  512. set(KERNEL_BIN_NAME ${KERNEL_NAME}.bin)
  513. set(KERNEL_HEX_NAME ${KERNEL_NAME}.hex)
  514. set(KERNEL_UF2_NAME ${KERNEL_NAME}.uf2)
  515. set(KERNEL_MAP_NAME ${KERNEL_NAME}.map)
  516. set(KERNEL_LST_NAME ${KERNEL_NAME}.lst)
  517. set(KERNEL_S19_NAME ${KERNEL_NAME}.s19)
  518. set(KERNEL_EXE_NAME ${KERNEL_NAME}.exe)
  519. set(KERNEL_STAT_NAME ${KERNEL_NAME}.stat)
  520. set(KERNEL_STRIP_NAME ${KERNEL_NAME}.strip)
  521. include(${BOARD_DIR}/board.cmake OPTIONAL)
  522. # If we are using a suitable ethernet driver inside qemu, then these options
  523. # must be set, otherwise a zephyr instance cannot receive any network packets.
  524. # The Qemu supported ethernet driver should define CONFIG_ETH_NIC_MODEL
  525. # string that tells what nic model Qemu should use.
  526. if(CONFIG_QEMU_TARGET)
  527. if ((CONFIG_NET_QEMU_ETHERNET OR CONFIG_NET_QEMU_USER) AND NOT CONFIG_ETH_NIC_MODEL)
  528. message(FATAL_ERROR "
  529. No Qemu ethernet driver configured!
  530. Enable Qemu supported ethernet driver like e1000 at drivers/ethernet"
  531. )
  532. elseif(CONFIG_NET_QEMU_ETHERNET)
  533. if(CONFIG_ETH_QEMU_EXTRA_ARGS)
  534. set(NET_QEMU_ETH_EXTRA_ARGS ",${CONFIG_ETH_QEMU_EXTRA_ARGS}")
  535. endif()
  536. list(APPEND QEMU_FLAGS_${ARCH}
  537. -nic tap,model=${CONFIG_ETH_NIC_MODEL},script=no,downscript=no,ifname=${CONFIG_ETH_QEMU_IFACE_NAME}${NET_QEMU_ETH_EXTRA_ARGS}
  538. )
  539. elseif(CONFIG_NET_QEMU_USER)
  540. list(APPEND QEMU_FLAGS_${ARCH}
  541. -nic user,model=${CONFIG_ETH_NIC_MODEL},${CONFIG_NET_QEMU_USER_EXTRA_ARGS}
  542. )
  543. else()
  544. list(APPEND QEMU_FLAGS_${ARCH}
  545. -net none
  546. )
  547. endif()
  548. endif()
  549. # General purpose Zephyr target.
  550. # This target can be used for custom zephyr settings that needs to be used elsewhere in the build system
  551. #
  552. # Currently used properties:
  553. # - COMPILES_OPTIONS: Used by application memory partition feature
  554. add_custom_target(zephyr_property_target)
  555. # "app" is a CMake library containing all the application code and is
  556. # modified by the entry point ${APPLICATION_SOURCE_DIR}/CMakeLists.txt
  557. # that was specified when cmake was called.
  558. zephyr_library_named(app)
  559. set_property(TARGET app PROPERTY ARCHIVE_OUTPUT_DIRECTORY app)
  560. add_subdirectory(${ZEPHYR_BASE} ${__build_dir})
  561. # Link 'app' with the Zephyr interface libraries.
  562. #
  563. # NB: This must be done in boilerplate.cmake because 'app' can only be
  564. # modified in the CMakeLists.txt file that created it. And it must be
  565. # done after 'add_subdirectory(${ZEPHYR_BASE} ${__build_dir})'
  566. # because interface libraries are defined while processing that
  567. # subdirectory.
  568. get_property(ZEPHYR_INTERFACE_LIBS_PROPERTY GLOBAL PROPERTY ZEPHYR_INTERFACE_LIBS)
  569. foreach(boilerplate_lib ${ZEPHYR_INTERFACE_LIBS_PROPERTY})
  570. # Linking 'app' with 'boilerplate_lib' causes 'app' to inherit the INTERFACE
  571. # properties of 'boilerplate_lib'. The most common property is 'include
  572. # directories', but it is also possible to have defines and compiler
  573. # flags in the interface of a library.
  574. #
  575. string(TOUPPER ${boilerplate_lib} boilerplate_lib_upper_case) # Support lowercase lib names
  576. target_link_libraries_ifdef(
  577. CONFIG_APP_LINK_WITH_${boilerplate_lib_upper_case}
  578. app
  579. PUBLIC
  580. ${boilerplate_lib}
  581. )
  582. endforeach()
  583. if("${CMAKE_EXTRA_GENERATOR}" STREQUAL "Eclipse CDT4")
  584. # Call the amendment function before .project and .cproject generation
  585. # C and CXX includes, defines in .cproject without __cplusplus
  586. # with project includes and defines
  587. include(${ZEPHYR_BASE}/cmake/ide/eclipse_cdt4_generator_amendment.cmake)
  588. eclipse_cdt4_generator_amendment(1)
  589. endif()