cmake_minimum_required(VERSION 3.25)
project(leetcode C CXX)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(GTEST_VERSION 1.17.0)
enable_testing()

if (MSVC)
    message(FATAL_ERROR "MSVC is not supported. Please use GCC or Clang.")
endif ()

include_directories(include)

include(FetchContent)

add_compile_options(
        -Wall
        -Wextra
)

if (UNIX)
    add_compile_options(
            -fsanitize=address
            -fsanitize=undefined
    )
    add_link_options(
            -fsanitize=address
            -fsanitize=undefined
            -lm
    )
endif ()

FetchContent_Declare(
        googletest
        URL https://gh-proxy.org/https://github.com/google/googletest/releases/download/v${GTEST_VERSION}/googletest-${GTEST_VERSION}.tar.gz
)

FetchContent_MakeAvailable(googletest)

file(GLOB SRC_FILES "src/*.c")

set(EXECUTABLE_SOURCES
        "${CMAKE_SOURCE_DIR}/src/13.c"
        "${CMAKE_SOURCE_DIR}/src/88.c"
        "${CMAKE_SOURCE_DIR}/src/100.c"
        "${CMAKE_SOURCE_DIR}/src/101.c"
        "${CMAKE_SOURCE_DIR}/src/104.c"
        "${CMAKE_SOURCE_DIR}/src/108.c"
        "${CMAKE_SOURCE_DIR}/src/110.c"
        "${CMAKE_SOURCE_DIR}/src/111.c"
        "${CMAKE_SOURCE_DIR}/src/112.c"
        "${CMAKE_SOURCE_DIR}/src/136.c"
        "${CMAKE_SOURCE_DIR}/src/141.c"
        "${CMAKE_SOURCE_DIR}/src/144.c"
        "${CMAKE_SOURCE_DIR}/src/145.c"
        "${CMAKE_SOURCE_DIR}/src/160.c"
        "${CMAKE_SOURCE_DIR}/src/168.c"
        "${CMAKE_SOURCE_DIR}/src/169.c"
        "${CMAKE_SOURCE_DIR}/src/171.c"
        "${CMAKE_SOURCE_DIR}/src/190.c"
        "${CMAKE_SOURCE_DIR}/src/191.c"
        "${CMAKE_SOURCE_DIR}/src/202.c"
        "${CMAKE_SOURCE_DIR}/src/225.c"
        "${CMAKE_SOURCE_DIR}/src/228.c"
        "${CMAKE_SOURCE_DIR}/src/231.c"
        "${CMAKE_SOURCE_DIR}/src/263.c"
        "${CMAKE_SOURCE_DIR}/src/268.c"
        "${CMAKE_SOURCE_DIR}/src/278.c"
        "${CMAKE_SOURCE_DIR}/src/338.c"
        "${CMAKE_SOURCE_DIR}/src/374.c"
        "${CMAKE_SOURCE_DIR}/src/2786.c"
        "${CMAKE_SOURCE_DIR}/src/3110.c"
)

foreach (src_file IN LISTS EXECUTABLE_SOURCES)
    get_filename_component(exe_name "${src_file}" NAME_WE)
    add_executable(${exe_name} ${src_file})
endforeach ()

set(LIBRARY_SOURCES)

foreach (SRC_FILE ${SRC_FILES})
    if (NOT SRC_FILE IN_LIST EXECUTABLE_SOURCES)
        list(APPEND LIBRARY_SOURCES ${SRC_FILE})
    endif ()
endforeach ()

add_library(leetcode STATIC ${LIBRARY_SOURCES})

add_subdirectory(tests)
