if(EXISTS /usr/include/dlfcn.h)
  set(CMAKE_DL_LIBS "")
  set(CMAKE_C_COMPILE_OPTIONS_PIC "-fPIC")
  set(CMAKE_C_COMPILE_OPTIONS_PIE "-fPIE")
  set(CMAKE_SHARED_LIBRARY_C_FLAGS "-fPIC")            # -pic
  set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-shared")       # -shared
  set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")         # +s, flag for exe link to use shared lib
  set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG "-Wl,-rpath,")       # -rpath
  set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG_SEP ":")   # : or empty
  set(CMAKE_SHARED_LIBRARY_RPATH_LINK_C_FLAG "-Wl,-rpath-link,")
  set(CMAKE_EXE_EXPORTS_C_FLAG "-Wl,--export-dynamic")
endif()

include(Platform/UnixPaths)

if($ENV{LOCALBASE})
  set(OPENBSD_LOCALBASE $ENV{LOCALBASE})
else($ENV{LOCALBASE})
  set(OPENBSD_LOCALBASE /usr/local)
endif($ENV{LOCALBASE})

if($ENV{X11BASE})
  set(OPENBSD_X11BASE $ENV{X11BASE})
else($ENV{X11BASE})
  set(OPENBSD_X11BASE /usr/X11R6)
endif($ENV{X11BASE})

set(CMAKE_SYSTEM_INCLUDE_PATH ${CMAKE_SYSTEM_INCLUDE_PATH} /usr/include
  ${OPENBSD_LOCALBASE}/include ${OPENBSD_X11BASE}/include)
set(CMAKE_SYSTEM_LIBRARY_PATH ${CMAKE_SYSTEM_LIBRARY_PATH} /usr/lib
  ${OPENBSD_LOCALBASE}/lib ${OPENBSD_X11BASE}/lib)
set(CMAKE_SYSTEM_PROGRAM_PATH ${CMAKE_SYSTEM_PROGRAM_PATH} /bin /usr/bin
  ${OPENBSD_LOCALBASE}/bin /sbin /usr/sbin ${OPENBSD_LOCALBASE}/sbin)

# On OpenBSD, the compile time linker does not share it's configuration with
# the runtime linker.  This will extract the library search paths from the
# system's ld.so.hints file which will allow CMake to set the appropriate
# -rpath-link flags
if(NOT CMAKE_PLATFORM_RUNTIME_PATH)
  execute_process(COMMAND /sbin/ldconfig -r
                  OUTPUT_VARIABLE LDCONFIG_HINTS
                  ERROR_QUIET)
  string(REGEX REPLACE ".*search\\ directories:\\ ([^\n]*).*" "\\1"
         LDCONFIG_HINTS "${LDCONFIG_HINTS}")
  string(REGEX REPLACE ":" ";"
         CMAKE_PLATFORM_RUNTIME_PATH
         "${LDCONFIG_HINTS}")
endif()

set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_OPENBSD_VERSIONING 1)

# OpenBSD policy requires that shared libraries be installed without
# executable permission.
set(CMAKE_INSTALL_SO_NO_EXE 1)
