Skip to content

Instantly share code, notes, and snippets.

@hgraca
Created January 22, 2021 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hgraca/8ad7ceafc28be06641eedc31b7d865ec to your computer and use it in GitHub Desktop.
Save hgraca/8ad7ceafc28be06641eedc31b7d865ec to your computer and use it in GitHub Desktop.
Makefile example
# Makefile
#
# Execute targets as often as wanted
.PHONY: config
# Mute all `make` specific output. Comment this out to get some debug information.
.SILENT:
# make commands be run with `bash` instead of the default `sh`
SHELL='/bin/bash'
include Makefile.defaults.mk
# .DEFAULT: If the command does not exist in this makefile
# default: If no command was specified
.DEFAULT default:
if [ -f ./Makefile.custom.mk ]; then \
$(MAKE) -f Makefile.custom.mk "$@"; \
else \
if [ "$@" != "default" ]; then echo "Command '$@' not found."; fi; \
$(MAKE) help; \
if [ "$@" != "default" ]; then exit 2; fi; \
fi
help: ## Show this help
@echo "Usage:"
@echo " [ARG=VALUE] [...] make [command]"
@echo " make hello"
@echo " NAME=\"John\" make hello"
@echo
@echo "Available commands:"
@grep '^[^#[:space:]].*:' Makefile | grep -v '^default' | grep -v '^\.' | grep -v '=' | grep -v '^_' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m %-30s\033[0m %s\n", $$1, $$2}' | sed 's/://'
########################################################################################################################
#
# Please keep the commands in alphabetical order
#
hello: ## Show "Hello world" or whatevre name you set as environment variable when running the make command
echo "Hello ${NAME}"
# Makefile.custom
#
# This file contains the custom commands and/or aliases.
#
# To use it, copy/paste this file with the name Makefile.custom and add commands as you wish.
# This file is in .gitignore, so it will not be committed and is specific to you.
#
# Mute all `make` specific output. Comment this out to get some debug information.
.SILENT:
# make commands be run with `bash` instead of the default `sh`
SHELL='/bin/bash'
include Makefile.defaults.mk
# .DEFAULT: If command does not exist in this makefile
# default: If no command was specified:
.DEFAULT default:
if [ "$@" != "default" ]; then echo "Command '$@' not found."; fi;
$(MAKE) help # goes to the main Makefile
$(MAKE) -f Makefile.custom.mk help # goes to this Makefile
if [ "$@" != "default" ]; then exit 2; fi;
help:
@echo
@echo "Available custom commands:"
@grep '^[^#[:space:]].*:' Makefile.custom.mk | grep -v '^help' | grep -v '^default' | grep -v '^\.' | grep -v '=' | grep -v '^_' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m %-30s\033[0m %s\n", $$1, $$2}' | sed 's/://'
########################################################################################################################
# set default ${NAME} value, it can be overridden by passing it in the cli as > NAME=XXX make hello
NAME ?= 'world'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment