cmake_minimum_required(VERSION 3.25)
project(lab LANGUAGES C)

# clangd reads compile_commands.json to learn the flags.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Every .c file in src/ is part of the program. New files are picked up on the
# next build; there is nothing to add here.
file(GLOB sources CONFIGURE_DEPENDS src/*.c)
add_executable(app ${sources})

# C23. Language extensions stay on (CMake's default), so this is -std=gnu23.
# No -Wpedantic: GNU extensions (statement expressions, zero-length arrays,
# case ranges, ##__VA_ARGS__, ...) are normal in low-level code.
target_compile_features(app PRIVATE c_std_23)
target_compile_options(app PRIVATE
  -Wall -Wextra -Wshadow -Wformat=2 -Wimplicit-fallthrough -Wstrict-prototypes)
# Declare every POSIX and GNU API in the C library headers (fork, getline,
# pipe2, asprintf, ...), so no source file has to define _GNU_SOURCE itself.
target_compile_definitions(app PRIVATE _GNU_SOURCE)

# Libraries. readline (apt: libreadline-dev) has no CMake package, but like
# most C libraries it ships a pkg-config file. The imported target carries its
# include paths and link flags. To add another library, find its pkg-config
# name (`pkg-config --list-all`) and repeat the last two lines for it.
find_package(PkgConfig REQUIRED)
pkg_check_modules(READLINE REQUIRED IMPORTED_TARGET readline)
target_link_libraries(app PRIVATE PkgConfig::READLINE)
