cmake_minimum_required(VERSION 3.5)
project(lgtv-audio-cap C)

# Built with the openlgtv arm-webos-linux-gnueabi buildroot SDK:
#
#   source /path/to/arm-webos-linux-gnueabi_sdk-buildroot/environment-setup
#   cmake -B build -DCMAKE_BUILD_TYPE=Release native
#   cmake --build build
#
# The SDK's environment-setup exports CC/SYSROOT and puts its pkg-config in
# front, so luna-service2 and glib resolve to the TV's versions rather than the
# host's.

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

find_package(PkgConfig REQUIRED)
pkg_check_modules(LUNASERVICE REQUIRED luna-service2)
pkg_check_modules(GLIB REQUIRED glib-2.0)
pkg_check_modules(GTHREAD REQUIRED gthread-2.0)
# PmLogLib is how webOS services normally reach the system log. It is optional
# here: the logger writes to stderr, which the service launcher captures.
pkg_check_modules(PMLOG PmLogLib)

add_executable(audiocap-service
  src/main.c
  src/service.c
  src/engine.c
  src/config.c
  src/dsp.c
  src/common/log.c
  src/common/json.c
  src/common/ringbuf.c
  src/capture/capture.c
  src/capture/cap_pulse.c
  src/capture/cap_alsa.c
  src/capture/cap_exec.c
  src/capture/cap_tone.c
  src/net/flatbuf.c
  src/net/hyperion.c
  src/net/streamserv.c
  src/sinks/sink.c
  src/sinks/sink_hyperhdr.c
  src/sinks/sink_hyperhdr_viz.c
  src/sinks/sink_hyperhdr_adjust.c
  src/sinks/sink_udp.c
  src/sinks/sink_tcp.c
  src/sinks/sink_http.c
)

target_include_directories(audiocap-service PRIVATE
  src
  ${LUNASERVICE_INCLUDE_DIRS}
  ${GLIB_INCLUDE_DIRS}
  ${GTHREAD_INCLUDE_DIRS}
  ${PMLOG_INCLUDE_DIRS}
)

target_compile_options(audiocap-service PRIVATE
  -Wall -Wextra -Wno-unused-parameter
  ${LUNASERVICE_CFLAGS_OTHER}
  ${GLIB_CFLAGS_OTHER}
)

target_compile_definitions(audiocap-service PRIVATE
  _GNU_SOURCE
  # Lets LSRegisterPubPriv resolve on firmware older than 3.5 without making
  # the symbol mandatory on newer builds.
  SECURITY_COMPATIBILITY
)

# The audio backends are dlopen'd at runtime, so libpulse and libasound are
# deliberately absent from this list: the service has to start on a TV that has
# neither, report that in the diagnostics, and let the user pick another path.
target_link_libraries(audiocap-service PRIVATE
  ${LUNASERVICE_LIBRARIES}
  ${GLIB_LIBRARIES}
  ${GTHREAD_LIBRARIES}
  ${PMLOG_LIBRARIES}
  pthread
  dl
  m
)

target_link_directories(audiocap-service PRIVATE
  ${LUNASERVICE_LIBRARY_DIRS}
  ${GLIB_LIBRARY_DIRS}
  ${GTHREAD_LIBRARY_DIRS}
  ${PMLOG_LIBRARY_DIRS}
)

if(CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm")
  # webOS 5/6 TVs are Cortex-A9 (and A53 on later chassis, which runs A9 code
  # fine). softfp matches the SDK's ABI.
  target_compile_options(audiocap-service PRIVATE
    -mcpu=cortex-a9 -mfloat-abi=softfp -mfpu=neon -ffast-math
  )
endif()

# Homebrew services are unpacked to an arbitrary directory, so anything we ship
# alongside the binary has to be found relative to it.
set_target_properties(audiocap-service PROPERTIES
  BUILD_RPATH "$ORIGIN"
  INSTALL_RPATH "$ORIGIN"
)

install(TARGETS audiocap-service RUNTIME DESTINATION .)
