aboutsummaryrefslogtreecommitdiff
path: root/cmake/FindPythonModules.cmake
blob: bb8d4fb2093fd520e38ab53f11dd48b2997cd5f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#-------------------------------------------------------------------------------
# Copyright (c) 2019, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
#-------------------------------------------------------------------------------

#FindPythonModules
#-----------
#This module checks availability of Python modules.
#
#This module has the following parameters:
#   PYTHON_EXECUTABLE - Location of python interpreter.
#   COMPONENTS        - List of python modules to look for.
#
#This module defines the following variables:
#   PY_XXX       - Cached string variable with the location of the module.
#   PY_XXX_FOUND - Set if the module is available.
#
#   Where XXX is the upper case name of the module.
#
#Examples
#   To look for m2r and report error if not found
#       find_module(PythonModules COMPONENTS m2r)
#       if (PY_M2R_FOUND)
#           do something
#       endif()
#
#   To look for m2r and do not report error if not found
#       find_module(PythonModules OPTIONAL_COMPONENTS m2r)
#       if (PY_M2R_FOUND)
#           do something
#       endif()

if(NOT DEFINED PYTHON_EXECUTABLE)
	message(FATAL_ERROR "FindPythonModules: mandatory parameter PYTHON_EXECUTABLE is missing.")
endif()

include(Common/Utils)

foreach(_mod ${PythonModules_FIND_COMPONENTS})
	string(TOUPPER ${_mod} _mod_upper)
	string(REPLACE "-" "_" _modname "${_mod}")
	if (NOT PY_${_mod_upper})
		#Execute python and try to include the module.
		execute_process(
			COMMAND ${PYTHON_EXECUTABLE} -c "import ${_modname}; print(${_modname}.__file__);"
			RESULT_VARIABLE ${_mod}_status
			OUTPUT_VARIABLE ${_mod}_path
			ERROR_QUIET
			OUTPUT_STRIP_TRAILING_WHITESPACE)
		#If suceeded
		if(NOT ${_mod}_status)
			#Avoid trouble with directory separator on windows.
			win_fix_dir_sep(PATH ${_mod}_path)
			set("PY_${_mod_upper}" "${${_mod}_path}" CACHE STRING
				"Location of Python module ${_mod}")
		endif()
	endif()
	find_package_handle_standard_args(PY_${_mod_upper}
		FOUND_VAR PY_${_mod_upper}_FOUND
		REQUIRED_VARS PY_${_mod_upper}
		FAIL_MESSAGE "Can not find Python module ${_mod}")
endforeach()