# gparted-engined: the GParted for ClawBox root daemon.
# Our front end (src/) compiled with the upstream non-UI GParted 1.5.0 sources.
# GPL-2.0-or-later (it links GParted).
#
#   cmake -S engine -B build/engine && cmake --build build/engine -j2
#   xvfb-run -a ctest --test-dir build/engine --output-on-failure
cmake_minimum_required(VERSION 3.16)
project(clawbox_gparted_engine CXX)

set(ENGINE_VERSION "0.1.0")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()

set(VENDOR ${CMAKE_CURRENT_SOURCE_DIR}/../vendor/gparted-1.5.0)
set(GP ${CMAKE_CURRENT_BINARY_DIR}/gparted)

# --- Upstream sources: a patched copy in the build dir (vendor/ stays untouched) --------
file(GLOB PATCHES ${CMAKE_CURRENT_SOURCE_DIR}/patches/*.patch)
list(SORT PATCHES)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${PATCHES})
file(REMOVE_RECURSE ${GP})
file(COPY ${VENDOR}/src ${VENDOR}/include DESTINATION ${GP})
foreach(p ${PATCHES})
  execute_process(COMMAND patch -p1 --forward --batch --silent -i ${p}
                  WORKING_DIRECTORY ${GP} RESULT_VARIABLE rc OUTPUT_VARIABLE out ERROR_VARIABLE err)
  if(NOT rc EQUAL 0)
    message(FATAL_ERROR "applying ${p} failed: ${out}${err}")
  endif()
  message(STATUS "applied ${p}")
endforeach()
file(WRITE ${GP}/config.h
"/* config.h for the GParted core inside gparted-engined (written by engine/CMakeLists.txt) */
#define PACKAGE \"gparted\"
#define VERSION \"1.5.0\"
#define PACKAGE_VERSION \"1.5.0\"
#define GETTEXT_PACKAGE \"gparted\"
#define ENABLE_NLS 1
#define HAVE_LIBPARTED_FS_RESIZE 1
#define ENABLE_ONLINE_RESIZE 1
")

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTKMM REQUIRED IMPORTED_TARGET gtkmm-3.0)
pkg_check_modules(GTHREAD REQUIRED IMPORTED_TARGET gthread-2.0)
pkg_check_modules(PARTED REQUIRED IMPORTED_TARGET libparted)
pkg_check_modules(PARTEDFS REQUIRED IMPORTED_TARGET libparted-fs-resize)
pkg_check_modules(UUID REQUIRED IMPORTED_TARGET uuid)
find_path(NLOHMANN_JSON_INCLUDE nlohmann/json.hpp REQUIRED)

# The non-UI list of vendor tests/Makefile.am (gparted_core_OBJECTS) plus the other
# Operation*.cc the engine builds queues from.
set(CORE_NAMES
  BCache_Info BlockSpecial CopyBlocks DMRaid Device FS_Info FileSystem GParted_Core
  LUKS_Info LVM2_PV_Info Mount_Info Operation OperationCopy OperationDetail Partition
  PartitionLUKS PartitionVector PasswordRAMStore PipeCapture Proc_Partitions_Info
  ProgressBar SupportedFileSystems SWRaid_Info Utils btrfs exfat ext2 f2fs fat16 hfs
  hfsplus jfs linux_swap luks lvm2_pv minix nilfs2 ntfs reiser4 reiserfs udf xfs
  OperationChangeUUID OperationCheck OperationCreate OperationDelete OperationFormat
  OperationLabelFileSystem OperationNamePartition OperationResizeMove)
set(CORE_SOURCES)
foreach(n ${CORE_NAMES})
  list(APPEND CORE_SOURCES ${GP}/src/${n}.cc)
endforeach()

add_library(gpcore STATIC ${CORE_SOURCES})
target_include_directories(gpcore SYSTEM PUBLIC ${GP}/include ${GP})
target_compile_definitions(gpcore PUBLIC HAVE_CONFIG_H)
target_compile_options(gpcore PRIVATE -w)  # upstream code, upstream warnings
target_link_libraries(gpcore PUBLIC PkgConfig::GTKMM PkgConfig::GTHREAD PkgConfig::PARTED
                                    PkgConfig::PARTEDFS PkgConfig::UUID)

# --- Our front end ----------------------------------------------------------------------
add_library(engine_lib STATIC src/rpc.cc src/policy.cc src/model.cc src/planner.cc src/tokens.cc)
target_include_directories(engine_lib PUBLIC src ${NLOHMANN_JSON_INCLUDE})
target_compile_options(engine_lib PRIVATE -Wall -Wextra -Wno-unused-parameter -Wno-deprecated-copy)
target_compile_definitions(engine_lib PUBLIC ENGINE_VERSION="${ENGINE_VERSION}")
target_link_libraries(engine_lib PUBLIC gpcore)

add_library(engine_daemon STATIC src/engine.cc src/server.cc)
target_compile_options(engine_daemon PRIVATE -Wall -Wextra -Wno-unused-parameter -Wno-deprecated-copy -Wno-deprecated-declarations)
target_link_libraries(engine_daemon PUBLIC engine_lib)

add_executable(gparted-engined src/main.cc)
target_compile_options(gparted-engined PRIVATE -Wall -Wextra -Wno-deprecated-declarations)
target_link_libraries(gparted-engined engine_daemon)
install(TARGETS gparted-engined RUNTIME DESTINATION libexec/clawbox-gparted)

# --- Tests (vendored googletest from vendor/gparted-1.5.0/lib/gtest) --------------------
enable_testing()
add_library(gtest_vendored STATIC ${VENDOR}/lib/gtest/src/gtest-all.cc ${VENDOR}/lib/gtest/src/gtest_main.cc)
target_include_directories(gtest_vendored SYSTEM PUBLIC ${VENDOR}/lib/gtest/include PRIVATE ${VENDOR}/lib/gtest)
target_compile_options(gtest_vendored PRIVATE -w)
find_package(Threads REQUIRED)
target_link_libraries(gtest_vendored PUBLIC Threads::Threads)

foreach(t test_policy test_plan test_json test_tokens)
  add_executable(${t} tests/${t}.cc)
  target_link_libraries(${t} engine_lib gtest_vendored)
  add_test(NAME ${t} COMMAND ${t})
endforeach()

# Root-less end to end: the daemon in dev mode on scratch image files (needs xvfb-run,
# parted and python3; scratch files go to $DEVMODE_TMP or ${TMPDIR:-/tmp}).
add_test(NAME devmode COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/tests/devmode.sh ${CMAKE_CURRENT_BINARY_DIR})
set_tests_properties(devmode PROPERTIES TIMEOUT 900)
