Skip to content

Instantly share code, notes, and snippets.

@jmervine
Created July 11, 2014 19:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmervine/5c64fafa5cd8f0c23189 to your computer and use it in GitHub Desktop.
Save jmervine/5c64fafa5cd8f0c23189 to your computer and use it in GitHub Desktop.
###
# Usage:
#
# Download this file and start by running 'make'. Then run
# each target listed, 'make lesson_one', 'make lesson_two',
# etc.
#
introduction:
# Introduction:
#
# Lesson One (lesson_one)
# - Makefiles and makefiles.
# - Default target.
#
# Lesson Two (lesson_two)
# - Targets (tasks) and prerequisite targets (tasks).
#
# Lesson Three (lesson_three)
# - Running targets.
# - Changing directories.
#
# Lesson Four (lesson_four)
# - Hiding commands and comments.
# - Ignoring errors.
#
# Lesson Five (lesson_five)
# - Variables
#
# Lesson Six (lesson_six)
# - Shell commands.
# - $$(shell [command])
#
# Lesson Seven (lesson_seven)
# - Tasks and their association with files
# and directories. on disk.
# - .PHONY
#
# Note: lesson tasks are aliased to l#, example:
#
# make l1 # is the same as make lesson_one
lesson_one:
# Lesson One
#
# The standard file naming for 'make' is Makefile, alhtough
# I've often seen 'makefile' as well. The accepted extension
# is '.mk', often used for include files - I'll cover includes
# later.
#
# The default target, that is to say, the target that is
# run when 'make' is executed without a target, will always
# be the first target in the 'Makefile'. In this file,
# 'introduction' is the default target.
l1: lesson_one
lesson_two:
# Lesson Two
#
# Make targets, often called "tasks" (especially by me), are
# the basic break down of make and running actions. If you've
# ever built anything on from source on a linux system, you've
# run some basic tasks like "make install" or "make test".
#
# At it's core a Makefile is simply a list of targets that
# perform requested actions.
#
# build:
# go build -o myprog myprog.go
#
# install:
# cp myprog /home/user/go/bin
#
# clean:
# rm myprog
@sleep 1
@echo
# Make targets can also request that prerequisite targets are run
# prior to running the requested target. In the above, it might be
# nice to run 'build' before running install. In doing so, we
# ensure that we have something to install.
#
# build:
# go build -o myprog myprog.go
#
# install: build
# cp myprog /home/user/go/bin
#
# Additionally, when running 'make install', it will only attempt
# to install 'myprog' if the build task successfully completes.
l2: lesson_two
lesson_three: _clean
# Lesson Three
#
# Make runs commands, one after another.
pwd
mkdir lesson_two
@echo
cd lesson_two
pwd
@echo
# Make's commands are always run from the root directory
# it was called from, it ignores 'cd'.
@sleep 1
@echo
# To change directory, you must chain commands with '&&'
# or ';'.
cd lesson_two; pwd
l3: lesson_three
lesson_four:
# Lesson Four
#
# You can hide the command or comment that is run, by
# prefixing it with '@'.
@echo
echo "This command will be shown, and then it's output will be shown."
@echo
@echo "This command is prefixed with '@', only it's output will be shown."
@echo
@sleep 1
# This comment will be shown, but comments prefixed with '@' won't be.
#
# Example: '@#' hidden comment.
l4: lesson_four
lesson_five:
# Lesson Five
#
# Variable's are set and called, much like in bash, the difference
# being that aside from being prefixed with a dollar sign ($$), the
# variable must wrapped in parentheses - e.g. "$$(FOO)".
#
# FOO=bar # setting
# $$(FOO) # calling
#
# Additionally, variables can only be set external to targets.
l5: lesson_five
lesson_six:
# Lesson Six
#
# Because make runs basic shell command already you can simply use
# standard shell in many cases.
@echo
echo "My host `hostname`"
@echo
# To easily insert the output of a shell command, use $$(shell [cmd]).
# This is useful when wanting to do things like set make variables or
# comment text to the output of a shell command.
#
# Variable's example:
#
# TEST_FILES=$$(shell find . -type f -name "*_test.js")
#
# Again, variables can only be set external to targets.
#
# Example using '$$(shell hostname)'
#
# This comment has the output of a shell command
#
# --> host: $(shell hostname)
l6: lesson_six
lesson_seven: _clean
# Lesson Seven
#
# Make task names are designed to match file and directory locations
# on disk.
@sleep 1
#
# In this example, 'lesson_seven.out' will only be generated if
# it does not already exist.
#
# lesson_seven.out:
# echo "lesson seven" > lesson_seven.out
@echo
make lesson_seven.out
@echo
@sleep 1
#
# Now if we re-run the same task, make will inform us that everything
# is up to date and therefore nothing to be done.
@echo
make lesson_seven.out
@echo
@sleep 1
# The same is true for directories as well, the following will only
# create 'public/assets' if it doesn't already exist.
#
# public/assets:
# mkdir public/assts
@sleep 1
#
# Make provides a way to turn off this 'feature', through a task called
# .PHONY. To use this, there are two steps. First create a .PHONY task
# somewhere in your Makefile -- by convention, this tends to be the last
# task in the file. Second, either include .PHONY as a prerequisite task of your
# task, or your task as a prerequisite task of .PHONY.
@sleep 1
#
# Example;
#
# mytask: .PHONY
# echo "my action"
#
# .PHONY
#
l7: lesson_seven
lesson_seven.out:
echo "lesson seven" > lesson_seven.out
# All tasks below this point are helper tasks for this tutorial.
####
_clean:
@-rm -rf lesson_two
@-rm -rf lesson_six.out
.PHONY:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment