Skip to content

Instantly share code, notes, and snippets.

@D3f0
Last active January 19, 2018 17:20
Show Gist options
  • Save D3f0/0c510f454d774838cc5c0d055f6f66f8 to your computer and use it in GitHub Desktop.
Save D3f0/0c510f454d774838cc5c0d055f6f66f8 to your computer and use it in GitHub Desktop.
Makefile biolerplate
.DEFAULT_GOAL := help
# Some Makefile utilities
define PRINT_HELP_PYSCRIPT
import re, sys
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
lines = []
print('''
{BOLD}Makefile boilerplate{ENDC}
'''.format(**bcolors.__dict__))
targets = []
for line in sys.stdin:
match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line)
if match:
target, help = match.groups()
targets.append((target, help))
for target, help in sorted(targets):
print(r"%s%-32s%s %s" % (bcolors.OKGREEN, target, bcolors.ENDC, help))
print("""
{HEADER}make help{ENDC}
""".format(**bcolors.__dict__))
endef
export PRINT_HELP_PYSCRIPT
define BROWSER_PYSCRIPT
import os, webbrowser, sys
try:
from urllib import pathname2url
except:
from urllib.request import pathname2url
webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])))
endef
export BROWSER_PYSCRIPT
BROWSER := python -c "$$BROWSER_PYSCRIPT"
define SHOW_VARS_SCRIPT
import re
is_var_def = re.compile(r'(\w+)\s?\?\=\s?([\w\.\-]*)')
with open('Makefile') as fp:
for line in fp.readlines():
match = is_var_def.match(line)
if match:
print('{} = {}'.format(match.group(1), match.group(2)))
endef
export SHOW_VARS_SCRIPT
help:
@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) | less
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment