# Project
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(rapidcsv VERSION 1.0 LANGUAGES CXX)
set (CMAKE_CXX_STANDARD 11)
if(MSVC)
  if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
    string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  else()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
  endif()
  add_compile_options("/utf-8")
else()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra -Wpedantic -Wshadow -Wpointer-arith \
                       -Wcast-qual -Wno-missing-braces -Wswitch-default -Wcast-align \
                       -Wunreachable-code -Wundef -Wuninitialized -Wold-style-cast \
                       -Wsign-conversion -Weffc++")
endif()

# Build type
set(DEFAULT_BUILD_TYPE "Release")
if((EXISTS "${CMAKE_SOURCE_DIR}/.svn") OR (EXISTS "${CMAKE_SOURCE_DIR}/.git"))
  set(DEFAULT_BUILD_TYPE "Debug")
endif()
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  message(STATUS "Using build type '${DEFAULT_BUILD_TYPE}' (default).")
  set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}")
else()
  message(STATUS "Using build type '${CMAKE_BUILD_TYPE}'.")
endif()

# Library
add_library(rapidcsv INTERFACE)
target_include_directories(rapidcsv INTERFACE src)

# Tests
option(RAPIDCSV_BUILD_TESTS "Build tests" OFF)
message(STATUS "Build tests: ${RAPIDCSV_BUILD_TESTS}")
if(RAPIDCSV_BUILD_TESTS)

  # Test init
  include(CTest)
  enable_testing()

  # Ccache
  find_program(CCACHE_PROGRAM ccache)
  if(CCACHE_PROGRAM)
    message(STATUS "Found ccache")
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
  endif()

  # Enable codecvt tests if supported
  if (CMAKE_CXX_STANDARD VERSION_LESS "17")
    include(CheckIncludeFileCXX)
    check_include_file_cxx(codecvt HAS_CODECVT)
    if(HAS_CODECVT)
      target_compile_definitions(rapidcsv INTERFACE HAS_CODECVT)
    endif()
  else()
    message(STATUS "Skip check for C++ include codecvt - C++${CMAKE_CXX_STANDARD}")
  endif()

  # Test macro add_perf_test
  macro(add_perf_test testname)
    if(CMAKE_BUILD_TYPE MATCHES "Release")
      FILE(GLOB sources tests/${testname}*.cpp)
      add_executable(${testname} ${sources})
      add_test(NAME ${testname} COMMAND "${PROJECT_BINARY_DIR}/${testname}")
      target_link_libraries(${testname} PUBLIC rapidcsv)
    endif()
  endmacro(add_perf_test)

  # Test macro add_unit_test
  macro(add_unit_test testname)
    if(CMAKE_BUILD_TYPE MATCHES "Debug")
      FILE(GLOB sources tests/${testname}*.cpp)
      add_executable(${testname} ${sources})
      add_test(NAME ${testname} COMMAND "${PROJECT_BINARY_DIR}/${testname}")
      target_link_libraries(${testname} PUBLIC rapidcsv)
    endif()
  endmacro(add_unit_test)

  # Unit tests
  add_unit_test(test001)
  add_unit_test(test002)
  add_unit_test(test003)
  add_unit_test(test004)
  add_unit_test(test005)
  add_unit_test(test006)
  add_unit_test(test007)
  add_unit_test(test008)
  add_unit_test(test009)
  add_unit_test(test010)
  add_unit_test(test011)
  add_unit_test(test012)
  add_unit_test(test013)
  add_unit_test(test014)
  add_unit_test(test015)
  add_unit_test(test016)
  add_unit_test(test017)
  add_unit_test(test018)
  add_unit_test(test019)
  add_unit_test(test020)
  add_unit_test(test021)
  add_unit_test(test022)
  add_unit_test(test023)
  add_unit_test(test024)
  add_unit_test(test025)
  add_unit_test(test026)
  add_unit_test(test027)
  add_unit_test(test028)
  add_unit_test(test029)
  add_unit_test(test030)
  add_unit_test(test031)
  add_unit_test(test032)
  add_unit_test(test033)
  add_unit_test(test034)
  add_unit_test(test035)
  add_unit_test(test036)
  add_unit_test(test037)
  add_unit_test(test038)
  add_unit_test(test039)
  add_unit_test(test040)
  add_unit_test(test041)
  add_unit_test(test042)
  add_unit_test(test043)
  add_unit_test(test044)
  add_unit_test(test045)
  add_unit_test(test046)
  add_unit_test(test047)
  add_unit_test(test048)
  add_unit_test(test049)
  add_unit_test(test050)
  add_unit_test(test051)
  add_unit_test(test052)
  add_unit_test(test053)
  add_unit_test(test054)
  add_unit_test(test055)
  add_unit_test(test056)
  if(HAS_CODECVT)
    add_unit_test(test057)
    add_unit_test(test058)
    add_unit_test(test059)
    add_unit_test(test060)
  endif()
  add_unit_test(test061)
  add_unit_test(test062)
  add_unit_test(test063)
  add_unit_test(test064)
  add_unit_test(test065)
  add_unit_test(test066)
  add_unit_test(test067)
  add_unit_test(test068)
  add_unit_test(test069)
  add_unit_test(test070)
  add_unit_test(test071)
  add_unit_test(test072)
  add_unit_test(test073)
  add_unit_test(test074)
  add_unit_test(test075)
  add_unit_test(test076)
  add_unit_test(test077)
  add_unit_test(test078)
  add_unit_test(test079)
  add_unit_test(test080)
  add_unit_test(test081)
  add_unit_test(test082)
  add_unit_test(test083)
  add_unit_test(test084)
  if(HAS_CODECVT)
    add_unit_test(test085)
  endif()
  add_unit_test(test086)
  add_unit_test(test087)
  add_unit_test(test088)
  add_unit_test(test089)
  add_unit_test(test090)
  add_unit_test(test091)
  add_unit_test(test092)
  add_unit_test(test093)
  add_unit_test(test094)
  add_unit_test(test095)

  # perf tests
  add_perf_test(ptest001)
  add_perf_test(ptest002)

  # Examples
  # Test macro add_example
  macro(add_example testname)
    if(CMAKE_BUILD_TYPE MATCHES "Debug")
      add_executable(${testname} examples/${testname}.cpp)
      target_link_libraries(${testname} PUBLIC rapidcsv)
    endif()
  endmacro(add_example)

  add_example(ex001 examples/ex001.cpp)
  add_example(ex002 examples/ex002.cpp)
  add_example(ex003 examples/ex003.cpp)
  add_example(ex004 examples/ex004.cpp)
  add_example(ex005 examples/ex005.cpp)
  add_example(ex006 examples/ex006.cpp)
  add_example(ex007 examples/ex007.cpp)
  add_example(ex008 examples/ex008.cpp)
  add_example(ex009 examples/ex009.cpp)

endif()

# Install / Uninstall targets for standalone rapidcsv build
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
  message(STATUS "Install/uninstall targets enabled.")

  # Install
  install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/rapidcsv.h DESTINATION include)

  # Uninstall
  add_custom_target(uninstall COMMAND "${CMAKE_COMMAND}" -E remove "${CMAKE_INSTALL_PREFIX}/include/rapidcsv.h"
)
else()
  message(STATUS "Install/uninstall targets disabled.")
endif()
