how.wtf

Check if a program exists from a Makefile

· Thomas Taylor

A Makefile can check if a shell program or executable exists before running recipes.

Check if all programs exist

1REQUIRED_BINS := hugo terraform aws exiftool jpegoptim optipng mogrify
2$(foreach bin,$(REQUIRED_BINS),\
3    $(if $(shell command -v $(bin) 2> /dev/null),,$(error Please install `$(bin)`)))

This method will run before any make <command> is completed. It ensures that each program listed in REQUIRED_BINS is executable. If not, it will output an error quickly.

Check if a specific program exists

If it’s preferred to check a shell program before running a specific command, the syntax below will enable for that use-case:

 1tools := hugo terraform aws exiftool jpegoptim optipng mogrify
 2$(tools):
 3	@which $@ > /dev/null
 4	
 5.PHONY: serve
 6serve: hugo
 7	$(HUGO) server -D
 8
 9.PHONY: validate
10validate: terraform
11	$(TERRAFORM) validate

This works by using automatic variables with static patterns. All items in tools will become targets. The $@ in the recipe will be be substituted with the target’s name.

In the example above, make serve will first check if the hugo target is completed. That target runs @which hugo > /dev/null.

#makefile  

Reply to this post by email ↪