Can a Makefile have a dependency on the result of a shell script?

makefile, shell

Solution

AFAIK, Makefile rules only depend on the date of local files. Therefore, unfortunately, you have to do something like what @xbug showed, and in the more generic case:

file-I-care-about.ext: .some-precondition.dep
     run-command > file-I-care-about.ext

.some-precondition.dep:
     run-some-logic && touch .some-precondition.dep

For instance, I want to make sure I have a utility I need installed, but this completely generalizes to any conditions you want to assert:

# Generate README from my source file
README.md: src1.cc .extractor-installed.dep
    mdextract src1.cc > $@

# Install doc extractor if it's not already installed
.extractor-installed.dep:
    which mdextract && touch $@ || npm install -g mdextract && touch $@

`which mdextract` is my "validation logic" and if it passes, I update the dependency, but otherwise I run the expensive command and then update the dependency.

Problem

Is there a way to use the return value of a shell script as a dependency in a Makefile? For example: Makefile: ``` proj: getsource.sh cc src1.c src2.c ... getsource.sh: checksource.sh wget http://www.something.com/src1.c ``` checksource.sh: ``` #!/bin/sh # bash pseudo code because I can never remember bash's syntax if [[ -not -exists src1.c ]] exit 1 else exit 0 ... ``` When executed without the source present, the Makefile would run the getsource.sh target, then the proj target. If the source is present, it would only run the proj target.

Original source