cmake_minimum_required(VERSION 3.0)

if("$ENV{PSXSDK_PATH}" STREQUAL "")
    message(FATAL_ERROR "Please set PSXSDK_PATH env variable first")
endif()

set(CMAKE_C_COMPILER psx-gcc)
set(CMAKE_CXX_COMPILER psx-g++)
set(CMAKE_AR mipsel-unknown-elf-ar)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_CROSSCOMPILING 1)

project(pinboid C)

add_executable(${PROJECT_NAME} "src/main.c")

set(modules
    "res"
    "src/game"
    "src/gfx"
    "src/system"
)

set(mode pal) # pal or ntsc

foreach(c ${modules})
    add_subdirectory(${c})
endforeach()

target_link_libraries(${PROJECT_NAME} PUBLIC init game)

target_link_directories(${PROJECT_NAME} PUBLIC $ENV{PSXSDK_PATH}/lib)
target_compile_options(${PROJECT_NAME} PUBLIC -DFIXMATH_FAST_SIN -D_PAL_MODE_
    -DPSXSDK_DEBUG -DNO_CDDA -DNO_INTRO -Wall -g3 -Og -ffunction-sections
    -fdata-sections)
target_link_libraries(${PROJECT_NAME} PUBLIC -lpsx -lfixmath)
target_include_directories(${PROJECT_NAME} PRIVATE . $ENV{PSXSDK_PATH}/include)
set(cdroot ${CMAKE_SOURCE_DIR}/cdimg)
add_custom_target(exe ALL elf2exe ${PROJECT_NAME}
    ${cdroot}/${PROJECT_NAME}.exe -mark="A homebrew game created with PSXSDK"
    DEPENDS ${PROJECT_NAME})
add_custom_target(iso ALL mkisofs -o ${PROJECT_NAME}.iso -V ${PROJECT_NAME}
    -sysid PLAYSTATION ${cdroot} DEPENDS exe)
set(license $ENV{PSXSDK_PATH}/share/licenses/infoeur.dat)
add_custom_target(bin_cue ALL mkpsxiso ${PROJECT_NAME}.iso ${PROJECT_NAME}.bin
    ${license} -s DEPENDS iso)
