# Hand-rolled build of the FTGL that ROOT used to ship (graf3d/ftgl of ROOT 6.28).
#
# ROOT >= 6.32 no longer installs these headers and its builtin FTGL is now
# upstream 2.4.0, which lacks ROOT's Render(text, w_max, w_fade) fade-out
# extension that GledCore/Rnr/GL relies on.  So we build ROOT's variant here
# and install it into the gled external prefix.
#
# Source: ROOT 6.28.06, root-6.28.06/graf3d/ftgl (FTGL 2.1.2 + ROOT's changes,
# notably FTFont::Render(text, w_max, w_fade) and PreRender/PostRender).
# Local changes on top of that:
#   - inc/FTGL.h includes <GL/glew.h> instead of ROOT's TGLIncludes.h, which
#     ROOT no longer installs;
#   - src/FTVectoriser.cxx casts FT_Outline::tags, unsigned char* since
#     freetype 2.10.
#
# There is no configure; PREFIX is passed in by the gled-builder recipe.

PREFIX   ?= /usr/local

SRCS     := $(wildcard src/*.cxx)
OBJS     := $(SRCS:.cxx=.o)
# A *static* PIC archive on purpose.  ROOT's libRGL.so statically links its own
# builtin FTGL (upstream 2.4.0) but still exports those symbols with default
# visibility -- 103 mangled names collide with this fork, FTFont::FaceSize,
# FTFont::Depth and FTFace::Glyph among them, and the two versions have
# incompatible object layouts (2.4.0 is pimpl'd).  Whichever library the
# dynamic loader saw first would win for both.  Linking this archive into
# libGledCore_Rnr_GL.so with -Wl,--exclude-libs keeps our copy local to that
# library, so neither side can interpose the other.
LIB      := libFTGL.a
HDRS     := $(wildcard inc/*.h)

CXX      ?= c++
AR       ?= ar
CPPFLAGS += -Iinc $(shell pkg-config --cflags freetype2)
CXXFLAGS += -O2 -fPIC -pipe -std=c++17 -Wno-deprecated-declarations

all: $(LIB)

$(LIB): $(OBJS)
	rm -f $@
	$(AR) rcs $@ $(OBJS)

%.o: %.cxx
	$(CXX) -c $< $(CPPFLAGS) $(CXXFLAGS) -o $@

install: $(LIB)
	install -d $(PREFIX)/include $(PREFIX)/lib
	install -m 644 $(HDRS) $(PREFIX)/include
	install -m 644 $(LIB)   $(PREFIX)/lib

uninstall:
	rm -f $(addprefix $(PREFIX)/include/,$(notdir $(HDRS)))
	rm -f $(PREFIX)/lib/$(LIB)

clean:
	rm -f $(OBJS) $(LIB)

.PHONY: all install uninstall clean
