cmake_minimum_required(VERSION 3.15)

project(keypme_card_management)

find_package(Java COMPONENTS Development)
if (NOT ${Java_FOUND})
    message(WARNING "No Java development environment found.")
endif()

# Build type: If invalid then we set DEBUG build by default
if (NOT (CMAKE_BUILD_TYPE MATCHES "Debug|Release"))
    if (NOT CMAKE_BUILD_TYPE)
        message(STATUS "CMAKE_BUILD_TYPE not set, defining it by default to Debug, and caching this setting.")
    else()
        message(SEND_ERROR "Invalid CMAKE_BUILD_TYPE.")
    endif()
    set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo" FORCE)
endif()

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    if ((NOT WIN32) AND (NOT APPLE) AND (NOT ANDROID))
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage -pg")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage --coverage -pg")
        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
        set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
    endif()
elseif (CMAKE_BUILD_TYPE STREQUAL "Release")
    add_compile_options(-Werror)
endif()

if(SANITIZE)
    add_compile_options(-fsanitize=undefined -fsanitize=address -fsanitize=bounds -fsanitize=alignment)
    add_link_options(-fsanitize=undefined -fsanitize=address -fsanitize=bounds -fsanitize=alignment)
endif()

add_custom_target(generate_protobuf ALL
    COMMAND ./generate_java_proto.sh
    COMMAND ./generate_python_proto.sh
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/protocol/keypme_card_management.proto
    COMMENT "Generate protobuf files for Java and Python")

add_subdirectory(client)
