Skip to content

Instantly share code, notes, and snippets.

@pinebright
Created March 27, 2019 11:35
Show Gist options
  • Save pinebright/dce3905fbb4917df1e7c031a81378c06 to your computer and use it in GitHub Desktop.
Save pinebright/dce3905fbb4917df1e7c031a81378c06 to your computer and use it in GitHub Desktop.
汎用的なmakefile
CC = $(CROSS_COMPILE)gcc
STRIP = $(CROSS_COMPILE)strip
CFLAGS = -MMD -MP -Wall -Wextra -Winit-self -Wno-missing-field-initializers
INCLUDES = -I$(DEVROOT)/include
LDFLAGS = -L$(DEVROOT)/lib
LIBS =
SRCS = $(foreach srcdir, $(shell find ./src -type d), $(wildcard $(srcdir)/*.c))
OBJS = $(subst ./src/, ./obj/, $(SRCS:.c=.o))
DEPS = $(OBJS:.o=.d)
TARGETS = program_name
GIT_HASH = $(shell git rev-parse HEAD)
BUILD_TYPE = Debug
ifeq ($(BUILD_TYPE), Release)
CFLAGS += -O3 -s -DBUILD_TYPE=\"Release\"
endif
ifeq ($(BUILD_TYPE), Debug)
CFLAGS += -O0 -g -DBUILD_TYPE=\"Debug\"
endif
all: $(TARGETS)
$(TARGETS): $(OBJS) $(LIBS)
@echo $(BUILD_TYPE) build
$(CC) $(LDFAGS) -o $@ $^ $(LDFLAGS)
$(STRIP) $@
./obj/%.o: ./src/%.c
-mkdir -p $(dir $(OBJS))
$(CC) $(CFLAGS) -DGIT_HASH=\"$(GIT_HASH)\" $(INCLUDES) -o $@ -c $<
rebuild: clean all
clean:
$(RM) $(OBJS) $(DEPS) $(TARGETS)
docs:
doxygen Doxyfile
debug:
$(MAKE) rebuild BUILD_TYPE=Debug
release:
$(MAKE) rebuild BUILD_TYPE=Release
install:
install -m 0755 -D $(TARGETS) $(ROOTFS)/usr/local/bin/
-include $(DEPS)
.PHONY: all clean test doc debug release install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment