bashgccmakefilewarnings

Make decision on GCC compiler output [if warning.. do this] [if error.. do this]


How can I individually handle errors and warnings outputed by the GCC compiler?
For example, if a program is compiled,
how would I get all the errors to go to "error.txt"
and all of the warnings to go to "warnings.txt"
I assume there is an easy solution for this, but let me provide more detail:

I've written a GNU makefile to compile with GCC and the makefile rules are performed by bash.
I would like this branching decision to take place in the rule of my makefile.
So maybe something like this for example:

if (PROGRAM_HAS_NO_ERRORS_OR_WARNINGS); then\  
    echo "Everything went well";\  
else\  
    if (PROGRAM_HAS_WARNINGS); then\  
        echo "HERE ARE THE WARNINGS: " $(PROGRAM_WARNINGS);\  
    fi;\
    if (PROGRAM_HAS_ERRORS); then\  
        echo "HERE ARE THE ERRORS: " $(PROGRAM_ERRORS);\  
    fi;\
fi;

I was under the impression that the warnings were cout.. and errors were cerr..
and that cout corresponded to 1 and cerr corresponded to 2.
However, when I run something like this:

$(COMPILE) 1>warnings.txt 2>errors.txt  

everything goes to errors.txt

So I definitely don't want to send it to a file and then parse through it if there is an easier way.


some other notes that may not be important:

if ($(COMPILE)); then\

/\ even if there are warnings, it will evaluate to true... UNLESS I provide the flag "-Werror" which just makes the warnings errors and gcc refuses to compile the program


Solution

  • This lends itself to a named pipe and a case statement similar to this:

    mkfifo errors.txt
    
    while read LINE; do
      case "$LINE" in
        *[Ee]rror*)error_function $LINE;;
        *[Ww]arning*)warning_function $LINE;;
        *)echo GENERAL ERROR MSG : $LINE;;
      esac
    done <errors.txt &
    
    make 2> errors.txt &