test.c/Makefile
2019-07-13 22:36:04 +02:00

57 lines
1.2 KiB
Makefile

CROSS_COMPILE ?=
TARGET_FILE ?= test
CC = $(CROSS_COMPILE)gcc
CPP = $(CROSS_COMPILE)cpp
SRC_DIR = src
OBJ_DIR = obj
BIN_DIR = bin
INCLUDES := inc
#Alpine Linux names libftdi libftdi1
ifneq "$(findstring Alpine,$(shell head -1 /etc/issue))" ""
INCLUDES += /usr/include/libftdi1
LIBS := ftdi1
else
LIBS := ftdi
endif
LD_FLAGS :=
C_FLAGS := -O0 -g
CPP_FLAGS := $(addprefix -I, $(INCLUDES))
C_SRCS = $(wildcard $(SRC_DIR)/*.c)
C_OBJS = $(patsubst $(SRC_DIR)%,$(OBJ_DIR)%,$(patsubst %.c,%.o,$(C_SRCS)))
OBJS = $(C_OBJS)
TARGET = $(BIN_DIR)/$(TARGET_FILE)
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
.PHONY: all
all: $(TARGET)
$(TARGET): $(OBJS) $(THIS_MAKEFILE)
@mkdir -p $(BIN_DIR)
$(CC) $(C_FLAGS) $(LD_FLAGS) $(OBJS) $(addprefix -l,$(LIBS)) -o $@
.PRECIOUS: $(OBJ_DIR)/%.d
$(OBJ_DIR)/%.d: $(SRC_DIR)/%.c
@mkdir -p $(OBJ_DIR)
$(CPP) -MM -MF $@ -MP -MT $(patsubst %.d,%.o,$@) $(CPP_FLAGS) $(patsubst $(OBJ_DIR)/%.d,$(SRC_DIR)/%.c,$@)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(OBJ_DIR)/%.d
@mkdir -p $(OBJ_DIR)
$(CC) -c $(CPP_FLAGS) $(C_FLAGS) $< -o $@
.PHONY: clean
clean:
rm -rf $(OBJS) $(patsubst %.o,%.d,$(OBJS)) $(TARGET)
ifneq ($(MAKECMDGOALS),clean)
-include $(patsubst %.o,%.d,$(OBJS))
endif