CXX ?= g++
CXXFLAGS ?= -O3 -std=c++20 -Wall -Wextra -Wpedantic -pthread
LDLIBS = $(shell pkg-config --libs libcurl openssl libxml-2.0)
CPPFLAGS = $(shell pkg-config --cflags libcurl openssl libxml-2.0)

.PHONY: all
all: ibm_replay cachesim cacheconvert

CACHE_SOURCES = cachefuncs/cache.cpp cachefuncs/extcache.cpp
CACHE_HEADERS = cachefuncs/cache.hpp cachefuncs/extcache.hpp cachefuncs/key.hpp cachefuncs/index.hpp
CACHEFLAGS ?= -O3 -std=c++20 -Wall -Wextra -Wpedantic -flto

cachesim: cachesim.cpp cachesim_io.hpp $(CACHE_SOURCES) $(CACHE_HEADERS)
	$(CXX) $(CACHEFLAGS) cachesim.cpp $(CACHE_SOURCES) -o $@

cacheconvert: cacheconvert.cpp cachesim_io.hpp
	$(CXX) $(CACHEFLAGS) cacheconvert.cpp -o $@

ibm_replay: ibm_replay.cpp replay_plan.hpp replay_http.hpp
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) ibm_replay.cpp $(LDLIBS) -o $@

.PHONY: test test-cachesim
test_cachefuncs: test_cachefuncs.cpp $(CACHE_SOURCES) $(CACHE_HEADERS)
	$(CXX) $(CACHEFLAGS) test_cachefuncs.cpp $(CACHE_SOURCES) -o $@

test-cachesim: cachesim cacheconvert test_cachefuncs
	./test_cachefuncs
	python3 -m unittest test_cachesim.py -v

test_replay: test_replay.cpp replay_plan.hpp replay_http.hpp
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) test_replay.cpp $(LDLIBS) -o $@

test: ibm_replay test_replay test-cachesim
	./test_replay
