0

I am using Makefile PHONY for CI. But I found the order of commands under the target is not run in sequential, more specifically, the command inside $() seems to run ahead of time. Is this some default behavior of Makefile?

SHELL := /bin/bash

.PHONY: test
test:
    cat input-test.txt > output.txt
    @[ $(shell wc -l < output-test.txt) -eq $(shell wc -l < input-test.txt) ]
    rm output-test.txt

by running make test it shows

/bin/sh: output-test.txt: No such file or directory
cat input-test.txt > output.txt
/bin/sh: line 0: [: -eq: unary operator expected
make: *** [test] Error 2
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Bob
  • 15
  • 1
  • 3
  • 1
    Indeed, any `make` syntax gets evaluated when `make` parses the `Makefile`, before it even knows which recipes it is going to run. – tripleee Nov 17 '20 at 06:27

1 Answers1

1

Make commands like $(shell ...) are executed before any of your actual recipes are run. Therefore, the line

cat input-test.txt > output.txt

hasn't been run yet.

If you want that command to be run in the proper order, replace it with

@[ $$(wc -l < output-test.txt) -eq $$(wc -l < input-test.txt) ]
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • 1
    More specifically, the entire recipe is completely expanded (including all variables and functions) before any command in the recipe is executed. Also, in your example you need to escape the `$` in `$(wc ...)`: it should be `$$(wc ...)` – MadScientist Nov 17 '20 at 14:25