Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 1 | #------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2019, Arm Limited. All rights reserved. |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | # |
| 6 | #------------------------------------------------------------------------------- |
| 7 | |
| 8 | #FindPythonModules |
| 9 | #----------- |
| 10 | #This module checks availability of Python modules. |
| 11 | # |
| 12 | #This module has the following parameters: |
| 13 | # PYTHON_EXECUTABLE - Location of python interpreter. |
| 14 | # COMPONENTS - List of python modules to look for. |
| 15 | # |
| 16 | #This module defines the following variables: |
| 17 | # PY_XXX - Cached string variable with the location of the module. |
| 18 | # PY_XXX_FOUND - Set if the module is available. |
| 19 | # |
| 20 | # Where XXX is the upper case name of the module. |
| 21 | # |
| 22 | #Examples |
| 23 | # To look for m2r and report error if not found |
| 24 | # find_module(PythonModules COMPONENTS m2r) |
| 25 | # if (PY_M2R_FOUND) |
| 26 | # do something |
| 27 | # endif() |
| 28 | # |
| 29 | # To look for m2r and do not report error if not found |
| 30 | # find_module(PythonModules OPTIONAL_COMPONENTS m2r) |
| 31 | # if (PY_M2R_FOUND) |
| 32 | # do something |
| 33 | # endif() |
| 34 | |
| 35 | if(NOT DEFINED PYTHON_EXECUTABLE) |
| 36 | message(FATAL_ERROR "FindPythonModules: mandatory parameter PYTHON_EXECUTABLE is missing.") |
| 37 | endif() |
| 38 | |
Anton Komlev | 6be1603 | 2021-10-13 21:51:23 +0100 | [diff] [blame] | 39 | include(Utils) |
Gyorgy Szing | 74dae3c | 2018-09-27 17:00:46 +0200 | [diff] [blame] | 40 | |
| 41 | foreach(_mod ${PythonModules_FIND_COMPONENTS}) |
| 42 | string(TOUPPER ${_mod} _mod_upper) |
| 43 | string(REPLACE "-" "_" _modname "${_mod}") |
| 44 | if (NOT PY_${_mod_upper}) |
| 45 | #Execute python and try to include the module. |
| 46 | execute_process( |
| 47 | COMMAND ${PYTHON_EXECUTABLE} -c "import ${_modname}; print(${_modname}.__file__);" |
| 48 | RESULT_VARIABLE ${_mod}_status |
| 49 | OUTPUT_VARIABLE ${_mod}_path |
| 50 | ERROR_QUIET |
| 51 | OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 52 | #If suceeded |
| 53 | if(NOT ${_mod}_status) |
| 54 | #Avoid trouble with directory separator on windows. |
| 55 | win_fix_dir_sep(PATH ${_mod}_path) |
| 56 | set("PY_${_mod_upper}" "${${_mod}_path}" CACHE STRING |
| 57 | "Location of Python module ${_mod}") |
| 58 | endif() |
| 59 | endif() |
| 60 | find_package_handle_standard_args(PY_${_mod_upper} |
| 61 | FOUND_VAR PY_${_mod_upper}_FOUND |
| 62 | REQUIRED_VARS PY_${_mod_upper} |
| 63 | FAIL_MESSAGE "Can not find Python module ${_mod}") |
| 64 | endforeach() |