1# SPDX-License-Identifier: Apache-2.0
2
3include_guard(GLOBAL)
4
5include(python)
6
7# west is an optional dependency. We need to run west using the same
8# Python interpreter as everything else, though, so we play some extra
9# tricks.
10
11# When west runs cmake, it sets WEST_PYTHON to its interpreter. If
12# it's defined, we assume west is installed. We do these checks here
13# instead of in the failure paths below to avoid CMake warnings about
14# WEST_PYTHON not being used.
15if(DEFINED WEST_PYTHON)
16  # Cut out any symbolic links, e.g. python3.x -> python
17  get_filename_component(west_realpath ${WEST_PYTHON} REALPATH)
18  get_filename_component(python_realpath ${PYTHON_EXECUTABLE} REALPATH)
19
20  # If realpaths differ from the variables we're using, add extra
21  # diagnostics.
22  if(NOT ("${west_realpath}" STREQUAL "${WEST_PYTHON}"))
23    set(west_realpath_msg " (real path ${west_realpath})")
24  else()
25    set(west_realpath_msg "")
26  endif()
27  if(NOT ("${python_realpath}" STREQUAL "${PYTHON_EXECUTABLE}"))
28    set(python_realpath_msg " (real path ${python_realpath})")
29  else()
30    set(python_realpath_msg "")
31  endif()
32endif()
33
34execute_process(
35  COMMAND
36  ${PYTHON_EXECUTABLE}
37  -c
38  "import west.version; print(west.version.__version__, end='')"
39  OUTPUT_VARIABLE west_version
40  ERROR_VARIABLE west_version_err
41  RESULT_VARIABLE west_version_output_result
42  )
43
44if(west_version_output_result)
45  if(DEFINED WEST_PYTHON)
46    if(NOT (${west_realpath} STREQUAL ${python_realpath}))
47      set(PYTHON_EXECUTABLE_OUT_OF_SYNC "\nOr verify these installations:\n\
48  The Python version used by west is:  ${WEST_PYTHON}${west_realpath_msg}\n\
49  The Python version used by CMake is: ${PYTHON_EXECUTABLE}${python_realpath_msg}")
50    endif()
51
52    message(FATAL_ERROR "Unable to import west.version from '${PYTHON_EXECUTABLE}':\n${west_version_err}\
53Please install with:\n\
54    ${PYTHON_EXECUTABLE} -m pip install west\
55${PYTHON_EXECUTABLE_OUT_OF_SYNC}")
56  else()
57    # WEST_PYTHON is undefined and we couldn't import west. That's
58    # fine; it's optional.
59    set(WEST WEST-NOTFOUND CACHE INTERNAL "West")
60  endif()
61else()
62  # We can import west from PYTHON_EXECUTABLE and have its version.
63
64  # Make sure its version matches the minimum required one.
65  # Keep this version identical to the one in scripts/requirements-base.txt
66  set_ifndef(MIN_WEST_VERSION 0.14.0)
67  if(${west_version} VERSION_LESS ${MIN_WEST_VERSION})
68    message(FATAL_ERROR "The detected west version, ${west_version}, is unsupported.\n\
69  The minimum supported version is ${MIN_WEST_VERSION}.\n\
70  Please upgrade with:\n\
71      ${PYTHON_EXECUTABLE} -m pip install --upgrade west\
72  ${PYTHON_EXECUTABLE_OUT_OF_SYNC}\n")
73  endif()
74
75  # Set WEST to a COMMAND prefix as if it were a find_program() result, and
76  # cache the value so the Zephyr Eclipse plugin knows how to invoke West.
77  set(WEST ${PYTHON_EXECUTABLE} -m west CACHE INTERNAL "West")
78
79  # Print information about the west module we're relying on. This
80  # will still work even after output is one line.
81  message(STATUS "Found west (found suitable version \"${west_version}\", minimum required is \"${MIN_WEST_VERSION}\")")
82
83  execute_process(
84    COMMAND ${WEST} topdir
85    OUTPUT_VARIABLE WEST_TOPDIR
86    ERROR_QUIET
87    RESULT_VARIABLE west_topdir_result
88    OUTPUT_STRIP_TRAILING_WHITESPACE
89    WORKING_DIRECTORY ${ZEPHYR_BASE}
90    )
91
92  if(west_topdir_result)
93    # west topdir is undefined.
94    # That's fine; west is optional, so could be custom Zephyr project.
95    set(WEST WEST-NOTFOUND CACHE INTERNAL "West")
96  endif()
97endif()
98