cmake_minimum_required(VERSION 3.20)
project(
  arraylist
  VERSION 1.0
  LANGUAGES C)

# ------------------------------------------------
# Estandard C
# ------------------------------------------------
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

# Exportar compile_commands.json (clangd / IDEs)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# ------------------------------------------------
# Is Top project?
# ------------------------------------------------
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
  set(ARRAYLIST_IS_TOP_LEVEL ON)
else()
  set(ARRAYLIST_IS_TOP_LEVEL OFF)
endif()

# ------------------------------------------------
# Opciones
# ------------------------------------------------
option(ARRAYLIST_BUILD_TESTS "Build tests" ${ARRAYLIST_IS_TOP_LEVEL})
option(ARRAYLIST_BUILD_EXAMPLE "Build example" ${ARRAYLIST_IS_TOP_LEVEL})
option(ARRAYLIST_ENABLE_INSTALL "Enable install" ${ARRAYLIST_IS_TOP_LEVEL})
option(ARRAYLIST_ENABLE_SANITIZERS "Enable Sanitizers for test" ON)

# ------------------------------------------------
# Warning Flags (GCC/Clang vs MSVC)
# ------------------------------------------------
if(MSVC)
  set(ARRAYLIST_WARNING_FLAGS /W4)
else()
  set(ARRAYLIST_WARNING_FLAGS -Wall -Wextra -Wpedantic)
endif()

# ------------------------------------------------
# Main Library
# ------------------------------------------------
add_library(arraylist src/arraylist.c)

# Alias con namespace → los consumidores via CPM/FetchContent pueden escribir
# target_link_libraries(mi_app arraylist::arraylist)
add_library(arraylist::arraylist ALIAS arraylist)

target_include_directories(
  arraylist PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
                   $<INSTALL_INTERFACE:include>)

# Flagas only for compile lib
target_compile_options(arraylist PRIVATE ${ARRAYLIST_WARNING_FLAGS})

# Propagates c11 as requirement
target_compile_features(arraylist PUBLIC c_std_11)

# ------------------------------------------------
# Example only built when is main proyect
# ------------------------------------------------
if(ARRAYLIST_BUILD_EXAMPLE)
  add_executable(arraylist_example src/main.c)
  target_link_libraries(arraylist_example PRIVATE arraylist)
endif()

# ------------------------------------------------
# Tests
# ------------------------------------------------
if(ARRAYLIST_BUILD_TESTS)
  enable_testing()
  add_subdirectory(test)
endif()

# ------------------------------------------------
# Installation rules
# ------------------------------------------------
if(ARRAYLIST_ENABLE_INSTALL)
  include(GNUInstallDirs)

  install(
    TARGETS arraylist
    EXPORT arraylistTargets
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

  install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

  install(
    EXPORT arraylistTargets
    FILE arraylistTargets.cmake
    NAMESPACE arraylist::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/arraylist)
endif()
