From 5e748a96af9537ec94e7687edfefc8487128d7bf Mon Sep 17 00:00:00 2001 From: Steins7 Date: Sun, 22 Sep 2024 17:24:13 +0200 Subject: [PATCH] Setup basic hello world Adds minimal Makefile and main.c --- Makefile | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 8 +++ 2 files changed, 172 insertions(+) create mode 100644 Makefile create mode 100644 src/main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f3e4789 --- /dev/null +++ b/Makefile @@ -0,0 +1,164 @@ +# --- minimal linux makefile ------------------------------------------------- # +# This makefile works on linux with options to crosscompile by changing the # +# TARGET option. By default, compile the defined application for the native # +# platform. # +# # +# Before use, make sure to adapt the parameters in the following sections # +# - project architecture : general folder layout # +# - toolchain configuration : compiler to use and other little things # +# - defines settings : list of defines to pass to the compiler # +# - general settings : include control and release options # +# ---------------------------------------------------------------------------- # + +# --- project architecture +# program name +EXE_PREFIX=main +# project folder architecture +SRC=src +BIN=bin +INC= +OBJ=$(BIN)/obj + +# --- toolchain configuration +TARGET= +CC=$(TARGET)gcc +AS=$(TARGET)gcc -x assembler-with-cpp -c + +# --- defines settings +#list all C defines here with -D before the name +C_DEFS= +#list all ASM defines here with -D before the name +A_DEFS= +#list all linker flags here with -l before the name +LDFLAGS= + +# --- general settings +# RELEASE=0 -> disable optimisation, then enable debug +# RELEASE=1 -> enable optimisation, then disable debug +RELEASE=0 + +#------------------------------------------------------------------------------- +# files discovery and paths solving +# down here is black magic, you probably don't want to modify anything + +#list all directories, sort them and remove duplicates +DIRS:=$(sort $(shell find . -type d)) +#find paths to src files from directory list +SRC_PATHS=$(foreach DIR, $(SRC), $(filter ./$(DIR)%, $(DIRS))) + +#list c files +C_FILES=$(foreach PATH, . $(SRC_PATHS), $(wildcard $(PATH)/*.c)) +#filter useless STmcube files +ifeq ($(strip $(FILTER)), 1) + C_FILES:=$(filter-out %template.c, $(C_FILES)) +endif + +#list h files +#H_FILES=$(foreach PATH, $(INC_PATHS), $(wildcard $(PATH)/*.h)) +#list asm files +ASM_FILES=$(foreach PATH, . $(SRC_PATHS), $(wildcard $(PATH)/*.s)) + +#separate main c file from other c files +MAIN_C_FILES:=$(filter %$(EXE_PREFIX).c, $(C_FILES)) +C_FILES:=$(filter-out %$(EXE_PREFIX).c, $(C_FILES)) + +#create new paths to store o files +MAIN_OBJ_FILES=$(patsubst ./%.c, ./$(OBJ)/%.o, $(MAIN_C_FILES)) +OBJ_FILES=$(patsubst ./%.c, ./$(OBJ)/%.o, $(C_FILES)) \ + $(patsubst ./%.s, ./$(OBJ)/%.o, $(ASM_FILES)) +DEPEND_FILES=$(MAIN_OBJ_FILES:%.o,%.d)$(OBJ_FILES:%.o,%.d) + +#------------------------------------------------------------------------------- +# makefile incantation + +# --- include path flag management +ifneq ($(strip $(INC)),) + INCLUDE=-I$(INC) +else + INCLUDE= +endif + +# --- release flag management +ifeq ($(strip $(RELEASE)), 0) + CFLAGS=-g3 -Os + LDFLAGS=-g3 -Os +else + CFLAGS=-O2 +endif + +# --- flags definition +#merge all c compiler options +CFLAGS+=$(CPUFLAGS) -std=c23 -Wall -Wextra -Warray-bounds \ + -Wno-unused-parameter -fomit-frame-pointer $(C_DEFS) +#merge all asm compiler options +ASFLAG=-Wa,--gdwarf2 -D__ASSEMBLY__ $(C_DEFS) $(A_DEFS) +#merge all linker options +LDFLAGS+=-lc -lgcc -lgcov -lm -Wl,-Map=$@.map,--gc-sections + +# --- folder creation +DIR_GUARD=@mkdir -p $(@D) + +#------------------------------------------------------------------------------- +# makefile rules + +# --- main rule +all: $(BIN)/$(EXE_PREFIX) + +# --- rules configuration +rebuild : clean all + +.SUFFIXES: +.SECONDARY: +#define rules name +.PHONY: all clean rebuild + +# --- secondary rules +#exec file compilation +$(BIN)/$(EXE_PREFIX) : $(MAIN_OBJ_FILES) $(OBJ_FILES) + @echo + @printf '\e[1;32m==== linking $@ ====\e[0m' + @echo + $(DIR_GUARD) + $(CC) $(LIBRARIES) $(LDFLAGS) $(CFLAGS) -o $@ $^ + @$(SKIP_LINE) + +#c files compilation +$(OBJ)/%.o : %.c + @echo + @printf '\e[1;34m==== compiling [opt=$(opt)] $< ====\e[0m' + @echo + $(DIR_GUARD) + $(CC) $(INCLUDE) -c $(CFLAGS) $< -o $@ $(LIBRARIES) + @$(SKIP_LINE) + +$(BIN)/%.o : %.c + @echo + @printf '\e[1;34m==== compiling [opt=$(opt)] $< ====\e[0m' + @echo + $(DIR_GUARD) + $(CC) $(INCLUDE) -c $(CFLAGS) $< -o $@ $(LIBRARIES) + @$(SKIP_LINE) + +#asm files compilation +$(OBJ)/%.o : %.s + @echo + @printf '\e[1;35m==== compiling [opt=$(opt)] $^ $@ $(LIBRARIES)====\e[0m' + @echo + $(DIR_GUARD) + $(AS) $(ASFLAGS) $< -o $@ $(LIBRARIES) + @$(SKIP_LINE) + +$(BIN)/%.o : %.s + @echo + @printf '\e[1;35m==== compiling [opt=$(opt)] $^ $@ $(LIBRARIES)====\e[0m' + @echo + $(DIR_GUARD) + $(AS) $(ASFLAGS) $< -o $@ $(LIBRARIES) + @$(SKIP_LINE) + +# --- remove generated files +clean: + -rm -rf $(BIN)/* + +-include $(DEPEND_FILES) + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..35efec3 --- /dev/null +++ b/src/main.c @@ -0,0 +1,8 @@ + +#include + +int main(void) { + printf("hello world\n"); + return 0; +} +