# ptouch_label — build (libptouch, ptouch-print)
#
# Author: knb
# Email: knb@artif.org

cmake_minimum_required(VERSION 3.16)
project(ptouch_label VERSION 1.0.1 LANGUAGES C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)

include(GNUInstallDirs)

find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBUSB REQUIRED IMPORTED_TARGET libusb-1.0)
find_package(PNG REQUIRED)
pkg_check_modules(LIBRSVG IMPORTED_TARGET librsvg-2.0)

configure_file(
	"${CMAKE_CURRENT_SOURCE_DIR}/include/libptouch_version.h.in"
	"${CMAKE_CURRENT_BINARY_DIR}/include/libptouch_version.h"
	@ONLY
)

set(LIBPTOUCH_SOURCES
	src/lib/libptouch_core.c
	src/lib/libptouch_usb.c
	src/lib/libptouch_protocol.c
	src/lib/libptouch_layout.c
	src/lib/libptouch_media_info.c
	src/lib/libptouch_trim.c
	src/lib/libptouch_print.c
	src/lib/libptouch_status.c
	src/lib/libptouch_png.c
	src/lib/libptouch_svg.c
)

add_library(ptouch STATIC ${LIBPTOUCH_SOURCES})
add_library(ptouch_shared SHARED ${LIBPTOUCH_SOURCES})
set_target_properties(ptouch_shared PROPERTIES OUTPUT_NAME ptouch
	SOVERSION ${PROJECT_VERSION_MAJOR})
target_include_directories(ptouch PUBLIC
	"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
	"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
	"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
target_include_directories(ptouch PRIVATE
	"${CMAKE_CURRENT_SOURCE_DIR}/src/lib"
)
target_include_directories(ptouch_shared PUBLIC
	"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
	"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
	"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
target_include_directories(ptouch_shared PRIVATE
	"${CMAKE_CURRENT_SOURCE_DIR}/src/lib"
)
target_link_libraries(ptouch PRIVATE PkgConfig::LIBUSB PNG::PNG)
target_link_libraries(ptouch_shared PRIVATE PkgConfig::LIBUSB PNG::PNG)
if(LIBRSVG_FOUND)
	target_link_libraries(ptouch PRIVATE PkgConfig::LIBRSVG)
	target_link_libraries(ptouch_shared PRIVATE PkgConfig::LIBRSVG)
	target_compile_definitions(ptouch PRIVATE LIBPTOUCH_HAS_RSVG=1)
	target_compile_definitions(ptouch_shared PRIVATE LIBPTOUCH_HAS_RSVG=1)
endif()
if(NOT MSVC)
	target_compile_options(ptouch PRIVATE -Wall -Wextra -Wpedantic)
	target_compile_options(ptouch_shared PRIVATE -Wall -Wextra -Wpedantic)
endif()

add_executable(ptouch-print src/cli/main.c)
target_include_directories(ptouch-print PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(ptouch-print PRIVATE ptouch PkgConfig::LIBUSB)
if(NOT MSVC)
	target_compile_options(ptouch-print PRIVATE -Wall -Wextra -Wpedantic)
endif()

enable_testing()
add_executable(ptouch-protocol-regression-test
	tests/protocol_regression_test.c
	src/lib/libptouch_protocol.c
)
target_include_directories(ptouch-protocol-regression-test PRIVATE
	"${CMAKE_CURRENT_SOURCE_DIR}/src/lib"
)
if(NOT MSVC)
	target_compile_options(ptouch-protocol-regression-test PRIVATE -Wall -Wextra -Wpedantic)
endif()
add_test(NAME protocol_regression_test COMMAND ptouch-protocol-regression-test)

install(TARGETS ptouch ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")
install(TARGETS ptouch_shared LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
	RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(TARGETS ptouch-print RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(FILES
	"${CMAKE_CURRENT_SOURCE_DIR}/include/libptouch.h"
	"${CMAKE_CURRENT_BINARY_DIR}/include/libptouch_version.h"
	DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
