| 1 | # Sample Makefile to build simple project. |
| 2 | # |
| 3 | # This Makefile expect all source files (.c) to be at the same level, in the |
| 4 | # $(SRC_PATH) directory. |
| 5 | # |
| 6 | # This Makefile expect all embedded library source files (.c) to be at the same level, in the |
| 7 | # $(LIBRARY_PATH) directory. |
| 8 | # |
| 9 | # It will automatically generate dependencies, compile all files, and produce a |
| 10 | # binary using the provided name linked against the library if necessary. |
| 11 | # |
| 12 | # Set BINARY_NAME to the name of the binary file to build. |
| 13 | # Set LIBRARY_NAME to the name of the library file to build. |
| 14 | # The default path for the library code and object is lib. |
| 15 | # By default the linker will look for $(BINARY_NAME) library name. |
| 16 | # Set BUILD_TYPE to either debug or release. |
| 17 | # |
| 18 | # Automatic dependencies code from: |
| 19 | # http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr |
| 20 | BINARY_NAME=exo1 |
| 21 | SRC_PATH:=src |
| 22 | LIBRARY_NAME=libexo1 |
| 23 | LIBRARY_PATH:=lib |
| 24 | BUILD_TYPE=debug |
| 25 | #BUILD_TYPE=release |
| 26 | |
| 27 | # ==================================== |
| 28 | # DO NOT CHANGE STUFF BEYOND THIS LINE |
| 29 | # ==================================== |
| 30 | |
| 31 | all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).static |
| 32 | |
| 33 | CC=gcc |
| 34 | LD=gcc |
| 35 | AR=ar |
| 36 | |
| 37 | WARN_FLAGS = -Wall -Wextra |
| 38 | STD_FLAG = -std=c11 |
| 39 | |
| 40 | ifeq ($(BUILD_TYPE),debug) |
| 41 | BUILDDIR := .build/debug |
| 42 | DEBUG_FLAG = -g |
| 43 | STRIP_FLAG = |
| 44 | OPTI_FLAG = -O0 |
| 45 | LTO_SUPPORT = yes |
| 46 | GOLD_SUPPORT = yes |
| 47 | else |
| 48 | BUILDDIR := .build/release |
| 49 | DEBUG_FLAG = |
| 50 | STRIP_FLAG = -s |
| 51 | OPTI_FLAG = -O3 |
| 52 | LTO_SUPPORT = yes |
| 53 | GOLD_SUPPORT = yes |
| 54 | endif |
| 55 | |
| 56 | ifeq ($(LTO_SUPPORT),yes) |
| 57 | CFLAGS_LTO = -flto -ffat-lto-objects |
| 58 | LDFLAGS_LTO = -fuse-linker-plugin -flto |
| 59 | endif |
| 60 | |
| 61 | ifeq ($(GOLD_SUPPORT),yes) |
| 62 | LDFLAGS_GOLD = -fuse-ld=gold |
| 63 | endif |
| 64 | |
| 65 | # Putting header files in the source directory is not the purpose of this INCLUDES variable |
| 66 | INCLUDES := $(INCLUDES) -I$(LIBRARY_PATH) |
| 67 | CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES) |
| 68 | LIBCFLAGS := -fPIC $(CFLAGS) |
| 69 | LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG) |
| 70 | LIBLDFLAGS := -shared $(LDFLAGS) |
| 71 | STATICLIBLDFLAGS := -static $(LDFLAGS) |
| 72 | LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME) |
| 73 | |
| 74 | OBJDIR := $(BUILDDIR)/objs |
| 75 | $(shell mkdir -p $(OBJDIR)) |
| 76 | |
| 77 | SRCS=$(wildcard $(SRC_PATH)/*.c) |
| 78 | LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c) |
| 79 | OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS))) |
| 80 | LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS))) |
| 81 | |
| 82 | DEPDIR := $(BUILDDIR)/deps |
| 83 | $(shell mkdir -p $(DEPDIR)) |
| 84 | DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td |
| 85 | POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d |
| 86 | |
| 87 | $(LIBRARY_PATH)/$(LIBRARY_NAME).a: $(LIBOBJS) |
| 88 | @echo "[AR StO] $@" |
| 89 | @$(AR) rcs $@ $^ |
| 90 | |
| 91 | $(LIBRARY_PATH)/$(LIBRARY_NAME).so: $(LIBOBJS) |
| 92 | @echo "[LD ShO] $@" |
| 93 | @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@ |
| 94 | |
| 95 | #$(BINARY_NAME): $(OBJS) $(LIBOBJS) |
| 96 | # @echo "[LD ] $@" |
| 97 | # @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ |
| 98 | |
| 99 | $(BINARY_NAME): $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a |
| 100 | @echo "[LD ] $@" |
| 101 | @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ |
| 102 | |
| 103 | $(BINARY_NAME).static: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a |
| 104 | @echo "[LD ] $@" |
| 105 | @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@ |
| 106 | |
| 107 | $(BINARY_NAME).dynamic: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).so |
| 108 | @echo "[LD ] $@" |
| 109 | @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ |
| 110 | |
| 111 | $(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d |
| 112 | @echo "[C ] $(notdir $*)" |
| 113 | @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@ |
| 114 | @$(POSTCOMPILE) |
| 115 | |
| 116 | include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))) |
| 117 | |
| 118 | $(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d |
| 119 | @echo "[C ] $(notdir $*)" |
| 120 | @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@ |
| 121 | @$(POSTCOMPILE) |
| 122 | |
| 123 | include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS)))) |
| 124 | |
| 125 | $(DEPDIR)/%.d: ; |
| 126 | |
| 127 | .PRECIOUS: $(DEPDIR)/%.d |
| 128 | |
| 129 | #FIXME: add an install target |
| 130 | |
| 131 | clean: |
| 132 | @echo "[CLN]" |
| 133 | -@rm -r $(BUILDDIR) |
| 134 | -@rm $(BINARY_NAME) |
| 135 | -@rm $(BINARY_NAME).static |
| 136 | -@rm $(BINARY_NAME).dynamic |
| 137 | -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).a |
| 138 | -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).so |
| 139 | |
| 140 | disassemble: $(BINARY_NAME) |
| 141 | objdump -d $< | less |
| 142 | |
| 143 | symbols: $(BINARY_NAME) |
| 144 | objdump -t $< | sort | less |