blob: 625bfec94a864653590753b5bbf85d9bd6f71605 [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001#-------------------------------------------------------------------------------
2# SPDX-License-Identifier: BSD-3-Clause
3# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4#-------------------------------------------------------------------------------
5
6#FindPythonModules
7#-----------
8#This module checks availability of Python modules.
9#
10#This module has the following parameters:
11# PYTHON_EXECUTABLE - Location of python interpreter.
12# COMPONENTS - List of python modules to look for.
13#
14#This module defines the following variables:
15# PY_XXX - Cached string variable with the location of the module.
16# PY_XXX_FOUND - Set if the module is available.
17#
18# Where XXX is the upper case name of the module.
19#
20#Examples
21# To look for m2r and report error if not found
22# find_module(PythonModules COMPONENTS m2r)
23# if (PY_M2R_FOUND)
24# do something
25# endif()
26#
27# To look for m2r and do not report error if not found
28# find_module(PythonModules OPTIONAL_COMPONENTS m2r)
29# if (PY_M2R_FOUND)
30# do something
31# endif()
32
33if(NOT DEFINED PYTHON_EXECUTABLE)
34 message(FATAL_ERROR "FindPythonModules: mandatory parameter PYTHON_EXECUTABLE is missing.")
35endif()
36
37foreach(_mod ${PythonModules_FIND_COMPONENTS})
38 string(TOUPPER ${_mod} _mod_upper)
39 string(REPLACE "-" "_" _modname "${_mod}")
40 if (NOT PY_${_mod_upper})
41 #Execute python and try to include the module.
42 execute_process(
43 COMMAND ${PYTHON_EXECUTABLE} -c "import ${_modname}; print(${_modname}.__file__);"
44 RESULT_VARIABLE ${_mod}_status
45 OUTPUT_VARIABLE ${_mod}_path
46 ERROR_QUIET
47 OUTPUT_STRIP_TRAILING_WHITESPACE)
48 #If suceeded
49 if(NOT ${_mod}_status)
50 #Avoid trouble with directory separator on windows.
51 set("PY_${_mod_upper}" "${${_mod}_path}" CACHE STRING
52 "Location of Python module ${_mod}")
53 endif()
54 endif()
55 #Set "standard" find module return values
56 include(FindPackageHandleStandardArgs)
57 find_package_handle_standard_args(PY_${_mod_upper}
58 REQUIRED_VARS PY_${_mod_upper}
59 NAME_MISMATCHED
60 FAIL_MESSAGE "Can not find Python module ${_mod}")
61endforeach()