changeset 47:42de8b600c0c

Support of Apple OS X
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 04 Aug 2020 15:32:49 +0200
parents 3e2ff3616e57
children 0f8c9ce124d6
files CMakeLists.txt NEWS Sources/Plugin.cpp
diffstat 3 files changed, 89 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Tue Aug 04 13:31:27 2020 +0200
+++ b/CMakeLists.txt	Tue Aug 04 15:32:49 2020 +0200
@@ -12,10 +12,18 @@
 endif()
 
 
-set(PYTHON_VERSION "3.6" CACHE STRING "Version of Python to be used")
-set(PYTHON_WINDOWS_ROOT "" CACHE STRING "")
-set(PYTHON_LIBRARY_NAME "" CACHE STRING "")
-set(PYTHON_WINDOWS_USE_RELEASE_LIBS ON CACHE BOOL "Use the release Python libraries when building with Microsoft Visual Studio, even when compiling in _DEBUG mode (set it to OFF if you require linking to a Python debug build)")
+if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
+  # The Python version cannot be controlled on OS X (yet)
+  set(PYTHON_VERSION "3.6" CACHE STRING "Version of Python to be used")
+endif()
+
+if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
+  # Windows-specific options
+  set(PYTHON_WINDOWS_ROOT "" CACHE STRING "")
+  set(PYTHON_LIBRARY_NAME "" CACHE STRING "")
+  set(PYTHON_WINDOWS_USE_RELEASE_LIBS ON CACHE BOOL "Use the release Python libraries when building with Microsoft Visual Studio, even when compiling in _DEBUG mode (set it to OFF if you require linking to a Python debug build)")
+endif()
+
 
 
 # Parameters of the build
@@ -60,70 +68,80 @@
 include(CheckLibraryExists)
 include(FindPythonInterp)
 
-string(REGEX REPLACE "^([0-9]*)\\.([0-9]*)$" "\\1" PYTHON_VERSION_MAJOR ${PYTHON_VERSION})
-string(REGEX REPLACE "^([0-9]*)\\.([0-9]*)$" "\\2" PYTHON_VERSION_MINOR ${PYTHON_VERSION})
+
+if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
+  find_package(PythonLibs)
+  if (NOT PYTHONLIBS_FOUND)
+    message(FATAL_ERROR "Cannot find the Python libraries")
+  endif()
 
-if (NOT PYTHON_VERSION STREQUAL
-    "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
-  message(FATAL_ERROR "Error in the (x.y) format of the Python version: ${PYTHON_VERSION}")
-endif()
+  message("Python library - Found version: ${PYTHONLIBS_VERSION_STRING}")
+  message("Python library - Path to include directory: ${PYTHON_INCLUDE_DIRS}")
+  message("Python library - Shared library: ${PYTHON_LIBRARIES}")
+
+else()
+  string(REGEX REPLACE "^([0-9]*)\\.([0-9]*)$" "\\1" PYTHON_VERSION_MAJOR ${PYTHON_VERSION})
+  string(REGEX REPLACE "^([0-9]*)\\.([0-9]*)$" "\\2" PYTHON_VERSION_MINOR ${PYTHON_VERSION})
 
-if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
-  if ("${PYTHON_LIBRARY_NAME}" STREQUAL "")
-    if (MSVC)
-      set(Prefix "")
-      set(Suffix ".lib")
-      if(PYTHON_WINDOWS_USE_RELEASE_LIBS)
-        add_definitions(-DORTHANC_PYTHON_WINDOWS_USE_RELEASE_LIBS=1)
+  if (NOT PYTHON_VERSION STREQUAL
+      "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
+    message(FATAL_ERROR "Error in the (x.y) format of the Python version: ${PYTHON_VERSION}")
+  endif()
+
+  if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
+    if ("${PYTHON_LIBRARY_NAME}" STREQUAL "")
+      if (MSVC)
+        set(Prefix "")
+        set(Suffix ".lib")
+        if(PYTHON_WINDOWS_USE_RELEASE_LIBS)
+          add_definitions(-DORTHANC_PYTHON_WINDOWS_USE_RELEASE_LIBS=1)
+        endif()
+      else()
+        list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 Prefix)
+        set(Suffix ".a")
       endif()
-    else()
-      list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 Prefix)
-      set(Suffix ".a")
+      
+      set(PYTHON_LIBRARY_NAME ${Prefix}python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}${Suffix})
     endif()
     
-    set(PYTHON_LIBRARY_NAME ${Prefix}python${PYTHON_VERSION_MAJOR}${PYTHON_VERSION_MINOR}${Suffix})
-  endif()
-  
-  if (CMAKE_COMPILER_IS_GNUCXX AND
-      "${CMAKE_SIZEOF_VOID_P}" STREQUAL "8" AND
-      "${PYTHON_VERSION}" STREQUAL "2.7")
-    # Fix for MinGW 64bit: https://stackoverflow.com/a/19867426/881731
-    add_definitions(-DMS_WIN64)
-  endif()
+    if (CMAKE_COMPILER_IS_GNUCXX AND
+        "${CMAKE_SIZEOF_VOID_P}" STREQUAL "8" AND
+        "${PYTHON_VERSION}" STREQUAL "2.7")
+      # Fix for MinGW 64bit: https://stackoverflow.com/a/19867426/881731
+      add_definitions(-DMS_WIN64)
+    endif()
+    
+    set(PYTHON_INCLUDE_DIRS ${PYTHON_WINDOWS_ROOT}/include)
+    set(PYTHON_LIBRARIES ${PYTHON_WINDOWS_ROOT}/libs/${PYTHON_LIBRARY_NAME})
     
-  set(PYTHON_INCLUDE_DIRS ${PYTHON_WINDOWS_ROOT}/include)
-  set(PYTHON_LIBRARIES ${PYTHON_WINDOWS_ROOT}/libs/${PYTHON_LIBRARY_NAME})
-  
-else()
-  find_package(PkgConfig REQUIRED)
-  pkg_check_modules(PYTHON_1 python-${PYTHON_VERSION}-embed)
+    execute_process(
+      COMMAND 
+      ${PYTHON_EXECUTABLE} ${ORTHANC_FRAMEWORK_ROOT}/../Resources/WindowsResources.py
+      ${PLUGIN_VERSION} "Python plugin" OrthancPython.dll
+      "Plugin to create Orthanc plugins using Python"
+      ERROR_VARIABLE Failure
+      OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/Version.rc
+      )
 
-  if (PYTHON_1_FOUND)
-    set(PYTHON_INCLUDE_DIRS ${PYTHON_1_INCLUDE_DIRS})
-    set(PYTHON_LIBRARIES ${PYTHON_1_LIBRARIES})
+    if (Failure)
+      message(FATAL_ERROR "Error while computing the version information: ${Failure}")
+    endif()
+
+    set(WINDOWS_RESOURCES ${CMAKE_CURRENT_BINARY_DIR}/Version.rc)
+
   else()
-    pkg_check_modules(PYTHON_2 REQUIRED python-${PYTHON_VERSION})
-    set(PYTHON_INCLUDE_DIRS ${PYTHON_2_INCLUDE_DIRS})
-    set(PYTHON_LIBRARIES ${PYTHON_2_LIBRARIES})
-  endif()
-endif()
-
+    find_package(PkgConfig REQUIRED)
+    pkg_check_modules(PYTHON_1 python-${PYTHON_VERSION}-embed)
 
-if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
-  execute_process(
-    COMMAND 
-    ${PYTHON_EXECUTABLE} ${ORTHANC_FRAMEWORK_ROOT}/../Resources/WindowsResources.py
-    ${PLUGIN_VERSION} "Python plugin" OrthancPython.dll
-    "Plugin to create Orthanc plugins using Python"
-    ERROR_VARIABLE Failure
-    OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/Version.rc
-    )
-
-  if (Failure)
-    message(FATAL_ERROR "Error while computing the version information: ${Failure}")
+    if (PYTHON_1_FOUND)
+      set(PYTHON_INCLUDE_DIRS ${PYTHON_1_INCLUDE_DIRS})
+      set(PYTHON_LIBRARIES ${PYTHON_1_LIBRARIES})
+    else()
+      pkg_check_modules(PYTHON_2 REQUIRED python-${PYTHON_VERSION})
+      set(PYTHON_INCLUDE_DIRS ${PYTHON_2_INCLUDE_DIRS})
+      set(PYTHON_LIBRARIES ${PYTHON_2_LIBRARIES})
+    endif()
   endif()
-
-  set(WINDOWS_RESOURCES ${CMAKE_CURRENT_BINARY_DIR}/Version.rc)
 endif()
 
 
--- a/NEWS	Tue Aug 04 13:31:27 2020 +0200
+++ b/NEWS	Tue Aug 04 15:32:49 2020 +0200
@@ -1,6 +1,7 @@
 Pending changes in the mainline
 ===============================
 
+* Support of Apple OS X
 * Fix issue #185 (segfaults on non-UTF8 special characters in request URI)
 
 
--- a/Sources/Plugin.cpp	Tue Aug 04 13:31:27 2020 +0200
+++ b/Sources/Plugin.cpp	Tue Aug 04 15:32:49 2020 +0200
@@ -36,8 +36,15 @@
 #include <boost/algorithm/string/predicate.hpp>
 #include <boost/filesystem.hpp>
 
-#if !defined(_WIN32)
-#  include <dlfcn.h>
+
+// The "dl_iterate_phdr()" function (to walk through shared libraries)
+// is not available on Microsoft Windows and Apple OS X
+#if defined(_WIN32)
+#  define HAS_DL_ITERATE  0
+#elif defined(__APPLE__) && defined(__MACH__)
+#  define HAS_DL_ITERATE  0
+#else
+#  define HAS_DL_ITERATE  1
 #endif
 
 
@@ -120,10 +127,9 @@
 
 
 
-
+#if HAS_DL_ITERATE == 1
 
-#if !defined(_WIN32)
-
+#include <dlfcn.h>
 #include <link.h>  // For dl_phdr_info
 
 static int ForceImportCallback(struct dl_phdr_info *info, size_t size, void *data)
@@ -233,7 +239,7 @@
          * Initialization of Python
          **/
 
-#if !defined(_WIN32)
+#if HAS_DL_ITERATE == 1
         dl_iterate_phdr(ForceImportCallback, NULL);
 #endif