#######################
#
#  Licensed to the Apache Software Foundation (ASF) under one or more contributor license
#  agreements.  See the NOTICE file distributed with this work for additional information regarding
#  copyright ownership.  The ASF licenses this file to you under the Apache License, Version 2.0
#  (the "License"); you may not use this file except in compliance with the License.  You may obtain
#  a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software distributed under the License
#  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
#  or implied. See the License for the specific language governing permissions and limitations under
#  the License.
#
#######################

set(TSUTIL_PUBLIC_HEADERS
    ${PROJECT_SOURCE_DIR}/include/tsutil/Assert.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/Metrics.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/SourceLocation.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/DbgCtl.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/ts_bw_format.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/ts_diag_levels.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/ts_errata.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/Bravo.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/Convert.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/DenseThreadId.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/Histogram.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/LocalBuffer.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/PostScript.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/Strerror.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/StringConvert.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/Regex.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/TsMutex.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/TsSharedMutex.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/ts_thread_safety.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/YamlCfg.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/ts_ip.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/ts_meta.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/ts_time_parser.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/ts_unit_parser.h
    ${PROJECT_SOURCE_DIR}/include/tsutil/ts_errata.h
)
add_library(
  tsutil
  Assert.cc
  Metrics.cc
  DbgCtl.cc
  SourceLocation.cc
  ts_diags.cc
  ts_ip.cc
  YamlCfg.cc
  ts_unit_parser.cc
  Regex.cc
  ts_errata.cc
)

add_library(ts::tsutil ALIAS tsutil)
set_target_properties(tsutil PROPERTIES POSITION_INDEPENDENT_CODE TRUE PUBLIC_HEADER "${TSUTIL_PUBLIC_HEADERS}")
target_link_libraries(tsutil PUBLIC libswoc::libswoc yaml-cpp::yaml-cpp PkgConfig::PCRE2)

if(BUILD_SHARED_LIBS)
  install(
    TARGETS tsutil
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tsutil
  )
else()
  install(FILES ${TSUTIL_PUBLIC_HEADERS} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tsutil)
endif()

if(BUILD_TESTING)
  add_executable(
    test_tsutil
    unit_tests/test_Bravo.cc
    unit_tests/test_LocalBuffer.cc
    unit_tests/test_Metrics.cc
    unit_tests/test_PostScript.cc
    unit_tests/test_Strerror.cc
    unit_tests/test_StringConvert.cc
    unit_tests/test_Regex.cc
    unit_tests/test_ts_meta.cc
    unit_tests/test_thread_safety.cc
    unit_tests/test_time_parser.cc
  )

  # The thread-safety test doubles as a compile-time check: build it with the
  # analysis enabled and treat any finding as an error so a broken annotation
  # chain fails the build. Scoped to the same lanes as the global analysis block
  # in the top-level CMakeLists.txt (Clang, excluding FreeBSD); a no-op for GCC,
  # where the macros expand to nothing.
  if(NOT CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
    set_source_files_properties(
      unit_tests/test_thread_safety.cc
      PROPERTIES COMPILE_OPTIONS "$<$<CXX_COMPILER_ID:Clang,AppleClang>:-Wthread-safety;-Werror=thread-safety>"
    )
  endif()

  target_link_libraries(test_tsutil PRIVATE tsutil Catch2::Catch2WithMain)

  add_catch2_test(NAME test_tsutil COMMAND $<TARGET_FILE:test_tsutil>)
endif()

clang_tidy_check(tsutil)
