CC=gcc

# Make sure that no implicit -pie is used - -no-pie can not be used on old compilers
ifneq (,$(strip $(shell $(CC) -dM -E - < /dev/null  | grep -i __pie__)))
CFLAGS_NO_PIE=-fno-pie
LDFLAGS_NO_PIE=-no-pie
else
CFLAGS_NO_PIE=
LDFLAGS_NO_PIE=
endif

CFLAGS=-Iinclude -O2 -g -Wall -Werror
LDFLAGS=

# To link dynamically with expat library on system, comment line below
USE_PROVIDED_EXPAT=y

ifeq ($(strip $(USE_PROVIDED_EXPAT)),y)
# Using provided static expat library
EXPATDIR=ext/expat
CFLAGS  += -I$(EXPATDIR)/include $(CFLAGS_NO_PIE)
LDFLAGS += $(EXPATDIR)/lib/libexpat.a  $(LDFLAGS_NO_PIE)
else
CFLAGS  +=
LDFLAGS += -lexpat
endif

TARGETS=scanxml xml-format.txt

.PHONY: all
all: $(TARGETS)

scanxml: src/scanxml.c Makefile
	$(CC) $(CFLAGS) src/scanxml.c src/slist.c $(LDFLAGS) -o scanxml

xml-format.txt: scanxml Makefile
	echo "[GENERATED FILE: running make regenerates it running scanxml -d]" > xml-format.txt
	echo "" >> xml-format.txt
	./scanxml -d >> xml-format.txt

.PHONY: clean
clean:
	rm -f $(TARGETS)
