# lib/qsastime/CMakeLists.txt for PLplot
###
### Process this file with cmake to produce Makefile
###
# Copyright (C) 2009-2015 Alan W. Irwin
#
# This file is part of PLplot.
#
# PLplot is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; version 2 of the License.
#
# PLplot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with PLplot; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

# Required to configure qsastimeP.h here and also for deciding whether
# to build test applications later.
option(
  TEST_QSASTIME
  "Build any of the provided libqsastime test applications."
  OFF
  )

configure_file(
  ${CMAKE_CURRENT_SOURCE_DIR}/qsastimeP.h.in
  ${CMAKE_CURRENT_BINARY_DIR}/qsastimeP.h
  )

include_directories(
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_CURRENT_BINARY_DIR}
  )

# Routine for generating a header containing data to be used by the
# TAI-UTC transformation code.

if(NOT CMAKE_CROSSCOMPILING)
  add_executable(tai-utc-gen ${CMAKE_CURRENT_SOURCE_DIR}/tai-utc-gen.c)
  if(MATH_LIB)
    target_link_libraries(tai-utc-gen ${MATH_LIB})
  endif(MATH_LIB)
  add_executable(deltaT-gen ${CMAKE_CURRENT_SOURCE_DIR}/deltaT-gen.c ${CMAKE_CURRENT_SOURCE_DIR}/dspline.c)
  export(TARGETS tai-utc-gen FILE ${CMAKE_CURRENT_BINARY_DIR}/tai-utc-gen.cmake )
  export(TARGETS deltaT-gen FILE ${CMAKE_CURRENT_BINARY_DIR}/deltaT-gen.cmake )
else(NOT CMAKE_CROSSCOMPILING)
  # Generated by initial non-cross build above.
  include(${CMAKE_NATIVE_BINARY_DIR}/lib/qsastime/tai-utc-gen.cmake)
  include(${CMAKE_NATIVE_BINARY_DIR}/lib/qsastime/deltaT-gen.cmake)
endif(NOT CMAKE_CROSSCOMPILING)

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/tai-utc.h
  COMMAND tai-utc-gen
  ${CMAKE_CURRENT_SOURCE_DIR}/tai-utc.dat
  ${CMAKE_CURRENT_BINARY_DIR}/tai-utc.h
  DEPENDS
  ${CMAKE_CURRENT_SOURCE_DIR}/tai-utc.dat
  tai-utc-gen
  )

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/deltaT.h
  COMMAND deltaT-gen
  ${CMAKE_CURRENT_SOURCE_DIR}/deltaT.dat
  ${CMAKE_CURRENT_BINARY_DIR}/deltaT.h
  DEPENDS
  ${CMAKE_CURRENT_SOURCE_DIR}/deltaT.dat
  deltaT-gen
  )

# These target-level dependencies needed below.
add_custom_target(
  tai-utc.h_built
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tai-utc.h
  )

add_custom_target(
  deltaT.h_built
  DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/deltaT.h
  )

set(qsastime_LIB_SRCS
  qsastime.c dsplint.c
  )

if(BUILD_SHARED_LIBS)
  set_source_files_properties( ${qsastime_LIB_SRCS}
    PROPERTIES COMPILE_FLAGS "-DUSINGDLL"
    )
endif(BUILD_SHARED_LIBS)
add_library(qsastime ${qsastime_LIB_SRCS})
add_dependencies(qsastime tai-utc.h_built deltaT.h_built)

if(NON_TRANSITIVE)
  # empty list ==> non-transitive linking for everything that links to
  # libqsatime in the shared libraries case.
  target_link_libraries(qsastime LINK_INTERFACE_LIBRARIES)
endif(NON_TRANSITIVE)

if(MATH_LIB)
  target_link_libraries(qsastime ${MATH_LIB})
endif(MATH_LIB)
set_target_properties(
  qsastime
  PROPERTIES
  SOVERSION ${qsastime_SOVERSION}
  VERSION ${qsastime_VERSION}
  INSTALL_NAME_DIR "${LIB_DIR}"
  )
set(qsastime_INSTALLED_HEADERS
  qsastime.h
  qsastimedll.h
  )

install(FILES ${qsastime_INSTALLED_HEADERS} DESTINATION ${INCLUDE_DIR})

install(TARGETS qsastime
  EXPORT export_plplot
  ARCHIVE DESTINATION ${LIB_DIR}
  LIBRARY DESTINATION ${LIB_DIR}
  RUNTIME DESTINATION ${BIN_DIR}
  )

# Build test routines if so desired.
if(TEST_QSASTIME)
  option(
    BUILD_QSASTIME_TESTLIB
    "Build qsastime_testlib application which is used to test the qsastime library against the C library time routines"
    OFF
    )
  # Don't bother with building qsastime_testlib on Windows because its 
  # C library is incapable of providing useful time routines to act
  # as comparisons with qsastime library results.
  if(WIN32_OR_CYGWIN)
    set(BUILD_QSASTIME_TESTLIB OFF CACHE BOOL "Build qsastime_testlib application which is used to test the qsastime library against the C library time routines" FORCE)
  endif(WIN32_OR_CYGWIN)

  # qsastime_extra.[ch] provides helper functions for qsastime_test that should
  # not be part of the library.
  # Add in qsastime.c rather than use the library because don't want
  # to make the private library API visible.

  add_executable(qsastime_test qsastime_test.c qsastime_extra.c qsastime.c)
  add_dependencies(qsastime_test tai-utc.h_built)
  if(MATH_LIB)
    target_link_libraries(qsastime_test ${MATH_LIB})
  endif(MATH_LIB)

  # Routine for testing bhunt_search function in qsastime.c.
  add_executable(bhunt_search_test bhunt_search_test.c qsastime.c)
  add_dependencies(bhunt_search_test tai-utc.h_built)
  if(MATH_LIB)
    target_link_libraries(bhunt_search_test ${MATH_LIB})
  endif(MATH_LIB)

  # Routine for generating interpolated values for delta T (difference between
  # ephemeris time and GMT) on standard grid.
  # Add in dsplint.c rather than use the library because don't want
  # to make the private library API visible.
  add_executable(deltaT_test deltaT_test.c dsplint.c)
  add_dependencies(deltaT_test deltaT.h_built)

  # Routine for testing just the library functions against their library
  # inverses (to test self-consistency when such inverses exist) and also
  # equivalent C library routines.
  # N.B. the routine immediately errors out at run time if sizeof(time_t) < 8 or
  # sizeof(int) !=4.  The time_t constraint is because we want access to a large
  # C library date range to do the tests.  The int constraint is so we
  # emulate library results on 32-bit systems.
  # The test executable normally only passes these two criteria for
  # 64-bit platforms.
  # Add in qsastime.c rather than use the library because don't want
  # to make the private library API visible.

  if(BUILD_QSASTIME_TESTLIB)
    add_executable(qsastime_testlib qsastime_testlib.c qsastime.c)
    add_dependencies(qsastime_testlib tai-utc.h_built)
    if(MATH_LIB)
      target_link_libraries(qsastime_testlib ${MATH_LIB})
    endif(MATH_LIB)
  endif(BUILD_QSASTIME_TESTLIB)
endif(TEST_QSASTIME)
