cmake_minimum_required (VERSION 3.10)
project (H4H5 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.
#

#-----------------------------------------------------------------------------
# Instructions for use : Normal Build
#
# For standard build of H4H5 libraries,tests and tools.
# Run cmake using the H4H5 source tree to generate a build tree.
# Enable/Disable options according to requirements and
# set CMAKE_INSTALL_PREFIX to the required install path.
# Make install can be used to install all components for system-wide use.
#
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Allow Visual Studio solution directories
#-----------------------------------------------------------------------------
# Provide a way for Visual Studio Express users to turn OFF the new FOLDER
# organization feature. Default to ON for non-Express users. Express users must
# explicitly turn off this option to build H4H5 in the Express IDE...
#
option (H4H5_USE_FOLDERS "Enable folder grouping of projects in IDEs." ON)
mark_as_advanced (H4H5_USE_FOLDERS)
if (H4H5_USE_FOLDERS)
  set_property (GLOBAL PROPERTY USE_FOLDERS ON)
endif ()
option (H4H5_NO_PACKAGES "CPACK - Disable packaging" OFF)
mark_as_advanced (H4H5_NO_PACKAGES)

#-----------------------------------------------------------------------------
# Set the core names of all the libraries
#-----------------------------------------------------------------------------
set (H4H5_LIB_CORENAME              "H4H5")
set (H4H5_SRC_LIB_CORENAME          "h4h5")

#-----------------------------------------------------------------------------
# Set the true names of all the libraries if customized by external project
#-----------------------------------------------------------------------------
set (H4H5_LIB_NAME              "${H4H5_EXTERNAL_LIB_PREFIX}${H4H5_LIB_CORENAME}")
set (H4H5_SRC_LIB_NAME          "${H4H5_EXTERNAL_LIB_PREFIX}${H4H5_SRC_LIB_CORENAME}")

#-----------------------------------------------------------------------------
# Set the target names of all the libraries
#-----------------------------------------------------------------------------
set (H4H5_LIB_TARGET              ${H4H5_LIB_CORENAME})
set (H4H5_SRC_LIB_TARGET          ${H4H5_SRC_LIB_CORENAME}-static)
set (H4H5_SRC_LIBSH_TARGET        ${H4H5_SRC_LIB_CORENAME}-shared)

#-----------------------------------------------------------------------------
# Define some CMake variables for use later in the project
#-----------------------------------------------------------------------------
set (HDF_RESOURCES_DIR        ${H4H5_SOURCE_DIR}/config/cmake)
set (HDF_RESOURCES_EXT_DIR    ${H4H5_SOURCE_DIR}/config/cmake_ext_mod)
set (H4H5_SRC_DIR             ${H4H5_SOURCE_DIR}/lib/src)
set (H4H5_TEST_SRC_DIR        ${H4H5_SOURCE_DIR}/lib/test)
set (H4H5_UTILS_SRC_DIR       ${H4H5_SOURCE_DIR}/utils)

set (CMAKE_MODULE_PATH ${HDF_RESOURCES_DIR} ${HDF_RESOURCES_EXT_DIR} ${CMAKE_MODULE_PATH})

#-----------------------------------------------------------------------------
# parse the full version number from ?.h and include in H4H5_VERS_INFO
#-----------------------------------------------------------------------------
file (READ ${H4H5_SRC_DIR}/h4toh5.h _h4toh5_h_contents)
string (REGEX REPLACE ".*#define[ \t]+H4TOH5_LIBVER_MAJOR[ \t]+([0-9]*).*$"
    "\\1" H4H5_VERS_MAJOR ${_h4toh5_h_contents})
string (REGEX REPLACE ".*#define[ \t]+H4TOH5_LIBVER_MINOR[ \t]+([0-9]*).*$"
    "\\1" H4H5_VERS_MINOR ${_h4toh5_h_contents})
string (REGEX REPLACE ".*#define[ \t]+H4TOH5_LIBVER_RELEASE[ \t]+([0-9]*).*$"
    "\\1" H4H5_VERS_RELEASE ${_h4toh5_h_contents})
message (STATUS "VERSION: ${H4H5_VERSION}")

#set (H4H5_VERS_MAJOR 2)
#set (H4H5_VERS_MINOR 2)
#set (H4H5_VERS_RELEASE 3)

#set (H4H5_SOVERS_MAJOR 2)
set (H4H5_SOVERS_MINOR 2)
set (H4H5_SOVERS_INTERFACE 3)
set (H4H5_SOVERS_RELEASE 0)
math (EXPR H4H5_SOVERS_MAJOR ${H4H5_SOVERS_INTERFACE}-${H4H5_SOVERS_RELEASE})

#-----------------------------------------------------------------------------
# Basic H4H5 stuff here
#-----------------------------------------------------------------------------
set (H4H5_PACKAGE "h4h5")
set (H4H5_PACKAGE_NAME "H4H5")
set (H4H5_PACKAGE_VERSION "${H4H5_VERS_MAJOR}.${H4H5_VERS_MINOR}.${H4H5_VERS_RELEASE}")
set (H4H5_PACKAGE_VERSION_MAJOR "${H4H5_VERS_MAJOR}.${H4H5_VERS_MINOR}")
set (H4H5_PACKAGE_VERSION_MINOR "${H4H5_VERS_RELEASE}")
if (NOT "${H4H5_VERS_SUBRELEASE}" STREQUAL "")
  set (H4H5_PACKAGE_VERSION_STRING "${H4H5_PACKAGE_VERSION}-${H4H5_VERS_SUBRELEASE}")
else (NOT "${H4H5_VERS_SUBRELEASE}" STREQUAL "")
  set (H4H5_PACKAGE_VERSION_STRING "${H4H5_PACKAGE_VERSION}")
endif (NOT "${H4H5_VERS_SUBRELEASE}" STREQUAL "")
set (H4H5_PACKAGE_STRING "${H4H5_PACKAGE_NAME} ${H4H5_PACKAGE_VERSION_STRING}")
set (H4H5_PACKAGE_TARNAME "${H4H5_PACKAGE}${H4H5_PACKAGE_EXT}")
set (H4H5_PACKAGE_URL "https://support.hdfgroup.org/products/")
set (H4H5_PACKAGE_BUGREPORT "help@hdfgroup.org")
set (H4H5_PACKAGE_SOVERSION "${H4H5_SOVERS_MAJOR}.${H4H5_SOVERS_RELEASE}.${H4H5_SOVERS_MINOR}")
set (H4H5_PACKAGE_SOVERSION_MAJOR "${H4H5_SOVERS_MAJOR}")

#-----------------------------------------------------------------------------
# Include some macros for reusable code
#-----------------------------------------------------------------------------
include (${HDF_RESOURCES_EXT_DIR}/HDFMacros.cmake)

HDF_DIR_PATHS(${H4H5_PACKAGE_NAME})

include (${HDF_RESOURCES_EXT_DIR}/HDFLibMacros.cmake)
include (${HDF_RESOURCES_DIR}/H4H5Macros.cmake)
include (${HDF_RESOURCES_DIR}/HDF45Macros.cmake)

#-----------------------------------------------------------------------------
# Targets built within this project are exported at Install time for use
# by other projects.
#-----------------------------------------------------------------------------
if (NOT H4H5_EXPORTED_TARGETS)
  set (H4H5_EXPORTED_TARGETS "h4h5-targets")
endif ()

#-----------------------------------------------------------------------------
# To include a library in the list exported by the project AT BUILD TIME,
# add it to this variable. This is NOT used by Make Install, but for projects
# which include H4H5 as a sub-project within their build tree
#-----------------------------------------------------------------------------
set_global_variable (H4H5_LIBRARIES_TO_EXPORT "")
set_global_variable (H4H5_APPS_TO_EXPORT "")

set (EXTERNAL_HEADER_LIST "")
set (EXTERNAL_LIBRARY_LIST "")
set (EXTERNAL_LIBRARYDLL_LIST "")

#-----------------------------------------------------------------------------
# Run all the CMake configuration tests for our build environment
#-----------------------------------------------------------------------------
include (${HDF_RESOURCES_DIR}/ConfigureChecks.cmake)

set (CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE ON)

#-----------------------------------------------------------------------------
# Mac OS X Options
#-----------------------------------------------------------------------------
if (H4H5_BUILD_FRAMEWORKS AND NOT BUILD_SHARED_LIBS)
  set (BUILD_SHARED_LIBS ON CACHE BOOL "Build Shared Libraries")
endif ()

#-----------------------------------------------------------------------------
# Option to Build Shared and Static libs, default is static
#-----------------------------------------------------------------------------
option (ONLY_SHARED_LIBS "Only Build Shared Libraries" OFF)
set (H4H5_ENABLE_STATIC_LIB YES)
if (ONLY_SHARED_LIBS)
  set (H4H5_ENABLE_STATIC_LIB NO)
  set (BUILD_SHARED_LIBS ON CACHE BOOL "Build Shared Libraries")
endif ()
option (BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
set (H4H5_ENABLE_SHARED_LIB NO)
if (BUILD_SHARED_LIBS)
  set (H4H5_ENABLE_SHARED_LIB YES)
endif ()
set (CMAKE_POSITION_INDEPENDENT_CODE ON)

#Use static hdf4 and hdf5 libraries for building H4H5 tools and libs
#If USE_SHARED_LIBS is enabled then H4H5 shared tools and libs will use shared hdf4 - hdf5 libs
set (H4H5_USE_STATIC_LIBRARIES 0)
if (NOT USE_SHARED_LIBS)
  set (H4H5_USE_STATIC_LIBRARIES 1)
endif ()

add_definitions (-DBIG_LONGS -DSWAP)
add_definitions (-DHAVE_CONFIG_H)

#-----------------------------------------------------------------------------
# Option to use code coverage
#-----------------------------------------------------------------------------
option (HDF_ENABLE_COVERAGE "Enable code coverage for Libraries and Programs" OFF)
if (HDF_ENABLE_COVERAGE)
  set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 --coverage -fprofile-arcs -ftest-coverage")
  set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g --coverage -O0 -fprofile-arcs -ftest-coverage")
  if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
    set (LDFLAGS "${LDFLAGS} -fprofile-arcs -ftest-coverage")
    link_libraries (gcov)
  else ()
    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
  endif ()
endif ()

#-----------------------------------------------------------------------------
# Option to use 1.6.x API
#-----------------------------------------------------------------------------
option (HDF5_USE_16_API_DEFAULT "Use the HDF5 1.6.x API by default" ON)
set (H5_USE_16_API_DEFAULT 0)
if (HDF5_USE_16_API_DEFAULT)
  set (H5_USE_16_API_DEFAULT 1)
endif ()

#-----------------------------------------------------------------------------
# When building utility executables that generate other (source) files :
# we make use of the following variables defined in the root CMakeLists.
# Certain systems may add /Debug or /Release to output paths
# and we need to call the executable from inside the CMake configuration
#-----------------------------------------------------------------------------
set (EXE_EXT "")
if (WIN32 OR MINGW)
  set (EXE_EXT ".exe")
  add_definitions (-D_BIND_TO_CURRENT_VCLIBS_VERSION=1)
  add_definitions (-D_CRT_SECURE_NO_WARNINGS)
  add_definitions (-D_CONSOLE)
endif ()

if (MSVC)
  set (CMAKE_MFC_FLAG 0)
  set (WIN_COMPILE_FLAGS "")
  set (WIN_LINK_FLAGS "")
endif ()

set (MAKE_SYSTEM)
if (CMAKE_BUILD_TOOL MATCHES "make")
  set (MAKE_SYSTEM 1)
endif ()

set (CFG_INIT "/${CMAKE_CFG_INTDIR}")
if (MAKE_SYSTEM)
  set (CFG_INIT "")
endif ()

include (${HDF_RESOURCES_DIR}/HDFCompilerFlags.cmake)
set (CMAKE_MODULE_PATH ${HDF_RESOURCES_DIR} ${HDF_RESOURCES_EXT_DIR} ${CMAKE_MODULE_PATH})

#-----------------------------------------------------------------------------
# Include user macros
#-----------------------------------------------------------------------------
include (UserMacros.cmake)

#-----------------------------------------------------------------------------
# Include filter (zlib, szip, etc.) and HDF-HDF5 macros
#-----------------------------------------------------------------------------
include (CMakeFilters.cmake)

#-----------------------------------------------------------------------------
# Option for external libraries on windows
#-----------------------------------------------------------------------------
option (HDF_PACKAGE_EXTLIBS "CPACK - include external libraries" OFF)
if (HDF_PACKAGE_EXTLIBS)
  if (HDF4_FOUND)
    PACKAGE_HDF4_LIBRARY (${HDF_ALLOW_EXTERNAL_SUPPORT})
  endif ()

  if (HDF5_FOUND)
    PACKAGE_HDF5_LIBRARY (${HDF_ALLOW_EXTERNAL_SUPPORT})
  endif ()
endif ()

#-----------------------------------------------------------------------------
# Dashboard and Testing Settings
#-----------------------------------------------------------------------------
option (BUILD_TESTING "Build H4H5 Unit Testing" OFF)
if (BUILD_TESTING)
  set (DART_TESTING_TIMEOUT 1200
      CACHE STRING
      "Timeout in seconds for each test (default 1200=20minutes)"
  )
  enable_testing ()
  include (CTest)
  include (${H4H5_SOURCE_DIR}/CTestConfig.cmake)
  configure_file (${HDF_RESOURCES_DIR}/CTestCustom.cmake ${H4H5_BINARY_DIR}/CTestCustom.ctest @ONLY)
endif ()

#-----------------------------------------------------------------------------
# Add the H4H5 Library Target to the build
#-----------------------------------------------------------------------------
add_subdirectory (lib)

#-----------------------------------------------------------------------------
# Option to build H4H5 Utilities
#-----------------------------------------------------------------------------
option (HDF_BUILD_UTILS  "Build H4H5 Utilities" OFF)
if (HDF_BUILD_UTILS)
  add_subdirectory (utils)
endif ()

include (CMakeInstallation.cmake)
