# the current build target is ubuntu 22.04 LTS, it ships with cmake 3.22
cmake_minimum_required(VERSION 3.22)

if (POLICY CMP0074)
  cmake_policy(SET CMP0074 NEW)
endif()

project(fluffos
        DESCRIPTION "FluffOS LPC Driver"
        LANGUAGES C CXX)

set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})

# Default Build Type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE PATH "..." FORCE)
endif()

if(WIN32)
  # minimal support version is Windows 7
  add_compile_options("-DWINVER=0x0601")
  add_compile_options("-D_WIN32_WINNT=0x0601")
  add_compile_options("-D_UNICODE")
  add_compile_options("-DUNICODE")
endif()

#
# Determine the project version.
#  - Release tarballs (built by release.sh) ship a VERSION file (YYYYMMDD).
#  - In-repo developer builds derive it from git.
#  - Otherwise it is "unknown".
#

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION")
  file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" PROJECT_VERSION)
  string(STRIP "${PROJECT_VERSION}" PROJECT_VERSION)
else()
  find_program (BASH_EXECUTABLE bash REQUIRED)
  if(NOT WIN32)
    find_package(Git)
  else()
    execute_process(
            COMMAND ${BASH_EXECUTABLE} -c "git status"
            RESULT_VARIABLE STATUS
            OUTPUT_VARIABLE OUTPUT2
            ERROR_QUIET )
    if(STATUS AND NOT STATUS EQUAL 0)
    else()
      set(GIT_EXECUTABLE "git")
    endif()
  endif()
  # find_package(Git) only confirms the binary exists; make sure we're actually
  # inside a git work tree before running git (avoids fatal: not a git repo).
  if(GIT_EXECUTABLE)
    execute_process(
            WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
            COMMAND ${GIT_EXECUTABLE} rev-parse --is-inside-work-tree
            RESULT_VARIABLE GIT_REPO_RESULT
            OUTPUT_QUIET ERROR_QUIET)
    if(NOT GIT_REPO_RESULT EQUAL 0)
      set(GIT_EXECUTABLE "")
    endif()
  endif()
  if(GIT_EXECUTABLE)
    execute_process(
            WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
            COMMAND ${BASH_EXECUTABLE} -c "${GIT_EXECUTABLE} log --committer='Yucong Sun' -1 --date=format:%Y%m%d --format=%cd-%h"
            OUTPUT_VARIABLE GIT_TIMESTAMP
            OUTPUT_STRIP_TRAILING_WHITESPACE)
    execute_process(
            WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
            COMMAND ${BASH_EXECUTABLE} -c "if [[ $(${GIT_EXECUTABLE} log --committer='Yucong Sun' -1 --format=%h) != $( ${GIT_EXECUTABLE} log -1 --format=%h ) ]]; then echo -$(${GIT_EXECUTABLE} log -1 --format=%h); fi"
            OUTPUT_VARIABLE GIT_LOCAL_COMMIT
            OUTPUT_STRIP_TRAILING_WHITESPACE)
    execute_process(
            WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
            COMMAND ${BASH_EXECUTABLE} -c "${GIT_EXECUTABLE} diff-index --quiet HEAD -- || echo -uncommited"
            OUTPUT_VARIABLE GIT_DIRTY
            OUTPUT_STRIP_TRAILING_WHITESPACE)
    set(PROJECT_VERSION "${GIT_TIMESTAMP}${GIT_LOCAL_COMMIT}${GIT_DIRTY}")
  else()
    set(PROJECT_VERSION "unknown")
  endif()
endif()

message("LODOS ${PROJECT_VERSION} (${CMAKE_BUILD_TYPE})")
foreach(lang C CXX)
message("  ${lang}: '${CMAKE_${lang}_COMPILER}' (${CMAKE_${lang}_COMPILER_ID} ${CMAKE_${lang}_COMPILER_VERSION})")
endforeach()
message("Building on: '${CMAKE_HOST_SYSTEM}' for '${CMAKE_SYSTEM}'")
message("Installing to ${CMAKE_INSTALL_PREFIX}")

find_package(GTest)
if(${GTEST_FOUND})
  include(GoogleTest)
  enable_testing()
endif()

add_subdirectory(src)
