diff options
| author | Sopár Adrián <adrian.sopar@protonmail.com> | 2026-06-25 22:19:10 +0200 |
|---|---|---|
| committer | Sopár Adrián <adrian.sopar@protonmail.com> | 2026-06-25 22:19:10 +0200 |
| commit | c0ec763cc186bbb67660b5172b095833177c2fcb (patch) | |
| tree | 363c42f54677c1b47ac20509f527ac453313afa5 | |
| parent | 054377a51ac6e1ff14d630542caa708c7aa625ae (diff) | |
Replace Makefile with CMake build supporting Termux, update README.md and .gitignore accordingly.develop
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | CMakeLists.txt | 80 | ||||
| -rw-r--r-- | Makefile | 83 | ||||
| -rw-r--r-- | README.md | 38 | ||||
| -rwxr-xr-x | debug.sh | 5 |
5 files changed, 101 insertions, 107 deletions
@@ -1,2 +1,2 @@ -bin/ +build/ *.swp diff --git a/CMakeLists.txt b/CMakeLists.txt index c6617f8..b50c204 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,14 +1,78 @@ cmake_minimum_required(VERSION 3.15) -project(maze VERSION 0.9) +project(maze VERSION 0.9 LANGUAGES C) -set(CURSES_NEED_WIDE TRUE) -find_package(Curses REQUIRED) -set(CMAKE_CXX_FLAGS "-D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600") -include_directories(${CURSES_INCLUDE_DIR}) +set(CMAKE_C_STANDARD 11) +set(CMAKE_C_STANDARD_REQUIRED ON) -file(GLOB_RECURSE maze_SRC CONFIGURE_DEPENDS "src/*") +# --- 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/) -target_link_libraries(maze ${CURSES_LIBRARIES}) +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 $<CONFIG:Debug> 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 + $<$<CONFIG:Debug>:-Wall> + $<$<CONFIG:Debug>:-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() diff --git a/Makefile b/Makefile deleted file mode 100644 index 8102aec..0000000 --- a/Makefile +++ /dev/null @@ -1,83 +0,0 @@ -CC = gcc -CFLAGS = -Isrc $$(ncursesw5-config --cflags) -LIBS = $$(ncursesw5-config --libs) -#LIBS = -lncursesw - -PREFIX = /usr/local -MANPREFIX = $(PREFIX)/man - -all: bin/maze - -debug: CFLAGS += -Wall -DEBUG -g -debug: bin/maze - -app_sources = $(wildcard src/*.c src/game/*.c) -app_objects = $(patsubst src/%.c, bin/%.o, $(app_sources)) - -bin/maze: $(app_objects) - $(CC) $(CFLAGS) $^ -o $@ $(LIBS) - -bin/main.o: src/main.c bin - $(CC) $(CFLAGS) -c $< -o $@ - -bin/args.o: src/args.c bin - $(CC) $(CFLAGS) -c $< -o $@ - -bin/config.o: src/config.c bin - $(CC) $(CFLAGS) -c $< -o $@ - -bin/color.o: src/color.c bin - $(CC) $(CFLAGS) -c $< -o $@ - -bin/menu.o: src/menu.c bin - $(CC) $(CFLAGS) -c $< -o $@ - -bin/game/entry.o: src/game/entry.c bin/game - $(CC) $(CFLAGS) -c $< -o $@ - -bin/game/game.o: src/game/game.c bin/game - $(CC) $(CFLAGS) -c $< -o $@ - -bin/game/operations.o: src/game/operations.c bin/game - $(CC) $(CFLAGS) -c $< -o $@ - -bin/game/commands.o: src/game/commands.c bin/game - $(CC) $(CFLAGS) -c $< -o $@ - -bin/file.o: src/file.c bin - $(CC) $(CFLAGS) -c $< -o $@ - -bin/maze_generator.o: src/maze_generator.c bin - $(CC) $(CFLAGS) -c $< -o $@ - -bin/maze_solver.o: src/maze_solver.c bin - $(CC) $(CFLAGS) -c $< -o $@ - -bin/utilities.o: src/utilities.c bin - $(CC) $(CFLAGS) -c $< -o $@ - -bin: - mkdir -p $@ - -bin/game: - mkdir -p $@ - -clean: - rm -rf bin/* - -install: bin/maze - @echo INSTALL execuatble - mkdir -p $(PREFIX)/bin - cp $< $(PREFIX)/bin/maze - chmod 755 $(PREFIX)/bin/maze - @echo INSTALL man page - gzip -c doc/maze.1 > $(MANPREFIX)/man1/maze.1.gz - chmod 644 $(MANPREFIX)/man1/maze.1.gz - -uninstall: - @echo REMOVE execuatble - rm -f $(PREFIX)/bin/maze - @echo REMOVE man page - rm -f $(MANPREFIX)/man1/maze.1.gz - -.PHONY: clean install uninstall @@ -20,29 +20,41 @@ On ubuntu, you can install the dependencies by running the following command wit # apt install libncurses-dev -Installation ------------- +Building and Installation +------------------------- + +Currently, this program is not available in any package manager, so you have to build it from source. The build uses CMake, which works in two steps — configure, then build: + + $ cmake -B build + $ cmake --build build + +The first command inspects your system and generates the build files under `build/`; the second compiles the program. After the first configure, you only need to re-run `cmake --build build` to pick up source changes. + +Once built, the executable is at `build/maze`, and you can run it without installing: + + $ ./build/maze -Currently, this program is not available in any package manager, so you have to build it from source. You can run this command that will build and install the program on your system: +To install it on your system, run a third command: - # make install + # cmake --install build -By default, maze is installed using the prefix "/usr/local", so the full path of the executable will be "/usr/local/bin/maze". +By default, maze is installed using the prefix "/usr/local", so the full path of the executable will be "/usr/local/bin/maze" and the man page will be installed under "/usr/local/share/man/man1". Installing there requires root privileges. -You can install maze into a directory of your choice by changing the second command to: +You can install maze into a directory of your choice by setting the prefix at configure time: - # make PREFIX="/your/dir" install + $ cmake -B build -DCMAKE_INSTALL_PREFIX="/your/dir" + $ cmake --build build + $ cmake --install build -In order to remove the program from your system, run the following command: +On Termux this is handled for you: the prefix defaults to $PREFIX (Termux's own install root), so a plain `cmake --install build` installs to $PREFIX/bin with no root and no extra flags. - # make uninstall +To remove the program from your system, delete the files recorded in the install manifest that CMake writes during installation (drop the leading `#`/root on Termux): -Please note that these commands require root privileges. + # xargs rm < build/install_manifest.txt -If you don't want to install, just run it, you can do it in this way: +If you ever want a clean rebuild from scratch, remove the build directory and configure again: - $ make - $ ./bin/maze + $ rm -rf build Usage ----- @@ -1,2 +1,3 @@ -make clean -make debug
\ No newline at end of file +rm -rf build +cmake -B build -DCMAKE_BUILD_TYPE=Debug +cmake --build build |
