From 6ad8103ab4391c087aa348085b11f4db5d649024 Mon Sep 17 00:00:00 2001
From: Gerardo Tauriello <gerardo.tauriello@unibas.ch>
Date: Fri, 14 Jun 2019 13:07:10 +0200
Subject: [PATCH] Enable different namings of Boost Python library.

---
 CMakeLists.txt          |  8 +++++++-
 cmake_support/OST.cmake | 26 +++++++++++++++++++++++---
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1f02b1808..f4e1d2c98 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -188,6 +188,13 @@ if (ENABLE_STATIC AND UNIX AND NOT APPLE)
   set (CMAKE_FIND_LIBRARY_SUFFIXES .a)
 endif()
 
+# Python needed before Boost
+find_package(Python 2.4 REQUIRED)
+# Split version string
+string(REPLACE "." ";" _python_version_list ${PYTHON_VERSION})
+list(GET _python_version_list 0 PYTHON_VERSION_MAJOR)
+list(GET _python_version_list 1 PYTHON_VERSION_MINOR)
+
 if(BOOST_VERSION)
   message(STATUS "forcing boost to version ${BOOST_VERSION}")
   set(_BOOST_MIN_VERSION ${BOOST_VERSION})
@@ -233,7 +240,6 @@ else()
 endif()
 
 find_package(Eigen 3.2.0 REQUIRED)
-find_package(Python 2.4 REQUIRED)
 find_package(SQLite3 3.7.13 REQUIRED)
 
 if(USE_NUMPY)
diff --git a/cmake_support/OST.cmake b/cmake_support/OST.cmake
index 4364b0c94..22e774656 100644
--- a/cmake_support/OST.cmake
+++ b/cmake_support/OST.cmake
@@ -770,7 +770,7 @@ macro(ost_match_boost_python_version)
   # this variable may either be a simple library path or list that contains
   # different libraries for different build-options. For example:
   # optimized;<lib1>;debug;<lib2>
-  set(_BOOST_PYTHON_LIBRARY ${Boost_PYTHON_LIBRARY})
+  set(_BOOST_PYTHON_LIBRARY ${BOOST_PYTHON_LIBRARIES})
   list(LENGTH _BOOST_PYTHON_LIBRARY _BP_LENGTH)
   if (_BP_LENGTH GREATER 1)
     list(FIND _BOOST_PYTHON_LIBRARY optimized _OPTIMIZED_INDEX)
@@ -902,8 +902,28 @@ set(_BOOST_MIN_VERSION 1.31)
 
 macro(setup_boost)
   set (Boost_NO_BOOST_CMAKE TRUE)
-  find_package(Boost ${_BOOST_MIN_VERSION} COMPONENTS python REQUIRED)
-  set(BOOST_PYTHON_LIBRARIES ${Boost_LIBRARIES})
+  # starting with CMake 3.11 we could use the following instead of the foreach
+  # find_package(Boost ${_BOOST_MIN_VERSION} COMPONENTS
+  #              python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR} REQUIRED)
+  # set(BOOST_PYTHON_LIBRARIES ${Boost_LIBRARIES})
+  # see https://cmake.org/cmake/help/v3.11/module/FindBoost.html
+  foreach(_python_lib_name python
+                           python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}
+                           python${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}
+                           python${PYTHON_VERSION_MAJOR})
+    find_package(Boost ${_BOOST_MIN_VERSION} COMPONENTS ${_python_lib_name} QUIET)
+    if(Boost_FOUND)
+      message(STATUS "Found Boost package: " ${_python_lib_name})
+      set(BOOST_PYTHON_LIBRARIES ${Boost_LIBRARIES})
+      break()
+    else()
+      message(STATUS "Boost package not found: " ${_python_lib_name}
+                     ". Trying alternative names!")
+    endif()
+  endforeach(_python_lib_name)
+  if(NOT BOOST_PYTHON_LIBRARIES)
+    message(FATAL_ERROR "Failed to find any Boost Python library!")
+  endif()
   set(Boost_LIBRARIES)
   find_package(Boost ${_BOOST_MIN_VERSION}
                COMPONENTS unit_test_framework REQUIRED)
-- 
GitLab