# LAF Base Library
# Copyright (c) 2019-2024 Igara Studio S.A.
# Copyright (c) 2001-2018 David Capello

include(CheckIncludeFiles)
include(CheckFunctionExists)
include(CheckCXXSourceCompiles)
include(TestBigEndian)

check_include_files(stdint.h HAVE_STDINT_H)
check_include_files(dlfcn.h HAVE_DLFCN_H)
check_function_exists(sched_yield HAVE_SCHED_YIELD)
check_cxx_source_compiles("
  #include <cstdlib>
  int main() { return std::system(\"\"); }
  " HAVE_SYSTEM)

test_big_endian(LAF_BIG_ENDIAN)
if(NOT LAF_BIG_ENDIAN)
  set(LAF_LITTLE_ENDIAN TRUE)
endif()

# Generate config.h file
configure_file(${CMAKE_CURRENT_LIST_DIR}/config.h.cmakein
               ${LAF_BINARY_DIR}/base/config.h @ONLY)

set(BASE_SOURCES
  base64.cpp
  cfile.cpp
  chrono.cpp
  convert_to.cpp
  debug.cpp
  dll.cpp
  errno_string.cpp
  exception.cpp
  file_content.cpp
  file_handle.cpp
  fs.cpp
  launcher.cpp
  log.cpp
  mem_utils.cpp
  memory.cpp
  memory_dump.cpp
  platform.cpp
  process.cpp
  program_options.cpp
  replace_string.cpp
  rw_lock.cpp
  serialization.cpp
  sha1.cpp
  sha1_rfc3174.c
  split_string.cpp
  string.cpp
  system_console.cpp
  task.cpp
  thread.cpp
  thread_pool.cpp
  time.cpp
  version.cpp)

if(WIN32)
  set(BASE_SOURCES ${BASE_SOURCES}
    platform_win.cpp
    uuid_win.cpp
    win/registry.cpp
    win/ver_query_values.cpp
    win/win32_exception.cpp)
elseif(APPLE)
  set(BASE_SOURCES ${BASE_SOURCES}
    fs_osx.mm
    platform_osx.mm
    uuid_osx.mm)
else()
  set(BASE_SOURCES ${BASE_SOURCES}
    platform_unix.cpp
    uuid_unix.cpp)
endif()

add_library(laf-base ${BASE_SOURCES})

# To #include "base/base.h" and "base/config.h"
target_include_directories(laf-base PUBLIC
  ${LAF_LIST_DIR} ${LAF_BINARY_DIR})

if(WIN32)
  target_compile_definitions(laf-base PUBLIC LAF_WINDOWS
    # Windows Vista is the minimum supported platform but we're defining
    # Windows 10 to get the all constant/structure definitions.
    _WIN32_WINNT=0x0A00 WINVER=0x0A00
    # We only support the Unicode API
    UNICODE _UNICODE)

  if(MSVC)
    target_compile_options(laf-base PUBLIC
      # Disable warnings about possible loss of data
      -wd4244

      # Disable warnings about conversion from size_t to int
      -wd4267

      # Disable warning about using 'this' used in base member initializer list
      -wd4355

      # Disable function not inlined (generated by MSVC header files)
      -wd4710

      # Avoid warnings about usage of insecure standard C library
      # functions and C++ STL functions
      -D_CRT_SECURE_NO_WARNINGS
      -D_SCL_SECURE_NO_WARNINGS)
  endif()

  target_link_libraries(laf-base dbghelp shlwapi version)
else()
  if(APPLE)
    target_compile_options(laf-base PRIVATE -fobjc-arc)

    find_library(FOUNDATION_LIBRARY Foundation)
    target_compile_definitions(laf-base PUBLIC LAF_MACOS)
    target_link_libraries(laf-base ${FOUNDATION_LIBRARY})
  else()
    target_compile_definitions(laf-base PUBLIC LAF_LINUX)
    target_link_libraries(laf-base pthread)
  endif()

  find_library(DL_LIBRARY NAMES dl)
  if(DL_LIBRARY)
    target_link_libraries(laf-base ${DL_LIBRARY})
  endif()
endif()

if(LAF_WITH_TESTS)
  laf_find_tests(. laf-base)
  if(WIN32)
    laf_find_tests(win laf-base)
  endif()
endif()
