Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame^] | 1 | # Find python module and version |
| 2 | # |
| 3 | # It sets the variables (given module called MODULE) |
| 4 | # unless MODULE is __FUTURE__. In that case |
| 5 | # it only checks if it has been found. |
| 6 | # |
| 7 | # MODULE_FOUND - has the module been found? |
| 8 | # MODULE_VERSION - module version as a string |
| 9 | # MODULE_VERSION_MAJOR - major version number |
| 10 | # MODULE_VERSION_MINOR - minor version number |
| 11 | # MODULE_VERSION_PATCH - patch version number |
| 12 | # MODULE_VERSION_DECIMAL - e.g. version 1.6.1 is 10601 |
| 13 | # |
| 14 | |
| 15 | |
| 16 | function(find_python_module module) |
| 17 | # Write module in upper and lower case |
| 18 | string(TOUPPER ${module} module_upper) |
| 19 | string(TOLOWER ${module} module_lower) |
| 20 | |
| 21 | unset(${module_upper}_VERSION) |
| 22 | # |
| 23 | # if(ARGC GREATER 1 AND ARGV1 STREQUAL "REQUIRED") |
| 24 | # set(${module_upper}_FIND_REQUIRED TRUE) |
| 25 | # endif() |
| 26 | |
| 27 | if(PYTHONINTERP_FOUND) |
| 28 | if (NOT ${module} STREQUAL "__future__") |
| 29 | execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" |
| 30 | "import ${module_lower} as n; print(n.__version__);" |
| 31 | RESULT_VARIABLE __result |
| 32 | OUTPUT_VARIABLE __output |
| 33 | ERROR_QUIET |
| 34 | OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 35 | |
| 36 | if(__result MATCHES 0) |
| 37 | string(REGEX REPLACE ";" "\\\\;" __values ${__output}) |
| 38 | string(REGEX REPLACE "\r?\n" ";" __values ${__values}) |
| 39 | list(GET __values 0 ${module_upper}_VERSION) |
| 40 | |
| 41 | string(REGEX MATCH "^([0-9])+\\.([0-9])+\\.([0-9])+" __ver_check "${${module_upper}_VERSION}") |
| 42 | |
| 43 | if(NOT "${__ver_check}" STREQUAL "") |
| 44 | set(${module_upper}_VERSION_MAJOR ${CMAKE_MATCH_1}) |
| 45 | set(${module_upper}_VERSION_MINOR ${CMAKE_MATCH_2}) |
| 46 | set(${module_upper}_VERSION_PATCH ${CMAKE_MATCH_3}) |
| 47 | math(EXPR ${module_upper}_VERSION_DECIMAL |
| 48 | "(${CMAKE_MATCH_1} * 10000) + (${CMAKE_MATCH_2} * 100) + ${CMAKE_MATCH_3}") |
| 49 | else() |
| 50 | unset(${module_upper}_VERSION) |
| 51 | message(STATUS "Requested ${module_lower} version, but got instead:\n${__output}\n") |
| 52 | endif() |
| 53 | |
| 54 | find_package_handle_standard_args(${module_upper} |
| 55 | FOUND_VAR ${module_upper}_FOUND |
| 56 | REQUIRED_VARS ${module_upper}_VERSION |
| 57 | VERSION_VAR ${module_upper}_VERSION) |
| 58 | |
| 59 | endif() |
| 60 | else() |
| 61 | execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" |
| 62 | "import ${module_lower} as n" |
| 63 | RESULT_VARIABLE __result |
| 64 | OUTPUT_VARIABLE __output |
| 65 | ERROR_QUIET |
| 66 | OUTPUT_STRIP_TRAILING_WHITESPACE) |
| 67 | if(NOT __result) |
| 68 | set(${module_upper}_FOUND ON) |
| 69 | endif() |
| 70 | message(STATUS "Found Python __FUTURE__") |
| 71 | endif() |
| 72 | |
| 73 | |
| 74 | else() |
| 75 | message(STATUS "Python interpreter not found. To find ${module} you need the Python interpreter.") |
| 76 | endif() |
| 77 | |
| 78 | |
| 79 | # Set variables in parent scope |
| 80 | if(${module_upper}_FOUND) |
| 81 | set(${module_upper}_FOUND ON PARENT_SCOPE) |
| 82 | if (NOT ${module} STREQUAL "__future__") |
| 83 | set(${module_upper}_VERSION ${${module_upper}_VERSION} PARENT_SCOPE) |
| 84 | set(${module_upper}_VERSION_MAJOR ${${module_upper}_VERSION_MAJOR} PARENT_SCOPE) |
| 85 | set(${module_upper}_VERSION_MINOR ${${module_upper}_VERSION_MINOR} PARENT_SCOPE) |
| 86 | set(${module_upper}_VERSION_PATCH ${${module_upper}_VERSION_PATCH} PARENT_SCOPE) |
| 87 | set(${module_upper}_VERSION_DECIMAL ${${module_upper}_VERSION_DECIMAL} PARENT_SCOPE) |
| 88 | endif() |
| 89 | endif() |
| 90 | |
| 91 | # Clear variables |
| 92 | osqp_clear_vars(__result __output __values __ver_check) |
| 93 | |
| 94 | endfunction(find_python_module) |