From 164f451124ec60f37e6e1eff8392151b6bf3519f Mon Sep 17 00:00:00 2001 From: Markus Blatt Date: Wed, 10 Mar 2021 15:29:18 +0100 Subject: [PATCH 1/5] Explictly search for python3 and use that instead PythonInterpret et al. Otherwise python2 might be found, but OPM needs python3. --- CMakeLists.txt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 442254056..a606b29e5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,10 +152,11 @@ endmacro (install_hook) # OpmnLibMain function. Here only the library dependency is implemented, the # bulk of the python configuration is further down in the file. if (OPM_ENABLE_PYTHON) - find_package(PythonInterp REQUIRED) + find_package(Python3 REQUIRED COMPONENTS Interpreter Development) + set(PYTHON_LIBRARIES Python3::Python) + if (OPM_ENABLE_EMBEDDED_PYTHON) - find_package(PythonLibs REQUIRED) - list(APPEND opm-common_LIBRARIES ${PYTHON_LIBRARY}) + list(APPEND opm-common_LIBRARIES ${PYTHON_LIBRARIES}) endif() endif() @@ -305,17 +306,17 @@ if (OPM_ENABLE_PYTHON) # Generate versioned setup.py configure_file (${PROJECT_SOURCE_DIR}/python/setup.py.in ${PROJECT_BINARY_DIR}/python/setup.py) file(COPY ${PROJECT_SOURCE_DIR}/python/README.md DESTINATION ${PROJECT_BINARY_DIR}/python) - execute_process(COMMAND ${PYTHON_EXECUTABLE} target_name.py + execute_process(COMMAND ${Python3_EXECUTABLE} target_name.py WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/python OUTPUT_VARIABLE python_lib_target) add_custom_target(copy_python ALL - COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/python/install.py ${PROJECT_SOURCE_DIR}/python ${PROJECT_BINARY_DIR} 0) + COMMAND ${Python3_EXECUTABLE} ${PROJECT_SOURCE_DIR}/python/install.py ${PROJECT_SOURCE_DIR}/python ${PROJECT_BINARY_DIR} 0) add_custom_command(OUTPUT python/opm/${python_lib_target} DEPENDS ${PYTHON_CXX_DEPENDS} DEPENDS copy_python - COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_BINARY_DIR}/python/setup.py + COMMAND ${Python3_EXECUTABLE} ${PROJECT_BINARY_DIR}/python/setup.py build build_ext --build-lib=${PROJECT_BINARY_DIR}/python @@ -339,7 +340,7 @@ if (OPM_ENABLE_PYTHON) # setup.py install manually - optionally with the generated script # setup-install.sh - and completely bypass cmake in the installation phase. if (OPM_INSTALL_PYTHON) - install( CODE "execute_process(COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_BINARY_DIR}/python/install.py ${PROJECT_BINARY_DIR}/python/opm ${DEST_PREFIX}${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX} 1)") + install( CODE "execute_process(COMMAND ${Python3_EXECUTABLE} ${PROJECT_BINARY_DIR}/python/install.py ${PROJECT_BINARY_DIR}/python/opm ${DEST_PREFIX}${CMAKE_INSTALL_PREFIX}/${PYTHON_INSTALL_PREFIX} 1)") endif() # Observe that if the opmcommon library has been built as a shared library the @@ -347,7 +348,7 @@ if (OPM_ENABLE_PYTHON) # testing. add_test(NAME python_tests WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/python - COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH=${PROJECT_BINARY_DIR}/lib ${PYTHON_EXECUTABLE} setup.py build_ext --dry-run --build-lib ${PROJECT_BINARY_DIR}/python test + COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH=${PROJECT_BINARY_DIR}/lib ${Python3_EXECUTABLE} setup.py build_ext --dry-run --build-lib ${PROJECT_BINARY_DIR}/python test ) set_target_properties(opmcommon PROPERTIES POSITION_INDEPENDENT_CODE ON) From 974f7da6a1ea537b07cb2ecc198a48e266ade18c Mon Sep 17 00:00:00 2001 From: Markus Blatt Date: Thu, 11 Mar 2021 11:50:27 +0100 Subject: [PATCH 2/5] Use python3 for setup scripts, too --- python/setup-build.sh.in | 2 +- python/setup-install.sh.in | 2 +- python/setup-package.sh.in | 2 +- python/setup-test.sh.in | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/python/setup-build.sh.in b/python/setup-build.sh.in index 7f214b676..afaa246f9 100644 --- a/python/setup-build.sh.in +++ b/python/setup-build.sh.in @@ -14,7 +14,7 @@ rm -f opm/libopmcommon_python* export CC=@CMAKE_CXX_COMPILER@ -@PYTHON_EXECUTABLE@ setup.py build build_ext \ +@Python3_EXECUTABLE@ setup.py build build_ext \ --library-dirs=@_setup_lib_dirs@ \ @_rpath_arg@ \ --include-dirs=@_setup_include_dirs@ diff --git a/python/setup-install.sh.in b/python/setup-install.sh.in index aff7baba5..fb6d0ae0c 100644 --- a/python/setup-install.sh.in +++ b/python/setup-install.sh.in @@ -14,4 +14,4 @@ export PYTHONPPATH=@CMAKE_INSTALL_PREFIX@/@PYTHON_PREFIX@ export LD_LIBRARY_PATH=@PROJECT_BINARY_DIR@/lib:@_setup_lib_dirs@:$LD_LIBRARY_PATH -@PYTHON_EXECUTABLE@ setup.py build_ext --dry-run install --prefix=@DEST_PREFIX@@CMAKE_INSTALL_PREFIX@ +@Python3_EXECUTABLE@ setup.py build_ext --dry-run install --prefix=@DEST_PREFIX@@CMAKE_INSTALL_PREFIX@ diff --git a/python/setup-package.sh.in b/python/setup-package.sh.in index 8469ee1ea..724b7c29d 100644 --- a/python/setup-package.sh.in +++ b/python/setup-package.sh.in @@ -14,7 +14,7 @@ rm -rf opm/libopmcommon_python* export CC=@CMAKE_CXX_COMPILER@ -@PYTHON_EXECUTABLE@ setup.py sdist bdist_wheel build_ext \ +@Python3_EXECUTABLE@ setup.py sdist bdist_wheel build_ext \ --library-dirs=@_setup_lib_dirs@ \ @_rpath_arg@ \ --include-dirs=@_setup_include_dirs@ diff --git a/python/setup-test.sh.in b/python/setup-test.sh.in index a1fd4269d..ed97c1927 100644 --- a/python/setup-test.sh.in +++ b/python/setup-test.sh.in @@ -14,4 +14,4 @@ export PYTHONPATH=@PROJECT_BINARY_DIR@/python:$PYTHONPATH export LD_LIBRARY_PATH=@PROJECT_BINARY_DIR@/lib:@_setup_lib_dirs@:$LD_LIBRARY_PATH -@PYTHON_EXECUTABLE@ setup.py build_ext --dry-run --build-lib @PROJECT_BINARY_DIR@/python test +@Python3_EXECUTABLE@ setup.py build_ext --dry-run --build-lib @PROJECT_BINARY_DIR@/python test From c98f74c2064ebfdc0ef8b4259de07414448c1e33 Mon Sep 17 00:00:00 2001 From: Markus Blatt Date: Thu, 11 Mar 2021 11:41:43 +0100 Subject: [PATCH 3/5] Make imported Python3 targets available downstream --- opm-common-prereqs.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/opm-common-prereqs.cmake b/opm-common-prereqs.cmake index 49466efde..61347578f 100644 --- a/opm-common-prereqs.cmake +++ b/opm-common-prereqs.cmake @@ -15,6 +15,7 @@ set (opm-common_DEPS list(APPEND opm-common_DEPS # various runtime library enhancements "Boost 1.44.0 COMPONENTS system unit_test_framework REQUIRED" + "Python3 COMPONENTS Interpreter Development" "OpenMP QUIET" ) From 9a25b78b98944ff0d36df5951ae418879c7a0dc7 Mon Sep 17 00:00:00 2001 From: Markus Blatt Date: Fri, 12 Mar 2021 16:47:27 +0100 Subject: [PATCH 4/5] Cater for missing FindPython3 in CMake version < 3.12.0 For these we need to use the old approach. At least we now produce an error if only python2 is found. Needed e.g. for Ubuntu LTS 18.04. --- CMakeLists.txt | 16 ++++++++++++++-- opm-common-prereqs.cmake | 8 ++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a606b29e5..26f32d4e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,8 +152,20 @@ endmacro (install_hook) # OpmnLibMain function. Here only the library dependency is implemented, the # bulk of the python configuration is further down in the file. if (OPM_ENABLE_PYTHON) - find_package(Python3 REQUIRED COMPONENTS Interpreter Development) - set(PYTHON_LIBRARIES Python3::Python) + # We need to be compatible with older CMake versions + # that do not offer FindPython3 + # e.g. Ubuntu LTS 18.04 uses cmake 3.10 + if(${CMAKE_VERSION} VERSION_LESS "3.12.0") + find_package(PythonInterp REQUIRED) + find_package(PythonLibs REQUIRED) + if(PYTHON_VERSION_MAJOR LESS 3) + message(SEND_ERROR "OPM requires version 3 of Python but only version ${PYTHON_VERSION_STRING} was found") + endif() + set(Python3_EXECUTABLE ${PYTHON_EXECUTABLE}) + set(Python3_LIBRARIES ${PYTHON_LIBRARIES}) + else() + find_package(Python3 REQUIRED COMPONENTS Interpreter Development) + endif() if (OPM_ENABLE_EMBEDDED_PYTHON) list(APPEND opm-common_LIBRARIES ${PYTHON_LIBRARIES}) diff --git a/opm-common-prereqs.cmake b/opm-common-prereqs.cmake index 61347578f..a263d099a 100644 --- a/opm-common-prereqs.cmake +++ b/opm-common-prereqs.cmake @@ -15,8 +15,12 @@ set (opm-common_DEPS list(APPEND opm-common_DEPS # various runtime library enhancements "Boost 1.44.0 COMPONENTS system unit_test_framework REQUIRED" - "Python3 COMPONENTS Interpreter Development" "OpenMP QUIET" ) - +if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12.0") + list(APPEND opm-common_DEPS + # Needed for the imported target Python3::Python + "Python3 COMPONENTS Interpreter Development" + ) +endif() find_package_deps(opm-common) From af3c825588c0f4c0ef2c9fddbada4cc40718d5fe Mon Sep 17 00:00:00 2001 From: Markus Blatt Date: Fri, 12 Mar 2021 16:48:26 +0100 Subject: [PATCH 5/5] Make sure any python search finds Python3. FindCWrap issues another find_package(PythonInterpret). By setting the PYTHON_EXECUTABLE variable we make sure it will find python3 This also fixes the CMake confusions about multiple linker paths ``` Make Warning at cmake/Modules/OpmSatellites.cmake:320 (add_executable): Cannot generate a safe linker search path for target test_ERft because files in some directories may conflict with libraries in implicit directories: link library [libpython3.7m.so] in /usr/lib/x86_64-linux-gnu may be hidden by files in: /usr/lib/python3.7/config-3.7m-x86_64-linux-gnu Some of these libraries may not be found correctly. ``` --- CMakeLists.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 26f32d4e1..7fcfaeab9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -165,6 +165,12 @@ if (OPM_ENABLE_PYTHON) set(Python3_LIBRARIES ${PYTHON_LIBRARIES}) else() find_package(Python3 REQUIRED COMPONENTS Interpreter Development) + # Compatibility settings for PythonInterp and PythonLibs + # used e.g. in FindCwrap + set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE}) + set(PYTHON_LIBRARIES Python3::Python) + get_target_property(_lib_path Python3::Python IMPORTED_LOCATION) + set(PYTHON_LIBRARY ${_lib_path}) endif() if (OPM_ENABLE_EMBEDDED_PYTHON)