zephyr/cmake/zephyr_module.cmake
Torsten Rasmussen ef3c5e5516 cmake: west: invoke west using same python as rest of build system
When running CMake, then Python3 will be used.
This is detected through FindPython3, with a preference for using the
python or python3 in path, if any of those matches the required Python
minimal version in Zephyr.

It is also possible for users to specify a different Python, as example
by using:
`cmake -DPYTHON_PREFER=/usr/bin/python3.x`

However, when running `west` as native command, then west will be
invoked on linux based on the python defined in:
`west` launcher, which could be: `#!/usr/bin/python3.y`

Thus there could be mismatch in Pythons used for `west` and the python
used for other scripts.

This is even worse on windows, where a user might experience:
```
>.\opt\bin\Scripts\west.exe --version
Traceback (most recent call last):
  File "C:\Python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  ...
  File "C:\Python37\lib\socket.py", line 49, in <module>
    import _socket
ImportError: Module use of python38.dll conflicts with this version of
Python.
```

when testing out a newer Python, but the python in path is still a 3.7.

By importing `west` into zephyr_module.py and by using, as example
`python -c "from west.util import west_topdir; print(topdir())"`
we ensure the same python is used in all python scripts.

Also it allows the user to control the python to use for west.

It also ensures that the west version being tested, is also the version
being used, where old code would test the version imported by python,
but using the west in path (which could be a different version)

If the west version installed in the current Python, and west invocation
is using a different Python interpreter, then an additional help text
is printed, to easier assist users with debugging.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2020-07-08 23:23:04 -04:00

55 lines
1.6 KiB
CMake

# SPDX-License-Identifier: Apache-2.0
# This cmake file provides functionality to import additional out-of-tree, OoT
# CMakeLists.txt and Kconfig files into Zephyr build system.
# It uses -DZEPHYR_MODULES=<oot-path-to-module>[;<additional-oot-module(s)>]
# given to CMake for a list of folders to search.
# It looks for: <oot-module>/zephyr/module.yml or
# <oot-module>/zephyr/CMakeLists.txt
# to load the oot-module into Zephyr build system.
# If west is available, it uses `west list` to obtain a list of projects to
# search for zephyr/module.yml
if(ZEPHYR_MODULES)
set(ZEPHYR_MODULES_ARG "--modules" ${ZEPHYR_MODULES})
endif()
if(ZEPHYR_EXTRA_MODULES)
set(ZEPHYR_EXTRA_MODULES_ARG "--extra-modules" ${ZEPHYR_EXTRA_MODULES})
endif()
set(KCONFIG_MODULES_FILE ${CMAKE_BINARY_DIR}/Kconfig.modules)
if(WEST)
set(WEST_ARG "--zephyr-base" ${ZEPHYR_BASE})
endif()
if(WEST OR ZEPHYR_MODULES)
# Zephyr module uses west, so only call it if west is installed or
# ZEPHYR_MODULES was provided as argument to CMake.
execute_process(
COMMAND
${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/zephyr_module.py
${WEST_ARG}
${ZEPHYR_MODULES_ARG}
${ZEPHYR_EXTRA_MODULES_ARG}
--kconfig-out ${KCONFIG_MODULES_FILE}
--cmake-out ${CMAKE_BINARY_DIR}/zephyr_modules.txt
ERROR_VARIABLE
zephyr_module_error_text
RESULT_VARIABLE
zephyr_module_return
)
if(${zephyr_module_return})
message(FATAL_ERROR "${zephyr_module_error_text}")
endif()
else()
file(WRITE ${KCONFIG_MODULES_FILE}
"# No west and no modules\n"
)
endif()