From 1ed6166f1f0bc9a028322ba09446d59197ce7848 Mon Sep 17 00:00:00 2001 From: Markus Blatt Date: Tue, 16 Mar 2021 19:33:43 +0100 Subject: [PATCH] Determine Python include directory to fix docker pypi builds Whenever OPM_ENABLE_PYTHON is on we need to set PYTHON_INCLUDE_DIRS as it is needed to find Python.h (shouldn't we even need to link the library?). This could (in theory) be done by using by issuing find_package(Python3 REQUIRED COMPONENTS Interpreter Development) or find_package(PythonLibs REQUIRED). Unfortunately, both do not work (for yet unknown reasons) when we build pypi packages in docker (even with setting the PYTHON_EXECUTABLE or Python3_EXECUTABLE correctly). Hence we now use python manually to determine these. --- CMakeLists.txt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 36723a6ca..e2a7bcb49 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -183,13 +183,29 @@ if (OPM_ENABLE_PYTHON) get_target_property(_lib_path Python3::Python IMPORTED_LOCATION) set(PYTHON_LIBRARY ${_lib_path}) list(APPEND opm-common_LIBRARIES ${PYTHON_LIBRARIES}) + set(PYTHON_INCLUDE_DIRS ${Python3_INCLUDE_DIRS}) else() find_package(Python3 REQUIRED COMPONENTS Interpreter) endif() # Compatibility settings for PythonInterp and PythonLibs - # used e.g. in FindCwrap + # used e.g. in FindCwrap, pybind11 set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE}) endif() + # We always need the PYTHON_INCLUDE_DIR. Unfortunately + # When we build pypi packages CMake will fail to determine + # these via the usual find_package(PythonLibs or + # find_package(Python3 REQUIRED COMPONENTS Interpreter Development) + # Hence we overwrite them here. + if(NOT PYTHON_INCLUDE_DIRS) + execute_process(COMMAND "${PYTHON_EXECUTABLE}" "-c" "from distutils import sysconfig; print(sysconfig.get_python_inc(plat_specific=True));" + RESULT_VARIABLE _PYTHON_DIR_SUCCESS + OUTPUT_VARIABLE PYTHON_INCLUDE_DIR + ERROR_VARIABLE _PYTHON_ERROR_VALUE) + if(NOT _PYTHON_DIR_SUCCESS MATCHES 0) + message(FATAL_ERROR "Could not determine Python include directory. Error: ${_PYTHON_ERROR_VALUE}.") + endif() + set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIR}) + endif() endif()