# Portable build (no cmake required). # make test build + run every tests/test_*.cpp (pure; never links SDL) # make app build build/libreopolis, the SDL3 front end (cross-platform via pkg-config) CXX ?= c++ CXXFLAGS ?= -std=c++20 -Wall -Wextra -O2 -Iinclude -MMD -MP BUILD := build # Zero-initialise auto (stack) variables — a recognised hardening (clang 8+/gcc 12+) that eliminates the # whole "read of an uninitialised local" UB class at ~zero cost. Probed for support so the build stays # portable to older toolchains. (Added after a latent uninit read surfaced only under -O2 in one test # sequence — masked by ASan/UBSan/-O1/any observation; this makes the behaviour deterministic + safe.) INITZERO := $(shell printf 'int main(){}' | $(CXX) -ftrivial-auto-var-init=zero -x c++ - -o /dev/null 2>/dev/null && echo -ftrivial-auto-var-init=zero) CXXFLAGS += $(INITZERO) # FreeType renders the UI text (ui/font.hpp rasterises the bundled OFL face in assets/fonts/). It is # the one library the tests link besides the C++ runtime — headless and ubiquitous, discovered the # same way as SDL. The UI-framework migration reuses the same dependency for its font engine. FT_CFLAGS := $(shell pkg-config --cflags freetype2 2>/dev/null) FT_LIBS := $(shell pkg-config --libs freetype2 2>/dev/null) CXXFLAGS += $(FT_CFLAGS) ZLIB_CFLAGS := $(shell pkg-config --cflags zlib 2>/dev/null) ZLIB_LIBS := $(shell pkg-config --libs zlib 2>/dev/null || echo -lz) CXXFLAGS += $(ZLIB_CFLAGS) # RmlUi (the UI-framework migration): pinned source fetched by scripts/fetch_rmlui.sh into # third_party/ (git-ignored), its core built once into build/rmlui/ by CMake, plus the two stock # SDL3 backend translation units compiled below (-w: vendored code, not ours to keep warning-clean). # App-only — the tests never link it. SDL3_image is the backends' texture loader. RMLUI_VER := 6.2 RMLUI_DIR := third_party/RmlUi-$(RMLUI_VER) RMLUI_LIB := $(BUILD)/rmlui/librmlui.a RMLUI_CFLAGS := -I$(RMLUI_DIR)/Include -I$(RMLUI_DIR)/Backends -DRMLUI_SDL_VERSION_MAJOR=3 RMLUI_OBJ := $(BUILD)/rmlui_platform.o $(BUILD)/rmlui_renderer.o SDLIMG_CFLAGS := $(shell pkg-config --cflags sdl3-image 2>/dev/null) SDLIMG_LIBS := $(shell pkg-config --libs sdl3-image 2>/dev/null) # Developer debug layer (HUD / field overlays / tile inspector / logging) — gated behind SC2K_DEBUG # and compiled out for release, so a shipping build pays nothing for it. Tests ALWAYS build with it # (so the pure debug headers are exercised); the SDL front end opts in with `make app DEBUG=1`. Plain # `make app` is the release build with all debug code #ifdef'd away. Switching DEBUG on/off for the # SDL front end forces a rebuild — build/libreopolis depends on a stamp of the active flag (see APP_STAMP # below), so no `make clean` is needed to flip between debug and release. TEST_DEBUG := -DSC2K_DEBUG ifeq ($(DEBUG),1) APP_DEBUG := -DSC2K_DEBUG endif TESTSRC := $(wildcard tests/test_*.cpp) TESTOBJ := $(patsubst tests/%.cpp,$(BUILD)/%.o,$(TESTSRC)) TESTBIN := $(patsubst tests/%.cpp,$(BUILD)/%,$(TESTSRC)) DEPS := $(TESTOBJ:.o=.d) # ── Shared static library: src/*.cpp ───────────────────────────────────────── # The heaviest sim subsystems (things, disasters, growth, …) keep their DECLARATIONS in the headers # under include/sc2k/ but move their function BODIES into src/. Editing an implementation then # recompiles one .cpp + relinks, instead of re-parsing a ~1,000-line inline header in every test TU # that pulls it in. Built in two variants because the sim honours -DSC2K_DEBUG (a debug-only ship # breadcrumb trail in things.cpp): tests always build with it, the release app without — so a single # archive can't serve both. Each variant is compiled with its consumer's flags. (The command-line # tools don't reach these headers, so they link no library.) LIBSRC := $(wildcard src/*.cpp) LIBOBJ_T := $(patsubst src/%.cpp,$(BUILD)/lib-test/%.o,$(LIBSRC)) LIBOBJ_A := $(patsubst src/%.cpp,$(BUILD)/lib-app/%.o,$(LIBSRC)) LIBTEST := $(BUILD)/libsc2ksim-test.a LIBAPP := $(BUILD)/libsc2ksim-app.a LIBDEPS := $(LIBOBJ_T:.o=.d) $(LIBOBJ_A:.o=.d) # SDL3 is discovered with pkg-config — the same flags on Linux/macOS; on Windows use the SDL CMake # package or a pkg-config shim. Only the `app` target needs it; tests stay dependency-free. SDL2_CFLAGS := $(shell pkg-config --cflags sdl3 2>/dev/null) SDL2_LIBS := $(shell pkg-config --libs sdl3 2>/dev/null) # SDL3_mixer (optional): when present it drives both SFX and the MIDI music playlist. Without it the app # still builds with WAV SFX via raw SDL3 audio (no music). The -DSC2K_HAVE_MIXER define picks the path. ifeq ($(shell pkg-config --exists sdl3-mixer && echo y),y) SDL2_CFLAGS += $(shell pkg-config --cflags sdl3-mixer) -DSC2K_HAVE_MIXER SDL2_LIBS += $(shell pkg-config --libs sdl3-mixer) endif .PHONY: all test clean app tools format format-check terrain-authority-check city-wire-action-policy-check FORCE all: $(TESTBIN) # Formatting: .clang-format at the repo root defines the style (hand-packed data tables sit # inside clang-format off/on guards). `make format` rewrites in place; `make format-check` # fails if anything is out of shape. Uses clang-format from PATH, or Homebrew LLVM's. CLANG_FORMAT ?= $(shell [ -x /opt/homebrew/opt/llvm/bin/clang-format ] && echo /opt/homebrew/opt/llvm/bin/clang-format || command -v clang-format) FORMAT_SRC = $(foreach f,$(shell git ls-files '*.hpp' '*.cpp' '*.h' '*.c'),$(if $(wildcard $(f)),$(f))) format: @$(CLANG_FORMAT) -i $(FORMAT_SRC) format-check: terrain-authority-check city-wire-action-policy-check @$(CLANG_FORMAT) --dry-run -Werror $(FORMAT_SRC) && echo "FORMAT OK" terrain-authority-check: @scripts/check_terrain_authority.sh city-wire-action-policy-check: @sh scripts/check_city_wire_action_policy.sh # Pure command-line tools (no SDL): build/make_scenario etc. `make tools` builds them all. TOOLBIN := $(patsubst tools/%.cpp,$(BUILD)/%,$(wildcard tools/*.cpp)) tools: $(TOOLBIN) $(BUILD)/%: tools/%.cpp | $(BUILD) $(CXX) $(CXXFLAGS) $< -o $@ $(ZLIB_LIBS) # Always-out-of-date target: anything depending on FORCE has its recipe run every build. Used to # re-evaluate the DEBUG flag stamp (see APP_STAMP) without marking the stamp file itself phony. FORCE: # Tests are compiled to an object ONCE (its .d tracks the headers it includes) and then linked against # the shared library. Decoupling compile from link is what makes the src/ split pay off: editing a # library body re-archives the lib and RELINKS the tests, recompiling none of them. (A header edit still # recompiles the objects that include it, exactly as before — via the .d files below.) $(BUILD)/%.o: tests/%.cpp | $(BUILD) $(CXX) $(CXXFLAGS) $(TEST_DEBUG) -c $< -o $@ $(BUILD)/%: $(BUILD)/%.o $(LIBTEST) | $(BUILD) $(CXX) $(CXXFLAGS) $(TEST_DEBUG) $< $(LIBTEST) -o $@ $(FT_LIBS) $(ZLIB_LIBS) # DEBUG-flag stamp: make keys rebuilds off mtimes, so on its own it can't see that `make app DEBUG=1` # and a later plain `make app` ask for different compile flags. We record the active APP_DEBUG flag in # this stamp; its recipe runs every build (via FORCE) but only rewrites the file — and deletes the stale # binary — when the flag actually changed. APP_STAMP := $(BUILD)/.app_debug_stamp $(APP_STAMP): FORCE | $(BUILD) @printf '%s' '$(APP_DEBUG)' | cmp -s - $@ 2>/dev/null || { printf '%s' '$(APP_DEBUG)' > $@; rm -f $(BUILD)/libreopolis; } # Static-library objects + archives (see the LIB* block near the top). The lib-test objects carry # -DSC2K_DEBUG like every test binary; the lib-app objects track the app's APP_DEBUG through the same # stamp main.cpp uses, so flipping DEBUG rebuilds the app's library slice in lockstep with the front end. $(BUILD)/lib-test/%.o: src/%.cpp | $(BUILD)/lib-test $(CXX) $(CXXFLAGS) $(TEST_DEBUG) -c $< -o $@ $(BUILD)/lib-app/%.o: src/%.cpp $(APP_STAMP) | $(BUILD)/lib-app $(CXX) $(CXXFLAGS) $(APP_DEBUG) -c $< -o $@ $(LIBTEST): $(LIBOBJ_T) @rm -f $@ && ar rcs $@ $(LIBOBJ_T) $(LIBAPP): $(LIBOBJ_A) @rm -f $@ && ar rcs $@ $(LIBOBJ_A) $(BUILD)/lib-test $(BUILD)/lib-app: @mkdir -p $@ # Front end (SDL2). Not part of `all`/`test`, so a machine without SDL can still run the test suite. # `app` reconciles the stamp (deleting build/libreopolis if DEBUG flipped) and THEN builds the binary in a fresh # sub-make. The split is deliberate: GNU make 3.81 (still Apple's /usr/bin/make) compares mtimes at whole- # second granularity and won't re-stat a target after a prerequisite's recipe ran — so a same-second stamp # bump or in-place delete is missed on back-to-back builds. The sub-make stats build/libreopolis anew, so it # reliably notices the just-deleted binary and rebuilds with the current flag. (DEBUG= propagates to the # sub-make automatically as a command-line variable.) app: $(APP_STAMP) @$(MAKE) --no-print-directory $(BUILD)/libreopolis # RmlUi core: a one-shot CMake build of the vendored source into a static library. The two SDL3 # backend TUs are compiled here (they are shipped as sources, not part of the core library). $(RMLUI_LIB): | $(BUILD) @[ -d "$(RMLUI_DIR)" ] || { echo "RmlUi source not found — run scripts/fetch_rmlui.sh first"; exit 1; } @cmake -S $(RMLUI_DIR) -B $(BUILD)/rmlui -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Release -DRMLUI_SAMPLES=OFF -DCMAKE_POLICY_VERSION_MINIMUM=3.5 >/dev/null @cmake --build $(BUILD)/rmlui -j >/dev/null $(BUILD)/rmlui_platform.o: $(RMLUI_DIR)/Backends/RmlUi_Platform_SDL.cpp $(RMLUI_LIB) | $(BUILD) $(CXX) $(CXXFLAGS) -w $(SDL2_CFLAGS) $(SDLIMG_CFLAGS) $(RMLUI_CFLAGS) -c $< -o $@ $(BUILD)/rmlui_renderer.o: $(RMLUI_DIR)/Backends/RmlUi_Renderer_SDL.cpp $(RMLUI_LIB) | $(BUILD) $(CXX) $(CXXFLAGS) -w $(SDL2_CFLAGS) $(SDLIMG_CFLAGS) $(RMLUI_CFLAGS) -c $< -o $@ $(BUILD)/libreopolis: app/main.cpp $(LIBAPP) $(RMLUI_LIB) $(RMLUI_OBJ) | $(BUILD) @[ -n "$(SDL2_LIBS)" ] || { echo "SDL3 not found via pkg-config. Install: brew install sdl3 (macOS) / libsdl3-dev or sdl3 from your distro (Linux) / vcpkg install sdl3 (Windows)"; exit 1; } @[ -n "$(FT_LIBS)" ] || { echo "FreeType not found via pkg-config. Install: brew install freetype (macOS) / libfreetype-dev (Linux) / vcpkg install freetype (Windows)"; exit 1; } @[ -n "$(SDLIMG_LIBS)" ] || { echo "SDL3_image not found via pkg-config. Install: brew install sdl3_image (macOS) / libsdl3-image-dev (Linux) / vcpkg install sdl3-image (Windows)"; exit 1; } $(CXX) $(CXXFLAGS) $(APP_DEBUG) $(SDL2_CFLAGS) $(SDLIMG_CFLAGS) $(RMLUI_CFLAGS) $< $(LIBAPP) $(RMLUI_OBJ) $(RMLUI_LIB) -o $@ $(SDL2_LIBS) $(SDLIMG_LIBS) $(FT_LIBS) $(ZLIB_LIBS) -include $(DEPS) -include $(LIBDEPS) -include $(BUILD)/libreopolis.d $(BUILD): @mkdir -p $(BUILD) test: terrain-authority-check city-wire-action-policy-check all @ok=1; for t in $(TESTBIN); do echo "== $$t =="; $$t || ok=0; done; \ [ $$ok -eq 1 ] && echo "ALL TESTS PASSED" || { echo "SOME TESTS FAILED"; exit 1; } clean: @rm -rf $(BUILD)