﻿file(GLOB socket CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/websocket/*.cpp)
file(GLOB recorder CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/recorder/*.cpp)

add_library(germanairlinesva_xplugin SHARED
    ${socket}
    ${recorder}
    main.cpp
)

target_include_directories(germanairlinesva_xplugin PRIVATE
    ${CMAKE_SOURCE_DIR}/xplugin/include
    ${CMAKE_SOURCE_DIR}/file/include
    ${CMAKE_SOURCE_DIR}/recorder/include
    ${CMAKE_SOURCE_DIR}/simdata/include
    ${CMAKE_SOURCE_DIR}/websocket/include
    ${CMAKE_SOURCE_DIR}/utilities/include
    ${CMAKE_SOURCE_DIR}/ixwebsocket/include
    ${CMAKE_SOURCE_DIR}/nlohmann/include
    ${CMAKE_SOURCE_DIR}/XPSDK/CHeaders
)

set_target_properties(germanairlinesva_xplugin PROPERTIES
    PREFIX ""
    SUFFIX ".xpl"
)
target_compile_definitions(germanairlinesva_xplugin PRIVATE
    XPLM200
    XPLM210
)
target_compile_options(germanairlinesva_xplugin PRIVATE
    -Wall
    -Wextra
    -pedantic
    -fvisibility=hidden
)
if(DEBUG)
    target_compile_options(germanairlinesva_xplugin PRIVATE
        -g
    )
    target_link_options(germanairlinesva_xplugin PRIVATE
        -g
    )
else()
    target_compile_options(germanairlinesva_xplugin PRIVATE
        -O2
    )
endif()

if(APPLE)
    message("Building for MacOSX Universal into ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}/${BIT}")

    set_target_properties(germanairlinesva_xplugin PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}
        INSTALL_RPATH "@loader_path"
        OUTPUT_NAME "mac"
    )

    target_compile_definitions(germanairlinesva_xplugin PUBLIC
        APL
        XP
    )
    target_compile_options(germanairlinesva_xplugin PRIVATE
        "SHELL:-arch x86_64"
        "SHELL:-arch arm64"
    )
    target_link_options(germanairlinesva_xplugin PRIVATE
        "SHELL:-arch x86_64"
        "SHELL:-arch arm64"
    )
    target_link_libraries(germanairlinesva_xplugin PRIVATE
        ${CMAKE_SOURCE_DIR}/XPSDK/Libraries/Mac/XPLM.framework/XPLM
    )
elseif(UNIX)
    message("Building for Linux ${BIT} into ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}/${BIT}")

    set_target_properties(germanairlinesva_xplugin PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}/${BIT}
        INSTALL_RPATH "\$ORIGIN"
        OUTPUT_NAME "lin"
    )

    target_compile_definitions(germanairlinesva_xplugin PUBLIC
        LIN
        XP
    )
    target_compile_options(germanairlinesva_xplugin PRIVATE
        -nodefaultlibs
    )
elseif(WIN32)
    message("Building for Windows ${BIT} into ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}/${BIT}")

    set_target_properties(germanairlinesva_xplugin PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/X-Plane/${PLUGIN_NAME}/${BIT}
        OUTPUT_NAME "win"
    )

    target_compile_definitions(germanairlinesva_xplugin PUBLIC
        IBM
        XP
    )
    if(DEBUG)
        target_compile_options(germanairlinesva_xplugin PRIVATE
            -gcodeview
        )
        target_link_options(germanairlinesva_xplugin PRIVATE
            -Wl,-pdb=
        )
    endif()
    target_link_libraries(germanairlinesva_xplugin PRIVATE
        ${CMAKE_SOURCE_DIR}/XPSDK/Libraries/Win/XPLM_64.lib
    )
    target_sources(germanairlinesva_xplugin PRIVATE
        xPluginWin.cpp
    )
endif()

target_link_libraries(germanairlinesva_xplugin PRIVATE
    ixwebsocket
    pthread
)
