From: Mathias Preiner Date: Tue, 6 Apr 2021 18:51:06 +0000 (-0700) Subject: cmake: Add helper to check if a given Python module is installed. (#6299) X-Git-Tag: cvc5-1.0.0~1958 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1fb3561da22565b92a5d31db6ce5d91877c897a1;p=cvc5.git cmake: Add helper to check if a given Python module is installed. (#6299) --- diff --git a/cmake/Helpers.cmake b/cmake/Helpers.cmake index 8b448d57d..9d668843e 100644 --- a/cmake/Helpers.cmake +++ b/cmake/Helpers.cmake @@ -177,3 +177,24 @@ macro(libcvc4_add_sources) set(${_append_to} ${${_append_to}} PARENT_SCOPE) endif() endmacro() + +# Check if given Python module is installed and raises a FATAL_ERROR error +# if the module cannot be found. +function(check_python_module module) + execute_process( + COMMAND + ${PYTHON_EXECUTABLE} -c "import ${module}" + RESULT_VARIABLE + RET_MODULE_TEST + ERROR_QUIET + ) + + if(RET_MODULE_TEST) + message(FATAL_ERROR + "Could not find module ${module} for Python " + "version ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}. " + "Make sure to install ${module} for this Python version " + "via \n`${PYTHON_EXECUTABLE} -m pip install ${module}'.\n" + "Note: You need to have pip installed for this Python version.") + endif() +endfunction() diff --git a/src/options/CMakeLists.txt b/src/options/CMakeLists.txt index d3106b398..574432085 100644 --- a/src/options/CMakeLists.txt +++ b/src/options/CMakeLists.txt @@ -9,22 +9,8 @@ ## directory for licensing information. ## # Check if the toml Python module is installed. -execute_process( - COMMAND - ${PYTHON_EXECUTABLE} -c "import toml" - RESULT_VARIABLE - RET_TOML - ERROR_QUIET -) -if(RET_TOML) - message(FATAL_ERROR - "Could not find toml for Python " - "version ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}. " - "Make sure to install toml for this Python version " - "via \n`${PYTHON_EXECUTABLE} -m pip install toml'.\nNote: You need to " - "have pip installed for this Python version.") -endif() +check_python_module("toml") libcvc4_add_sources( base_handlers.h diff --git a/test/python/CMakeLists.txt b/test/python/CMakeLists.txt index 675f6a7c5..bc652bd10 100644 --- a/test/python/CMakeLists.txt +++ b/test/python/CMakeLists.txt @@ -12,22 +12,7 @@ # Add Python bindings API tests # Check if the pytest Python module is installed. -execute_process( - COMMAND - ${PYTHON_EXECUTABLE} -c "import pytest" - RESULT_VARIABLE - RET_PYTEST - ERROR_QUIET -) - -if(RET_PYTEST) - message(FATAL_ERROR - "Could not find pytest for Python " - "version ${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}. " - "Make sure to install pytest for this Python version " - "via \n`${PYTHON_EXECUTABLE} -m pip install pytest'.\nNote: You need to " - "have pip installed for this Python version.") -endif() +check_python_module("pytest") macro(cvc4_add_python_api_test name filename)