cmake_minimum_required(VERSION 3.16)

if (NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    if (DEFINED ENV{VITASDK})
        set(CMAKE_TOOLCHAIN_FILE "$ENV{VITASDK}/share/vita.toolchain.cmake" CACHE PATH "toolchain file")
    else ()
        message(FATAL_ERROR "Please define VITASDK to point to your SDK path!")
    endif ()
endif ()

project(vita_steamlink C)
include("${VITASDK}/share/vita.cmake" REQUIRED)

set(VITA_APP_NAME "Vita Steam Link")
set(VITA_TITLEID "VSLK00001")
set(VITA_VERSION "01.00")

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_EXTENSIONS ON)
if (NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif ()
# Keep assertions in release builds: ihslib checks its buffer bounds and protocol invariants
# with assert(), and a clean stop with a log line beats silent memory corruption. Debug info
# stays in the ELF (the eboot is stripped), so crash dumps can be symbolized.
string(REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
string(REPLACE "-O3" "-O2" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -g")

# protobuf-c runtime (vendored, there is no vdpm package for it)
add_library(protobuf-c STATIC third_party/protobuf-c/protobuf-c/protobuf-c.c)
target_include_directories(protobuf-c PUBLIC third_party/protobuf-c)

# ihslib: Steam Remote Play protocol. Its own CMake project wants pkg-config, SDL2 and a
# host protobuf-c, so the sources are built directly, with the Vita backends from
# src/platform standing in for the SDL/POSIX ones.
set(IHSLIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/ihslib)
file(GLOB_RECURSE IHSLIB_SOURCES CONFIGURE_DEPENDS ${IHSLIB_DIR}/src/*.c)
list(FILTER IHSLIB_SOURCES EXCLUDE REGEX "/src/hid/sdl/|/src/platforms/|/src/crypto/impl_openssl\\.c$")

add_library(ihslib STATIC
        ${IHSLIB_SOURCES}
        src/platform/ihs_thread_vita.c
        src/platform/ihs_net_vita.c
        src/platform/hid_vita.c)
target_include_directories(ihslib PUBLIC ${IHSLIB_DIR}/include PRIVATE ${IHSLIB_DIR}/src)
target_compile_options(ihslib PRIVATE -w)
target_link_libraries(ihslib PUBLIC protobuf-c mbedcrypto pthread m SceNet_stub)

add_executable(${PROJECT_NAME}
        src/main.c
        src/config.c
        src/log.c
        src/video.c
        src/audio.c
        src/input.c
        src/actions.c
        src/settings.c)
target_include_directories(${PROJECT_NAME} PRIVATE src)
# ihslib's public headers carry clang-only pragmas
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wno-unknown-pragmas)
target_link_libraries(${PROJECT_NAME}
        ihslib
        vita2d
        opus
        freetype
        png
        jpeg
        z
        m
        pthread
        SceAppMgr_stub
        SceAudio_stub
        SceCommonDialog_stub
        SceCtrl_stub
        SceDisplay_stub
        SceGxm_stub
        SceLibKernel_stub
        SceNet_stub
        SceNetCtl_stub
        ScePgf_stub
        ScePower_stub
        ScePvf_stub
        SceShellSvc_stub
        SceSysmodule_stub
        SceTouch_stub
        SceVideodec_stub)

vita_create_self(eboot.bin ${PROJECT_NAME})
vita_create_vpk(VitaSteamLink.vpk ${VITA_TITLEID} eboot.bin
        VERSION ${VITA_VERSION}
        NAME ${VITA_APP_NAME}
        FILE sce_sys/icon0.png sce_sys/icon0.png
        FILE sce_sys/pic0.png sce_sys/pic0.png
        FILE sce_sys/livearea/contents/bg0.png sce_sys/livearea/contents/bg0.png
        FILE sce_sys/livearea/contents/startup.png sce_sys/livearea/contents/startup.png
        FILE sce_sys/livearea/contents/template.xml sce_sys/livearea/contents/template.xml)
