# wip, a small TCP/IP stack.
# Copyright (C) 2025  Xavier Del Campo Romero
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

cmake_minimum_required(VERSION 3.19)
project(wip LANGUAGES C VERSION 0.0.0)
option(WIP_LOG "Enables logging to stderr")
option(WIP_LOG_CUSTOM "Allows user code to define wip_log. Enables WIP_LOG")
add_library(${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} PUBLIC include
    PRIVATE private_include)

set(compilers
    "GNU"
    "Clang"
    "TinyCC"
)

if(WIP_LOG_CUSTOM)
    set(WIP_LOG ON)
endif()

foreach(c ${compilers})
    if(CMAKE_C_COMPILER_ID STREQUAL ${c})
        set(cflags_np
            -pedantic
            -Wall
        )

        if(CMAKE_BUILD_TYPE STREQUAL "Debug")
            set(cflags_np ${cflags_np} -Og -g)
        else()
            set(cflags_np ${cflags_np} -Os)
        endif()

        break()
    endif()
endforeach()

include(CheckCompilerFlag)

foreach(f ${cflags_np})
    string(REPLACE "-" "_" var supported_${f})
    check_compiler_flag(C ${f} ${var})

    if(${var})
        set(sup_cflags ${sup_cflags} ${f})
    endif()
endforeach()

if(WIP_LOG)
    target_compile_definitions(${PROJECT_NAME} PRIVATE WIP_LOG)
endif()

target_compile_options(${PROJECT_NAME} PRIVATE ${sup_cflags})
set_target_properties(${PROJECT_NAME} PROPERTIES C_STANDARD 90 C_EXTENSIONS OFF)
add_subdirectory(src)
install(TARGETS ${PROJECT_NAME})
