| 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:=othello |
| 21 | SRC_PATH:=src |
| 22 | LIBRARY_NAME:=lib$(BINARY_NAME) |
| 23 | LIBRARY_PATH:=lib |
| 24 | LDLIBS:=-l$(BINARY_NAME) -lncurses -ltinfo |
| 25 | #BUILD_TYPE:=debug |
| 26 | BUILD_TYPE:=release |
| 27 | |
| 28 | # ==================================== |
| 29 | # DO NOT CHANGE STUFF BEYOND THIS LINE |
| 30 | # ==================================== |
| 31 | |
| 32 | CC=gcc |
| 33 | LD=gcc |
| 34 | AR=ar |
| 35 | |
| 36 | WARN_FLAGS = -Wall -Wextra |
| 37 | STD_FLAG = -std=c11 |
| 38 | UNAME := $(shell uname -s) |
| 39 | |
| 40 | ifeq ($(BUILD_TYPE),debug) |
| 41 | BUILDDIR := .build/debug |
| 42 | DEBUG_FLAG = -g |
| 43 | DEBUG = 1 |
| 44 | STRIP_FLAG = |
| 45 | OPTI_FLAG = -O0 |
| 46 | LTO_SUPPORT = yes |
| 47 | GOLD_SUPPORT = yes |
| 48 | else |
| 49 | BUILDDIR := .build/release |
| 50 | DEBUG_FLAG = |
| 51 | DEBUG = 0 |
| 52 | STRIP_FLAG = -s |
| 53 | OPTI_FLAG = -O3 |
| 54 | LTO_SUPPORT = yes |
| 55 | GOLD_SUPPORT = yes |
| 56 | endif |
| 57 | |
| 58 | BINARY_SUFFIX := |
| 59 | DLIB_PREFIX := |
| 60 | SDLIB_PREFIX := |
| 61 | SHLIBLDFLAG := -shared |
| 62 | DLIB_SUFFIX := .so |
| 63 | SLIB_SUFFIX := .a |
| 64 | |
| 65 | OBJDIR := $(BUILDDIR)/objs |
| 66 | $(shell mkdir -p $(OBJDIR)) |
| 67 | |
| 68 | SRCS=$(wildcard $(SRC_PATH)/*.c) |
| 69 | LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c) |
| 70 | OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS))) |
| 71 | LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS))) |
| 72 | |
| 73 | DEPDIR := $(BUILDDIR)/deps |
| 74 | $(shell mkdir -p $(DEPDIR)) |
| 75 | DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td |
| 76 | POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d |
| 77 | |
| 78 | ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT) |
| 79 | CYGWIN_DLIBRARY_NAME := $(subst lib,cyg,$(LIBRARY_NAME)) |
| 80 | GOLD_SUPPORT = no |
| 81 | # Cygwin ncurses do not contain tinfo |
| 82 | LDLIBS := $(filter-out -ltinfo,$(LDLIBS)) |
| 83 | BINARY_SUFFIX := .exe |
| 84 | DLIB_SUFFIX := .dll |
| 85 | # do not archive external libraries |
| 86 | CYGWIN_LDLIBS_NOARCHIVE := $(filter-out -l$(BINARY_NAME),$(LDLIBS)) |
| 87 | CYGWIN_LIBLDFLAGS := -Wl,--out-implib=$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)$(SLIB_SUFFIX) \ |
| 88 | -Wl,--export-all-symbols \ |
| 89 | -Wl,--enable-auto-import |
| 90 | endif |
| 91 | |
| 92 | ifeq ($(UNAME),Darwin) |
| 93 | SHLIBLDFLAG=-dynamiclib |
| 94 | LDLIBS := $(filter-out -ltinfo,$(LDLIBS)) |
| 95 | DLIB_SUFFIX=.dylib |
| 96 | LTO_SUPPORT=no |
| 97 | GOLD_SUPPORT=no |
| 98 | endif |
| 99 | |
| 100 | ifeq ($(LTO_SUPPORT),yes) |
| 101 | CFLAGS_LTO = -flto -ffat-lto-objects |
| 102 | LDFLAGS_LTO = -fuse-linker-plugin -flto |
| 103 | endif |
| 104 | |
| 105 | ifeq ($(GOLD_SUPPORT),yes) |
| 106 | LDFLAGS_GOLD = -fuse-ld=gold |
| 107 | endif |
| 108 | |
| 109 | # Putting header files in the source directory is not the purpose of this INCLUDES_PATH variable |
| 110 | INCLUDES_PATH := $(INCLUDES_PATH) -I$(LIBRARY_PATH) |
| 111 | CFLAGS := -DDEBUG=$(DEBUG) $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES_PATH) |
| 112 | LIBCFLAGS := -fPIC $(CFLAGS) |
| 113 | LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG) |
| 114 | LIBLDFLAGS := $(SHLIBLDFLAG) $(LDFLAGS) |
| 115 | STATICLIBLDFLAGS := -static $(LDFLAGS) |
| 116 | LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) |
| 117 | |
| 118 | # Mac OS X do not support static linking |
| 119 | ifeq ($(UNAME),Darwin) |
| 120 | all: $(BINARY_NAME)$(BINARY_SUFFIX) $(BINARY_NAME).dynamic$(BINARY_SUFFIX) |
| 121 | else |
| 122 | all: $(BINARY_NAME)$(BINARY_SUFFIX) $(BINARY_NAME).dynamic$(BINARY_SUFFIX) $(BINARY_NAME).static$(BINARY_SUFFIX) |
| 123 | endif |
| 124 | |
| 125 | $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX): $(LIBOBJS) |
| 126 | @echo "[AR StO] $@" |
| 127 | @$(AR) rcs $@ $^ |
| 128 | |
| 129 | ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT) |
| 130 | $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)$(SLIB_SUFFIX): $(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX) |
| 131 | $(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS) |
| 132 | @echo "[LD DLL] $@" |
| 133 | @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $(CYGWIN_LIBLDFLAGS) -Wl,--whole-archive $^ -Wl,--no-whole-archive $(CYGWIN_LDLIBS_NOARCHIVE) -o $@ |
| 134 | else |
| 135 | $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS) |
| 136 | @echo "[LD ShO] $@" |
| 137 | @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ $(LDLIBS) -o $@ |
| 138 | endif |
| 139 | |
| 140 | #$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBOBJS) |
| 141 | # @echo "[LD ] $@" |
| 142 | # @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ |
| 143 | |
| 144 | $(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX) |
| 145 | @echo "[LD ] $@" |
| 146 | @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ |
| 147 | |
| 148 | $(BINARY_NAME).static$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX) |
| 149 | @echo "[LD ] $@" |
| 150 | @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@ |
| 151 | |
| 152 | ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT) |
| 153 | $(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)$(SLIB_SUFFIX) |
| 154 | else |
| 155 | $(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX) |
| 156 | endif |
| 157 | @echo "[LD ] $@" |
| 158 | @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ |
| 159 | |
| 160 | $(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d |
| 161 | @echo "[C ] $(notdir $*)" |
| 162 | @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@ |
| 163 | @$(POSTCOMPILE) |
| 164 | |
| 165 | include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))) |
| 166 | |
| 167 | $(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d |
| 168 | @echo "[C ] $(notdir $*)" |
| 169 | @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@ |
| 170 | @$(POSTCOMPILE) |
| 171 | |
| 172 | include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS)))) |
| 173 | |
| 174 | $(DEPDIR)/%.d: ; |
| 175 | |
| 176 | .PRECIOUS: $(DEPDIR)/%.d |
| 177 | |
| 178 | #FIXME: add an install target |
| 179 | |
| 180 | clean: |
| 181 | @echo "[CLN]" |
| 182 | -@rm -r $(BUILDDIR) |
| 183 | -@rm $(BINARY_NAME)$(BINARY_SUFFIX) |
| 184 | ifneq ($(UNAME),Darwin) |
| 185 | -@rm $(BINARY_NAME).static$(BINARY_SUFFIX) |
| 186 | endif |
| 187 | -@rm $(BINARY_NAME).dynamic$(BINARY_SUFFIX) |
| 188 | -@rm $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX) |
| 189 | ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT) |
| 190 | -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX) |
| 191 | -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)$(SLIB_SUFFIX) |
| 192 | else |
| 193 | -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX) |
| 194 | endif |
| 195 | |
| 196 | run: $(BINARY_NAME) |
| 197 | ./$(BINARY_NAME) |
| 198 | |
| 199 | disassemble: $(BINARY_NAME)$(BINARY_SUFFIX) |
| 200 | objdump -d $< | less |
| 201 | |
| 202 | symbols: $(BINARY_NAME)$(BINARY_SUFFIX) |
| 203 | objdump -t $< | sort | less |