#####################
# Clang-tidy Module #
#####################
#
# This module provides both a `tidy` target and an option to enable building
# the project with clang-tidy analysis.
#
# To enable clang-tidy output during the build process, set the `BUILD_WITH_CLANG_TIDY_ANALYSIS`
# option to "ON".
#
# You can control the behavior of clang-tidy by setting the following variables
# before you include this module:
#   CLANG_TIDY_DIRS, which is a CMake list of directories to include in clang-tidy analysis
#     By default, the src and test directories are included
#   CLANG_TIDY_FILETYPES, which is a CMake list of file types to include in clang-tidy analysis,
#     specified as globs (e.g.: "*.c")
#     By default, we analyze *.c, and *.cpp files
#
# You can completely override the default vaules by setting the variables above.
# If you wish to use the defaults for CLANG_TIDY_DIRS and CLANG_TIDY_FILETYPES
# but add additional directories or files, you can set these variables:
#   CLANG_TIDY_ADDITIONAL_DIRS
#   CLANG_TIDY_ADDITIONAL_FILETYPES
#
# If you wish to supply any additional arguments to clang-tidy, you can populate the
# CLANG_TIDY_ADDITIONAL_OPTIONS variable. This should be a CMake list of flags and values. They
# will be directly forwarded to the clang-tidy command.
#   Example:
#     set(CLANG_TIDY_ADDITIONAL_OPTIONS --fix)
#     include(cmake/analysis/clang-tidy.cmake)

if(USING_VISUAL_GDB)
  set(CLANG_TIDY_SEARCH_PATH "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin")
  find_program(CLANG_TIDY clang-tidy PATHS ${CLANG_TIDY_SEARCH_PATH})
else()
  find_program(CLANG_TIDY clang-tidy)
endif()

if(CLANG_TIDY)
  option(BUILD_WITH_CLANG_TIDY_ANALYSIS
    "Compile the project with clang-tidy support"
    OFF)

  if(BUILD_WITH_CLANG_TIDY_ANALYSIS)
    set(CMAKE_C_CLANG_TIDY ${CLANG_TIDY})
    set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY})
  endif()

  if(CLANG_TIDY_FILES)
    add_custom_target(tidy
      COMMAND ${CLANG_TIDY} -p ${CMAKE_BINARY_DIR} ${CLANG_TIDY_FILES} ${CLANG_TIDY_OPTIONAL_ARGS}
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    )
  else()
    message("To enable the tidy command, please define a list of files called CLANG_TIDY_FILES before invoking the module.")
  endif()
else()
  message("[WARNING] Clang-tidy is not installed. Clang-tidy targets are disabled.")
endif()
