# ════════════════════════════════════════════════════════════════════════════
# GNU-free CMake build for gptfdisk (sgdisk)  (FenrirOS)
#
# gptfdisk ships only a GNU Makefile. This builds the scriptable GPT partitioner
# `sgdisk` (the one the installer drives) via CMake/ninja on the musl/LLVM
# toolchain. It links statically against the e2fsprogs libuuid and the FenrirOS
# libpopt (both installed under /usr/local), so no util-linux / GNU popt .so is
# pulled into the system image.
#
# Only sgdisk is built: gdisk/cgdisk are interactive (cgdisk also needs ncurses)
# and unnecessary for an automated installer.
# ════════════════════════════════════════════════════════════════════════════
cmake_minimum_required(VERSION 3.13)
project(gptfdisk CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT DEFINED GPTFDISK_VERSION)
    set(GPTFDISK_VERSION "1.0.10")
endif()

# Locate the statically-installed libuuid (e2fsprogs) + libpopt + their headers
# in the shared /usr/local prefix.
find_path(UUID_INCLUDE_DIR uuid/uuid.h PATHS /usr/local/include REQUIRED)
find_library(UUID_LIBRARY  NAMES uuid  PATHS /usr/local/lib     REQUIRED)
find_path(POPT_INCLUDE_DIR popt.h      PATHS /usr/local/include REQUIRED)
find_library(POPT_LIBRARY  NAMES popt  PATHS /usr/local/lib     REQUIRED)

set(S "${CMAKE_SOURCE_DIR}")
include_directories(${S} ${UUID_INCLUDE_DIR} ${POPT_INCLUDE_DIR})

# Core library objects (Makefile LIB_NAMES) shared by all gptfdisk front-ends.
set(GPT_LIB_SRC
    ${S}/crc32.cc
    ${S}/support.cc
    ${S}/guid.cc
    ${S}/gptpart.cc
    ${S}/mbrpart.cc
    ${S}/basicmbr.cc
    ${S}/mbr.cc
    ${S}/gpt.cc
    ${S}/bsd.cc
    ${S}/parttypes.cc
    ${S}/attributes.cc
    ${S}/diskio.cc
    ${S}/diskio-unix.cc
)

# sgdisk = core lib + the command-line front-end (sgdisk.cc + gptcl.cc → popt).
add_executable(sgdisk ${GPT_LIB_SRC} ${S}/sgdisk.cc ${S}/gptcl.cc)
target_link_libraries(sgdisk PRIVATE ${UUID_LIBRARY} ${POPT_LIBRARY})

install(TARGETS sgdisk RUNTIME DESTINATION sbin)
