
CC=gcc

CFLAGS=-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1  -m64 -march=x86-64-v3 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -mtls-dialect=gnu2   

# for local development
#CFLAGS=$(CFLAGS) -fdiagnostics-color=always

SHAREDFLAGS=-fPIC -shared

DBLIBS=-L/usr/lib64/mysql -lmysqlclient -lz  -lzstd  -lssl  -lcrypto -lresolv -lm

DBFLAGS=-I/usr/include/mysql -m64 

THREADLIB=-pthread

NDOSOURCEFILES=../include/ndo.h ../include/mysql-helpers.h ndo.c ndo-startup.c ndo-handlers.c ndo-startup-queue.c ndo-handlers-queue.c

ndo.so: $(NDOSOURCEFILES)
	$(CC) $(SHAREDFLAGS) $(CFLAGS) $(DBFLAGS) ndo.c -o $@ $(DBLIBS) $(THREADLIB)

expanded:
	@rm -rf ../expanded
	@mkdir -p ../expanded/src
	@mkdir -p ../expanded/include
	@cat ../include/mysql-helpers.h | sed '/^do/d' | sed '/^} while (0)/d' > ../expanded/include/mysql-helpers.h
	@cat ../include/ndo.h | grep -v "mysql.h" | grep -v "include/nagios" | sed '/^do/d' | sed '/^} while (0)/d' > ../expanded/include/ndo.h
	@cp ../include/config.h ../expanded/include/config.h
	@cat ndo.c | grep -v "include/nagios" | grep -v "errmsg.h" > ../expanded/src/ndo.c
	@sed -i '/^do {/d;/^} while/d' ../expanded/src/ndo.c
	@cp ndo-*.c ../expanded/src/
	@gcc -E ../expanded/src/ndo.c 2>/dev/null | grep -v "^#" | sed 's/((void \*)0)/NULL/g;s/^\s*NULL.*$$/NULL/' > expanded-tmp.c
	@php -r '$$file = file_get_contents("expanded-tmp.c"); $$file = str_replace("\nNULL\n", "NULL", $$file); echo $$file;' 2>/dev/null | astyle > ndo-expanded.c
	@sed -i 's/NULL \s*/NULL /;s/NULL\s*\([;)]\)/NULL\1/;s/;;/;/g' ndo-expanded.c
	@rm -rf ../expanded
	@echo "Your expanded file is in src/ndo-expanded.c"
