[CMake] Patch - FindPythonLibs provides PYTHON_VERSION

David Gobbi david.gobbi at gmail.com
Wed Jul 6 13:40:39 EDT 2011


I've attached a slightly modified patch, this one uses [0-9A-Za-z\\.]+ as
the version regex instead of [0-9\\.]+ just in case the version is a
pre-release with "a", "b" etc. as part of the version string.

 - David

On Wed, Jul 6, 2011 at 11:10 AM, David Gobbi <david.gobbi at gmail.com> wrote:

> Hi All,
>
> The attached patch changes FindPythonLibs.cmake so that it returns the
> following variables:
> PYTHON_VERSION
> PYTHON_MAJOR_VERSION
> PYTHON_MINOR_VERSION
> PYTHON_MICRO_VERSION
>
> The purpose of this patch is to allow CMake scripts to perform conditional
> configuration based on what python version was found, e.g. in case different
> configuration is required for Python 3 versus Python 2.
>
>  - David
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110706/532d5970/attachment.htm>
-------------- next part --------------
diff --git a/Modules/FindPythonLibs.cmake b/Modules/FindPythonLibs.cmake
index adcec46..d520841 100644
--- a/Modules/FindPythonLibs.cmake
+++ b/Modules/FindPythonLibs.cmake
@@ -8,6 +8,12 @@
 #  PYTHON_INCLUDE_PATH        - path to where Python.h is found (deprecated)
 #  PYTHON_INCLUDE_DIRS        - path to where Python.h is found
 #  PYTHON_DEBUG_LIBRARIES     - path to the debug library
+#  PYTHON_VERSION             - python version string e.g. 2.7.1
+#  PYTHON_MAJOR_VERSION       - python major version number
+#  PYTHON_MINOR_VERSION       - python minor version number
+#  PYTHON_MICRO_VERSION       - python release version number
+#
+# This code uses the following variables:
 #  Python_ADDITIONAL_VERSIONS - list of additional Python versions to search for
 
 #=============================================================================
@@ -95,6 +101,26 @@ MARK_AS_ADVANCED(
   PYTHON_INCLUDE_DIR
 )
 
+
+# look in PYTHON_INCLUDE_DIR for patchlevel.h, which contains the
+# version number macros in all versions of python from 1.5 through
+# at least version 3.2, and set these vars:  PYTHON_VERSION,
+# PYTHON_MAJOR_VERSION, PYTHON_MINOR_VERSION, PYTHON_MICRO_VERSION.
+IF(PYTHON_INCLUDE_DIR)
+  SET(_VERSION_REGEX
+      "^#define[ \t]+PY([A-Z_]*_VERSION)[ \t]+[\"]*([0-9A-Za-z\\.]+)[\"]*[ \t]*$")
+  FILE(STRINGS "${PYTHON_INCLUDE_DIR}/patchlevel.h" _VERSION_STRINGS
+       LIMIT_COUNT 10 REGEX ${_VERSION_REGEX})
+  FOREACH(_VERSION_STRING ${_VERSION_STRINGS})
+    STRING(REGEX REPLACE ${_VERSION_REGEX} "PYTHON\\1"
+           _VERSION_VARIABLE "${_VERSION_STRING}")
+    STRING(REGEX REPLACE ${_VERSION_REGEX} "\\2"
+           _VERSION_NUMBER "${_VERSION_STRING}")
+    SET(${_VERSION_VARIABLE} ${_VERSION_NUMBER})
+  ENDFOREACH(_VERSION_STRING ${_VERSION_STRINGS})
+ENDIF(PYTHON_INCLUDE_DIR)
+
+
 # We use PYTHON_INCLUDE_DIR, PYTHON_LIBRARY and PYTHON_DEBUG_LIBRARY for the
 # cache entries because they are meant to specify the location of a single
 # library. We now set the variables listed by the documentation for this


More information about the CMake mailing list