all: testing

#---------------------------------------------------------customizable variables

TARGET = TestCppLog

BUILD_DIR = build

SOURCE_DIR = src

CXX_FLAGS = -g3 -Wall -Wno-sign-compare

UNITTEST_PP_DIR = ../../../UnitTest++
POCO_BASE_DIR = ../../../poco.rev.1094

INC_PATHS = \
-I ../../include \
-I $(UNITTEST_PP_DIR)/src \
-I $(POCO_BASE_DIR)/Foundation/include \
-I $(POCO_BASE_DIR)/Data/include \
-I $(POCO_BASE_DIR)/Data/SQLite/include

LIB_PATHS = \
-L $(UNITTEST_PP_DIR) \
-L $(POCO_BASE_DIR)/lib

LIBS = \
-l UnitTest++ \
-l PocoSQLited \
-l PocoDatad \
-l PocoFoundationd \
-l pthread \
-l dl

#-----------------------------------------------------------------automatic part

# get all sources
VPATH := $(SOURCE_DIR)

# compute objects
OBJECTS := $(patsubst $(SOURCE_DIR)/%.cpp, $(BUILD_DIR)/%.o, $(wildcard $(SOURCE_DIR)/*.cpp))

# create build directory
build_dir:
	@echo "Creating build directory: $(BUILD_DIR)"
	@mkdir -p $(BUILD_DIR)

# build objects from sources
$(BUILD_DIR)/%.o: %.cpp
	@echo "Compiling $< to target: $@"
	@$(CXX) $(CXX_FLAGS) -c $< $(INC_PATHS) -o $@

# link objects to executable
test_runner: build_dir $(OBJECTS)
	@echo "Creating executable: $(BUILD_DIR)/$(TARGET)"
	@$(CXX) -o $(BUILD_DIR)/$(TARGET) $(OBJECTS) $(LIB_PATHS) $(LIBS)

# preparations for testing
prep:
	@rm -f $(BUILD_DIR)/*.db

# run all tests automatically
testing: prep test_runner
	@echo "Running tests..."
	@cd $(BUILD_DIR); ./$(TARGET);

# clean build by removing
clean:
	@echo "Removing build directory: $(BUILD_DIR)"
	@rm -rf $(BUILD_DIR)
