From 09e402e3eac9c50ad17e6d8b5fc80d9db447eb24 Mon Sep 17 00:00:00 2001
From: Yen-Chen Chen <yen-chen.chen@kit.edu>
Date: Tue, 10 Dec 2024 14:16:00 +0000
Subject: [PATCH] Preliminary CMake implementation

---
 CMakeLists.txt                | 75 +++++++++++++++++++++++++++++++++++
 cmake/config.cmake.in         | 11 +++++
 config.h.in                   | 28 +++++++++++++
 doc/CMakeLists.txt            | 21 ++++++++++
 examples/CMakeLists.txt       | 53 +++++++++++++++++++++++++
 examples/example_helper.cmake | 17 ++++++++
 src/CMakeLists.txt            | 65 ++++++++++++++++++++++++++++++
 test/CMakeLists.txt           | 45 +++++++++++++++++++++
 8 files changed, 315 insertions(+)
 create mode 100644 CMakeLists.txt
 create mode 100644 cmake/config.cmake.in
 create mode 100644 config.h.in
 create mode 100644 doc/CMakeLists.txt
 create mode 100644 examples/CMakeLists.txt
 create mode 100644 examples/example_helper.cmake
 create mode 100644 src/CMakeLists.txt
 create mode 100644 test/CMakeLists.txt

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 00000000..c59ddef9
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,75 @@
+# Copyright (c) 2013-2024 MPI-M, Luis Kornblueh, Rahul Sinha and DWD, Florian Prill. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+cmake_minimum_required(VERSION 3.18)
+
+project(mtime VERSION 1.2.0 LANGUAGES Fortran C)
+
+option(BUILD_TESTING "Build tests" ON)
+option(BUILD_EXAMPLES "Build examples" ON)
+option(BUILD_DOCUMENTATION "Build documentation" OFF)
+
+option(MT_ENABLE_MIXED_PRECISION "Use mixed precision" OFF)
+option(MT_ENABLE_FORTRAN_HL "Enable high-level interface" ON)
+
+# Set line limit to none for GNU compiler (default is 132) to enable long __FILE__ macro
+if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
+  add_compile_options($<$<COMPILE_LANGUAGE:Fortran>:-ffree-line-length-none>)
+endif()
+
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/config.h.in
+  ${CMAKE_CURRENT_BINARY_DIR}/config.h)
+
+include(GNUInstallDirs)
+
+add_subdirectory(src)
+
+# Allow for 'make test' even if the tests are disabled:
+enable_testing()
+if(BUILD_TESTING)
+  add_subdirectory(test)
+endif()
+
+if(BUILD_EXAMPLES)
+  add_subdirectory(examples)
+endif()
+
+if(BUILD_DOCUMENTATION)
+  add_subdirectory(doc)
+endif()
+
+export(
+  EXPORT "${PROJECT_NAME}-targets"
+  FILE "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-targets.cmake"
+)
+
+include(CMakePackageConfigHelpers)
+configure_package_config_file(
+  "${PROJECT_SOURCE_DIR}/cmake/config.cmake.in"
+  "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
+  INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake"
+  NO_SET_AND_CHECK_MACRO
+  NO_CHECK_REQUIRED_COMPONENTS_MACRO
+)
+
+write_basic_package_version_file(
+  "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
+  VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}"
+  COMPATIBILITY AnyNewerVersion
+)
+
+install(
+  EXPORT "${PROJECT_NAME}-targets"
+  DESTINATION "${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}/cmake"
+)
+
+install(
+  FILES
+    "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
+    "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
+  DESTINATION
+    "${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}/cmake"
+)
diff --git a/cmake/config.cmake.in b/cmake/config.cmake.in
new file mode 100644
index 00000000..01ef3506
--- /dev/null
+++ b/cmake/config.cmake.in
@@ -0,0 +1,11 @@
+# Copyright (c) 2013-2024 MPI-M, Luis Kornblueh, Rahul Sinha and DWD, Florian Prill. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+@PACKAGE_INIT@
+
+include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake")
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(@PROJECT_NAME@ REQUIRED_VARS @PROJECT_NAME@_DIR)
diff --git a/config.h.in b/config.h.in
new file mode 100644
index 00000000..6e5315b3
--- /dev/null
+++ b/config.h.in
@@ -0,0 +1,28 @@
+// Copyright (c) 2013-2024 MPI-M, Luis Kornblueh, Rahul Sinha and DWD, Florian Prill. All rights reserved.
+//
+// SPDX-License-Identifier: BSD-3-Clause
+//
+
+// Name of package
+#define PACKAGE "@PROJECT_NAME@"
+
+// Define to the full name of this package.
+#define PACKAGE_NAME "@PROJECT_NAME@"
+
+// Define to the full name and version of this package.
+#define PACKAGE_STRING "@PROJECT_NAME@ @PROJECT_VERSION@"
+
+// Define to the one symbol short name of this package.
+#define PACKAGE_TARNAME "@PROJECT_NAME@"
+
+// Define to the version of this package.
+#define PACKAGE_VERSION "@PROJECT_VERSION@"
+
+// Version number of package
+#define VERSION "@PROJECT_VERSION@"
+
+// Enable testing
+#cmakedefine BUILD_TESTING
+#ifdef BUILD_TESTING
+  #define ENABLE_CHECK 1
+#endif
diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt
new file mode 100644
index 00000000..8570ed4c
--- /dev/null
+++ b/doc/CMakeLists.txt
@@ -0,0 +1,21 @@
+# Copyright (c) 2013-2024 MPI-M, Luis Kornblueh, Rahul Sinha and DWD, Florian Prill. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+find_package(Doxygen REQUIRED dot)
+
+# Autoconf args translation
+set(PACKAGE_NAME "${PROJECT_NAME}")
+set(PACKAGE_VERSION "${PROJECT_VERSION}")
+set(DX_DOCDIR "doxygen-doc")
+set(top_srcdir "${PROJECT_SOURCE_DIR}")
+set(srcdir "${CMAKE_CURRENT_SOURCE_DIR}")
+
+configure_file(
+  ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in
+  ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
+
+add_custom_target(doc
+  COMMAND
+    doxygen Doxyfile)
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
new file mode 100644
index 00000000..136f2808
--- /dev/null
+++ b/examples/CMakeLists.txt
@@ -0,0 +1,53 @@
+# Copyright (c) 2013-2024 MPI-M, Luis Kornblueh, Rahul Sinha and DWD, Florian Prill. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+include("${CMAKE_CURRENT_SOURCE_DIR}/example_helper.cmake")
+
+mtime_add_example(callback_test callback_test.f90)
+
+mtime_add_example(comp_weights comp_weights.f90)
+
+mtime_add_example(duration duration.f90 DEPENDS mo_kind.f90)
+
+mtime_add_example(example example.f90)
+
+add_library(mtime_event_manager mo_event_manager.f90)
+target_link_libraries(mtime_event_manager PUBLIC mtime::mtime)
+
+file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/iconatm.nml" DESTINATION
+     "${CMAKE_CURRENT_BINARY_DIR}")
+mtime_add_example(iconatm iconatm.f90 LIBRARIES mtime_event_manager)
+
+file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/iconoce.nml" DESTINATION
+     "${CMAKE_CURRENT_BINARY_DIR}")
+mtime_add_example(iconoce iconoce.f90 LIBRARIES mtime_event_manager)
+
+mtime_add_example(model_integration model_integration.c)
+
+mtime_add_example(modulo modulo.c)
+
+mtime_add_example(output_control output_control.f90)
+
+mtime_add_example(recurrence recurrence.c)
+
+mtime_add_example(repetitor repetitor.f90)
+
+mtime_add_example(tas tas.f90)
+
+mtime_add_example(test_cf_timeaxis test_cf_timeaxis.f90)
+
+mtime_add_example(test_dace test_dace.f90 LIBRARIES mtime_event_manager)
+
+mtime_add_example(test_jd_logic test_jd_logic.f90)
+
+mtime_add_example(uniq uniq.f90)
+
+if(MT_ENABLE_FORTRAN_HL)
+     mtime_add_example(example_hl example_hl.f90)
+
+     mtime_add_example(iconoce_hl iconoce_hl.f90)
+
+     mtime_add_example(simulate_iau simulate_iau.f90)
+endif()
diff --git a/examples/example_helper.cmake b/examples/example_helper.cmake
new file mode 100644
index 00000000..4461aa5a
--- /dev/null
+++ b/examples/example_helper.cmake
@@ -0,0 +1,17 @@
+# Copyright (c) 2013-2024 MPI-M, Luis Kornblueh, Rahul Sinha and DWD, Florian Prill. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+function(mtime_add_example test_name file_name)
+
+  cmake_parse_arguments(PARSE_ARGV 2 ARG "" "" "LIBRARIES;DEPENDS")
+
+  add_executable("${test_name}" ${file_name} ${ARG_DEPENDS})
+  target_link_libraries("${test_name}" PRIVATE mtime::mtime ${ARG_LIBRARIES})
+  set_target_properties(${test_name}
+    PROPERTIES Fortran_PREPROCESS ON)
+  if(MT_ENABLE_MIXED_PRECISION)
+    target_compile_definitions(${test_name} PRIVATE __MIXED_PRECISION)
+  endif()
+endfunction()
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 00000000..42a37009
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,65 @@
+# Copyright (c) 2013-2024 MPI-M, Luis Kornblueh, Rahul Sinha and DWD, Florian Prill. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+add_library(mtime
+  kepler.c
+  libmtime.f90
+  mtime_c_bindings.f90
+  mtime_calendar.c
+  mtime_calendar360day.c
+  mtime_calendar365day.c
+  mtime_calendarGregorian.c
+  mtime_constants.f90
+  mtime_date.c
+  mtime_datetime.c
+  mtime_error_handling.f90
+  mtime_eventHandling.c
+  mtime_eventList.c
+  mtime_iso8601.c
+  mtime_julianDay.c
+  mtime_time.c
+  mtime_timedelta.c
+  mtime_utilities.c
+  orbit.c
+  vsop87.c
+  $<$<BOOL:${MT_ENABLE_FORTRAN_HL}>:libmtime_hl.f90>
+  $<$<BOOL:${MT_ENABLE_FORTRAN_HL}>:mtime_t_datetime.inc>
+  $<$<BOOL:${MT_ENABLE_FORTRAN_HL}>:mtime_t_event.inc>
+  $<$<BOOL:${MT_ENABLE_FORTRAN_HL}>:mtime_t_juliandelta.inc>
+  $<$<BOOL:${MT_ENABLE_FORTRAN_HL}>:mtime_t_timedelta.inc>)
+
+add_library(${PROJECT_NAME}::mtime ALIAS mtime)
+
+set(Fortran_MODULE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/mod")
+set_target_properties(mtime
+  PROPERTIES
+    Fortran_PREPROCESS ON
+    Fortran_MODULE_DIRECTORY "${Fortran_MODULE_DIRECTORY}"
+    C_STANDARD 99
+    C_STANDARD_REQUIRED ON
+    EXPORT_NAME ${PROJECT_NAME}::mtime
+)
+
+target_include_directories(mtime
+  PUBLIC
+# Path to the Fortran modules:
+    $<BUILD_INTERFACE:$<$<COMPILE_LANGUAGE:Fortran>:${Fortran_MODULE_DIRECTORY}>>
+    $<INSTALL_INTERFACE:$<$<COMPILE_LANGUAGE:Fortran>:${CMAKE_INSTALL_INCLUDEDIR}>>
+# Path to the C headers:
+    $<BUILD_INTERFACE:$<$<COMPILE_LANGUAGE:C>:${PROJECT_SOURCE_DIR}/include>>
+    $<INSTALL_INTERFACE:$<$<COMPILE_LANGUAGE:C>:${CMAKE_INSTALL_INCLUDEDIR}>>
+)
+
+install(
+  TARGETS mtime
+  EXPORT "${PROJECT_NAME}-targets"
+)
+
+install(
+  DIRECTORY
+    "${Fortran_MODULE_DIRECTORY}/"
+    "${PROJECT_SOURCE_DIR}/include/"
+  TYPE INCLUDE
+)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 00000000..2069abe4
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,45 @@
+# Copyright (c) 2013-2024 MPI-M, Luis Kornblueh, Rahul Sinha and DWD, Florian Prill. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+
+find_path(Check_INCLUDE_DIRS NAMES check.h HINTS ${Check_DIR} ENV Check_DIR PATH_SUFFIXES include src)
+find_library(Check_LIBRARIES check HINTS ${Check_DIR} ENV Check_DIR PATH_SUFFIXES lib lib64)
+find_library(Subunit_LIBRARIES subunit HINTS ${Check_DIR} ENV Check_DIR PATH_SUFFIXES lib lib64)
+
+if(Check_INCLUDE_DIRS)
+  message(STATUS "Found check.h in ${Check_INCLUDE_DIRS}")
+else()
+  message(WARNING "check.h header not found")
+endif()
+
+if(Check_LIBRARIES)
+  message(STATUS "Found libcheck at ${Check_LIBRARIES}")
+else()
+  message(WARNING "libcheck not found")
+endif()
+
+add_library(Check::check UNKNOWN IMPORTED)
+set_target_properties(Check::check PROPERTIES
+    INTERFACE_INCLUDE_DIRECTORIES "${Check_INCLUDE_DIRS}"
+    IMPORTED_LINK_INTERFACE_LANGUAGES "C"
+    IMPORTED_LOCATION "${Check_LIBRARIES}")
+
+add_library(mtime_check
+  mtime_calendar_test.c
+  mtime_date_test.c
+  mtime_datetime_test.c
+  mtime_julianDay_test.c
+  mtime_time_test.c
+  mtime_timedelta_test.c)
+
+target_include_directories(mtime_check
+  PUBLIC ${PROJECT_BINARY_DIR})
+
+target_link_libraries(mtime_check mtime::mtime Check::check)
+
+add_executable(test_runner test_runner.c)
+
+target_link_libraries(test_runner mtime::mtime mtime_check)
+
+add_test(NAME TestRunner COMMAND test_runner)
-- 
GitLab