
# Makefile for IVS Calculator Project


# Compiler & flags
CXX      = g++
CXXFLAGS = -Wall -Wextra -std=c++17 $(QT_CFLAGS)

# Qt tooling — try to find moc/uic automatically
QT_CFLAGS = $(shell pkg-config --cflags Qt6Widgets 2>/dev/null)
QT_LIBS   = $(shell pkg-config --libs Qt6Widgets 2>/dev/null)

# Locate moc and uic: try pkg-config paths, then common system locations
MOC := $(shell pkg-config --variable=libexecdir Qt6Core 2>/dev/null)/moc
UIC := $(shell pkg-config --variable=libexecdir Qt6Core 2>/dev/null)/uic

# Fallback if pkg-config didn't give us working paths
ifeq ($(wildcard $(MOC)),)
    MOC := $(shell which moc-qt6 2>/dev/null || which moc 2>/dev/null || echo /usr/lib64/qt6/libexec/moc)
endif
ifeq ($(wildcard $(UIC)),)
    UIC := $(shell which uic-qt6 2>/dev/null || which uic 2>/dev/null || echo /usr/lib64/qt6/libexec/uic)
endif

# Directories
SRCDIR   = src
DOCDIR   = $(SRCDIR)/doc
GUIDIR   = $(SRCDIR)/gui

# Targets
TARGET   = calc
STDDEV   = stddev
TEST_BIN = mathlib_tests

# Sources
SRC       = $(SRCDIR)/calc.cpp $(GUIDIR)/gui.cpp $(SRCDIR)/mathlib.cpp
MOC_SRC   = $(GUIDIR)/moc_gui.cpp
UI_HDR    = $(GUIDIR)/ui_gui.h

# Google Test
GTEST_LIBS = -lgtest -lgtest_main -lpthread

# Pack settings
TEAM     = xkurtam00_xjastrj00_xnunham00
PACK_DIR = $(TEAM)
PACK_FILE = $(PACK_DIR).zip


# Default target


.PHONY: all pack clean test doc run stddev help

all: $(TARGET) $(STDDEV)


# Qt code generation


$(MOC_SRC): $(GUIDIR)/gui.h
	$(MOC) $< -o $@

$(UI_HDR): $(GUIDIR)/gui.ui
	$(UIC) $< -o $@

# Main calculator binary


$(TARGET): $(SRC) $(UI_HDR) $(MOC_SRC)
	$(CXX) $(CXXFLAGS) -o $@ $(SRC) $(MOC_SRC) $(QT_LIBS)


# run — launch the calculator


run: $(TARGET)
	./$(TARGET)


# stddev — profiling program (standard deviation calculator)


$(STDDEV): $(SRCDIR)/profiling.cpp $(SRCDIR)/mathlib.cpp
	$(CXX) -Wall -Wextra -std=c++17 -pg -o $@ $^

stddev: $(STDDEV)


# test — build and run math library unit tests


$(TEST_BIN): $(SRCDIR)/mathlib_tests.cpp $(SRCDIR)/mathlib.cpp
	$(CXX) -Wall -Wextra -std=c++17 -o $@ $^ $(GTEST_LIBS)

test: $(TEST_BIN)
	./$(TEST_BIN)


# doc — generate Doxygen documentation


doc:
	cd $(SRCDIR) && doxygen Doxyfile


# pack — package project for submission


pack: clean doc
	mkdir -p $(PACK_DIR)/doc
	mkdir -p $(PACK_DIR)/install
	mkdir -p $(PACK_DIR)/repo
	# Documentation
	@if [ -d "$(DOCDIR)/html" ]; then \
		cp -r $(DOCDIR)/html $(PACK_DIR)/doc/; \
	fi
	# Installers
	@if [ -f "build_deb/kalkulacka.deb" ]; then \
		cp build_deb/kalkulacka.deb $(PACK_DIR)/install/; \
	fi
	@if [ -f "build_deb/profiling.deb" ]; then \
		cp build_deb/profiling.deb $(PACK_DIR)/install/; \
	fi
	# Repository — copy everything except the pack directory itself
	rsync -a --exclude=$(PACK_DIR) . $(PACK_DIR)/repo/
	# Create archive
	zip -r $(PACK_FILE) $(PACK_DIR)
	rm -rf $(PACK_DIR)

# clean — remove all generated/build files


clean:
	rm -f $(TARGET) $(STDDEV) $(TEST_BIN)
	rm -f $(GUIDIR)/ui_gui.h $(GUIDIR)/moc_gui.cpp
	rm -f $(SRCDIR)/*.o $(GUIDIR)/*.o *.o
	rm -f $(GUIDIR)/.qmake.stash $(GUIDIR)/Makefile
	rm -rf $(SRCDIR)/build $(GUIDIR)/build
	rm -rf $(DOCDIR)/html $(DOCDIR)/latex
	rm -f $(PACK_FILE)
	rm -rf $(PACK_DIR)
	rm -f $(GUIDIR)/calc $(GUIDIR)/gui
	rm -rf .idea/



# help — instructions for building on a fresh virtual machine


help:
	@echo "================================================================="
	@echo "  IVS Calculator — Build Instructions"
	@echo "================================================================="
	@echo ""
	@echo ""
	@echo "  Targets:"
	@echo "    make          Build calculator + profiling program (= make all)"
	@echo "    make run      Build and launch the calculator"
	@echo "    make test     Build and run math library unit tests"
	@echo "    make stddev   Build the standard deviation profiling program"
	@echo "    make doc      Generate Doxygen documentation"
	@echo "    make pack     Package project for submission"
	@echo "    make clean    Remove all generated files"
	@echo "    make help     Show this message"
	@echo ""
	@echo "  Quick start:"
	@echo "    make && make run"
	@echo "================================================================="