# Embed some git version information in the binary.
set(GIT_PRE_CONFIGURE_FILE "gitinfo.cpp.in")
set(GIT_POST_CONFIGURE_FILE "${CMAKE_CURRENT_BINARY_DIR}/gitinfo.cpp")
include(GitWatcher)

set(COMMON_SRC
    ${GIT_POST_CONFIGURE_FILE}
    _diptabl.cpp
    alloc.cpp
    auduncmp.cpp
    b64pipe.cpp
    b64straw.cpp
    base64.cpp
    bfiofile.cpp
    blowfish.cpp
    blowpipe.cpp
    blwstraw.cpp
    buff.cpp
    buffer.cpp
    buffglbl.cpp
    ccfile.cpp
    cdfile.cpp
    cliprect.cpp
    combuf.cpp
    control.cpp
    crc.cpp
    crew.cpp
    cstraw.cpp
    debugstring.cpp
    delay.cpp
    dipthong.cpp
    drawbuff.cpp
    drawline.cpp
    drawmisc.cpp
    face.cpp
    fading.cpp
    field.cpp
    file.cpp
    filepcx.cpp
    fixed.cpp
    font.cpp
    gadget.cpp
    getshape.cpp
    graphicsviewport.cpp
    hsv.cpp
    iff.cpp
    ini.cpp
    int.cpp
    internet.cpp
    irandom.cpp
    keybuff.cpp
    keyframe.cpp
    lcw.cpp
    lcwpipe.cpp
    lcwstraw.cpp
    linear.cpp
    link.cpp
    load.cpp
    memrev.cpp
    misc.cpp
    mixfile.cpp
    multiplayer_access_code.cpp
    mp.cpp
    newdel.cpp
    packet.cpp
    palette.cpp
    palettec.cpp
    paths.cpp
    pipe.cpp
    pk.cpp
    pkpipe.cpp
    pkstraw.cpp
    ramfile.cpp
    random.cpp
    rawfile.cpp
    readline.cpp
    rect.cpp
    rgb.cpp
    rndstraw.cpp
    settings.cpp
    sha.cpp
    shape.cpp
    shapipe.cpp
    shastraw.cpp
    soscodec.cpp
    stamp.cpp
    straw.cpp
    timer.cpp
    timerdwn.cpp
    tobuff.cpp
    toggle.cpp
    vector.cpp
    winasm.cpp
    windows.cpp
    winhide.cpp
    winstub.cpp
    wsa.cpp
    xordelta.cpp
    xpipe.cpp
    xstraw.cpp
)

if (WIN32)
    list(APPEND COMMON_SRC file_win.cpp paths_win.cpp)
else()
    list(APPEND COMMON_SRC file_posix.cpp paths_posix.cpp)
endif()

set(COMMONR_SRC
    framelimit.cpp
    gbuffer.cpp
    interpal.cpp
    soundio_null.cpp
    video_null.cpp
    wwkeyboard.cpp
    wwkeyboard_win32.cpp
    wwmouse.cpp
)

set(COMMONV_SRC
    app_lifecycle.cpp
    framelimit.cpp
    gbuffer.cpp
    hd_asset_pack.cpp
    interpal.cpp
    ipados_layout.cpp
    install_transaction.cpp
    iso9660.cpp
    recovery_transaction.cpp
    savegame_transfer.cpp
    unvqbuff.cpp
    vqaconfig.cpp
    vqadrawer.cpp
    vqaloader.cpp
    vqapalette.cpp
    vqatask.cpp
    vqaver.cpp
    wwkeyboard.cpp
    wwmouse.cpp
)

if(ANDROID_PORT AND SDL2)
    list(APPEND COMMONV_SRC
        android_sdl_audio_engine.cpp
        soundio_common.cpp
        soundio_sdl2.cpp
        vqaaudio_sdl2.cpp
    )
elseif(IPADOS_PORT OR MACOS_PORT)
    list(APPEND COMMONV_SRC
        audio_queue_state.cpp
        ipados_audio_engine.mm
        ipados_palette_renderer.mm
        soundio_common.cpp
        soundio_avengine.mm
        vqaaudio_avengine.mm
    )
elseif(DSOUND)
    list(APPEND COMMONV_SRC soundio.cpp vqaaudio_dsound.cpp)
    list(APPEND VANILLA_LIBS dsound)
elseif(OPENAL)
    list(APPEND COMMONV_SRC soundio_common.cpp soundio_openal.cpp vqaaudio_openal.cpp)
else()
    list(APPEND COMMONV_SRC soundio_null.cpp vqaaudio_null.cpp)
endif()

if(ANDROID_PORT)
    list(APPEND COMMONV_SRC
        android_diagnostics.cpp
        android_quest_events.cpp
        android_quest_presentation.cpp
        android_palette_renderer.cpp
    )
endif()

if(DDRAW)
    list(APPEND COMMONV_SRC video_ddraw.cpp wwkeyboard_win32.cpp)
    list(APPEND VANILLA_LIBS ddraw)
elseif(SDL2)
    list(APPEND COMMONV_SRC video_sdl2.cpp wwkeyboard_sdl2.cpp)
elseif(SDL1)
    list(APPEND COMMONV_SRC video_sdl1.cpp wwkeyboard_sdl1.cpp)
else()
    list(APPEND COMMONV_SRC video_null.cpp)
endif()

if(DSOUND OR DDRAW)
    list(APPEND VANILLA_LIBS dxguid)
endif()

if(NETWORKING)
    list(APPEND COMMONV_SRC
        connect.cpp
        ipxaddr.cpp
        wsproto.cpp
        wspudp.cpp
    )
endif()

file(GLOB_RECURSE COMMON_HEADERS "*.h")

add_library(common STATIC ${COMMON_SRC} ${COMMON_HEADERS})
target_link_libraries(common PUBLIC ${COMMON_LIBS})
target_include_directories(common PUBLIC .)
target_compile_definitions(common PRIVATE FIXIT_FAST_LOAD $<$<CONFIG:Debug>:_DEBUG>)
target_compile_options(common PUBLIC ${VC_CXX_FLAGS})
if(IPADOS_PORT)
    target_compile_definitions(common PRIVATE IPADOS_PORT)
    # CMake 4.4 may leave the compiler feature table empty with the Xcode 26
    # iOS generator. The project-wide CXX_STANDARD setting still emits C++11.
    set_target_properties(common PROPERTIES CXX_STANDARD 11 CXX_STANDARD_REQUIRED YES)
else()
    target_compile_features(common PUBLIC cxx_std_11)
endif()
if(MACOS_PORT)
    target_compile_definitions(common PRIVATE MACOS_PORT)
endif()
if(ANDROID_PORT)
    # SettingsClass is compiled into the shared common archive. Keep the
    # Android-only INI namespace and Quest startup policy active there rather
    # than only on commonv/TiberianDawn consumers.
    target_compile_definitions(common PRIVATE ANDROID_PORT)
endif()
# Make build check state of git to check for uncommitted changes.
add_dependencies(common check_git)

if(WIN32)
    if(WIN9X)
        target_compile_definitions(common PUBLIC _WIN32_WINNT=0x400)
    else()
        target_compile_definitions(common PUBLIC UNICODE _UNICODE _WIN32_WINNT=0x501)
    endif()
endif()

if(BUILD_REMASTERTD OR BUILD_REMASTERRA)
    add_library(commonr STATIC ${COMMONR_SRC})
    target_compile_definitions(commonr PUBLIC $<$<CONFIG:Debug>:_DEBUG> ${REMASTER_DEFS})
    target_link_libraries(commonr PUBLIC common ${REMASTER_LIBS})
endif()

# These options currently require directx components from the aug 2007 SDK.
if(BUILD_VANILLATD OR BUILD_VANILLARA OR BUILD_TESTS)
    add_library(commonv STATIC ${COMMONV_SRC})
    target_compile_definitions(commonv PUBLIC $<$<CONFIG:Debug>:_DEBUG> ${VANILLA_DEFS})
    target_link_libraries(commonv PUBLIC common ${VANILLA_LIBS})
    if(DSOUND)
        target_compile_definitions(commonv PUBLIC DSOUND_BUILD)
    endif()
    if(OPENAL)
        target_compile_definitions(commonv PUBLIC OPENAL_BUILD)
    endif()
    if(IPADOS_PORT OR MACOS_PORT)
        target_compile_definitions(commonv PUBLIC AVAUDIOENGINE_BUILD)
        set_target_properties(commonv PROPERTIES XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC "YES")
    endif()
    if(SDL2)
        target_compile_definitions(commonv PUBLIC SDL_BUILD SDL2_BUILD NEW_VIDEO_BUILD)
    endif()
    if(SDL1)
        target_compile_definitions(commonv PUBLIC SDL_BUILD SDL1_BUILD NEW_VIDEO_BUILD)
    endif()
    if(NETWORKING)
        target_compile_definitions(commonv PUBLIC WINSOCK_IPX NETWORKING)
    endif()
endif()
