add_library(libSceSystemService SHARED EXCLUDE_FROM_ALL Export.cpp)
target_include_directories(libSceSystemService PRIVATE ${LIBS_INCLUDE_DIR})
configure_windows_unwind(libSceSystemService)

if(MINGW)
    foreach(runtimeLinkFlag IN ITEMS -static-libgcc -static-libstdc++ -static)
        string(REPLACE "${runtimeLinkFlag}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
    endforeach()
endif()

if(BUILD_TESTING)
    add_test_executable(guest_system_exit_tests ${LIBS_INCLUDE_DIR}/tests/GuestSystemExit.cpp)
    target_include_directories(guest_system_exit_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_system_exit_tests PRIVATE libSceSystemService libc)
    configure_windows_unwind(guest_system_exit_tests)
    add_test(NAME guest_system_exit_parameters COMMAND guest_system_exit_tests)
    add_test(NAME guest_system_exit COMMAND guest_system_exit_tests --exit)
    add_test(NAME guest_quick_exit COMMAND guest_system_exit_tests --quick-exit)
    add_test(NAME guest_immediate_exit COMMAND guest_system_exit_tests --immediate)
    add_test(NAME guest_return_from_main COMMAND guest_system_exit_tests --return-from-main)
    set_tests_properties(guest_system_exit PROPERTIES
        PASS_REGULAR_EXPRESSION "Guest shutdown and atexit completed")
    set_tests_properties(guest_quick_exit PROPERTIES
        PASS_REGULAR_EXPRESSION "Guest quick_exit handlers completed")
    if(WIN32)
        set_tests_properties(guest_system_exit guest_system_exit_parameters guest_immediate_exit guest_return_from_main guest_quick_exit PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
    endif()
    add_test_executable(guest_system_service_tests ${LIBS_INCLUDE_DIR}/tests/GuestSystemService.cpp)
    target_include_directories(guest_system_service_tests PRIVATE ${LIBS_INCLUDE_DIR})
    target_link_libraries(guest_system_service_tests PRIVATE libSceSystemService libc)
    configure_windows_unwind(guest_system_service_tests)
    add_test(NAME guest_system_service COMMAND guest_system_service_tests)
    if(WIN32)
        set_tests_properties(guest_system_service PROPERTIES ENVIRONMENT_MODIFICATION
            "PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
    endif()
endif()
