# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: Copyright (C) 2026 Max A. Vilkki a.k.a. codise
#
# hardsid: the HardSID 4U driver (DESIGN §7.3). `protocol` is the wire format (constants,
# package encoder, status parser) with no I/O; `transport` is the thin USB seam; `device`
# is the open sequence, the fill rule and the failure rules over that seam; `sid_device` is
# that device as core/sidout's SidDevice, for the scheduler; `provider` is what the SID-out
# service gets its unit from (the USB host, made on the service's thread).
#
# The seam has two real sides. macOS uses IOKit directly (transport_iokit.cpp): libusb's
# interface claim panicked the macOS 26.6.2 kernel with a 4U (docs/hardsid-usb.md).
# Everything else uses libusb (transport_libusb.cpp, the only file that includes libusb.h).

add_library(flipsid_hardsid STATIC
    protocol.cpp
    transport.cpp
    device.cpp
    provider.cpp
    sid_device.cpp
)
if(APPLE)
    target_sources(flipsid_hardsid PRIVATE transport_iokit.cpp)
else()
    target_sources(flipsid_hardsid PRIVATE transport_libusb.cpp)
endif()
if(WIN32)
    target_sources(flipsid_hardsid PRIVATE windows_usb.cpp)
endif()
add_library(flipsid::hardsid ALIAS flipsid_hardsid)

target_include_directories(flipsid_hardsid PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")

# protocol.h takes sidout's DeviceOp. libusb stays private: no libusb type is in a header,
# and a user of this library that never calls createUsbHost() links none of it.
target_link_libraries(flipsid_hardsid PUBLIC flipsid::sidout)
if(APPLE)
    target_link_libraries(flipsid_hardsid PRIVATE "-framework IOKit" "-framework CoreFoundation")
else()
    target_link_libraries(flipsid_hardsid PRIVATE ext::libusb)
endif()
if(WIN32)
    # The units libusb can't see, the sound cards beside them, and the 1 ms timer while a
    # unit is open (windows_usb.cpp).
    target_link_libraries(flipsid_hardsid PRIVATE cfgmgr32 winmm)
endif()

flipsid_configure_target(flipsid_hardsid)
