Leonardo
Leonardo

Reputation: 1891

Makefile abort make if a folder is not found

I have a simple Makefile that relies on a specific folder structure. I'd like to add a test so the Makefile fails with a message telling the user the folder is missing.

My target looks like this:

check_folders:
    test -d ../some_folder || $(error The ../some_folder folder does not exist)

.PHONY: check_folders

I was expecting a short-circuit logic here, so if the first statement passes (and the folder exists), the second statement isn't executed. But it isn't working, the error is thrown even if the folder exists:

$ mkdir ../some_folder
$ make check_folders
makefile:24: *** The ../some_folder folder does not exist.  Stop.

Any help is appreciated!

Thanks!

Upvotes: 1

Views: 452

Answers (1)

MadScientist
MadScientist

Reputation: 100856

You are using a make capability for throwing errors, not a shell capability. All make variables and functions in a recipe are expanded first, BEFORE the recipe is invoked.

You have two options. The first is to switch completely to recipe failure: make will stop if a recipe exits with a failure. So you can write your rule like this:

check_folders:
        test -d ../some_folder || { echo The ../some_folder folder does not exist; exit 1; }

The other is to switch completely to make tests; this will happen before ANY recipe is invoked, as the makefile is parsed:

$(if $(wildcard ../some_folder/.),,$(error The ../some_folder folder does not exist))

Upvotes: 5

Related Questions