Alex’s Blog

Random musings on life, tech, and anything else

Buy me a hot chocolate! | About Me

Today I Learned: Order Only Dependencies In Make

date: Tue Jul 25 11:07:30 AM CEST 2023

Now I am almost sure I am using Makefiles/make wrong here. It seems make purists hate that some (most?) use make as a task runner of sorts? I forget where I heard this but I remember seeing it in tech circles like HackerNews or StackOverflow. In anycase I don’t care. I work as a data engineer. Everything looks like a pipeline or a transformation to me. :-)

So I had this Makefile and I wanted to run the make-a-venv-environment-for-python if and only if it didn’t exist:

the makefile I was working with

And the key here is the vertical bar | on line 4.

It says:

Order-only prerequisites are never checked when determining if the target is out of date; even order-only prerequisites marked as phony (see Phony Targets) will not cause the target to be rebuilt.

Source: https://www.gnu.org/software/make/manual/make.html#Prerequisite-Types And where I saw this first: https://stackoverflow.com/a/51750366

In other words: Just check that the thing or things after the | exist but don’t check if they’re newer or anything like that.

Mind blown.

Without this it was running the env recipe/set of steps every time.

Note: I could also just check for the existence fo the .venv folder instead of looking for the python3 binary but since I am going to use the binary in other parts of the makefile I figured why not.

Text of the makefile copied here for convience.

.ONESHELL: env deps run

PYTHON := .venv/bin/python3

env: | $(PYTHON)
    @python3 -m venv .venv

deps: env
    @pip3 install --disable-pip-version-check -q -r requirements.txt

run: deps 
    @$(PYTHON) -c "print('hello world')"

Feedback

Go Back Home

Copyright Alex Narayan - 2023