CXX := g++
CXXFLAGS := -std=c++20 -O2 -Wall -Wextra -Wpedantic -pthread
LDFLAGS := -pthread -lrt
ORCH_LDFLAGS := $(LDFLAGS) -lusb-1.0

BUILD_DIR := build
BIN_DIR := $(BUILD_DIR)/bin

INCLUDES := \
	-Idata_acq_and_processing/common_cpp/ipc/include \
	-Idata_acq_and_processing/common_cpp/config/include \
	-Idata_acq_and_processing/third_party \
	-Idata_acq_and_processing/third_party/eigen \
	-Idata_acq_and_processing/sweep_orchestrator/include \
	-Idata_acq_and_processing/sweep_orchestrator/device_drivers/interfaces \
	-Idata_acq_and_processing/sweep_orchestrator/device_drivers/radar \
	-Idata_acq_and_processing/sweep_orchestrator/device_drivers/switches \
	-Idata_acq_and_processing/preprocessing/calibration_master/include \
	-Idata_acq_and_processing/preprocessing/reference_master/include \
	-Idata_acq_and_processing/preprocessing/data_preprocessor/include \
	-Idata_acq_and_processing/processing/processors/include \
	-Idata_acq_and_processing/processing/data_processor/include

COMMON_SOURCES := \
	data_acq_and_processing/common_cpp/ipc/src/shm_ring.cpp \
	data_acq_and_processing/common_cpp/ipc/src/shared_types.cpp \
	data_acq_and_processing/common_cpp/config/src/run_config.cpp

ORCH_SOURCES := \
	data_acq_and_processing/sweep_orchestrator/device_drivers/radar/librevna/librevna_minimal_driver_lifecycle.cpp \
	data_acq_and_processing/sweep_orchestrator/device_drivers/radar/librevna/librevna_minimal_driver_transport.cpp \
	data_acq_and_processing/sweep_orchestrator/device_drivers/radar/librevna/librevna_minimal_driver_protocol.cpp \
	data_acq_and_processing/sweep_orchestrator/device_drivers/switches/h7992_minimal_driver.cpp \
	data_acq_and_processing/sweep_orchestrator/device_drivers/switches/hmc349a_minimal_driver.cpp \
	data_acq_and_processing/sweep_orchestrator/src/sweep_plan.cpp \
	data_acq_and_processing/sweep_orchestrator/src/sweep_orchestrator.cpp \
	data_acq_and_processing/sweep_orchestrator/src/main.cpp

PREPROC_SOURCES := \
	data_acq_and_processing/preprocessing/calibration_master/src/calibration_master.cpp \
	data_acq_and_processing/preprocessing/calibration_master/src/through_calibrator.cpp \
	data_acq_and_processing/preprocessing/reference_master/src/reference_master.cpp \
	data_acq_and_processing/preprocessing/data_preprocessor/src/data_preprocessor.cpp \
	data_acq_and_processing/preprocessing/data_preprocessor/src/main.cpp

PROCESSOR_SOURCES := \
	data_acq_and_processing/processing/processors/src/bscan_processor.cpp \
	data_acq_and_processing/processing/processors/src/passthrough_processor.cpp \
	data_acq_and_processing/processing/data_processor/src/processing_live_config.cpp \
	data_acq_and_processing/processing/data_processor/src/data_processor.cpp \
	data_acq_and_processing/processing/data_processor/src/main.cpp

SWEEP_ORCH_OBJS := $(addprefix $(BUILD_DIR)/,$(COMMON_SOURCES:.cpp=.o) $(ORCH_SOURCES:.cpp=.o))
PREPROCESSOR_OBJS := $(addprefix $(BUILD_DIR)/,$(COMMON_SOURCES:.cpp=.o) $(PREPROC_SOURCES:.cpp=.o))
DATA_PROCESSOR_OBJS := $(addprefix $(BUILD_DIR)/,$(COMMON_SOURCES:.cpp=.o) $(PROCESSOR_SOURCES:.cpp=.o))

TARGETS := \
	$(BIN_DIR)/sweep_orchestrator \
	$(BIN_DIR)/data_preprocessor \
	$(BIN_DIR)/data_processor

.PHONY: all clean

all: $(TARGETS)

$(BIN_DIR)/sweep_orchestrator: $(SWEEP_ORCH_OBJS)
	@mkdir -p $(BIN_DIR)
	$(CXX) $(SWEEP_ORCH_OBJS) -o $@ $(ORCH_LDFLAGS)

$(BIN_DIR)/data_preprocessor: $(PREPROCESSOR_OBJS)
	@mkdir -p $(BIN_DIR)
	$(CXX) $(PREPROCESSOR_OBJS) -o $@ $(LDFLAGS)

$(BIN_DIR)/data_processor: $(DATA_PROCESSOR_OBJS)
	@mkdir -p $(BIN_DIR)
	$(CXX) $(DATA_PROCESSOR_OBJS) -o $@ $(LDFLAGS)

$(BUILD_DIR)/%.o: %.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

clean:
	rm -rf $(BUILD_DIR)
