diff --git a/C/.ccls b/C/.ccls deleted file mode 100644 index 90584dd..0000000 --- a/C/.ccls +++ /dev/null @@ -1 +0,0 @@ -gcc diff --git a/C/.gitignore b/C/.gitignore index 4239b68..378eac2 100644 --- a/C/.gitignore +++ b/C/.gitignore @@ -1,2 +1 @@ -.ccls-cache build diff --git a/C/.vscode/tasks.json b/C/.vscode/tasks.json index dc4aebd..4d2fc7a 100644 --- a/C/.vscode/tasks.json +++ b/C/.vscode/tasks.json @@ -21,7 +21,7 @@ }, { "label": "Clean", - "detail": "Clean build directory and executable", + "detail": "Clean build directory", "type": "shell", "command": "make", "args": [ @@ -37,6 +37,25 @@ "clear": true } }, + { + "label": "Run", + "detail": "Build project and run", + "type": "shell", + "command": "make", + "args": [ + "-j$(nproc)", + "run" + ], + "group": "build", + "presentation": { + "echo": true, + "reveal": "always", + "focus": true, + "panel": "shared", + "showReuseMessage": true, + "clear": true + } + }, { "label": "Build Verbose", "detail": "Build project with verbosity on", @@ -56,11 +75,32 @@ } }, { - "label": "Run", - "detail": "Run executable", + "label": "Build Debug", + "detail": "Build project in debug mode", "type": "shell", "command": "make", "args": [ + "-j$(nproc)", + "DEBUG=2" + ], + "group": "build", + "presentation": { + "echo": true, + "reveal": "always", + "focus": true, + "panel": "shared", + "showReuseMessage": true, + "clear": true + } + }, + { + "label": "Run Debug", + "detail": "Build project in debug mode and run", + "type": "shell", + "command": "make", + "args": [ + "-j$(nproc)", + "DEBUG=2", "run" ], "group": "build", @@ -73,5 +113,5 @@ "clear": true } } - ] + ] } \ No newline at end of file diff --git a/C/Makefile b/C/Makefile index 0f48d4d..8490ae0 100644 --- a/C/Makefile +++ b/C/Makefile @@ -1,32 +1,47 @@ -# Build folder +export + +SHELL = /usr/bin/env bash + +ROOT := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) + +# +# User settings +# + BUILD_DIR = build -# Source files -SOURCES := \ - src/main.c +SOURCE_DIR = src -# Output target name -OUTPUT := C +OUTPUT = C_Executable -# Compiler and flags CC = gcc -CCFLAGS = -Wall -Wextra -std=c17 -LDFLAGS = +CFLAGS = --std=c17 -Os +LD_FLAGS = -# Include directories -INCLUDE_DIRS = +ifneq ($(DEBUG),) +CFLAGS += -O1 -g -DDEBUG=$(DEBUG) -Wextra -Wall --pedantic -Wno-missing-braces \ + -Wno-missing-field-initializers -Wformat=2 -Wswitch-default -Wswitch \ + -Wcast-align -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \ + -Wstrict-prototypes -Winline -Wundef -Wnested-externs -Wcast-qual \ + -Wshadow -Wunreachable-code -Wlogical-op -Wfloat-equal -Wstrict-aliasing=2 \ + -Wredundant-decls -Wold-style-definition +endif + +# +# End +# + +# Relocate vars +BUILD_DIR := $(ROOT)/$(BUILD_DIR) + +SOURCE_DIR := $(ROOT)/$(SOURCE_DIR) -# Change output location OUTPUT := $(BUILD_DIR)/$(OUTPUT) -# Deduce objects -OBJECTS = $(SOURCES:%.c=$(BUILD_DIR)/%.o) - -# Verbose mode +# Verbosity +export Q := @ ifeq ($(VERBOSE), 1) Q := -else -Q := @ endif # Colors @@ -34,44 +49,20 @@ GREEN := \033[32m YELLOW := \033[33m RESET := \033[m -# Current file nb to process -CURRENT_FILE := 0 - # Default targets -all: count_build build_dir end +all: $(OUTPUT) -# Create build directory -build_dir: - $(Q)mkdir -p $(dir $(OBJECTS)) +$(OUTPUT): FORCE + $(Q)cd $(SOURCE_DIR) && $(MAKE) $@ -# Link target -$(OUTPUT): $(OBJECTS) - @echo -e "[100%] $(YELLOW)Linking $(OUTPUT)$(RESET)" - $(Q)$(CC) $(LDFLAGS) -o $@ $(OBJECTS) +FORCE: -# Build .o files from .c -$(BUILD_DIR)/%.o: %.c - $(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE)+1)))) - $(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE)*100/$(TOTAL_FILES))))) - @echo -e "[$(PERCENTAGE)%] $(GREEN)Building C object $@$(RESET)" - $(Q)$(CC) $(CCFLAGS) $(INCLUDE_DIRS:%=-I%) -MMD -MP -c $< -o $@ - -# Source files dependencies --include $(OBJECTS:.o=.d) - -count_build: - $(eval export TOTAL_FILES := $(shell echo $$(($$(make -n $(OBJECTS) 2>/dev/null | grep -c "Building") + 1)))) - -# Print ending message -end: $(OUTPUT) - @echo "Built target $(OUTPUT)" - -# Clean -.PHONY: clean +.PHONY: clean run print-var-% clean: $(Q)rm -rf $(BUILD_DIR) -# Run executable -.PHONY: run run: all - $(Q)./$(OUTPUT) + $(Q)cd $(BUILD_DIR) && ./$(notdir $(OUTPUT)) + +print-var-% : + @echo $($*) diff --git a/C/src/Makefile b/C/src/Makefile new file mode 100644 index 0000000..9411342 --- /dev/null +++ b/C/src/Makefile @@ -0,0 +1,23 @@ +SOURCES += \ + main.c + +INCLUDES += + +OBJECTS := $(SOURCES:%=$(BUILD_DIR)/$(notdir $(SOURCE_DIR))/%.o) + +ifeq ($(TOTAL_BUILD_FILES),) +TOTAL_BUILD_FILES := $(shell echo $$(($$($(MAKE) -n $(OBJECTS) TOTAL_BUILD_FILES=1 2>/dev/null | grep -c Building) + 1))) +endif + +$(OUTPUT): $(OBJECTS) + @echo "[100%] $(YELLOW)Linking $(notdir $(OUTPUT))$(RESET)" + $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) -o $@ + +$(BUILD_DIR)/$(notdir $(SOURCE_DIR))/%.o: $(SOURCE_DIR)/% + $(Q)mkdir -p $(dir $@) + $(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE) + 1)))) + $(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE) * 100 / $(TOTAL_BUILD_FILES))))) + @echo "[$(shell printf "%3d" $(PERCENTAGE))%] $(GREEN)Building C object $@$(RESET)" + $(Q)$(CC) $(CFLAGS) $(INCLUDES:%=-I%) -MMD -MP -c $< -o $@ + +-include $(OBJECTS:.o=.d)