cmake_minimum_required (VERSION 3.1)
project (HDF4_MFHDF_XDR C CXX)

set (HDF4_MFHDF_XDR_SRCS
    ${HDF4_MFHDF_XDR_SOURCE_DIR}/xdr.c
    ${HDF4_MFHDF_XDR_SOURCE_DIR}/xdrfloat.c
    ${HDF4_MFHDF_XDR_SOURCE_DIR}/xdrstdio.c
    ${HDF4_MFHDF_XDR_SOURCE_DIR}/xdrarray.c
)

set (HDF4_MFHDF_XDR_HDRS
    ${HDF4_MFHDF_XDR_SOURCE_DIR}/xdr.h
    ${HDF4_MFHDF_XDR_SOURCE_DIR}/types.h
)

INCLUDE_DIRECTORIES (${HDF4_HDFSOURCE_DIR})
INCLUDE_DIRECTORIES (${HDF4_MFHDF_XDR_DIR})

add_library (${HDF4_MF_XDR_LIB_TARGET} STATIC ${HDF4_MFHDF_XDR_SRCS} ${HDF4_MFHDF_XDR_HDRS})
if (MSVC OR MINGW)
  target_link_libraries (${HDF4_MF_XDR_LIB_TARGET} PUBLIC "ws2_32.lib")
endif ()
TARGET_C_PROPERTIES (${HDF4_MF_XDR_LIB_TARGET} STATIC " " " ")
set_global_variable (HDF4_LIBRARIES_TO_EXPORT "${HDF4_LIBRARIES_TO_EXPORT};${HDF4_MF_XDR_LIB_TARGET}")
H4_SET_LIB_OPTIONS (${HDF4_MF_XDR_LIB_TARGET} ${HDF4_MF_XDR_LIB_NAME} STATIC)
set_target_properties (${HDF4_MF_XDR_LIB_TARGET} PROPERTIES
    FOLDER libraries
    COMPILE_DEFINITIONS "HDF"
    INTERFACE_INCLUDE_DIRECTORIES "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include>"
)
if (WIN32)
  set_property (TARGET ${HDF4_MF_XDR_LIB_TARGET} APPEND PROPERTY COMPILE_DEFINITIONS "DOS_FS;NO_SYS_XDR_INC")
endif ()
set (install_targets ${HDF4_MF_XDR_LIB_TARGET})

if (BUILD_SHARED_LIBS)
  add_library (${HDF4_MF_XDR_LIBSH_TARGET} SHARED ${HDF4_MFHDF_XDR_SRCS} ${HDF4_MFHDF_XDR_HDRS})
  if (MSVC OR MINGW)
    target_link_libraries (${HDF4_MF_XDR_LIBSH_TARGET} PUBLIC "ws2_32.lib")
  endif ()
  TARGET_C_PROPERTIES (${HDF4_MF_XDR_LIBSH_TARGET} SHARED " " " ")
  set_global_variable (HDF4_LIBRARIES_TO_EXPORT "${HDF4_LIBRARIES_TO_EXPORT};${HDF4_MF_XDR_LIBSH_TARGET}")
  H4_SET_LIB_OPTIONS (${HDF4_MF_XDR_LIBSH_TARGET} ${HDF4_MF_XDR_LIB_NAME} SHARED)
  set_target_properties (${HDF4_MF_XDR_LIBSH_TARGET} PROPERTIES
      FOLDER libraries
      COMPILE_DEFINITIONS "HDF;H4_BUILT_AS_DYNAMIC_LIB"
      INTERFACE_INCLUDE_DIRECTORIES "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include>"
      INTERFACE_COMPILE_DEFINITIONS H4_BUILT_AS_DYNAMIC_LIB=1
  )
  if (WIN32)
    set_property (TARGET ${HDF4_MF_XDR_LIBSH_TARGET} APPEND PROPERTY COMPILE_DEFINITIONS "DOS_FS;NO_SYS_XDR_INC")
  endif ()
  set (install_targets ${install_targets} ${HDF4_MF_XDR_LIBSH_TARGET})
endif ()

#-----------------------------------------------------------------------------
# Add library to CMake Install : Installs lib and cmake config info
#-----------------------------------------------------------------------------
if (BUILD_SHARED_LIBS)
  INSTALL_TARGET_PDB (${HDF4_MF_XDR_LIBSH_TARGET} ${HDF4_INSTALL_BIN_DIR} libraries)
endif ()
INSTALL_TARGET_PDB (${HDF4_MF_XDR_LIB_TARGET} ${HDF4_INSTALL_BIN_DIR} libraries)

install (
    TARGETS
        ${install_targets}
    EXPORT
        ${HDF4_EXPORTED_TARGETS}
    LIBRARY DESTINATION ${HDF4_INSTALL_LIB_DIR} COMPONENT libraries
    ARCHIVE DESTINATION ${HDF4_INSTALL_LIB_DIR} COMPONENT libraries
    RUNTIME DESTINATION ${HDF4_INSTALL_BIN_DIR} COMPONENT libraries
    FRAMEWORK DESTINATION ${HDF4_INSTALL_FWRK_DIR} COMPONENT libraries
    INCLUDES DESTINATION include
)
