cmake_minimum_required(VERSION 3.14)
include(GenerateExportHeader)
include(FetchContent)

FetchContent_Declare(json
  GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
  GIT_TAG v3.9.1)

FetchContent_GetProperties(json)
if(NOT json_POPULATED)
  FetchContent_Populate(json)
  add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()



project(R66)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)

# this makefile compiles with marshalling and C API, which requires UTF-16 strings
add_compile_definitions(TWITCHSDK_STR_U16STRING)

# get version number via git
find_package(Git QUIET)
execute_process(COMMAND "${GIT_EXECUTABLE}" describe --always
                WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
                RESULT_VARIABLE res
                OUTPUT_VARIABLE COMMIT
                OUTPUT_STRIP_TRAILING_WHITESPACE)
if (NOT res EQUAL 0)
    set(COMMIT "unknown")
endif()

# single source of truth for the SDK/plugin version (see set-version.ps1 at the repo root)
file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" TWITCHSDK_VERSION LIMIT_COUNT 1)
string(STRIP "${TWITCHSDK_VERSION}" TWITCHSDK_VERSION)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/VERSION")
message(STATUS "TwitchSDK version: ${TWITCHSDK_VERSION} (${COMMIT})")


option(BUILD_DOC "Build documentation" OFF)
option(DONT_COMPILE "Don't compile code (useful for building only the documentation)" OFF)
if (NOT DONT_COMPILE)
    set(VERSION_IN ${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp.in)
    set(VERSION_OUT ${CMAKE_CURRENT_BINARY_DIR}/version.cpp)
    configure_file(${VERSION_IN} ${VERSION_OUT} @ONLY)
    add_library(R66 SHARED ${VERSION_OUT} "src/twitchsdk.cpp" "include/twitchsdk.hpp" "include/pal.hpp"  "src/c_api.cpp" "src/c_api.hpp" "src/pal_proxy.cpp" "src/marshalling.hpp" "src/strings.cpp" "include/twitchsdk_strings.hpp" "src/twitchsdk_json.hpp" "src/marshalling_autogenerated.hpp" "src/marshalling.cpp" "src/urlencode.hpp" "src/urlencode.cpp" "include/pal_autogenerated.hpp" "src/pal_autogenerated.cpp" "src/twitchsdk_websockets.hpp" "src/twitchsdk_tasks.hpp" "src/c_api_autogenerated.hpp" "src/c_api_autogenerated.cpp" "include/twitchsdk_structs_autogenerated.hpp" "src/twitchsdk_auth.cpp" "src/twitchsdk_polls.cpp" "src/twitchsdk_es.cpp" "src/twitchsdk_userinfo.cpp" "src/twitchsdk_streaminfo.cpp" "src/twitchsdk_misc.cpp" "src/twitchsdk_predictions.cpp" "include/twitchsdkapi_autogenerated.hpp" "src/twitchsdk_impl.hpp"  "src/twitchsdk_channelpoints.cpp" "src/twitchsdk_drops.cpp")
    generate_export_header(R66)
    target_include_directories(R66 PUBLIC
      $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
      $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/>
    #  $<INSTALL_INTERFACE:include/r66>
    )
    target_link_libraries(R66 PRIVATE nlohmann_json::nlohmann_json)
endif (NOT DONT_COMPILE)

find_package(Doxygen)
if (BUILD_DOC)
	if (DOXYGEN_FOUND)
        set(DOXYGEN_IN ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
        set(DOXYGEN_OUT ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)

        # request to configure the file
        configure_file(${DOXYGEN_IN} ${DOXYGEN_OUT} @ONLY)
        message("Doxygen build started")

        # note the option ALL which allows to build the docs together with the application
        add_custom_target( docs ALL
            COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_OUT}
            WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
            COMMENT "Generating API documentation with Doxygen"
            VERBATIM )
    else (DOXYGEN_FOUND)
	  message("Doxygen need to be installed to generate the doxygen documentation")
	endif (DOXYGEN_FOUND)
endif (BUILD_DOC)
