Files
nethack/sys/unix/hints
Benjamin de WaalandClaude Opus 4.7 f6afa23e28 libnh: fix native build of libnh.a on macOS
Fixes four issues that prevented `make WANT_LIBNH=1 all` from producing
a libnh.a that could be linked into a host program on macOS.  Before
these patches, it built but the resulting archive was unusable: macOS
ld errored on a nested liblua archive member, was missing date.o and
hacklib symbols (`populate_nomakedefs`, `eos`, `lcase`, `mungspaces`,
...), and had duplicate definitions of `main`, `whoami`, etc.

Specific changes:

1. sys/libnh/libnhmain.c: drop `static` on `whoami()`.  src/earlyarg.c
   declares it `extern` and calls it from `scores_only()`; the static
   makes it file-local and that reference goes unresolved.

2. sys/libnh/libnhmain.c: gate the emscripten-only code in get_nhuuid
   with `#ifdef __EMSCRIPTEN__` instead of `#ifdef NHUUID`.  The macOS
   hints define NHUUID for the libnh build (they did so unconditionally
   before NO_NHUUID even existed), so on native builds the compiler
   tried to call `emscripten_run_script_int` / `_string` and failed
   with implicit-function-declaration errors.  __EMSCRIPTEN__ is the
   real signal for "this is being cross-compiled to WASM."

3. sys/unix/hints/macOS.500: in the WANT_LIBNH block, add an explicit
   `recover: lua_support` dependency (gated by MAKEFILE_TOP).  When
   $(GAME) is overridden to empty, the regular `recover: $(GAME)` chain
   no longer triggers `lua_support`, so include/nhlua.h never gets
   generated and recover.c's transitive #include of hack.h fails.

4. sys/unix/hints/macOS.500: rewrite the libnh.a rule.  The previous
   `ar rcs libnh.a $(HOBJ) $(LIBNHSYSOBJ) liblua-$(LUA_VERSION).a` had
   four problems: (a) ar archives liblua.a as a single opaque member
   that macOS ld can't dereference, (b) date.o (DATE_O, kept separate
   from HOBJ) was never archived, so `populate_nomakedefs` and
   `nomakedefs` were missing, (c) hacklib.a was likewise omitted, and
   (d) HOBJ already contains $(SYSOBJ) (with unixmain.o) and $(WINOBJ)
   (the tty windowport), which duplicated symbols from libnhmain.o /
   winshim.o.

   The fix uses `libtool -static` so hacklib.a and liblua's archive
   have their members merged rather than nested, depends on $(LUALIB)
   so lua_support runs first, includes $(DATE_O) and $(TARGET_HACKLIB),
   and uses $(filter-out $(SYSOBJ) $(WINOBJ),$(HOBJ)) to drop the
   duplicates.

Verified by clean rebuild on macOS 26 (arm64, Apple clang 17):
  make spotless
  make fetch-Lua
  make WANT_LIBNH=1 all
and link-tested with a tiny harness that calls
shim_graphics_set_callback() against the resulting libnh.a.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-06 07:06:41 +01:00
..
2018-11-11 10:29:52 -05:00
2026-04-26 10:41:11 -04:00
2026-04-26 10:41:11 -04:00
2026-04-26 10:41:11 -04:00
2026-04-26 10:41:11 -04:00