cmake_minimum_required(VERSION 3.16)
project(silk VERSION 2.0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

find_package(wxWidgets 3.2 REQUIRED COMPONENTS html core base)
include(${wxWidgets_USE_FILE})
find_package(Threads REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)   # for the log's Ctrl + wheel (GTK scroll signal)

# Everything that does not need wxWidgets: tool handling, parsers, settings,
# terminal mode and the root helper.
add_library(silkcore STATIC src/common.cpp src/parsers.cpp src/helper.cpp src/newconfig.cpp
            src/config.cpp src/cli.cpp src/pkginfo.cpp src/slackbuilds.cpp src/caches.cpp src/revdeps.cpp src/configfiles.cpp src/activitylog.cpp src/merge.cpp src/helptext.cpp src/libcheck.cpp)
target_include_directories(silkcore PUBLIC src)
find_package(ZLIB REQUIRED)   # slackpkg's file lists are gzip files
target_link_libraries(silkcore PUBLIC ZLIB::ZLIB)
target_compile_definitions(silkcore PRIVATE SILK_VERSION="${PROJECT_VERSION}")
target_compile_options(silkcore PRIVATE -Wall -Wextra)

# The logo, compiled into the executable (About dialog, window icon), so it
# shows even when Silk is not installed.
set(LOGO_PNG ${CMAKE_SOURCE_DIR}/media/silk-256.png)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${LOGO_PNG})
file(READ ${LOGO_PNG} LOGO_HEX HEX)
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," LOGO_BYTES "${LOGO_HEX}")
file(WRITE ${CMAKE_BINARY_DIR}/generated/logo_png.h
     "// Generated from media/silk-256.png by CMake; do not edit.\n"
     "static const unsigned char silk_logo_png[] = {${LOGO_BYTES}};\n")

# The single executable: GUI, terminal mode, and root helper (--helper).
add_executable(silk src/main.cpp src/gui.cpp src/helperclient.cpp)
target_link_libraries(silk PRIVATE silkcore ${wxWidgets_LIBRARIES} ${GTK3_LIBRARIES} Threads::Threads)
target_include_directories(silk PRIVATE ${CMAKE_BINARY_DIR}/generated ${GTK3_INCLUDE_DIRS})
target_compile_definitions(silk PRIVATE SILK_VERSION_STRING="${PROJECT_VERSION}")
target_compile_options(silk PRIVATE -Wall -Wextra)

include(GNUInstallDirs)
install(TARGETS silk DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES silk.desktop DESTINATION ${CMAKE_INSTALL_DATADIR}/applications)
install(FILES silk.png DESTINATION ${CMAKE_INSTALL_DATADIR}/pixmaps)
# Menu icons at the usual sizes (16 to 256 px; KDE favourites and panels use the
# small ones), scaled from silk.png (media/icons/).
foreach(size 16 22 24 32 48 64 128 256)
  install(FILES media/icons/silk-${size}.png DESTINATION ${CMAKE_INSTALL_DATADIR}/icons/hicolor/${size}x${size}/apps
          RENAME silk.png)
endforeach()

# The tests (build/silk-tests). Package builds may leave them out with
# -DSILK_BUILD_TESTS=OFF; they are never installed.
option(SILK_BUILD_TESTS "Build the tests" ON)
if(SILK_BUILD_TESTS)
  enable_testing()
  add_executable(silk-tests tests/parsers_test.cpp)
  target_link_libraries(silk-tests PRIVATE silkcore)
  target_compile_definitions(silk-tests PRIVATE SILK_SAMPLES_DIR="${CMAKE_SOURCE_DIR}/tests/samples")
  add_test(NAME parsers COMMAND silk-tests)
endif()
