Skip to content

Instantly share code, notes, and snippets.

@RcColes
Last active September 14, 2019 12:09
Show Gist options
  • Save RcColes/5bf968cdc903999d76610dddad882692 to your computer and use it in GitHub Desktop.
Save RcColes/5bf968cdc903999d76610dddad882692 to your computer and use it in GitHub Desktop.
Super makefile
# Copyright 2019 Raef Coles
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#===============================================================================
# You can touch this bit
EXE = dockingd dockingctl
SRC_DIR = ./src
BIN_DIR = ./bin
BLD_DIR = ./build
EMBED_DIR = ./embed
CCFLAGS = -c -O3 -g -Werror
LDFLAGS = -lrt
CC = gcc
LD = ld
#===============================================================================
# Here there be dragons
DIRS = $(BIN_DIR) $(BLD_DIR) $(MKF_DIR)
VPATH = $(shell find $(SRC_DIR) -type d) $(shell find $(EMBED_DIR 2>/dev/null) -type d)
_EXE = $(addprefix $(BIN_DIR)/, $(EXE))
SRC = $(notdir $(shell find $(SRC_DIR) -type f -name *.cpp -o -name *.c))
EMBED = $(notdir $(shell find $(EMBED_DIR) -type f 2>/dev/null))
OBJ = $(addprefix $(BLD_DIR)/, \
$(filter-out $(EXE:=.o), \
$(filter %.o, \
$(EMBED:=.o) $(SRC:.cpp=.o) $(SRC:.c=.o)))) \
.PHONY: all clean
all: $(_EXE)
$(BIN_DIR)/%: $(BLD_DIR)/%.o $(OBJ) | $(BIN_DIR)
$(CC) $(LDFLAGS) $^ -o $@
$(BLD_DIR)/%.o: %.c %.h | $(BLD_DIR)
$(CC) $(CCFLAGS) -c $< -o $@
$(BLD_DIR)/%.o: %.c | $(BLD_DIR)
$(CC) $(CCFLAGS) -c $< -o $@
$(BLD_DIR)/%.o: $(EMBED_DIR)/% | $(BLD_DIR)
$(LD) -r -b binary $< -o $@
$(DIRS):
mkdir -p $@
clean:
gio trash $(DIRS) || rm -rf $(DIRS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment