# ref/ -- the reflection library.
#
# reflect.cpp holds C++ functions that read the engine's OWN registries and
# character tables and return Klammertext (a "@table" call and such), which
# the .kt fragments in this directory then render for whichever target is
# in force.  They are reached through "@eval :cpp", so this builds a DSO
# loaded at run time, not part of libklammertext.so.
#
# Adapted from sks/document/Makefile.  Two things differ and both matter:
# ref/ has no sks dependencies (a generator returns Klammertext, so it
# needs none of kutil/target's markup helpers), and it sits ONE level below
# $(KLAMMERTEXT_HOME) rather than two, which changes the rpath.

K := $(KLAMMERTEXT_HOME)

include $(K)/env/makefile.env

# CPPFLAGS already carries -I$(KLAMMERTEXT_HOME)/mac, which is every header
# reflect.cpp includes (machine, character, error, show).  No local -I.

LIBDIR := $(K)/lib
LIBRARY := $(LIBDIR)/libklammertext.so

# "../lib", not "../../lib": reflect.so sits in $(K)/ref, so one level up
# reaches $(K)/lib.  (sks/document.so needs two, which is what this was
# copied from -- a wrong rpath here shows up only at dlopen time, and only
# where the loading process does not already have the library.)
REF_LDFLAGS := $(SHARED) $(EXPORT_DYNAMIC) -Wl,-rpath,'$(ORIGIN)/../lib' -L$(LIBDIR)

OBJECTS := reflect.o
DEPFILES := $(OBJECTS:.o=.d)

DEPFLAGS = -MMD -MP -MF $(@:.o=.d)

.PHONY: all clean

all : reflect.so

%.o : %.cpp
	@echo Compiling $<
	$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $(DEPFLAGS) $< -o $@

# The objects are LINKED here, never recompiled.  sks/document/Makefile
# names document.cpp on its link line as well, which is correct THERE only
# because document.o is the one object not in its LOCAL_OBJECTS; doing both
# for the same file -- compiling reflect.cpp inline AND linking reflect.o --
# defines every symbol in it twice.  Building through the pattern rule also
# gets reflect.d, so a header change rebuilds it.
reflect.so : $(OBJECTS) $(LIBRARY)
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(REF_LDFLAGS) $(LDFLAGS) \
		-o $@ $(OBJECTS) $(KLAMMERTEXT_LIB) $(LDLIBS)

clean :
	rm -f $(OBJECTS) $(DEPFILES) *.so *~

-include $(DEPFILES)
