#
# PDS HW3 Makefile
#
# Copyright (C) 2025 Christos Choutouridis <christos@choutouridis.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# ============== Project settings ==============
# Project's name
PROJECT         := PDS_homework_3

# Excecutable's name
TARGET          := bitonicCUDA

# Source directories list(space seperated). Makefile-relative path, UNDER current directory.
SRC_DIR_LIST    := src #test test/gtest

# Include directories list(space seperated). Makefile-relative path.
INC_DIR_LIST    := src
#                   test \
#                   test/gtest/ \


# Exclude files list(space seperated). Filenames only.
# EXC_FILE_LIST := bad.cpp old.cpp

# Build directories
BUILD_DIR       := bin
OBJ_DIR         := $(BUILD_DIR)/obj
DEP_DIR         := $(BUILD_DIR)/.dep

OUTPUT_DIR      := out

# ========== Compiler settings ==========
# Compiler flags for debug and release
DEB_CFLAGS      := -DDEBUG -std=c11 -Xcompiler "-Wall -Wextra -g -DDEBUG"
REL_CFLAGS      := -O3 -std=c11 -Xcompiler "-Wall -Wextra"
DEB_CXXFLAGS    := -DDEBUG -std=c++17 -Xcompiler "-Wall -Wextra -g -DDEBUG" 
REL_CXXFLAGS    := -O3 -std=c++17 -Xcompiler "-Wall -Wextra"

# Pre-defines
# PRE_DEFS := MYCAB=1729 SUPER_MODE
PRE_DEFS        := TARGET=$(TARGET)

# ============== Linker settings ==============
# Linker flags (example: -pthread -lm)
LDFLAGS         :=

# Map output file
MAP_FILE        := # output.map
MAP_FLAG        := # -Xlinker -Map=$(BUILD_DIR)/$(MAP_FILE)

# ============== Docker settings ==============
# We need:
#  - Bind the entire project directory(the dir that includes all the code) as volume.
#  - In docker instance, change to working directory(where the makefile is).
DOCKER_VOL_DIR  := $(shell pwd)
DOCKER_WRK_DIR  :=
DOCKER_RUN      := docker run --rm
DOCKER_FLAGS    := -v $(DOCKER_VOL_DIR):/usr/src/$(PROJECT) -w /usr/src/$(PROJECT)/$(DOCKER_WRK_DIR)

# docker invoke mechanism (edit with care)
#   note:
#   Here, `DOCKER` variable is empty. Rules can assign `DOCKER := DOCKER_CMD` when docker
#   functionality is needed.
DOCKER_CMD      = $(DOCKER_RUN) $(DOCKER_FLAGS) $(IMAGE)
DOCKER          :=

# ============== Tool selection ==============
# compiler and compiler flags.
CSIZE           := size
CFLAGS          := $(DEB_CFLAGS)
CXXFLAGS        := $(DEB_CXXFLAGS)
CXX             := g++
CC              := gcc
LINKER          := g++

#
# =========== Main body and Patterns ===========
#

INC     := $(foreach dir,$(INC_DIR_LIST),-I$(dir))
DEF     := $(foreach def,$(PRE_DEFS),-D$(def))
EXC     := $(foreach fil,$(EXC_FILE_LIST),                              \
               $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/$(fil))) \
           )
# source files. object and dependencies list
# recursive search into current and source directories
SRC     := $(wildcard *.cpp)
SRC     += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/*.cpp))
SRC     += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/**/*.cpp))
SRC     := $(filter-out $(EXC),$(SRC))
#SRC     := $(abspath $(SRC))

OBJ     := $(foreach file,$(SRC:%.cpp=%.o),$(OBJ_DIR)/$(file))
DEP     := $(foreach file,$(SRC:%.cpp=%.d),$(DEP_DIR)/$(file))


# c file objects depent on .c AND dependency files, which have an empty recipe 
$(OBJ_DIR)/%.o: %.c
	@mkdir -p $(@D)
	$(DOCKER) $(CC) -c $(CFLAGS) $(INC) $(DEF) -o $@ $<

# cpp file objects depend on .cpp AND dependency files, which have an empty recipe
$(OBJ_DIR)/%.o: %.cpp
	@mkdir -p $(@D)
	$(DOCKER) $(CXX) -c $(CXXFLAGS) $(INC) $(DEF) -o $@ $<

# main target rule
$(BUILD_DIR)/$(TARGET): $(OBJ)
	@mkdir -p $(@D)
	@echo Linking to target: $(TARGET)
	@echo $(DOCKER) $(LINKER) '$$(OBJ)' $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET)
	@$(DOCKER) $(LINKER) $(OBJ) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET)
	@echo
	@echo Print size information
	@$(CSIZE) $(@D)/$(TARGET)
	@echo Done


#
# ================ Default local build rules =================
# example:
# make debug

.DEFAULT_GOAL := all

.PHONY: clean
clean:
	@echo Cleaning build directories
	@rm -rf $(OBJ_DIR)
	@rm -rf $(DEP_DIR)
	@rm -rf $(BUILD_DIR)

debug: CFLAGS := $(DEB_CFLAGS)
debug: $(BUILD_DIR)/$(TARGET)

release: CFLAGS := $(REL_CFLAGS)
release: $(BUILD_DIR)/$(TARGET)

#
# ================ Build rules =================
#

bitonic_v0deb: CC := nvcc -G -g -x cu
bitonic_v0deb: CXX := nvcc -G -g -x cu
bitonic_v0deb: LINKER := nvcc
bitonic_v0deb: CFLAGS := $(DEB_CFLAGS) -DCODE_VERSION=V0
bitonic_v0deb: CXXFLAGS := $(DEB_CXXFLAGS) -DCODE_VERSION=V0
bitonic_v0deb: OUTPUT_DIR := $(OUTPUT_DIR)/v0
bitonic_v0deb: $(BUILD_DIR)/$(TARGET)
	@mkdir -p $(OUTPUT_DIR)
	cp $(BUILD_DIR)/$(TARGET) $(OUTPUT_DIR)/$(TARGET)


bitonic_v1deb: CC := nvcc -G -g -x cu
bitonic_v1deb: CXX := nvcc -G -g -x cu
bitonic_v1deb: LINKER := nvcc
bitonic_v1deb: CFLAGS := $(DEB_CFLAGS) -DCODE_VERSION=V1
bitonic_v1deb: CXXFLAGS := $(DEB_CXXFLAGS) -DCODE_VERSION=V1
bitonic_v1deb: OUTPUT_DIR := $(OUTPUT_DIR)/v1
bitonic_v1deb: $(BUILD_DIR)/$(TARGET)
	@mkdir -p $(OUTPUT_DIR)
	cp $(BUILD_DIR)/$(TARGET) $(OUTPUT_DIR)/$(TARGET)


bitonic_v2deb: CC := nvcc -G -g -x cu
bitonic_v2deb: CXX := nvcc -G -g -x cu
bitonic_v2deb: LINKER := nvcc
bitonic_v2deb: CFLAGS := $(DEB_CFLAGS) -DCODE_VERSION=V2
bitonic_v2deb: CXXFLAGS := $(DEB_CXXFLAGS) -DCODE_VERSION=V2
bitonic_v2deb: OUTPUT_DIR := $(OUTPUT_DIR)/v2
bitonic_v2deb: $(BUILD_DIR)/$(TARGET)
	@mkdir -p $(OUTPUT_DIR)
	cp $(BUILD_DIR)/$(TARGET) $(OUTPUT_DIR)/$(TARGET)


bitonic_ser: CC := nvcc -x cu
bitonic_ser: CXX := nvcc -x cu
bitonic_ser: LINKER := nvcc
bitonic_ser: CFLAGS := $(REL_CFLAGS) -DCODE_VERSION=SERIAL
bitonic_ser: CXXFLAGS := $(REL_CXXFLAGS) -DCODE_VERSION=SERIAL
bitonic_ser: OUTPUT_DIR := $(OUTPUT_DIR)/serial
bitonic_ser: $(BUILD_DIR)/$(TARGET)
	@mkdir -p $(OUTPUT_DIR)
	cp $(BUILD_DIR)/$(TARGET) $(OUTPUT_DIR)/$(TARGET)


bitonic_v0: CC := nvcc -x cu
bitonic_v0: CXX := nvcc -x cu
bitonic_v0: LINKER := nvcc
bitonic_v0: CFLAGS := $(REL_CFLAGS) -DCODE_VERSION=V0
bitonic_v0: CXXFLAGS := $(REL_CXXFLAGS) -DCODE_VERSION=V0
bitonic_v0: OUTPUT_DIR := $(OUTPUT_DIR)/v0
bitonic_v0: $(BUILD_DIR)/$(TARGET)
	@mkdir -p $(OUTPUT_DIR)
	cp $(BUILD_DIR)/$(TARGET) $(OUTPUT_DIR)/$(TARGET)


bitonic_v1: CC := nvcc -x cu
bitonic_v1: CXX := nvcc -x cu
bitonic_v1: LINKER := nvcc
bitonic_v1: CFLAGS := $(REL_CFLAGS) -DCODE_VERSION=V1
bitonic_v1: CXXFLAGS := $(REL_CXXFLAGS) -DCODE_VERSION=V1
bitonic_v1: OUTPUT_DIR := $(OUTPUT_DIR)/v1
bitonic_v1: $(BUILD_DIR)/$(TARGET)
	@mkdir -p $(OUTPUT_DIR)
	cp $(BUILD_DIR)/$(TARGET) $(OUTPUT_DIR)/$(TARGET)

bitonic_v2: CC := nvcc -x cu
bitonic_v2: CXX := nvcc -x cu
bitonic_v2: LINKER := nvcc
bitonic_v2: CFLAGS := $(REL_CFLAGS) -DCODE_VERSION=V2
bitonic_v2: CXXFLAGS := $(REL_CXXFLAGS) -DCODE_VERSION=V2
bitonic_v2: OUTPUT_DIR := $(OUTPUT_DIR)/v2
bitonic_v2: $(BUILD_DIR)/$(TARGET)
	@mkdir -p $(OUTPUT_DIR)
	cp $(BUILD_DIR)/$(TARGET) $(OUTPUT_DIR)/$(TARGET)


hpc-build:
	make clean
	make bitonic_v0
	make clean
	make bitonic_v1
	make clean
	make bitonic_v2


all: debug bitonic_v0
# Note:
#	Add a gcc based make rule here in order for clangd to successfully scan the project files.
#	Otherwise we do not need the gcc build.