1

Typically from a shell script, we can detect if the output is redirected to a file, along the lines of:

if [ -t 1 ]; then echo "shell"; fi

But, I'd like to do this inside of a makefile, which means that AFAIK, the only way to do it is:

ifeq ($(shell if [ -t 1 ]; then echo "1"; fi),1)
## in shell
endif

However, this appears to be the case all the time, whether the output is redirected to a file or not. I guess it's down to how $(shell...) is implemented. So my question is, is there anything that I can test in make that will let me know if the output is redirected to a file.

A little background, my makefiles generate lots of useful output and to make things a bit more obvious, I use some colouring of text - on the console this is fine, however in Hudson when the same build job runs, the output (which is redirected to a file) has all the control characters in it... So what I'd like to do is to disable all the colour codes in make if the file is redirected.

Nim
  • 33,299
  • 2
  • 62
  • 101

1 Answers1

1

You can't use $(shell ...) for this, because the output of $(shell ...) is always redirected: it has to be because the whole point of running $(shell ...) is for make to capture it's output. You could check whether stderr is a tty, on the assumption that people normally redirect both of them at the same time. However that could be a flawed assumption in some cases.

Other than that I don't see a straightforward way to perform this check from within make. The check must be done from within a make recipe, because that's the only place where make will invoke a shell without modifying its stdout. But you can't change makefile variables, etc. from within a recipe.

We might be able to come up with a really disgusting hack by using auto-generated include files where the build of the included file checked to see if stdout was a terminal and wrote a variable assignment into the included makefile. Then the trick is to get this included makefile to be rebuilt exactly once per invocation of make, which is not easy but probably could be done. However, this would involve a complete re-exec of make every time, just to set this variable.

Of course you could also provide a shell wrapper people would run instead of running make directly, which tested whether stdout was a tty then invoked make with a variable assignment based on the results.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • You include file hack solution align nicely with your handle!! ;) I think I'm just going to pass in a variable in the Hudson jobs which will disable the format, that's really where I'm concerned about these characters... – Nim Jul 19 '13 at 11:57
  • Actually, you are right, I changed it to detect stderr and this will suffice for my usecase! – Nim Jul 19 '13 at 12:04
  • Interesting, stdin works as well, as mentioned in https://stackoverflow.com/questions/4251559/how-can-i-tell-if-a-makefile-is-being-run-from-an-interactive-shell – Victor Sergienko Apr 13 '20 at 08:24