cmake_minimum_required(VERSION 3.15) project(maze VERSION 0.9 LANGUAGES C) set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD_REQUIRED ON) # --- Termux install prefix --------------------------------------------------- # Termux has no /usr/local; everything lives under $PREFIX # (/data/data/com.termux/files/usr). CMake's default install prefix would fail # there, so default to $PREFIX when we detect Termux and the user didn't pass # -DCMAKE_INSTALL_PREFIX explicitly. Harmless on every other platform. if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND DEFINED ENV{PREFIX} AND "$ENV{PREFIX}" MATCHES "com\\.termux") set(CMAKE_INSTALL_PREFIX "$ENV{PREFIX}" CACHE PATH "Install prefix" FORCE) endif() include(GNUInstallDirs) # --- Wide-character ncurses -------------------------------------------------- # Prefer pkg-config. Termux ships ncursesw.pc (with a compatibility ncurses.pc # symlink), and pkg-config returns the correct include dir and -lncursesw. # This avoids FindCurses fragility and Termux's occasionally-broken # $PREFIX/include/ncurses/ subdirectory symlinks. Falls back to FindCurses on # systems without pkg-config (or without a .pc file for ncursesw). find_package(PkgConfig QUIET) if(PkgConfig_FOUND) pkg_check_modules(NCURSESW IMPORTED_TARGET ncursesw) endif() # Recursive glob over *.c only (the old "src/*" pulled headers into the target). # CONFIGURE_DEPENDS re-runs the glob at build time so new files are picked up. file(GLOB_RECURSE maze_SRC CONFIGURE_DEPENDS src/*.c) add_executable(maze ${maze_SRC}) target_include_directories(maze PRIVATE src) # _XOPEN_SOURCE=600 unlocks the wide-character ncurses API (add_wch, get_wch, # waddnwstr, ...). _DEFAULT_SOURCE re-enables BSD/default declarations that a # strict _XOPEN_SOURCE would otherwise hide. These previously lived in # CMAKE_CXX_FLAGS and were silently ignored because this is a C project. # # Termux note: if you hit "implicit declaration of function" errors here on # Android/Bionic, ADD _GNU_SOURCE below rather than removing _XOPEN_SOURCE. target_compile_definitions(maze PRIVATE _DEFAULT_SOURCE _XOPEN_SOURCE=600 ) # Extra warnings, but only for Debug builds (configure with # -DCMAKE_BUILD_TYPE=Debug). The $ generator expression emits each # flag only when the active configuration is Debug, so this works under both # single-config generators (Make/Ninja, where the build type is fixed at # configure time) and multi-config generators (Visual Studio/Xcode/Ninja # Multi-Config, where it is chosen at build time). GCC/Clang flag syntax, which # is fine for Linux and Termux. target_compile_options(maze PRIVATE $<$:-Wall> $<$:-Wextra> ) if(TARGET PkgConfig::NCURSESW) target_link_libraries(maze PRIVATE PkgConfig::NCURSESW) else() set(CURSES_NEED_WIDE TRUE) set(CURSES_NEED_NCURSES TRUE) find_package(Curses REQUIRED) target_include_directories(maze PRIVATE ${CURSES_INCLUDE_DIRS}) target_link_libraries(maze PRIVATE ${CURSES_LIBRARIES}) endif() # --- Install ----------------------------------------------------------------- install(TARGETS maze RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) # Man page installs only if it is present. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/doc/maze.1") install(FILES doc/maze.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1) endif()