Enable parallel tests.
[hoomd-blue.git] / CMake / hoomd / HOOMDPythonSetup.cmake
blobbd2735f10cf2bb9a6106fbb828f729bbc694ce8a
1 ## Sets up python for hoomd
3 # macro for running python and getting output
4 macro(run_python code result)
5 execute_process(
6     COMMAND
7     ${PYTHON_EXECUTABLE} -c ${code}
8     OUTPUT_VARIABLE ${result}
9     RESULT_VARIABLE PY_ERR
10     OUTPUT_STRIP_TRAILING_WHITESPACE
11     )
13 if(PY_ERR)
14     message(STATUS "Error while querying python for information")
15 endif(PY_ERR)
16 endmacro(run_python)
18 # find the python interpreter, first
19 if (NOT PYTHON_SITEDIR)
20     find_program(PYTHON_EXECUTABLE NAMES python3 python)
21 endif()
23 find_package(PythonInterp REQUIRED)
25 # get the python installation prefix and version
26 run_python("import sys\; print('%d' % (sys.version_info[0]))" PYTHON_VERSION_MAJOR)
27 run_python("import sys\; print('%d' % (sys.version_info[1]))" PYTHON_VERSION_MINOR)
28 set(PYTHON_VERSION "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
29 string(REPLACE "." "" _python_version_no_dots ${PYTHON_VERSION})
31 # determine the include directory
32 if (PYTHON_VERSION VERSION_GREATER 3)
33     run_python("import sysconfig\; print(sysconfig.get_path('include'))" _python_include_hint)
34     run_python("import sysconfig\; print(sysconfig.get_config_var('LIBDIR'))" _python_lib_hint)
35     run_python("import sysconfig\; print(sysconfig.get_config_var('LDLIBRARY'))" _python_dynamic_lib_name)
36 else()
37     run_python("from distutils import sysconfig\; print sysconfig.get_python_inc()" _python_include_hint)
38     run_python("from distutils import sysconfig\; print sysconfig.PREFIX" _python_prefix_hint)
39     run_python("from distutils import sysconfig\; print sysconfig.get_config_var('LIBPL')" _python_static_hint)
40     run_python("from distutils import sysconfig\; print sysconfig.get_config_var('LIBRARY')" _python_static_lib_name)
41     run_python("from distutils import sysconfig\; print sysconfig.get_config_var('LDLIBRARY')" _python_dynamic_lib_name)
42 endif()
44 # always link the dynamic python library
45 get_filename_component(_python_lib_first ${_python_dynamic_lib_name} NAME)
47 find_path(PYTHON_INCLUDE_DIR Python.h
48           HINTS ${_python_include_hint}
49           NO_DEFAULT_PATH)
51 # add a blank suffix to the beginning to find the Python framework
52 set(_old_suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES})
53 set(CMAKE_FIND_LIBRARY_SUFFIXES ";${CMAKE_FIND_LIBRARY_SUFFIXES}")
54 find_library(PYTHON_LIBRARY
55              NAMES ${_python_lib_first} python${_python_version_no_dots} python${PYTHON_VERSION}
56              HINTS ${_python_prefix_hint} ${_python_static_hint} ${_python_lib_hint}
57              PATH_SUFFIXES lib64 lib libs
58              NO_DEFAULT_PATH
59              )
60 set(${CMAKE_FIND_LIBRARY_SUFFIXES} _old_suffixes)
62 MARK_AS_ADVANCED(
63   PYTHON_LIBRARY
64   PYTHON_INCLUDE_DIR
65   BOOST_PYTHON_COMPONENT
68 SET(PYTHON_INCLUDE_DIRS "${PYTHON_INCLUDE_DIR}")
69 SET(PYTHON_LIBRARIES "${PYTHON_LIBRARY}")
71 INCLUDE(FindPackageHandleStandardArgs)
72 FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonLibs DEFAULT_MSG PYTHON_LIBRARIES PYTHON_INCLUDE_DIRS)