From 1fb3561da22565b92a5d31db6ce5d91877c897a1 Mon Sep 17 00:00:00 2001 From: Mathias Preiner Date: Tue, 6 Apr 2021 11:51:06 -0700 Subject: [PATCH] cmake: Add helper to check if a given Python module is installed. (#6299) --- cmake/Helpers.cmake | 21 +++++++++++++++++++++ src/options/CMakeLists.txt | 16 +--------------- test/python/CMakeLists.txt | 17 +---------------- 3 files changed, 23 insertions(+), 31 deletions(-) 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) -- 2.30.2