cmake: Add helper to check if a given Python module is installed. (#6299)
authorMathias Preiner <mathias.preiner@gmail.com>
Tue, 6 Apr 2021 18:51:06 +0000 (11:51 -0700)
committerGitHub <noreply@github.com>
Tue, 6 Apr 2021 18:51:06 +0000 (11:51 -0700)
cmake/Helpers.cmake
src/options/CMakeLists.txt
test/python/CMakeLists.txt

index 8b448d57dab1490a1f3b069241b95dcd06d9b96d..9d668843e0a6ba2c55cef94bbde4d680a4b3664b 100644 (file)
@@ -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()
index d3106b398ace116646f93aac89fbf6233fee1f8a..5744320858ca5dfde1ec3e1a1329f961c8c05f42 100644 (file)
@@ -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
index 675f6a7c5a1e7165bc524039c181dfcbbd124e25..bc652bd10bf44f6a4ecbcb3a617d250cb9e691d5 100644 (file)
 # 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)