Skip to content

Instantly share code, notes, and snippets.

@ttsiodras
Last active May 22, 2017 11:11
Show Gist options
  • Save ttsiodras/4c1a43c38d9b3cd9fc456da26d6f5b4d to your computer and use it in GitHub Desktop.
Save ttsiodras/4c1a43c38d9b3cd9fc456da26d6f5b4d to your computer and use it in GitHub Desktop.
Makefile stdout redirection
# In relation to the discussion taking place at
# http://stackoverflow.com/questions/44097324/timing-of-makefile-include-statements-for-auto-generated-files/44103228
# OK, since I can't use PHONY to force the rebuild of piped.mk
# without losing the re-inclusion itself, I will just delete the file
# directly after using it (and thus force it to be re-created each
# time, as intended, without using .PHONY).
$ cat Makefile
all: piped.mk
ifeq ($(PIPED),1)
@echo Output of make is piped because PIPED is ${PIPED}
else
@echo Output of make is NOT piped because PIPED is ${PIPED}
endif
@rm -f piped.mk
piped.mk:
[ -t 1 ] && PIPED=0 || PIPED=1 ; echo "PIPED=$${PIPED}" > piped.mk
-include piped.mk
$ # And this seems to work...
$ make
[ -t 1 ] && PIPED=0 || PIPED=1 ; echo "PIPED=${PIPED}" > piped.mk
Output of make is NOT piped becaused PIPED is 0
$ # Until I saw this amazing result:
$ # The action reports that indeed PIPED is set to 1...
$ # ...but the action that is executed is wrong!
$ # It's the one that is surrounded with ifeq NOT set to 1!
$ make | more
[ -t 1 ] && PIPED=0 || PIPED=1 ; echo "PIPED=${PIPED}" > piped.mk
Output of make is NOT piped becaused PIPED is 1
$ # Looks like make has "cached" the decision somehow?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment