Skip to content

Commit

Permalink
Add custom CMake function to check for Python module
Browse files Browse the repository at this point in the history
  • Loading branch information
geographika committed Nov 25, 2018
1 parent 4bfd0c5 commit 5a8ad7f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Expand Up @@ -33,6 +33,10 @@ endif(MapServer_IS_DEV_VERSION)
MATH(EXPR MapServer_VERSION_NUM "${MapServer_VERSION_MAJOR}*10000+${MapServer_VERSION_MINOR}*100+${MapServer_VERSION_REVISION}")

SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})

# Add custom function to check Python modules are installed
include(FindPythonModule)

if (APPLE)
set(CMAKE_FIND_FRAMEWORK "LAST")
endif (APPLE)
Expand Down
24 changes: 24 additions & 0 deletions cmake/FindPythonModule.cmake
@@ -0,0 +1,24 @@
# Find if a Python module is installed
# Found at http://www.cmake.org/pipermail/cmake/2011-January/041666.html
# To use do: find_python_module(PyQt4 REQUIRED)
function(find_python_module module)
string(TOUPPER ${module} module_upper)
if(NOT PY_${module_upper})
if(ARGC GREATER 1 AND ARGV1 STREQUAL "REQUIRED")
set(${module}_FIND_REQUIRED TRUE)
endif()
# A module's location is usually a directory, but for binary modules
# it's a .so file.
execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c"
"import re, ${module}; print(re.compile('/__init__.py.*').sub('',${module}.__file__))"
RESULT_VARIABLE _${module}_status
OUTPUT_VARIABLE _${module}_location
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT _${module}_status)
set(PY_${module_upper} ${_${module}_location} CACHE STRING
"Location of Python module ${module}")
endif(NOT _${module}_status)
endif(NOT PY_${module_upper})
find_package_handle_standard_args(PY_${module} DEFAULT_MSG PY_${module_upper})
endfunction(find_python_module)
1 change: 1 addition & 0 deletions mapscript/python/CMakeLists.txt
@@ -1,6 +1,7 @@
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
find_package(PythonInterp)
find_python_module(setuptools REQUIRED)

# Python library/header finding doesn't seem to honor the python
# interpreter that was found beforehand, and defaults to the system
Expand Down

0 comments on commit 5a8ad7f

Please sign in to comment.