From d344e6cf17c0fda0680f05b746ad74dce0c9ecff Mon Sep 17 00:00:00 2001 From: Jerome Date: Sat, 18 Dec 2021 15:59:08 +0100 Subject: [PATCH] Adapted Makefile to allow for release/debug compiling and used new Makefile for test suite. --- Makefile | 9 ++- include/test_c_lib.h | 6 -- src/main.cpp | 6 +- src/test_c_lib.c | 1 - test/Makefile | 103 +++++++++++++++++++----------- test/{ => include}/utils_test.hpp | 0 test/{ => src}/1_test.cpp | 4 +- test/{ => src}/main_test.cpp | 0 8 files changed, 77 insertions(+), 52 deletions(-) delete mode 100644 include/test_c_lib.h delete mode 100644 src/test_c_lib.c rename test/{ => include}/utils_test.hpp (100%) rename test/{ => src}/1_test.cpp (94%) rename test/{ => src}/main_test.cpp (100%) diff --git a/Makefile b/Makefile index fbee9ab..5ea8524 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o) DEPS = $(OBJECTS:.o=.d) # flags # -COMPILE_FLAGS = -std=c++17 -Wall -Wextra -g # -O3 -funsafe-math-optimizations +COMPILE_FLAGS = -std=c++17 -Wall -Wextra # -O3 -funsafe-math-optimizations INCLUDES = -I include/ -I /usr/local/include # Space-separated pkg-config libraries used by this project LIBS = @@ -31,10 +31,15 @@ LIBS = default_target: release .PHONY: release -release: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) +release: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) -O3 # -funsafe-math-optimizations release: dirs @$(MAKE) all +.PHONY: debug +debug: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) -g +debug: dirs + @$(MAKE) all + .PHONY: dirs dirs: @echo "Creating directories" diff --git a/include/test_c_lib.h b/include/test_c_lib.h deleted file mode 100644 index 3e16bab..0000000 --- a/include/test_c_lib.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef DEF_test_c_lib -#define DEF_test_c_lib - - - -#endif diff --git a/src/main.cpp b/src/main.cpp index b378978..6ca6a6b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,14 +1,14 @@ #include -#include "utils.hpp" -#include "TestClass.hpp" +#include +#include using std::cout; using std::endl; int main() { - + return 0; } diff --git a/src/test_c_lib.c b/src/test_c_lib.c deleted file mode 100644 index dd20182..0000000 --- a/src/test_c_lib.c +++ /dev/null @@ -1 +0,0 @@ -#include "test_c_lib.h" diff --git a/test/Makefile b/test/Makefile index 3a12f85..1d7bb07 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,49 +1,76 @@ -# Declaration of variables -C = gcc -COMMON_FLAGS = -Wall -MMD -fprofile-arcs -ftest-coverage -C_FLAGS = $(COMMON_FLAGS) -CC = g++ -CC_FLAGS = $(COMMON_FLAGS) -std=c++17 -O0 -LD_FLAGS = -lgcov -INCLUDES = +CXX ?= g++ -# File names -EXEC = run -CSOURCES = $(wildcard *.c) -COBJECTS = $(CSOURCES:.c=.o) -SOURCES = $(wildcard *.cpp) -OBJECTS = $(SOURCES:.cpp=.o) +# path # +SRC_PATH = src +BUILD_PATH = build +BIN_PATH = $(BUILD_PATH)/bin -# Main target -$(EXEC): $(COBJECTS) $(OBJECTS) - $(CC) $(COBJECTS) $(OBJECTS) -o $(EXEC) $(LD_FLAGS) +# executable # +BIN_NAME = CppQuickStart_test -# To obtain object files -%.o: %.cpp - $(CC) $(INCLUDES) $(CC_FLAGS) -o $@ -c $< +# extensions # +SRC_EXT = cpp -# To obtain object files -%.o: %.c - $(C) $(INCLUDES) $(C_FLAGS) -o $@ -c $< +# code lists # +# Find all source files in the source directory, sorted by +# most recently modified +SOURCES = $(shell find $(SRC_PATH) -name '*.$(SRC_EXT)' | sort -k 1nr | cut -f2-) +# Set the object file names, with the source directory stripped +# from the path, and the build path prepended in its place +OBJECTS = $(SOURCES:$(SRC_PATH)/%.$(SRC_EXT)=$(BUILD_PATH)/%.o) +# Set the dependency files that will be used to add header dependencies +DEPS = $(OBJECTS:.o=.d) --include $(SOURCES:%.cpp=%.d) --include $(CSOURCES:%.c=%.d) +# flags # +COMPILE_FLAGS = -std=c++17 -Wall -Wextra -O0 -MMD -fprofile-arcs -ftest-coverage -g -lgcov +INCLUDES = -I include/ -I ../include/ -I /usr/local/include +# Space-separated pkg-config libraries used by this project +LIBS = -# To generate the documentation +.PHONY: default_target +default_target: debug + +.PHONY: debug +debug: export CXXFLAGS := $(CXXFLAGS) $(COMPILE_FLAGS) +debug: dirs + @$(MAKE) all + +.PHONY: dirs +dirs: + @echo "Creating directories" + @mkdir -p $(dir $(OBJECTS)) + @mkdir -p $(BIN_PATH) + +.PHONY: clean +clean: + @echo "Deleting $(BIN_NAME) symlink" + @$(RM) $(BIN_NAME) + @echo "Deleting directories" + @$(RM) -r $(BUILD_PATH) + @$(RM) -r $(BIN_PATH) + +# checks the executable and symlinks to the output +.PHONY: all +all: $(BIN_PATH)/$(BIN_NAME) + @echo "Making symlink: $(BIN_NAME) -> $<" + @$(RM) $(BIN_NAME) + @ln -s $(BIN_PATH)/$(BIN_NAME) $(BIN_NAME) + +.PHONY: doc doc: doxygen Doxyfile -# To generate human-readable coverage data -cov: - gcov *.gcda +# Creation of the executable +$(BIN_PATH)/$(BIN_NAME): $(OBJECTS) + @echo "Linking: $@" + $(CXX) $(OBJECTS) -o $@ ${LIBS} -# To remove all the files generated by gcov -cleancov: - rm -f *.gcov *.gcda +# Add dependency files, if they exist +-include $(DEPS) -# To remove generated files -clean: cleancov - rm -f $(COBJECTS) $(OBJECTS) $(SOURCES:%.cpp=%.d) $(CSOURCES:%.c=%.d) *.gcno - -cleaner: clean - rm -f $(EXEC) +# Source file rules +# After the first compilation they will be joined with the rules from the +# dependency files to provide header dependencies +$(BUILD_PATH)/%.o: $(SRC_PATH)/%.$(SRC_EXT) + @echo "Compiling: $< -> $@" + $(CXX) $(CXXFLAGS) $(INCLUDES) -MP -MMD -c $< -o $@ diff --git a/test/utils_test.hpp b/test/include/utils_test.hpp similarity index 100% rename from test/utils_test.hpp rename to test/include/utils_test.hpp diff --git a/test/1_test.cpp b/test/src/1_test.cpp similarity index 94% rename from test/1_test.cpp rename to test/src/1_test.cpp index fa45c9b..4d0920b 100644 --- a/test/1_test.cpp +++ b/test/src/1_test.cpp @@ -1,8 +1,8 @@ #include #include -#include "../utils.hpp" -#include "utils_test.hpp" +#include +#include #define print(x) PRINT_VAR(x); #define printvec(x) PRINT_VEC(x); diff --git a/test/main_test.cpp b/test/src/main_test.cpp similarity index 100% rename from test/main_test.cpp rename to test/src/main_test.cpp