# HG changeset patch
# User Jasper Lievisse Adriaanse <jasper@humppa.nl>
# Date 1502297997 -7200
#      Wed Aug 09 18:59:57 2017 +0200
# Branch openbsd
# Node ID ae7e06411374dbad9c5fb68307d5b8d966887ce8
# Parent  f58b6bdbd63b677ada4549c1e8145cc714911123
Add support for OpenBSD

diff -r f58b6bdbd63b -r ae7e06411374 Core/MultiThreading/Mutex.cpp
--- a/Core/MultiThreading/Mutex.cpp	Thu Jul 20 16:13:52 2017 +0200
+++ b/Core/MultiThreading/Mutex.cpp	Wed Aug 09 18:59:57 2017 +0200
@@ -38,7 +38,8 @@
 
 #if defined(_WIN32)
 #include <windows.h>
-#elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__FreeBSD__)
+#elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || \
+	defined(__FreeBSD__) || defined(__OpenBSD__)
 #include <pthread.h>
 #else
 #error Support your platform here
@@ -76,7 +77,8 @@
   }
 
 
-#elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__FreeBSD__)
+#elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || \
+	defined(__FreeBSD__) || defined(__OpenBSD__)
 
   struct Mutex::PImpl
   {
diff -r f58b6bdbd63b -r ae7e06411374 Core/SystemToolbox.cpp
--- a/Core/SystemToolbox.cpp	Thu Jul 20 16:13:52 2017 +0200
+++ b/Core/SystemToolbox.cpp	Wed Aug 09 18:59:57 2017 +0200
@@ -55,12 +55,16 @@
 #endif
 
 
-#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
+#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || \
+	defined(__OpenBSD__)
 #  include <limits.h>      /* PATH_MAX */
 #  include <signal.h>
 #  include <unistd.h>
 #endif
 
+#if defined(__OpenBSD__)
+#  include <sys/sysctl.h>
+#endif
 
 // Inclusions for UUID
 // http://stackoverflow.com/a/1626302
@@ -157,7 +161,8 @@
   {
 #if defined(_WIN32)
     ::Sleep(static_cast<DWORD>(microSeconds / static_cast<uint64_t>(1000)));
-#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__native_client__)
+#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD_kernel__) || \
+  defined(__FreeBSD__) || defined(__native_client__) || defined(__OpenBSD__)
     usleep(microSeconds);
 #else
 #error Support your platform here
@@ -370,6 +375,26 @@
     return std::string(pathbuf);
   }
 
+#elif defined(__OpenBSD__)
+  static std::string GetPathToExecutableInternal()
+  {
+    size_t len;
+    char **args;
+    int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
+
+    if (sysctl(mib, 4, NULL, &len, NULL, 0) == -1) {
+      throw OrthancException(ErrorCode_PathToExecutable);
+    }
+
+    args = (char **)malloc(len);
+
+    if (sysctl(mib, 4, args, &len, NULL, 0) == -1) {
+      free(args);
+      throw OrthancException(ErrorCode_PathToExecutable);
+    }
+
+    return std::string(args[0]);
+  }
 #else
 #error Support your platform here
 #endif
diff -r f58b6bdbd63b -r ae7e06411374 OrthancServer/FromDcmtkBridge.cpp
--- a/OrthancServer/FromDcmtkBridge.cpp	Thu Jul 20 16:13:52 2017 +0200
+++ b/OrthancServer/FromDcmtkBridge.cpp	Wed Aug 09 18:59:57 2017 +0200
@@ -209,7 +209,8 @@
         LOG(INFO) << "The dictionary of private tags has not been loaded";
       }
 
-#elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
+#elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || \
+	defined(__OpenBSD__)
       std::string path = DCMTK_DICTIONARY_DIR;
 
       const char* env = std::getenv(DCM_DICT_ENVIRONMENT_VARIABLE);
diff -r f58b6bdbd63b -r ae7e06411374 Plugins/Engine/PluginsManager.cpp
--- a/Plugins/Engine/PluginsManager.cpp	Thu Jul 20 16:13:52 2017 +0200
+++ b/Plugins/Engine/PluginsManager.cpp	Wed Aug 09 18:59:57 2017 +0200
@@ -49,7 +49,8 @@
 
 #ifdef WIN32
 #define PLUGIN_EXTENSION ".dll"
-#elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
+#elif defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || \
+	defined(__OpenBSD__)
 #define PLUGIN_EXTENSION ".so"
 #elif defined(__APPLE__) && defined(__MACH__)
 #define PLUGIN_EXTENSION ".dylib"
diff -r f58b6bdbd63b -r ae7e06411374 Plugins/Engine/SharedLibrary.cpp
--- a/Plugins/Engine/SharedLibrary.cpp	Thu Jul 20 16:13:52 2017 +0200
+++ b/Plugins/Engine/SharedLibrary.cpp	Wed Aug 09 18:59:57 2017 +0200
@@ -46,7 +46,8 @@
 
 #if defined(_WIN32)
 #include <windows.h>
-#elif defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
+#elif defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || \
+	defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__OpenBSD__)
 #include <dlfcn.h>
 #else
 #error Support your platform here
@@ -66,7 +67,8 @@
       throw OrthancException(ErrorCode_SharedLibrary);
     }
 
-#elif defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
+#elif defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || \
+	defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__OpenBSD__)
     handle_ = ::dlopen(path_.c_str(), RTLD_NOW);
     if (handle_ == NULL) 
     {
@@ -92,7 +94,8 @@
     {
 #if defined(_WIN32)
       ::FreeLibrary((HMODULE)handle_);
-#elif defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
+#elif defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || \
+	defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__OpenBSD__)
       ::dlclose(handle_);
 #else
 #error Support your platform here
@@ -110,7 +113,8 @@
 
 #if defined(_WIN32)
     return ::GetProcAddress((HMODULE)handle_, name.c_str());
-#elif defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD_kernel__) || defined(__FreeBSD__)
+#elif defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || \
+	defined(__FreeBSD_kernel__) || defined(__FreeBSD__) || defined(__OpenBSD__)
     return ::dlsym(handle_, name.c_str());
 #else
 #error Support your platform here
diff -r f58b6bdbd63b -r ae7e06411374 Resources/CMake/Compiler.cmake
--- a/Resources/CMake/Compiler.cmake	Thu Jul 20 16:13:52 2017 +0200
+++ b/Resources/CMake/Compiler.cmake	Wed Aug 09 18:59:57 2017 +0200
@@ -70,7 +70,10 @@
   # Remove the "-rdynamic" option
   # http://www.mail-archive.com/cmake@cmake.org/msg08837.html
   set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
-  link_libraries(uuid pthread rt)
+  if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
+    link_libraries(rt)
+  endif()
+  link_libraries(uuid pthread)
 
   if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed")
@@ -80,7 +83,9 @@
       -D_LARGEFILE64_SOURCE=1 
       -D_FILE_OFFSET_BITS=64
       )
-    link_libraries(dl)
+    if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
+      link_libraries(dl)
+    endif()
   endif()
 
   CHECK_INCLUDE_FILES(uuid/uuid.h HAVE_UUID_H)
@@ -155,7 +160,8 @@
 endif()
 
 
-if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
+if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD" OR
+    ${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
   # In FreeBSD, the "/usr/local/" folder contains the ports and need to be imported
   SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I/usr/local/include")
   SET(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -I/usr/local/include")
diff -r f58b6bdbd63b -r ae7e06411374 UnitTestsSources/PluginsTests.cpp
--- a/UnitTestsSources/PluginsTests.cpp	Thu Jul 20 16:13:52 2017 +0200
+++ b/UnitTestsSources/PluginsTests.cpp	Wed Aug 09 18:59:57 2017 +0200
@@ -65,8 +65,8 @@
   ASSERT_TRUE(l.HasFunction("dlclose"));
   ASSERT_FALSE(l.HasFunction("world"));
 
-#elif defined(__FreeBSD__)
-  // dlopen() in FreeBSD is supplied by libc, libc.so is
+#elif defined(__FreeBSD__) || defined(__OpenBSD__)
+  // dlopen() in FreeBSD/OpenBSD is supplied by libc, libc.so is
   // a ldscript, so we can't actually use it. Use thread
   // library instead - if it works - dlopen() is good.
   SharedLibrary l("libpthread.so");
diff -r f58b6bdbd63b -r ae7e06411374 UnitTestsSources/UnitTestsMain.cpp
--- a/UnitTestsSources/UnitTestsMain.cpp	Thu Jul 20 16:13:52 2017 +0200
+++ b/UnitTestsSources/UnitTestsMain.cpp	Wed Aug 09 18:59:57 2017 +0200
@@ -658,7 +658,7 @@
 
 
 
-#if defined(__linux__)
+#if defined(__linux__) || defined(__OpenBSD__)
 #include <endian.h>
 #elif defined(__FreeBSD__)
 #include <machine/endian.h>
@@ -697,10 +697,10 @@
 
   
   /**
-   * FreeBSD.
+   * FreeBSD/OpenBSD
    **/
   
-#elif defined(__FreeBSD__)
+#elif defined(__FreeBSD__) || defined(__OpenBSD__)
 #  if _BYTE_ORDER == _BIG_ENDIAN
    ASSERT_EQ(Endianness_Big, Toolbox::DetectEndianness());
 #  else // _LITTLE_ENDIAN
