ZephyrConfigVersion.cmake 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # SPDX-License-Identifier: Apache-2.0
  2. # This file provides Zephyr Config Package version information.
  3. #
  4. # The purpose of the version file is to ensure that CMake find_package can correctly locate a
  5. # usable Zephyr installation for building of applications.
  6. # Checking for version 0.0.0 is a way to allow other Zephyr installation to determine if there is a better match.
  7. # A better match would be an installed Zephyr that has a common index with currect source dir.
  8. # Version 0.0.0 indicates that we should just return, in order to obtain our path.
  9. if(0.0.0 STREQUAL PACKAGE_FIND_VERSION)
  10. return()
  11. endif()
  12. macro(check_zephyr_version)
  13. if(PACKAGE_VERSION VERSION_LESS PACKAGE_FIND_VERSION)
  14. if(IS_INCLUDED)
  15. # We are just a candidate, meaning we have been included from other installed module.
  16. message("\n The following Zephyr repository configuration file were considered but not accepted:")
  17. message("\n ${CMAKE_CURRENT_LIST_FILE}, version: ${PACKAGE_VERSION}\n")
  18. endif()
  19. set(PACKAGE_VERSION_COMPATIBLE FALSE)
  20. else()
  21. # For now, Zephyr is capable to find the right base on all older versions as long as they define
  22. # a Zephyr config package (This code)
  23. # In future, this is the place to update in case Zephyr 3.x is not backward compatible with version 2.x
  24. set(PACKAGE_VERSION_COMPATIBLE TRUE)
  25. if(PACKAGE_FIND_VERSION STREQUAL PACKAGE_VERSION)
  26. set(PACKAGE_VERSION_EXACT TRUE)
  27. endif()
  28. endif()
  29. endmacro()
  30. # First check to see if user has provided a Zephyr base manually and it is first run (cache not set).
  31. set(ENV_ZEPHYR_BASE $ENV{ZEPHYR_BASE})
  32. if((NOT DEFINED ZEPHYR_BASE) AND (DEFINED ENV_ZEPHYR_BASE))
  33. # Get rid of any double folder string before comparison, as example, user provides
  34. # ZEPHYR_BASE=//path/to//zephyr_base/
  35. # must also work.
  36. get_filename_component(ZEPHYR_BASE $ENV{ZEPHYR_BASE} ABSOLUTE)
  37. endif()
  38. # If ZEPHYR_CANDIDATE is set, it means this file was include instead of called via find_package directly.
  39. if(ZEPHYR_CANDIDATE)
  40. set(IS_INCLUDED TRUE)
  41. else()
  42. include(${CMAKE_CURRENT_LIST_DIR}/zephyr_package_search.cmake)
  43. endif()
  44. if((DEFINED ZEPHYR_BASE) OR (DEFINED ENV_ZEPHYR_BASE))
  45. # ZEPHYR_BASE was set in cache from earlier run or in environment (first run),
  46. # meaning the package version must be ignored and the Zephyr pointed to by
  47. # ZEPHYR_BASE is to be used regardless of version.
  48. if (${ZEPHYR_BASE}/share/zephyr-package/cmake STREQUAL ${CMAKE_CURRENT_LIST_DIR})
  49. # We are the Zephyr to be used
  50. set(NO_PRINT_VERSION True)
  51. include(${ZEPHYR_BASE}/cmake/version.cmake)
  52. # Zephyr uses project version, but CMake package uses PACKAGE_VERSION
  53. set(PACKAGE_VERSION ${PROJECT_VERSION})
  54. check_zephyr_version()
  55. if(IS_INCLUDED)
  56. # We are included, so we need to ensure that the version of the top-level
  57. # package file is returned. This Zephyr version has already been printed
  58. # as part of `check_zephyr_version()`
  59. if(NOT ${PACKAGE_VERSION_COMPATIBLE}
  60. OR (Zephyr_FIND_VERSION_EXACT AND NOT PACKAGE_VERSION_EXACT)
  61. )
  62. # When Zephyr base is set and we are checked as an included file
  63. # (IS_INCLUDED=True), then we are unable to retrieve the version of the
  64. # parent Zephyr, therefore just mark it as ignored.
  65. set(PACKAGE_VERSION "ignored (ZEPHYR_BASE is set)")
  66. endif()
  67. endif()
  68. elseif ((NOT IS_INCLUDED) AND (DEFINED ZEPHYR_BASE))
  69. check_zephyr_package(ZEPHYR_BASE ${ZEPHYR_BASE} VERSION_CHECK)
  70. else()
  71. # User has pointed to a different Zephyr installation, so don't use this version
  72. set(PACKAGE_VERSION_COMPATIBLE FALSE)
  73. endif()
  74. return()
  75. endif()
  76. # Find out the current Zephyr base.
  77. get_filename_component(CURRENT_ZEPHYR_DIR ${CMAKE_CURRENT_LIST_DIR}/../../.. ABSOLUTE)
  78. get_filename_component(CURRENT_WORKSPACE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../.. ABSOLUTE)
  79. # Temporary set local Zephyr base to allow using version.cmake to find this Zephyr repository current version
  80. set(ZEPHYR_BASE ${CURRENT_ZEPHYR_DIR})
  81. # Tell version.cmake to not print as printing version for all Zephyr installations being tested
  82. # will lead to confusion on which is being used.
  83. set(NO_PRINT_VERSION True)
  84. include(${ZEPHYR_BASE}/cmake/version.cmake)
  85. # Zephyr uses project version, but CMake package uses PACKAGE_VERSION
  86. set(PACKAGE_VERSION ${PROJECT_VERSION})
  87. set(ZEPHYR_BASE)
  88. # Do we share common index, if so, this is the correct version to check.
  89. string(FIND "${CMAKE_CURRENT_SOURCE_DIR}" "${CURRENT_ZEPHYR_DIR}/" COMMON_INDEX)
  90. if (COMMON_INDEX EQUAL 0)
  91. # Project is a Zephyr repository app.
  92. check_zephyr_version()
  93. return()
  94. endif()
  95. if(NOT IS_INCLUDED)
  96. # Only do this if we are an installed CMake Config package and checking for workspace candidates.
  97. string(FIND "${CMAKE_CURRENT_SOURCE_DIR}" "${CURRENT_WORKSPACE_DIR}/" COMMON_INDEX)
  98. if (COMMON_INDEX EQUAL 0)
  99. # Project is a Zephyr workspace app.
  100. # This means this Zephyr is likely the correct one, but there could be an alternative installed along-side
  101. # Thus, check if there is an even better candidate.
  102. check_zephyr_package(CURRENT_WORKSPACE_DIR ${CURRENT_WORKSPACE_DIR} VERSION_CHECK)
  103. # We are the best candidate, so let's check our own version.
  104. check_zephyr_version()
  105. return()
  106. endif()
  107. # Checking for installed candidates which could also be an workspace candidates.
  108. # This check works the following way.
  109. # CMake finds packages will look all packages registered in the user package registry.
  110. # As this code is processed inside registered packages, we simply test if
  111. # another package has a comon path with the current sample, and if so, we
  112. # will retrun here, and let CMake call into the other registered package for
  113. # real version checking.
  114. check_zephyr_package(CHECK_ONLY VERSION_CHECK)
  115. # Check for workspace candidates.
  116. check_zephyr_package(SEARCH_PARENTS VERSION_CHECK)
  117. endif()
  118. # Ending here means there were no candidates in workspace of the app.
  119. # Thus, the app is built as a Zephyr Freestanding application.
  120. # Let's do basic CMake version checking.
  121. check_zephyr_version()