Hello everyone,

I wrote a short macro for one project I work on to verify at configuration
time that a python library is available on the system on which the project
is configured. I have attached a patch to potentially integrate this new
macro to CMake. I saw other people doing similar things in other projects,
so it might be of interest to the community.
Hope this helps,

Francois
From 2c3225a2770f9628d11ab240f1b150df6ff095d9 Mon Sep 17 00:00:00 2001
From: Francois Budin <francois.bu...@gmail.com>
Date: Tue, 3 May 2016 14:27:45 -0400
Subject: [PATCH] ENH: Module to check that Python libraries are available.

This new file contains a macro that verifies if a python
library is available on the system. The macro sets two variables:
${library}_ERROR and ${library}_FOUND with ${library} being the
name of the library given as the first argument of the macro.
This allows the developer to verify that the necessary libraries
are available on the system and potentially print a message
if it is not the case.
---
 Modules/CMakeCheckPythonLibrary.cmake | 57 +++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 Modules/CMakeCheckPythonLibrary.cmake

diff --git a/Modules/CMakeCheckPythonLibrary.cmake b/Modules/CMakeCheckPythonLibrary.cmake
new file mode 100644
index 0000000..736820b
--- /dev/null
+++ b/Modules/CMakeCheckPythonLibrary.cmake
@@ -0,0 +1,57 @@
+# - Find python libraries
+# returns:
+# - ${library}_ERROR - TRUE if more than one argument is given to the macro
+# - ${library}_FOUND - TRUE if the library could be found
+#=============================================================================
+# Copyright 2001-2009 Kitware, Inc.
+# Copyright 2012 Continuum Analytics, Inc.
+#
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# * Neither the names of Kitware, Inc., the Insight Software Consortium,
+# nor the names of their contributors may be used to endorse or promote
+# products derived from this software without specific prior written
+# permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#=============================================================================
+
+macro(CMakeCheckPythonLibrary library)
+  set(${library}_ERROR TRUE)
+  set(${library}_FOUND FALSE)
+  #Sanity check: Make sure only one library name is given
+  if(${ARGC} EQUAL 1)
+    set(list_var "${ARGN}")
+    find_package(PythonInterp REQUIRED)
+
+    execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
+      "exec(\"import sys\\ntry:\\n  import numpy\\nexcept:\\n  sys.exit(0)\\nsys.exit(1)\")"
+      RESULT_VARIABLE ${library}_FOUND
+      OUTPUT_VARIABLE _PYTHON_VALUES
+      ERROR_VARIABLE _PYTHON_ERROR_VALUE
+      OUTPUT_STRIP_TRAILING_WHITESPACE)
+
+    set(${library}_ERROR FALSE)
+  endif()
+endmacro()
-- 
2.5.0

-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake-developers

Reply via email to