From 64dc18280cac281cbee2b3a59bc992ad65688bdf Mon Sep 17 00:00:00 2001 From: Christos Choutouridis Date: Wed, 25 Dec 2024 10:02:48 +0200 Subject: [PATCH] HW2: Init C++ project --- homework_2/Makefile | 216 ++++++++++++++++++++++++++++++++++++++++ homework_2/src/main.cpp | 3 + 2 files changed, 219 insertions(+) create mode 100644 homework_2/Makefile create mode 100644 homework_2/src/main.cpp diff --git a/homework_2/Makefile b/homework_2/Makefile new file mode 100644 index 0000000..c573b2e --- /dev/null +++ b/homework_2/Makefile @@ -0,0 +1,216 @@ +# +# PDS homework_1 Makefile +# +# Copyright (C) 2024 Christos Choutouridis +# +# 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 . +# + +# ============== Project settings ============== +# Project's name +PROJECT := PDS_homework_2 +# Excecutable's name +TARGET := distbitonic +# Source directories list(space seperated). Makefile-relative path, UNDER current directory. +SRC_DIR_LIST := src +# Include directories list(space seperated). Makefile-relative path. +INC_DIR_LIST := inc \ + src \ + +# 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 + + +# ========== Compiler settings ========== +# Compiler flags for debug and release +DEB_CFLAGS := -DDEBUG -g3 -Wall -Wextra -std=c11 +REL_CFLAGS := -Wall -Wextra -O3 -std=c11 +DEB_CXXFLAGS := -DDEBUG -g3 -Wall -Wextra -std=c++17 +REL_CXXFLAGS := -Wall -Wextra -O3 -std=c++17 + +# Pre-defines +# PRE_DEFS := MYCAB=1729 SUPER_MODE +PRE_DEFS := + +# ============== Linker settings ============== +# Linker flags (example: -pthread -lm) +LDFLAGS := -pthread + +# 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 icludes 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 + +# +# =========== Main body and Patterns =========== +# + +#ifeq ($(OS), Windows_NT) +# TARGET := $(TARGET).exe +#endif +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)) + + +# Make Dependencies pattern. +# This little trick enables recompilation only when dependencies change +# and it does so for changes both in source AND header files ;) +# +# It is based on Tom Tromey's method. +# +# Invoke cpp to create makefile rules with dependencies for each source file +$(DEP_DIR)/%.d: %.c + @mkdir -p $(@D) + @$(DOCKER) $(CC) -E $(CFLAGS) $(INC) $(DEF) -MM -MT $(OBJ_DIR)/$(<:.c=.o) -MF $@ $< + +# c file objects depent on .c AND dependency files, which have an empty recipe +$(OBJ_DIR)/%.o: %.c $(DEP_DIR)/%.d + @mkdir -p $(@D) + @$(DOCKER) $(CC) -c $(CFLAGS) $(INC) $(DEF) -o $@ $< + +$(DEP_DIR)/%.d: %.cpp + @mkdir -p $(@D) + @$(DOCKER) $(CXX) -E $(CXXFLAGS) $(INC) $(DEF) -MM -MT $(OBJ_DIR)/$(<:.cpp=.o) -MF $@ $< + +# cpp file objects depent on .cpp AND dependency files, which have an empty recipe +$(OBJ_DIR)/%.o: %.cpp $(DEP_DIR)/%.d + @mkdir -p $(@D) + @$(DOCKER) $(CXX) -c $(CXXFLAGS) $(INC) $(DEF) -o $@ $< + +# empty recipe for dependency files. This prevents make errors +$(DEP): + +# now include all dependencies +# After all they are makefile dependency rules ;) +include $(wildcard $(DEP)) + +# main target rule +$(BUILD_DIR)/$(TARGET): $(OBJ) + @mkdir -p $(@D) + @echo Linking to target: $(TARGET) + @echo $(DOCKER) $(CXX) '$$(OBJ)' $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) + @$(DOCKER) $(CXX) $(OBJ) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) + @echo + @echo Print size information + @$(CSIZE) $(@D)/$(TARGET) + @echo Done + +.PHONY: clean +clean: + @echo Cleaning build directories + @rm -rf $(OBJ_DIR) + @rm -rf $(DEP_DIR) + @rm -rf $(BUILD_DIR) + +# +# ================ Local build rules ================= +# example: +# make debug + +debug: CFLAGS := $(DEB_CFLAGS) +debug: $(BUILD_DIR)/$(TARGET) + +release: CFLAGS := $(REL_CFLAGS) +release: $(BUILD_DIR)/$(TARGET) + +all: release + +hpc-results/post: + $(CXX) $(CFLAGS) -o $@ hpc-results/main.cpp + +hpc-clean: + rm hpc-results/post + +# +# ================ Local (and/or) via docker build rules ================= +# +# examples: +# make IMAGE=hpcimage v0 +# make IMAGE=hpcimage v1_cilk +# +dist_v05: CFLAGS := $(DEB_CFLAGS) -DCODE_VERSION=50 +dist_v05: CXXFLAGS := $(DEB_CXXFLAGS) -DCODE_VERSION=50 +dist_v05: TARGET := dist_v05 +dist_v05: $(BUILD_DIR)/$(TARGET) + +dist_v1: CFLAGS := $(DEB_CFLAGS) -DCODE_VERSION=100 +dist_v1: CXXFLAGS := $(DEB_CXXFLAGS) -DCODE_VERSION=100 +dist_v1: TARGET := dist_v1 +dist_v1: $(BUILD_DIR)/$(TARGET) + +# +# ========= Inside CSAL Image build rules =========== +# +# 1) first jump into image (make sure you are in the directory where Makefile is): +# > docker run -it -v ${PWD}:/usr/src/exercise_1 -w /usr/src/exercise_1/ hpcimage +# 2) Clean binaries first **important** +# > make clean +# 3) for v4 cilk for example: +# > make csal_v4_cilk +# 4) run executables from `bin/`. Examples: +# > ./bin/tcount_ompv3 -i mtx/NACA0015.mtx --timing -r 3 -o /dev/null +# > ./bin/tcount_pthv4 -i mtx/com_Youtube.mtx --timing --dynamic --print_count + + + +# +# ======== Run from container ========= +# +# examples: +# +# make IMAGE=hpcimage EXEC=knnsearch_v1 run +# make IMAGE=hpcimage EXEC=knnsearch_v1 run +# + diff --git a/homework_2/src/main.cpp b/homework_2/src/main.cpp new file mode 100644 index 0000000..a51103c --- /dev/null +++ b/homework_2/src/main.cpp @@ -0,0 +1,3 @@ +int main(void) { + return 0; +} \ No newline at end of file