build: put the generated header under include/ for relocated builds

Previously the generated database/database.h was written to the build
top and exposed with -I${MAGICDIR}.  Out-of-tree that placed the raw
build directory on the compiler's include search path.

Introduce ${GENINC}, the single include dir for generated headers:
  * in-tree  (build top == source top): ${MAGICDIR}, so the header stays
    at $(builddir)/database/database.h and -I${GENINC} is exactly the old
    -I${MAGICDIR} -- behaviour is unchanged.
  * relocated (out-of-tree): ${MAGICDIR}/include, so the header is written
    to $(builddir)/include/database/database.h and only that clean
    include/ dir is on the search path; the raw build top never is.

* scripts/defs.mak.in: derive GENINC from BUILD_TOP vs MAGICSRC; add
  DATABASE_H = ${GENINC}/database/database.h; CPPFLAGS uses -I${GENINC}.
* Makefile.in: the makedbh rule targets ${DATABASE_H} (mkdir its dir);
  every prereq and the distclean use ${DATABASE_H}.
* database/Makefile: the fallback delegate targets ${DATABASE_H}.

Verified: relocated `make modules` builds all 33 module libraries with
the header at build/include/database/database.h and no raw build-top on
any -I line; in-tree the header stays at database/database.h and the
-I set is unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Darryl L. Miles 2026-07-22 15:49:58 +00:00 committed by R. Timothy Edwards
parent 955a61aeda
commit 1bdc3fa12a
3 changed files with 32 additions and 15 deletions

View File

@ -44,21 +44,23 @@ defs.mak:
config:
${MAGICSRC}/configure
tcllibrary: database/database.h modules
tcllibrary: ${DATABASE_H} modules
@echo --- making Tcl shared libraries
for dir in ${PROGRAMS}; do \
mkdir -p $$dir && ${MAKE} -C $$dir -f ${MAGICSRC}/$$dir/Makefile tcl-main || exit 1; done
mains: database/database.h modules libs
mains: ${DATABASE_H} modules libs
@echo --- making main programs
for dir in ${PROGRAMS}; do \
mkdir -p $$dir && ${MAKE} -C $$dir -f ${MAGICSRC}/$$dir/Makefile main || exit 1; done
# makedbh is the one generated helper script; it lives in the *build* tree.
database/database.h: ${MAGICSRC}/database/database.h.in
@echo --- making header file database/database.h
@mkdir -p database
${MAGICDIR}/scripts/makedbh ${MAGICSRC}/database/database.h.in database/database.h
# ${DATABASE_H} is $(builddir)/database/database.h in-tree, or
# $(builddir)/include/database/database.h when relocated (see defs.mak GENINC).
${DATABASE_H}: ${MAGICSRC}/database/database.h.in
@echo --- making header file ${DATABASE_H}
@mkdir -p $(dir ${DATABASE_H})
${MAGICDIR}/scripts/makedbh ${MAGICSRC}/database/database.h.in ${DATABASE_H}
# tiles xyz => tiles/libtiles.o xyz/libxyz.o
MODULES_SUBDIR := $(shell for i in ${MODULES}; do echo "$${i}/lib$${i}.o"; done)
@ -70,14 +72,14 @@ ${MODULES_SUBDIR}: FORCE
@d=$(dir $@); mkdir -p $$d && ${MAKE} -C $$d -f ${MAGICSRC}/$${d}Makefile module
.PHONY: modules
modules: database/database.h depend ${MODULES_SUBDIR}
modules: ${DATABASE_H} depend ${MODULES_SUBDIR}
${LIBS_SUBDIR}: FORCE
@d=$(dir $@); mkdir -p $$d && ${MAKE} -C $$d -f ${MAGICSRC}/$${d}Makefile lib
# Force the tiles/utils modules to exist first for libdatabase.a
.PHONY: libs
libs: database/database.h depend tiles/libtiles.o utils/libutils.o ${LIBS_SUBDIR}
libs: ${DATABASE_H} depend tiles/libtiles.o utils/libutils.o ${LIBS_SUBDIR}
#
# extcheck - utility tool
@ -94,7 +96,7 @@ SUBDIRS_FILTERED := $(shell echo ${MODULES} ${PROGRAMS} ${SUBDIRS} | tr ' ' '\n'
SUBDIRS_DEPEND = $(addsuffix /Depend, ${SUBDIRS_FILTERED})
${SUBDIRS_DEPEND}: database/database.h
${SUBDIRS_DEPEND}: ${DATABASE_H}
@echo --- making dependencies
@d=$(dir $@); mkdir -p $$d && ${MAKE} -C $$d -f ${MAGICSRC}/$${d}Makefile depend
@ -162,7 +164,7 @@ distclean:
${RM} defs.mak old.defs.mak
${RM} scripts/makedbh scripts/default.conf
${RM} config.log config.status
${RM} database/database.h
${RM} ${DATABASE_H}
${RM} scripts/magic.spec magic-${VERSION} magic-${VERSION}.tgz
${RM} *.log

View File

@ -25,8 +25,9 @@ LIB_OBJS += ${MAGICDIR}/tiles/libtiles.o ${MAGICDIR}/utils/libutils.o
# start of top level build) and removed last (near the end of a top level clean).
#CLEANS += database.h
# This is delegated back to the top level Makefile
database.h: ${MAGICDIR}/database/database.h.in
${MAKE} -C ${MAGICDIR} database/database.h
# This is delegated back to the top level Makefile, which owns ${DATABASE_H}
# (its location varies with in-tree vs relocated builds -- see defs.mak GENINC).
database.h: ${MAGICSRC}/database/database.h.in
${MAKE} -C ${MAGICDIR} $(patsubst ${MAGICDIR}/%,%,${DATABASE_H})
include ${MAGICDIR}/rules.mak

View File

@ -32,6 +32,20 @@ srcdir := $(MAGICSRC)$(MODULE_SUBPATH)
ifneq ($(MODULE_SUBPATH),)
VPATH := $(srcdir)
endif
# Generated headers (currently just database/database.h) are exposed through a
# single include directory, ${GENINC}:
# * in-tree (build top == source top): the top itself, so the header stays at
# $(builddir)/database/database.h and -I${GENINC} == the old -I${MAGICDIR}.
# * relocated (out-of-tree): a clean $(builddir)/include, so #include
# <database/database.h> resolves under include/ and the raw build top is
# never on the compiler search path.
ifeq ($(BUILD_TOP),$(MAGICSRC))
GENINC := ${MAGICDIR}
else
GENINC := ${MAGICDIR}/include
endif
DATABASE_H := ${GENINC}/database/database.h
# --------------------------------------------------------------------------
prefix = @prefix@
@ -141,10 +155,10 @@ CXX = @CXX@
### Status: NOT-STABILITY-AFFECTING (if your binary links you are good to continue)
#FEATURE_FLAGS += -DSUPPORT_REMOVE_MALLOC_LEGACY
# -I${MAGICDIR}: build top, for the generated database/database.h
# -I${GENINC}: generated headers (build top in-tree, build/include relocated)
# -I${MAGICSRC}: source top, for "<module>/<header>.h" includes
# -I${srcdir}: this module's source dir, for same-directory headers
CPPFLAGS = -I${MAGICDIR} -I${MAGICSRC} -I${srcdir} @CPPFLAGS@
CPPFLAGS = -I${GENINC} -I${MAGICSRC} -I${srcdir} @CPPFLAGS@
CXXFLAGS = @CXXFLAGS@
# Bundled GNU readline: its headers use <readline/*.h> includes, so the readline
# source's parent directory must be on the include path. Only for the bundled