Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Makefile execute code ONLY when an error has occurred?

Tags:

makefile

In my Makefile, I have some code that checks for network connectivity. This code takes a decent amount of time to run and I would only like to run it if another target fails to build.

Current Makefile

all: files network
    # compile files

files:
    # get files from network resources

network:
    # check for network connectivity
    # echo and return an error if it's not available

Execution Order:

if not network:
    # exit with error
if not files:
    # exit with error
if not all:
    # exit with error

Desired Makefile

In the above example, I would like the network target to be "made", only if the files target fails to get "made".

Execution Order:

if not files:
    if not network:
        # exit with error
if not all:
    # exit with error
like image 547
Jace Browning Avatar asked Mar 22 '13 11:03

Jace Browning


People also ask

How do I ignore an error in makefile?

To ignore errors in a recipe line, write a ' - ' at the beginning of the line's text (after the initial tab). The ' - ' is discarded before the line is passed to the shell for execution.

How do I call a target in makefile?

When you type make or make [target] , the Make will look through your current directory for a Makefile. This file must be called makefile or Makefile . Make will then look for the corresponding target in the makefile. If you don't provide a target, Make will just run the first target it finds.

What does $@ mean in makefile?

The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.

What does target mean in makefile?

A simple makefile consists of “rules” with the following shape: target … : prerequisites … recipe … … A target is usually the name of a file that is generated by a program; examples of targets are executable or object files. A target can also be the name of an action to carry out, such as ' clean ' (see Phony Targets).


1 Answers

Recursive make is your friend here I'm afraid.

.PHONY: all
all:
    ${MAKE} files || ${MAKE} network

If make files succeeds, your work is done and the exit code is success. On failure, the exit code is that for make network.

like image 109
bobbogo Avatar answered Oct 21 '22 20:10

bobbogo