diff --git a/PyIlmBase/CMakeLists.txt b/PyIlmBase/CMakeLists.txt index 0c6c7f974..71a130138 100644 --- a/PyIlmBase/CMakeLists.txt +++ b/PyIlmBase/CMakeLists.txt @@ -55,48 +55,39 @@ endif() # for our other dependency - boost. # Boost Python has some .. annoyances in that the python module # has version names attached to it +function(PYILMBASE_EXTRACT_REL_SITEARCH varname pyver pyexe pysitearch) + get_filename_component(_exedir ${pyexe} DIRECTORY) + string(FIND ${pysitearch} ${_exedir} _findloc) + string(LENGTH ${_exedir} _elen) + while(_findloc EQUAL -1 AND _elen GREATER 0) + get_filename_component(_nexedir ${_exedir} DIRECTORY) + string(FIND ${pysitearch} ${_nexedir} _findloc) + if (_nexedir STREQUAL _exedir) + message(FATAL_ERROR "Unable to get parent directory for ${_exedir}") + else() + set(_exedir ${_nexedir}) + endif() + string(LENGTH ${_exedir} _elen) + endwhile() + math(EXPR _elen "${_elen}+1") + string(SUBSTRING ${pysitearch} ${_elen} -1 _reldir) + set(${varname} ${_reldir} CACHE STRING "Destination sub-folder (relative) for the python ${pyver} modules") +endfunction() + if (TARGET Python2::Python) set(PYILMBASE_BOOST_PY2_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 - get_filename_component(_exedir ${Python2_EXECUTABLE} DIRECTORY) - string(FIND ${Python2_SITEARCH} ${_exedir} _findloc) - string(LENGTH ${_exedir} _elen) - while(_findloc EQUAL -1 AND _elen GREATER 0) - get_filename_component(_exedir ${_exedir} DIRECTORY) - string(FIND ${Python2_SITEARCH} ${_exedir} _findloc) - string(LENGTH ${_exedir} _elen) - endwhile() - math(EXPR _elen "${_elen}+1") - string(SUBSTRING ${Python2_SITEARCH} ${_elen} -1 _reldir) - set(PyIlmBase_Python2_SITEARCH_REL ${_reldir} CACHE STRING "Destination sub-folder (relative) for the python 2 modules") - unset(_elen) - unset(_exedir) - unset(_findloc) - unset(_reldir) + pyilmbase_extract_rel_sitearch(PyIlmBase_Python2_SITEARCH_REL 2 ${Python2_EXECUTABLE} ${Python2_SITEARCH}) message(STATUS " -> Installing to: \${CMAKE_INSTALL_PREFIX}/${PyIlmBase_Python2_SITEARCH_REL}") endif() if (TARGET Python3::Python) set(PYILMBASE_BOOST_PY3_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 - get_filename_component(_exedir ${Python3_EXECUTABLE} DIRECTORY) - string(FIND ${Python3_SITEARCH} ${_exedir} _findloc) - string(LENGTH ${_exedir} _elen) - while(_findloc EQUAL -1 AND _elen GREATER 0) - get_filename_component(_exedir ${_exedir} DIRECTORY) - string(FIND ${Python3_SITEARCH} ${_exedir} _findloc) - string(LENGTH ${_exedir} _elen) - endwhile() - math(EXPR _elen "${_elen}+1") - string(SUBSTRING ${Python3_SITEARCH} ${_elen} -1 _reldir) - set(PyIlmBase_Python3_SITEARCH_REL ${_reldir} CACHE STRING "Destination sub-folder (relative) for the python 3 modules") - unset(_elen) - unset(_exedir) - unset(_findloc) - unset(_reldir) + pyilmbase_extract_rel_sitearch(PyIlmBase_Python3_SITEARCH_REL 3 ${Python3_EXECUTABLE} ${Python3_SITEARCH}) message(STATUS " -> Installing to: \${CMAKE_INSTALL_PREFIX}/${PyIlmBase_Python3_SITEARCH_REL}") endif() # different flavors of O.S. have multiple versions of python