cmake_minimum_required (VERSION 3.10)
project (H4H5_LIB_EXAMPLES C)

#
# Copyright by The HDF Group.
# All rights reserved.
#
# This file is part of H4H5TOOLS. The full H4H5TOOLS copyright notice,
# including terms governing use, modification, and redistribution, is
# contained in the COPYING file, which can be found at the root of the
# source code distribution tree. The copyright notice can also be found
# at https://www.hdfgroup.org/licenses.  If you do not have access to
# either file, you may request a copy from help@hdfgroup.org.
#

#-----------------------------------------------------------------------------
# Build Example
#-----------------------------------------------------------------------------
if (NOT ONLY_SHARED_LIBS)
  add_executable (h4toh5example ${H4H5_LIB_EXAMPLES_SOURCE_DIR}/h4toh5example.c)
  target_include_directories(h4toh5example PRIVATE "${HDF4_INCLUDE_DIRS};${HDF5_INCLUDE_DIRS};${H4H5_SRC_DIR};${H4H5_LIB_BINARY_DIR}")
  TARGET_C_PROPERTIES (h4toh5example STATIC)
  target_link_libraries (h4toh5example PRIVATE ${H4H5_SRC_LIB_TARGET})
endif ()
if (BUILD_SHARED_LIBS)
  add_executable (h4toh5example-shared ${H4H5_LIB_EXAMPLES_SOURCE_DIR}/h4toh5example.c)
  target_include_directories(h4toh5example-shared PRIVATE "${HDF4_INCLUDE_DIRS};${HDF5_INCLUDE_DIRS};${H4H5_SRC_DIR};${H4H5_LIB_BINARY_DIR};${H4H5_LIB_SRC_BINARY_DIR}")
  TARGET_C_PROPERTIES (h4toh5example-shared SHARED)
  target_link_libraries (h4toh5example-shared PRIVATE ${H4H5_SRC_LIBSH_TARGET})
endif ()

if (NOT ONLY_SHARED_LIBS)
  file (MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/static")
endif ()
if (BUILD_SHARED_LIBS)
  file (MAKE_DIRECTORY "${PROJECT_BINARY_DIR}/shared")
endif ()

#-- Copy all the dat files from the source directory into the build directory
if (NOT ONLY_SHARED_LIBS)
  add_custom_command (
       TARGET     h4toh5example
       POST_BUILD
       COMMAND    ${CMAKE_COMMAND}
       ARGS       -E copy_if_different ${H4H5_LIB_EXAMPLES_SOURCE_DIR}/sds_lib_test.hdf "${PROJECT_BINARY_DIR}/static/sds_lib_test.hdf"
  )
endif ()
if (BUILD_SHARED_LIBS)
  add_custom_command (
       TARGET     h4toh5example
       POST_BUILD
       COMMAND    ${CMAKE_COMMAND}
       ARGS       -E copy_if_different ${H4H5_LIB_EXAMPLES_SOURCE_DIR}/sds_lib_test.hdf "${PROJECT_BINARY_DIR}/shared/sds_lib_test.hdf"
  )
endif ()

if (BUILD_TESTING)
  if (NOT ONLY_SHARED_LIBS)
    # Remove any output file left over from previous test run
    add_test (
        NAME h4toh5example-clearall-objects
        COMMAND ${CMAKE_COMMAND} -E remove sds_lib_test.h5
        WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/static
    )

    add_test (
        NAME h4toh5example
        COMMAND $<TARGET_FILE:h4toh5example>
        WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/static
    )
    set_tests_properties (h4toh5example PROPERTIES DEPENDS h4toh5example-clearall-objects)
  endif ()
  if (BUILD_SHARED_LIBS)
    # Remove any output file left over from previous test run
    add_test (
        NAME h4toh5example-shared-clearall-objects
        COMMAND ${CMAKE_COMMAND} -E remove sds_lib_test.h5
        WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/shared
    )

    add_test (
        NAME h4toh5example-shared
        COMMAND $<TARGET_FILE:h4toh5example>
        WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/shared
    )
    set_tests_properties (h4toh5example-shared PROPERTIES DEPENDS h4toh5example-shared-clearall-objects)
  endif ()
endif ()
