# Makefile — unit tests for mux_subnet::compare_to (the access-control
# subnet comparator; #799).
#
# Links the netmux-side netaddr object against libmux with driver-global
# stubs (in test_netaddr.cpp).  Needs a built netmux:
#   make install   (from the repo root) produces mux/src/netmux-netaddr.o
#
# Build: make
# Run:   make test

CXX      = g++

# This island is the only one that links an object built by the MAIN build
# rather than compiling everything itself.  On a tree configured
# --enable-sanitizers that object is instrumented and carries undefined
# __asan_* references, so linking it into an uninstrumented binary fails in
# ld -- before anything runs, which is why the LD_PRELOAD that rescues the
# other islands cannot help here (#1522).
#
# tests/format, tests/alarm and tests/dbt build every object themselves and
# link libmux as a shared library, so they never see the undefined symbols
# and are unaffected.
#
# Take the flags from the tree's own config.status rather than hardcoding a
# set: a tree configured with a different sanitizer selection gets that
# selection, and an ordinary tree gets an empty string and builds exactly as
# before.  Instrumenting the island is also the more useful answer -- the code
# under test here (mux_subnet::compare_to, parse_subnet) then runs
# instrumented, which is the point of a sanitizer run.
MUX_SANITIZE := $(shell grep -oE '\-fsanitize=[a-z,]+|\-fno-omit-frame-pointer' \
                  ../../mux/config.status 2>/dev/null | sort -u | tr '\n' ' ')

# -MMD -MP: emit header prerequisites next to each object (#1952).  Without
# them the .o depends only on the .cpp, so editing a header this harness
# compiles against rebuilds nothing and the suite reports green against the
# previous header.
CXXFLAGS = -std=c++17 -g -O2 -Wall -Wextra -fPIC -MMD -MP $(MUX_SANITIZE)
INCDIR   = ../../mux/include
LIBDIR   = ../../mux/lib
NETADDR_O = ../../mux/src/netmux-netaddr.o

TARGET   = test_netaddr

all: $(TARGET)

test_netaddr.o: test_netaddr.cpp
	$(CXX) $(CXXFLAGS) -DHAVE_CONFIG_H -I$(INCDIR) -c -o $@ $<

$(TARGET): test_netaddr.o
	$(CXX) $(CXXFLAGS) -o $@ $^ $(NETADDR_O) -L$(LIBDIR) -lmux -Wl,-rpath,$(LIBDIR)

# Prepend LIBDIR so the freshly-built libmux.so wins over any ambient
# LD_LIBRARY_PATH shadow (same rationale as tests/libmux).
test: $(TARGET)
	LD_LIBRARY_PATH=$(LIBDIR):$$LD_LIBRARY_PATH ./$(TARGET)

clean:
	rm -f *.o *.d $(TARGET)

# Generated by -MMD; absent on the first build, hence the leading '-'.
-include $(wildcard *.d)

.PHONY: all test clean
