cmake_minimum_required(VERSION 3.11)

set(REFCOUNT_USE_THREADS
    ON
    CACHE BOOL "Whether or not RefCount should be thread aware.")

if(REFCOUNT_USE_THREADS)
  message("Building with thread support, setting CMAKE_C_STANDARD to 11.")
else()
  message("Building without thread support, setting CMAKE_C_STANDARD to 99.")
endif()

if(REFCOUNT_USE_THREADS)
  set(CMAKE_C_STANDARD 11)
else()
  set(CMAKE_C_STANDARD 99)
endif()

project(
  refcount
  VERSION 1.0
  LANGUAGES C)

# For configuring config.h (I like this name better)
set(REFCOUNT_HAS_THREADS "${REFCOUNT_USE_THREADS}")

include(FetchContent)
FetchContent_Declare(
  ht
  GIT_REPOSITORY https://git.zander.im/Zander671/ht.git
  GIT_TAG c6bdb38bb77d45f9d5083706723c84c37db56c9c)

FetchContent_MakeAvailable(ht)

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
  include(CTest)
endif()

# add_compile_options(-fsanitize=address,leak,undefined)
# add_link_options(-fsanitize=address,leak,undefined)

configure_file(include/refcount/config.h.in include/refcount/config.h @ONLY)

add_library(refcount src/allocator.c src/list.c src/refcount.c)
target_link_libraries(refcount PUBLIC ht)
target_include_directories(refcount PUBLIC include/)
target_compile_options(refcount PRIVATE -Wall -Wpedantic)
target_include_directories(refcount
                           PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include")

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
  add_subdirectory(test/)
endif()
