project (airdcpp-wingui)
cmake_minimum_required (VERSION 3.16)

find_path(WTL_INCLUDE_DIRS "atlapp.h")

# WebView2 (embedded Edge/Chromium) for the internal browser frame. Under the
# *-windows-static triplets the vcpkg port installs only WebView2LoaderStatic.lib, so the
# imported target is a static lib and FulDC.exe gets no import entry for WebView2Loader.dll.
# That is deliberate: the exe then starts normally on machines with no WebView2 runtime
# installed (the feature just reports itself unavailable), and the installer, the portable
# package and WinUpdater::listUpdaterFiles all stay untouched.
find_package(unofficial-webview2 CONFIG REQUIRED)

# Get files
file (GLOB_RECURSE wingui_hdrs ${PROJECT_SOURCE_DIR}/windows/*.h)
file (GLOB_RECURSE wingui_srcs ${PROJECT_SOURCE_DIR}/windows/*.cpp ${PROJECT_SOURCE_DIR}/windows/*.c)

# Project file generation
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${wingui_srcs})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${wingui_hdrs})

unset(CMAKE_INSTALL_BINDIR)
add_executable(${PROJECT_NAME} WIN32)
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "FulDC")

set(resources
    AirDC.rc
    # Merged into the embedded manifest by mt.exe (cmake -E vs_link_exe).
    # Declares Windows 10/11 supportedOS + Per-Monitor v2 DPI awareness.
    res/AirDC.manifest
)

target_sources(${PROJECT_NAME}
    PRIVATE
        ${wingui_srcs}
        ${wingui_hdrs}
        ${resources}
)


target_include_directories(${PROJECT_NAME}
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/windows
    PUBLIC
        # where top-level project will look for the library's public headers
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)



# VC++ Directories > Include Directories
set(CMAKE_VS_SDK_INCLUDE_DIRECTORIES "$(VC_IncludePath);$(WindowsSDK_IncludePath)")

target_precompile_headers(${PROJECT_NAME} PUBLIC
  "$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/windows/stdafx.h>"
)

set_target_properties(${PROJECT_NAME}
    PROPERTIES
        DEBUG_POSTFIX ${CMAKE_DEBUG_POSTFIX}
        LINK_FLAGS
            "/NODEFAULTLIB:libcmt.lib;kernel32.lib;advapi32.lib;user32.lib;gdi32.lib;shell32.lib;comdlg32.lib;version.lib;mpr.lib;rasapi32.lib;winmm.lib;winspool.lib;vfw32.lib;secur32.lib;oleacc.lib;oledlg.lib;sensapi.lib \
            /MANIFESTDEPENDENCY:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\""
)


target_compile_definitions(${PROJECT_NAME}
    PRIVATE
		"_WTL_NEW_PAGE_NOTIFY_HANDLERS"
        "HAVE_NATUPNP_H"
        "_UNICODE"
        "UNICODE"
)



# LINKING
target_link_libraries (
	${PROJECT_NAME}

	airdcpp
	airdcpp-webapi

	unofficial::webview2::webview2

	WinMM # PlaySound
	powrprof # SetSuspendState
)

# Emit a stripped PDB (public symbols only) alongside the full one. This is the
# symbol file shipped in the updater/portable package: small enough to zip (the
# full PDB is ~200 MB and both bloats downloads and breaks zip creation), yet it
# still lets the in-app crash tracer (ExtendedTrace) resolve function names.
# The full FulDC.pdb stays in the build output for local/line-accurate debugging.
target_link_options(${PROJECT_NAME} PRIVATE
	"/PDBSTRIPPED:$<TARGET_FILE_DIR:${PROJECT_NAME}>/FulDC.stripped.pdb"

	# WebView2LoaderStatic.lib is built against the release static CRT (libcmt), so linking it
	# into the Debug build (libcmtd) raises LNK4098. Drop the release CRT in Debug only --
	# in Release libcmt IS the CRT and excluding it would break the link.
	# NB: the /NODEFAULTLIB:... string in LINK_FLAGS above is a single semicolon-joined token
	# and is therefore inert; do not "fix" it here, that is a separate and riskier change.
	"$<$<CONFIG:Debug>:/NODEFAULTLIB:libcmt.lib>"
)

install(FILES $<TARGET_PDB_FILE:${PROJECT_NAME}> DESTINATION bin OPTIONAL)
install(TARGETS ${PROJECT_NAME})

