cmake_minimum_required(VERSION 3.10)
project(pico-tcp)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Add all source files from current directory
set(SOURCES
    main.cpp
    picofunctions.cpp
    pugixml.cpp
)

# Add all source files from src directory
file(GLOB SRC_SOURCES "src/*.cpp")
list(APPEND SOURCES ${SRC_SOURCES})

# Create executable
add_executable(pico-tcp ${SOURCES})

# Include directories
target_include_directories(pico-tcp PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/inc
    "C:\\Program Files\\Pico Technology\\SDK\\inc"
    ${CMAKE_CURRENT_SOURCE_DIR}/libmsgpack11
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)

# Locate ps4000a library (expected to be provided by the PicoScope SDK)
find_library(PS4000A_LIB
    NAMES ps4000a ps4000a.lib
    PATHS "C:\\Program Files\\Pico Technology\\SDK\\lib"
          ENV PATH
    NO_DEFAULT_PATH
)

if(NOT PS4000A_LIB)
    message(FATAL_ERROR "ps4000a library not found.\n"
                       "Download the PicoScope 4000 series SDK and copy the ".
                       "appropriate ps4000a.lib into ${CMAKE_CURRENT_SOURCE_DIR}/libps4000a or ".
                       "set the PS4000A_LIB environment variable to the full path.")
endif()

# Link against the located library
# The variable may already contain the full filename, so don't append .lib
# also pull in WinSock for networking helpers used by SimpleSocket
if(MSVC)
    # WinSock functions live in ws2_32.lib
    add_compile_options(/utf-8)
    target_link_libraries(pico-tcp PRIVATE ${PS4000A_LIB} ws2_32)
elseif(MINGW)
    target_link_libraries(pico-tcp PRIVATE ${PS4000A_LIB} ws2_32)
endif()

# Check for obvious architecture mismatch in the chosen library
if(PS4000A_LIB)
    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        string(FIND "${PS4000A_LIB}" "Win32" _libIs32)
        if(NOT _libIs32 EQUAL -1)
            message(WARNING "ps4000a library appears to be 32‑bit while the target "
                            "architecture is x64; this will lead to unresolved symbols. "
                            "Use the 64‑bit version of ps4000a.lib or set PS4000A_LIB to the "
                            "correct path.")
        endif()
    endif()
endif()

# Set output directories for DLLs
set_target_properties(pico-tcp PROPERTIES
    VS_DEBUGGER_WORKING_DIRECTORY "$<CONFIG>"
)
