# Subdirectories

#add_subdirectory(libdev)
#add_subdirectory(projects)

set(GAME_COMPONENT_LIB_TYPE STATIC)

add_subdirectory(version)

set(MACHINES_SOURCES
    projects/machines/machines_main.cpp

    projects/machines/errorhnd.cpp
    projects/machines/errorhnd.hpp
    projects/machines/guistuff.cpp
    projects/machines/leaktrak.cpp
    projects/machines/leaktrak.hpp
    projects/machines/loadplan.cpp

    projects/machines/scrndump.cpp
    projects/machines/scrndump.hpp
    projects/machines/sdlapp.cpp
    projects/machines/sdlapp.hpp
    projects/machines/simstuff.cpp
    projects/machines/sndstuff.cpp
)

set(PLANETED_SOURCES
    projects/planeted/planeted_main.cpp

    projects/planeted/availods.cpp
    projects/planeted/availods.hpp
    projects/planeted/cameras.cpp
    projects/planeted/cameras.hpp
    projects/planeted/camrmrkr.cpp
    projects/planeted/camrmrkr.hpp
    projects/planeted/datas.hpp
    projects/planeted/domain.cpp
    projects/planeted/domain.hpp
    projects/planeted/editactor.cpp
    projects/planeted/editactor.hpp
    projects/planeted/editart.cpp
    projects/planeted/editart.hpp
    projects/planeted/editcamr.cpp
    projects/planeted/editcamr.hpp
    projects/planeted/editconstr.cpp
    projects/planeted/editconstr.hpp
    projects/planeted/editdoma.cpp
    projects/planeted/editdoma.hpp
    projects/planeted/editmach.cpp
    projects/planeted/editmach.hpp
    projects/planeted/editmode.cpp
    projects/planeted/editmode.hpp
    projects/planeted/editobst.cpp
    projects/planeted/editobst.hpp
    projects/planeted/editpoly.cpp
    projects/planeted/editpoly.hpp
    projects/planeted/editport.cpp
    projects/planeted/editport.hpp
    projects/planeted/edittile.cpp
    projects/planeted/edittile.hpp
    projects/planeted/errorhnd.cpp
    projects/planeted/errorhnd.hpp
    projects/planeted/keyboard.cpp
    projects/planeted/leaktrak.cpp
    projects/planeted/leaktrak.hpp
    projects/planeted/mapbmp.cpp
    projects/planeted/mapbmp.hpp

    projects/planeted/mpreload.cpp
    projects/planeted/mpreload.hpp
    projects/planeted/obstacle.cpp
    projects/planeted/obstacle.hpp
    projects/planeted/planeted.cpp
    projects/planeted/planeted.hpp
    projects/planeted/polygon.cpp
    projects/planeted/polygon.hpp
    projects/planeted/portal.cpp
    projects/planeted/portal.hpp
    projects/planeted/race.cpp
    projects/planeted/race.hpp
    projects/planeted/scenfile.cpp
    projects/planeted/scenfile.hpp
    projects/planeted/sdlapp.cpp
    projects/planeted/sdlapp.hpp
    projects/planeted/strings.hpp
    projects/planeted/testtibo.cpp
    projects/planeted/testtibo.hpp
    projects/planeted/tilemrkr.cpp
    projects/planeted/tilemrkr.hpp
    projects/planeted/vetxmrkr.cpp
    projects/planeted/vetxmrkr.hpp
    projects/planeted/vpolymark.cpp
    projects/planeted/vpolymark.hpp
    projects/planeted/wdebug.cpp
    projects/planeted/wdebug.hpp
)

if(PLATFORM_WINDOWS)
  set(MACHINES_SOURCES
      ${MACHINES_SOURCES}
      # Exe icon and meta info
      ${PROJECT_SOURCE_DIR}/windows/machines.rc
  )
  set(PLANETED_SOURCES
      ${PLANETED_SOURCES}
      ${PROJECT_SOURCE_DIR}/windows/planeted.rc
  )
endif()

# Includes
set(LOCAL_INCLUDES
    ${CMAKE_CURRENT_SOURCE_DIR}/libdev
    ${CMAKE_CURRENT_SOURCE_DIR}/projects
)

if(TARGET ffmpeg::swscale)
    list(APPEND LIBS ffmpeg::swscale)
endif()

include_directories(${LOCAL_INCLUDES})

# Targets
add_subdirectory(libdev/afx)
add_subdirectory(libdev/base)
add_subdirectory(libdev/crashdump)
add_subdirectory(libdev/ctl)
add_subdirectory(libdev/ftl)
add_subdirectory(libdev/mathex)
add_subdirectory(libdev/graph)
add_subdirectory(libdev/gxin)
add_subdirectory(libdev/xin)
add_subdirectory(libdev/phys)
add_subdirectory(libdev/stdlib)
add_subdirectory(libdev/profiler)
add_subdirectory(libdev/system)
add_subdirectory(libdev/device)
add_subdirectory(libdev/utility)
add_subdirectory(libdev/sound)
add_subdirectory(libdev/recorder)
add_subdirectory(libdev/world4d)
add_subdirectory(libdev/network)
add_subdirectory(libdev/render)
add_subdirectory(libdev/window_support/sdl)
add_subdirectory(libdev/gui)
add_subdirectory(libdev/sim)
add_subdirectory(libdev/machphys)
add_subdirectory(libdev/envirnmt)
add_subdirectory(libdev/ani)
add_subdirectory(libdev/machlog)
add_subdirectory(libdev/machgui)

add_library(libs INTERFACE)
target_sources(libs

    INTERFACE 
    $<TARGET_OBJECTS:utility>
    $<TARGET_OBJECTS:profiler>
    $<TARGET_OBJECTS:device>
    $<TARGET_OBJECTS:gxin>
    $<TARGET_OBJECTS:xin>
    $<TARGET_OBJECTS:stdlib>
    $<TARGET_OBJECTS:phys>
    $<TARGET_OBJECTS:recorder>
    $<TARGET_OBJECTS:envirnmt>
)

target_link_libraries(libs INTERFACE
    LibDev::afx
    LibDev::crashdump
    LibDev::network
    LibDev::render
    LibDev::mathex
    LibDev::machgui
    LibDev::machlog
    LibDev::machphys
    LibDev::sound
    LibDev::system
    LibDev::world4d
    LibDev::version
)

# Strip the release binaries. `-s` is a GNU linker option; the MSVC-style
# linkers (link.exe, lld-link) keep the symbols in a separate .pdb anyway.
#
# Where the debug information can be split out instead, it is: split_debug_info()
# does the stripping itself, after taking a copy of what `-s` would have
# discarded. Doing both would leave nothing to split.
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT MACHINES_CLANG_CL))
    if(NOT MACHINES_SPLIT_DEBUG_INFO)
        set(MACHINES_STRIP_LINK_FLAGS -s)
    endif()
endif()

add_executable(machines WIN32
    ${MACHINES_SOURCES}
)

set_target_properties(machines PROPERTIES LINK_FLAGS_RELEASE "${MACHINES_STRIP_LINK_FLAGS}")
set_target_properties(machines PROPERTIES
    COMPILE_FLAGS "${MACHINES_COMPILER_BIT_MODE}" LINK_FLAGS "${MACHINES_COMPILER_BIT_MODE}"
)
target_link_libraries(machines
    libs
    LibDev::window_support_sdl
)

split_debug_info(machines)

if(MSVC)
    # The application manifest is embedded via machines.rc; keep the linker
    # from embedding its own default manifest with the same resource id
    # (CVTRES error CVT1100: duplicate resource).
    target_link_options(machines PRIVATE "/MANIFEST:NO")
endif()

if(BUILD_PLANETED)
    add_executable(planeted WIN32
        ${PLANETED_SOURCES}
    )
    set_target_properties(planeted PROPERTIES LINK_FLAGS_RELEASE "${MACHINES_STRIP_LINK_FLAGS}")
    set_target_properties(planeted PROPERTIES
        COMPILE_FLAGS "${MACHINES_COMPILER_BIT_MODE}" LINK_FLAGS "${MACHINES_COMPILER_BIT_MODE}"
    )
    target_link_libraries(planeted
        libs
        LibDev::window_support_sdl
    )

    split_debug_info(planeted)
endif()
