0

I am looking for a way to log compilation errors within a makefile but here is my problem:

the line without log function is as follows:

$(OBJ_PATH)/%.obj: $(SRC_PATH_ASW)/%.c
    @echo -Compiling $<:
    $(CC) $(INCLUDE_ASW) $(INCLUDE_ASW_AUTO) $(INCLUDE_BSW) -c $< $(CFLAGS)

I tried to redirect stderr and/or stdout in a log file using the following command

$(OBJ_PATH)/%.obj: $(SRC_PATH_ASW)/%.c
    @echo -Compiling $<:
    $(CC) $(INCLUDE_ASW) $(INCLUDE_ASW_AUTO) $(INCLUDE_BSW) -c $< $(CFLAGS)  > $@.log

This solution won't display the message to the terminal but only within the log file. (i tried all possible kind of redirection without success)

I also tried with tee but the problem is different, in fact the makefile won't stop at first error since the tee command return code will always be successful.

$(OBJ_PATH)/%.obj: $(SRC_PATH_ASW)/%.c
    @echo -Compiling $<:
    $(CC) $(INCLUDE_ASW) $(INCLUDE_ASW_AUTO) $(INCLUDE_BSW) -c $< $(CFLAGS) | tee $@.log

What I would like if possible is to be able to log compilation result into a file as well as disply it but that the makefile stops at first compilation error and does not continue with other files compilation.

Thank you for your support.

S. Pech
  • 23
  • 4

2 Answers2

0

I've often wanted to do this too and the solution I came up with was to create this short shell script and alias make to run that instead.

#!/bin/sh

make $* | tee make.log
Chris Turner
  • 8,082
  • 1
  • 14
  • 18
0

You can use set -o pipefail or PIPESTATUS[0]

set -o pipefail; $(CC) ... | tee file.log

will return an error if the $(CC) ... or tee... commands fail. Alternatively if you just do

$(CC) ... | tee file.log

Then ${PIPESTATUS[0]} will be the error code of the first command ($(CC)...) (you can tack an exit ${PIPESTATUS[0]} at the end of the line for example.

blackghost
  • 1,730
  • 11
  • 24