Skip to content

Commit

Permalink
build one python binding only
Browse files Browse the repository at this point in the history
Bug: #108
Signed-off-by: Bernd Waibel <[email protected]>
  • Loading branch information
waebbl committed Mar 5, 2021
1 parent 2307be1 commit 87ce0f2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 115 deletions.
115 changes: 53 additions & 62 deletions src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,41 @@ if(NOT TARGET Python::Interpreter AND NOT TARGET Python::Python)
return()
endif()

# we don't need cmake_dependant_option here, because the file is
# included only if -DPYTHON=ON
option(USE_PYTHON3 "Whether to use Python version 3.x" ON)

# now determine which (or both), and compile for both
find_package(Python2 COMPONENTS Interpreter Development)
find_package(Python3 COMPONENTS Interpreter Development)
if(Python2_FOUND)
message(STATUS ": Found Python ${Python2_VERSION}")
elseif(Python2::Python)
message(WARNING ": Found Python ${Python2_VERSION} development libraries, but no interpreter")
elseif(Python2::Interpreter)
message(WARNING ": Found Python ${Python2_VERSION} interpreter, but no development libraries")
else()
message(WARNING ": Unable to find Python2 interpreter or development libraries")
endif()
if(Python3_FOUND)
message(STATUS ": Found Python ${Python3_VERSION}")
elseif(Python3::Python)
message(WARNING ": Found Python ${Python3_VERSION} development libraries, but no interpreter")
elseif(Python3::Interpreter)
message(WARNING ": Found Python ${Python3_VERSION} interpreter, but no development libraries")
if(USE_PYTHON3)
find_package(Python3 COMPONENTS Interpreter Development)
if(Python3_FOUND)
message(STATUS ": Found Python ${Python3_VERSION}")
elseif(Python3::Python)
message(WARNING ": Found Python ${Python3_VERSION} development libraries, but no interpreter")
elseif(Python3::Interpreter)
message(WARNING ": Found Python ${Python3_VERSION} interpreter, but no development libraries")
else()
message(WARNING ": Unable to find Python3 interpreter or development libraries")
endif()
set(PY_MAJOR_VERSION ${Python3_VERSION_MAJOR})
set(Python_VERSION_MAJOR ${Python3_VERSION_MAJOR})
set(Python_VERSION_MINOR ${Python3_VERSION_MINOR})
else()
message(WARNING ": Unable to find Python3 interpreter or development libraries")
find_package(Python2 COMPONENTS Interpreter Development)
if(Python2_FOUND)
message(STATUS ": Found Python ${Python2_VERSION}")
elseif(Python2::Python)
message(WARNING ": Found Python ${Python2_VERSION} development libraries, but no interpreter")
elseif(Python2::Interpreter)
message(WARNING ": Found Python ${Python2_VERSION} interpreter, but no development libraries")
else()
message(WARNING ": Unable to find Python2 interpreter or development libraries")
endif()
set(PY_MAJOR_VERSION ${Python2_VERSION_MAJOR})
set(Python_VERSION_MAJOR ${Python2_VERSION_MAJOR})
set(Python_VERSION_MINOR ${Python2_VERSION_MINOR})
endif()

if (NOT Python2_FOUND AND NOT Python3_FOUND)
message(WARNING ": Disabling PyImath")
return()
Expand Down Expand Up @@ -104,15 +118,14 @@ function(PYIMATH_EXTRACT_REL_SITEARCH varname pyver pyexe pysitearch)
endfunction()

if(Python2_FOUND)
set(PYIMATH_BOOST_PY2_COMPONENT "python${Python2_VERSION_MAJOR}${Python2_VERSION_MINOR}")
set(PYIMATH_BOOST_PY_COMPONENT "python${Python2_VERSION_MAJOR}${Python2_VERSION_MINOR}")
message(STATUS "Found Python2 libraries: ${Python2_VERSION_MAJOR}${Python2_VERSION_MINOR}")
# we can't just use the Python2_SITEARCH variable as that then will
# ignore CMAKE_INSTALL_PREFIX. Could extract this to a function somewhere
# if it is generally useful
pyimath_extract_rel_sitearch(PyImath_Python2_SITEARCH_REL 2 ${Python2_EXECUTABLE} ${Python2_SITEARCH})
endif()
if(Python3_FOUND)
set(PYIMATH_BOOST_PY3_COMPONENT "python${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR}")
elseif(Python3_FOUND)
set(PYIMATH_BOOST_PY_COMPONENT "python${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR}")
message(STATUS "Found Python3 libraries: ${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR}")
# and figure out the install root here
pyimath_extract_rel_sitearch(PyImath_Python3_SITEARCH_REL 3 ${Python3_EXECUTABLE} ${Python3_SITEARCH})
Expand All @@ -136,63 +149,41 @@ endif()

find_package(Boost OPTIONAL_COMPONENTS
python
python2
${PYIMATH_BOOST_PY2_COMPONENT}
python3
${PYIMATH_BOOST_PY3_COMPONENT})
python${Python_VERSION_MAJOR}
${PYIMATH_BOOST_PY_COMPONENT})
set(_pyimath_have_perver_boost)
if(PYIMATH_BOOST_PY2_COMPONENT)
string(TOUPPER ${PYIMATH_BOOST_PY2_COMPONENT} PYIMATH_PY2_UPPER)
if(PYIMATH_BOOST_PY_COMPONENT)
string(TOUPPER ${PYIMATH_BOOST_PY_COMPONENT} PYIMATH_PY_UPPER)
else()
set(PYIMATH_BOOST_PY2_COMPONENT python2x_NOTFOUND)
set(PYIMATH_PY2_UPPER PYTHON2X_NOTFOUND)
set(PYIMATH_BOOST_PY_COMPONENT python${Python_VERSION_MAJOR}x_NOTFOUND)
set(PYIMATH_PY_UPPER PYTHON${Python_VERSION_MAJOR}X_NOTFOUND)
endif()
if(PYIMATH_BOOST_PY3_COMPONENT)
string(TOUPPER ${PYIMATH_BOOST_PY3_COMPONENT} PYIMATH_PY3_UPPER)
else()
set(PYIMATH_BOOST_PY3_COMPONENT python3x_NOTFOUND)
set(PYIMATH_PY3_UPPER PYTHON3X_NOTFOUND)
endif()
if(Boost_PYTHON2_FOUND OR Boost_${PYIMATH_PY2_UPPER}_FOUND)
if(Boost_PYTHON${Python_VERSION_MAJOR}_FOUND OR Boost_${PYIMATH_PY_UPPER}_FOUND)
set(_pyimath_have_perver_boost TRUE)
if(NOT Boost_${PYIMATH_PY2_UPPER}_FOUND)
message(WARNING "Legacy Boost python2 found, but does not include minor version, this is an old configuration and may not be portable")
set(PYIMATH_BOOST_PY2_COMPONENT python2)
endif()
endif()
if(Boost_PYTHON3_FOUND OR Boost_${PYIMATH_PY3_UPPER}_FOUND)
set(_pyimath_have_perver_boost TRUE)
if(NOT Boost_${PYIMATH_PY3_UPPER}_FOUND)
message(WARNING "Legacy Boost python3 found, but does not include minor version, this is an old configuration and may not be portable")
set(PYIMATH_BOOST_PY3_COMPONENT python3)
if(NOT Boost_${PYIMATH_PY_UPPER}_FOUND)
message(WARNING "Legacy Boost python${Python_VERSION_MAJOR} found, but does not include minor version, this is an old configuration and may not be portable")
set(PYIMATH_BOOST_PY_COMPONENT python${Python_VERSION_MAJOR})
endif()
endif()

if(Boost_PYTHON_FOUND AND NOT _pyimath_have_perver_boost)
# old boost case, I guess we just warn and assume it is python2 (likely)
message(WARNING "Ambiguous boost python module found, assuming python 2. If you have a new boost library, try cleaning the cmake cache and reconfigure with -DBoost_NO_BOOST_CMAKE=ON")
set(PYIMATH_BOOST_PY2_COMPONENT python)
message(WARNING "Ambiguous boost python module found, assuming python ${Python_VERSION_MAJOR}. If you have a new boost library, try cleaning the cmake cache and reconfigure with -DBoost_NO_BOOST_CMAKE=ON")
set(PYIMATH_BOOST_PY_COMPONENT python)
# set it to a bogus string but not empty so later we don't test against a namespace only target
set(PYIMATH_BOOST_PY3_COMPONENT pythonIgnore)
# set(PYIMATH_BOOST_PY3_COMPONENT pythonIgnore)
elseif(NOT _pyimath_have_perver_boost)
message(WARNING "Unable to find boost::python library, disabling PyImath. If you believe this is wrong, check the cmake documentation and see if you need to set Boost_ROOT or Boost_NO_BOOST_CMAKE")
return()
else()
if(TARGET Boost::${PYIMATH_BOOST_PY2_COMPONENT})
message(STATUS " -> Found Python 2 boost: Boost::${PYIMATH_BOOST_PY2_COMPONENT}")
elseif(Boost_PYTHON2_FOUND OR Boost_${PYIMATH_PY2_UPPER}_FOUND)
message(WARNING "Found boost for python 2, but FindBoost did not create an import library. If you believe this is wrong, check the cmake documentation and see if you need to set Boost_ROOT or Boost_NO_BOOST_CMAKE")
return()
endif()
if(TARGET Boost::${PYIMATH_BOOST_PY3_COMPONENT})
message(STATUS " -> Found Python 3 boost: Boost::${PYIMATH_BOOST_PY3_COMPONENT}")
elseif(Boost_PYTHON3_FOUND OR Boost_${PYIMATH_PY3_UPPER}_FOUND)
message(WARNING "Found boost for python 3, but FindBoost did not create an import library. If you believe this is wrong, check the cmake documentation and see if you need to set Boost_ROOT or Boost_NO_BOOST_CMAKE")
if(TARGET Boost::${PYIMATH_BOOST_PY_COMPONENT})
message(STATUS " -> Found Python ${Python_VERSION_MAJOR} boost: Boost::${PYIMATH_BOOST_PY_COMPONENT}")
elseif(Boost_PYTHON_FOUND OR Boost_${PYIMATH_PY_UPPER}_FOUND)
message(WARNING "Found boost for python ${Python_VERSION_MAJOR}, but FindBoost did not create an import library. If you believe this is wrong, check the cmake documentation and see if you need to set Boost_ROOT or Boost_NO_BOOST_CMAKE")
return()
endif()
endif()
unset(PYIMATH_PY2_UPPER)
unset(PYIMATH_PY3_UPPER)
unset(PYIMATH_PY_UPPER)
unset(_pyimath_have_perver_boost)

# unfortunately, we can't use the boost numpy stuff, as that requires a
Expand Down
2 changes: 1 addition & 1 deletion src/python/PyImath.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ libsuffix=@LIB_SUFFIX_DASH@
Name: PyImath
Description: Python bindings for the Imath libraries
Version: @IMATH_VERSION@
Libs: -L${libdir} -lImath${libsuffix} -lPyImath${libsuffix}
Libs: -L${libdir} -lImath${libsuffix} -lPyImath@PYIMATH_LIB_PYTHONVER_ROOT@@Python_VERSION_MAJOR@_@Python_VERSION_MINOR@${libsuffix}
Cflags: -I${includedir} -I${includedir}/Imath
8 changes: 4 additions & 4 deletions src/python/PyImathNumpy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Copyright Contributors to the OpenEXR Project.

if(TARGET Python2::Python AND
TARGET Boost::${PYIMATH_BOOST_PY2_COMPONENT} AND
TARGET Boost::${PYIMATH_BOOST_PY_COMPONENT} AND
TARGET Python2::ImathNumPy)
set(moddeps_p2 PyImath)
list(TRANSFORM moddeps_p2 APPEND ${PYIMATH_LIB_PYTHONVER_ROOT}${Python2_VERSION_MAJOR}_${Python2_VERSION_MINOR})
Expand All @@ -14,7 +14,7 @@ if(TARGET Python2::Python AND
Imath
${moddeps_p2}
Python2::Python
Boost::${PYIMATH_BOOST_PY2_COMPONENT}
Boost::${PYIMATH_BOOST_PY_COMPONENT}
Python2::ImathNumPy
)
set_target_properties(imathnumpy_python2 PROPERTIES
Expand All @@ -26,7 +26,7 @@ if(TARGET Python2::Python AND
endif()

if(TARGET Python3::Python AND
TARGET Boost::${PYIMATH_BOOST_PY3_COMPONENT} AND
TARGET Boost::${PYIMATH_BOOST_PY_COMPONENT} AND
TARGET Python3::ImathNumPy)

set(moddeps_p3 PyImath)
Expand All @@ -40,7 +40,7 @@ if(TARGET Python3::Python AND
Imath
${moddeps_p3}
Python3::Python
Boost::${PYIMATH_BOOST_PY3_COMPONENT}
Boost::${PYIMATH_BOOST_PY_COMPONENT}
Python3::ImathNumPy
)
set_target_properties(imathnumpy_python3 PROPERTIES
Expand Down
98 changes: 50 additions & 48 deletions src/python/config/ModuleDefine.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -100,59 +100,61 @@ function(PYIMATH_DEFINE_MODULE modname)
# NB: make this one last so we can cheat and add the python and boost
# libs as private deps at the end regardless of whether it was provided
list(APPEND libarglist PRIVATE_DEPS ${PYIMATH_CURMOD_PRIVATE_DEPS})
if(TARGET Python2::Python AND TARGET Boost::${PYIMATH_BOOST_PY2_COMPONENT})
set(libname "${PYIMATH_CURMOD_LIBNAME}${PYIMATH_LIB_PYTHONVER_ROOT}${Python2_VERSION_MAJOR}_${Python2_VERSION_MINOR}")
set(extraDeps ${PYIMATH_CURMOD_MODULE_DEPS})
list(TRANSFORM extraDeps APPEND ${PYIMATH_LIB_PYTHONVER_ROOT}${Python2_VERSION_MAJOR}_${Python2_VERSION_MINOR})
if(USE_PYTHON3)
if(TARGET Python3::Python AND TARGET Boost::${PYIMATH_BOOST_PY_COMPONENT})
set(libname "${PYIMATH_CURMOD_LIBNAME}${PYIMATH_LIB_PYTHONVER_ROOT}${Python3_VERSION_MAJOR}_${Python3_VERSION_MINOR}")
set(extraDeps ${PYIMATH_CURMOD_MODULE_DEPS})
list(TRANSFORM extraDeps APPEND ${PYIMATH_LIB_PYTHONVER_ROOT}${Python3_VERSION_MAJOR}_${Python3_VERSION_MINOR})

pyimath_add_library_priv(${libname}
${libarglist}
${extraDeps}
Python2::Python
Boost::${PYIMATH_BOOST_PY2_COMPONENT}
)

Python2_add_library(${modname}_python2 MODULE ${PYIMATH_CURMOD_MODSOURCE})
target_link_libraries(${modname}_python2
PRIVATE
${libname}
pyimath_add_library_priv(${libname}
${libarglist}
${extraDeps}
${PYIMATH_CURMOD_DEPENDENCIES}
${PYIMATH_CURMOD_PRIVATE_DEPS}
Boost::${PYIMATH_BOOST_PY2_COMPONENT}
Python3::Python
Boost::${PYIMATH_BOOST_PY_COMPONENT}
)
set_target_properties(${modname}_python2 PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python${Python2_VERSION_MAJOR}_${Python2_VERSION_MINOR}/"
LIBRARY_OUTPUT_NAME "${modname}"
DEBUG_POSTFIX ""
)
install(TARGETS ${modname}_python2 DESTINATION ${PyImath_Python2_SITEARCH_REL})
endif()
Python3_add_library(${modname}_python3 MODULE ${PYIMATH_CURMOD_MODSOURCE})
target_link_libraries(${modname}_python3
PRIVATE
${libname} ${extraDeps}
${PYIMATH_CURMOD_DEPENDENCIES}
${PYIMATH_CURMOD_PRIVATE_DEPS}
Boost::${PYIMATH_BOOST_PY_COMPONENT}
)
set_target_properties(${modname}_python3 PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python${Python3_VERSION_MAJOR}_${Python3_VERSION_MINOR}/"
LIBRARY_OUTPUT_NAME "${modname}"
DEBUG_POSTFIX ""
)
install(TARGETS ${modname}_python3 DESTINATION ${PyImath_Python3_SITEARCH_REL})
endif()
else()
if(TARGET Python2::Python AND TARGET Boost::${PYIMATH_BOOST_PY_COMPONENT})
set(libname "${PYIMATH_CURMOD_LIBNAME}${PYIMATH_LIB_PYTHONVER_ROOT}${Python2_VERSION_MAJOR}_${Python2_VERSION_MINOR}")
set(extraDeps ${PYIMATH_CURMOD_MODULE_DEPS})
list(TRANSFORM extraDeps APPEND ${PYIMATH_LIB_PYTHONVER_ROOT}${Python2_VERSION_MAJOR}_${Python2_VERSION_MINOR})

if(TARGET Python3::Python AND TARGET Boost::${PYIMATH_BOOST_PY3_COMPONENT})
set(libname "${PYIMATH_CURMOD_LIBNAME}${PYIMATH_LIB_PYTHONVER_ROOT}${Python3_VERSION_MAJOR}_${Python3_VERSION_MINOR}")
set(extraDeps ${PYIMATH_CURMOD_MODULE_DEPS})
list(TRANSFORM extraDeps APPEND ${PYIMATH_LIB_PYTHONVER_ROOT}${Python3_VERSION_MAJOR}_${Python3_VERSION_MINOR})
pyimath_add_library_priv(${libname}
${libarglist}
${extraDeps}
Python2::Python
Boost::${PYIMATH_BOOST_PY_COMPONENT}
)

pyimath_add_library_priv(${libname}
${libarglist}
${extraDeps}
Python3::Python
Boost::${PYIMATH_BOOST_PY3_COMPONENT}
)
Python3_add_library(${modname}_python3 MODULE ${PYIMATH_CURMOD_MODSOURCE})
target_link_libraries(${modname}_python3
PRIVATE
${libname} ${extraDeps}
${PYIMATH_CURMOD_DEPENDENCIES}
${PYIMATH_CURMOD_PRIVATE_DEPS}
Boost::${PYIMATH_BOOST_PY3_COMPONENT}
Python2_add_library(${modname}_python2 MODULE ${PYIMATH_CURMOD_MODSOURCE})
target_link_libraries(${modname}_python2
PRIVATE
${libname}
${extraDeps}
${PYIMATH_CURMOD_DEPENDENCIES}
${PYIMATH_CURMOD_PRIVATE_DEPS}
Boost::${PYIMATH_BOOST_PY_COMPONENT}
)
set_target_properties(${modname}_python2 PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python${Python2_VERSION_MAJOR}_${Python2_VERSION_MINOR}/"
LIBRARY_OUTPUT_NAME "${modname}"
DEBUG_POSTFIX ""
)
set_target_properties(${modname}_python3 PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/python${Python3_VERSION_MAJOR}_${Python3_VERSION_MINOR}/"
LIBRARY_OUTPUT_NAME "${modname}"
DEBUG_POSTFIX ""
)
install(TARGETS ${modname}_python3 DESTINATION ${PyImath_Python3_SITEARCH_REL})
install(TARGETS ${modname}_python2 DESTINATION ${PyImath_Python2_SITEARCH_REL})
endif()
endif()
endfunction()

0 comments on commit 87ce0f2

Please sign in to comment.