#============================================================================
# CDF - src/lib
#============================================================================
set(CDFLIB_H)
set(CDFLIB_C)
set(CDF_INCLUDE_PATH ../include)

set(CDFLIB_PUBLIC_H
     ../include/cdfconfig.h ../include/cdfdist.h 
     ../include/cdf.h ../include/cdflib64.h ../include/cdflib.h)
set(CDFLIB_PRIVATE_H
     ../include/cdfmd5.h ../include/cdfrev.h ../include/cdftt2000.h)
list(APPEND CDFLIB_H ${CDFLIB_PUBLIC_H} ${CDFLIB_PRIVATE_H})

list(APPEND CDFLIB_C
     cdfalloc64.c cdfalloc.c cdfcks.c cdfclo64.c cdfclo.c
     cdfcon64.c cdfcon.c cdfcre64.c cdfcre.c cdfcsi.c
     cdfdel64.c cdfdel.c cdfed.c cdffii.c cdffsi2.c
     cdffsi3.c cdffsi.c cdfget64.c cdfget.c cdfgzip64.c
     cdfgzip.c cdfhuff64.c cdfhuff.c cdfhyper64.c cdfhyper.c
     cdflib.c cdfmd5.c cdfmem.c cdfmisc1_64.c cdfmisc1.c
     cdfmisc2_64.c cdfmisc2.c cdfmisc3_64.c cdfmisc3.c cdfope.c
     cdfput1_64.c cdfput1.c cdfput2_64.c cdfput2.c cdfread64.c
     cdfread.c cdfrle64.c cdfrle.c cdfsav.c cdfsel64.c
     cdfsel.c cdfstr.c cdftext.c cdftt2000.c cdftt2000f.c
     cdfvalidator64.c cdfvalidator.c cdfvalid.c cdfwrite64.c cdfwrite.c
     dirutils.c epochu.c epochuf.c vstream64.c
     vstream.c)

if (MSVC) 
  list(APPEND CDFLIB_C libmain.c)
endif()

set(CDFLIB_CORE ${CDFLIB_C} ${CDFLIB_H})
check_c_compiler_flag("-Wno-stringop-overflow" SUPPORTS_NO_STRINGOP_OVERFLOW) 

if(BUILD_STATIC_CDF)
  add_library(CDF_STATIC OBJECT ${CDFLIB_CORE})
  set_target_properties(CDF_STATIC PROPERTIES POSITION_INDEPENDENT_CODE OFF)
  if(TOZLIB-NG)
    target_compile_definitions(CDF_STATIC PRIVATE ZLIB_COMPAT _FILE_OFFSET_BITS=64 _LARGEFILE64_SOURCE=1 _LARGEFILE_SOURCE MALLOC_CHECK_=0)
    target_link_libraries(CDF_STATIC PRIVATE zlibstatic-ng)
  else()
    target_compile_definitions(CDF_STATIC PRIVATE Z_PREFIX _FILE_OFFSET_BITS=64 _LARGEFILE64_SOURCE=1 _LARGEFILE_SOURCE MALLOC_CHECK_=0)
    target_link_libraries(CDF_STATIC PRIVATE zlibstatic-orig)
  endif()
  if(MSVC)
    set_target_properties(CDF_STATIC PROPERTIES
             COMPILE_OPTIONS "$<$<CONFIG:Debug>:/MTd>$<$<CONFIG:Release>:/MT>"
    )
  else()
    if(SUPPORTS_NO_STRINGOP_OVERFLOW)
      target_compile_options(CDF_STATIC PRIVATE -Wno-stringop-overflow)
    endif()
  endif()
endif()

if(BUILD_SHARED_CDF)
  add_library(CDF_SHARED OBJECT ${CDFLIB_CORE})
  set_target_properties(CDF_SHARED PROPERTIES POSITION_INDEPENDENT_CODE ON)
  if(NOT MSVC)
    if(SUPPORTS_NO_STRINGOP_OVERFLOW)
      target_compile_options(CDF_SHARED PRIVATE -Wno-stringop-overflow)
    endif()
  endif()
  if(TOZLIB-NG)
    target_compile_definitions(CDF_SHARED PRIVATE ZLIB_COMPAT _FILE_OFFSET_BITS=64 _LARGEFILE64_SOURCE=1 _LARGEFILE_SOURCE MALLOC_CHECK_=0)
    target_link_libraries(CDF_SHARED PRIVATE zlib-ng)
  else()
    target_compile_definitions(CDF_SHARED PRIVATE Z_PREFIX _FILE_OFFSET_BITS=64 _LARGEFILE64_SOURCE=1 _LARGEFILE_SOURCE MALLOC_CHECK_=0)
    target_link_libraries(CDF_SHARED PRIVATE zlib-orig)
  endif()
endif()

if(TOZLIB-NG)
  include_directories(${CMAKE_CURRENT_BINARY_DIR}/zlib-ng)
else()
  include_directories(${CMAKE_CURRENT_BINARY_DIR}/zlib)
endif()

if(BUILD_SHARED_CDF)
  target_include_directories(CDF_SHARED PUBLIC ${CDF_INCLUDE_PATH})
  if(MSVC)
    target_compile_definitions(CDF_SHARED PRIVATE _USRDLL DLLCDF_EXPORTS BUILDINGforDLL)
  endif()
endif()

if(BUILD_STATIC_CDF)
  target_include_directories(CDF_STATIC PUBLIC ${CDF_INCLUDE_PATH})
  if(MSVC)
    target_compile_definitions(CDF_STATIC PRIVATE _LIB)
  endif()
endif()

