ZephyrConfig.cmake 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # SPDX-License-Identifier: Apache-2.0
  2. # This file provides Zephyr Config Package functionality.
  3. #
  4. # The purpose of this files is to allow users to decide if they want to:
  5. # - Use ZEPHYR_BASE environment setting for explicitly set select a zephyr installation
  6. # - Support automatic Zephyr installation lookup through the use of find_package(ZEPHYR)
  7. # First check to see if user has provided a Zephyr base manually.
  8. # Set Zephyr base to environment setting.
  9. # It will be empty if not set in environment.
  10. macro(include_boilerplate location)
  11. if(ZEPHYR_UNITTEST)
  12. set(ZephyrUnittest_FOUND True)
  13. set(BOILERPLATE_FILE ${ZEPHYR_BASE}/subsys/testsuite/unittest.cmake)
  14. else()
  15. set(Zephyr_FOUND True)
  16. set(BOILERPLATE_FILE ${ZEPHYR_BASE}/cmake/app/boilerplate.cmake)
  17. endif()
  18. if(NOT NO_BOILERPLATE)
  19. message("Including boilerplate (${location}): ${BOILERPLATE_FILE}")
  20. include(${BOILERPLATE_FILE} NO_POLICY_SCOPE)
  21. endif()
  22. endmacro()
  23. set(ENV_ZEPHYR_BASE $ENV{ZEPHYR_BASE})
  24. if((NOT DEFINED ZEPHYR_BASE) AND (DEFINED ENV_ZEPHYR_BASE))
  25. # Get rid of any double folder string before comparison, as example, user provides
  26. # ZEPHYR_BASE=//path/to//zephyr_base/
  27. # must also work.
  28. get_filename_component(ZEPHYR_BASE ${ENV_ZEPHYR_BASE} ABSOLUTE)
  29. set(ZEPHYR_BASE ${ZEPHYR_BASE} CACHE PATH "Zephyr base")
  30. include_boilerplate("Zephyr base")
  31. return()
  32. endif()
  33. if (DEFINED ZEPHYR_BASE)
  34. include_boilerplate("Zephyr base (cached)")
  35. return()
  36. endif()
  37. # If ZEPHYR_CANDIDATE is set, it means this file was include instead of called via find_package directly.
  38. if(ZEPHYR_CANDIDATE)
  39. set(IS_INCLUDED TRUE)
  40. else()
  41. include(${CMAKE_CURRENT_LIST_DIR}/zephyr_package_search.cmake)
  42. endif()
  43. # Find out the current Zephyr base.
  44. get_filename_component(CURRENT_ZEPHYR_DIR ${CMAKE_CURRENT_LIST_FILE}/${ZEPHYR_RELATIVE_DIR} ABSOLUTE)
  45. get_filename_component(CURRENT_WORKSPACE_DIR ${CMAKE_CURRENT_LIST_FILE}/${WORKSPACE_RELATIVE_DIR} ABSOLUTE)
  46. string(FIND "${CMAKE_CURRENT_SOURCE_DIR}" "${CURRENT_ZEPHYR_DIR}/" COMMON_INDEX)
  47. if (COMMON_INDEX EQUAL 0)
  48. # Project is in Zephyr repository.
  49. # We are in Zephyr repository.
  50. set(ZEPHYR_BASE ${CURRENT_ZEPHYR_DIR} CACHE PATH "Zephyr base")
  51. include_boilerplate("Zephyr repository")
  52. return()
  53. endif()
  54. if(IS_INCLUDED)
  55. # A higher level did the checking and included us and as we are not in Zephyr repository
  56. # (checked above) then we must be in Zephyr workspace.
  57. set(ZEPHYR_BASE ${CURRENT_ZEPHYR_DIR} CACHE PATH "Zephyr base")
  58. include_boilerplate("Zephyr workspace")
  59. endif()
  60. if(NOT IS_INCLUDED)
  61. string(FIND "${CMAKE_CURRENT_SOURCE_DIR}" "${CURRENT_WORKSPACE_DIR}/" COMMON_INDEX)
  62. if (COMMON_INDEX EQUAL 0)
  63. # Project is in Zephyr workspace.
  64. # This means this Zephyr is likely the correct one, but there could be an alternative installed along-side
  65. # Thus, check if there is an even better candidate.
  66. # This check works the following way.
  67. # CMake finds packages will look all packages registered in the user package registry.
  68. # As this code is processed inside registered packages, we simply test if another package has a
  69. # comon path with the current sample.
  70. # and if so, we will retrun here, and let CMake call into the other registered package for real
  71. # version checking.
  72. check_zephyr_package(CURRENT_WORKSPACE_DIR ${CURRENT_WORKSPACE_DIR})
  73. if(ZEPHYR_PREFER)
  74. check_zephyr_package(SEARCH_PARENTS CANDIDATES_PREFERENCE_LIST ${ZEPHYR_PREFER})
  75. endif()
  76. # We are the best candidate, so let's include boiler plate.
  77. set(ZEPHYR_BASE ${CURRENT_ZEPHYR_DIR} CACHE PATH "Zephyr base")
  78. include_boilerplate("Zephyr workspace")
  79. return()
  80. endif()
  81. check_zephyr_package(SEARCH_PARENTS CANDIDATES_PREFERENCE_LIST ${ZEPHYR_PREFER})
  82. # Ending here means there were no candidates in workspace of the app.
  83. # Thus, the app is built as a Zephyr Freestanding application.
  84. # CMake find_package has already done the version checking, so let's just include boiler plate.
  85. # Previous find_package would have cleared Zephyr_FOUND variable, thus set it again.
  86. set(ZEPHYR_BASE ${CURRENT_ZEPHYR_DIR} CACHE PATH "Zephyr base")
  87. include_boilerplate("Freestanding")
  88. endif()