From 89dceca80dc28fbabf262e38c9e1acf4863d97f6 Mon Sep 17 00:00:00 2001 From: Kimball Thurston Date: Wed, 6 Nov 2019 22:17:02 +1300 Subject: [PATCH] Extract to function, protect against infinite loop Seems like under windows, the introduced relative path search can cause an infinite loop if it doesn't match. Test for that, may need adjustment. Signed-off-by: Kimball Thurston --- PyIlmBase/CMakeLists.txt | 51 +++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/PyIlmBase/CMakeLists.txt b/PyIlmBase/CMakeLists.txt index 0c6c7f974d..71a1301383 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