How can I highlight the warning and error lines in the make output?

前端 未结 7 2255
醉话见心
醉话见心 2021-01-30 11:19

Sometimes, make\'s output fills the screen. It\'s a little bit hard to identify all the warning and error message lines. I know may shell color output can help Can anyone can he

7条回答
  •  天命终不由人
    2021-01-30 11:52

    How about the following?

    It is produced by a simplified version of this Makefile.

    PROJECT = programname
    SHELL   = /bin/bash
    OBJS    = $(patsubst src/%.cc,obj/%.o,$(wildcard src/*.cc))
    
    RESET          = \033[0m
    make_std_color = \033[3$1m      # defined for 1 through 7
    make_color     = \033[38;5;$1m  # defined for 1 through 255
    WRN_COLOR = $(strip $(call make_std_color,3))
    ERR_COLOR = $(strip $(call make_std_color,1))
    STD_COLOR = $(strip $(call make_color,8))
    
    COLOR_OUTPUT = 2>&1 |                                   \
        while IFS='' read -r line; do                       \
            if  [[ $$line == *:[\ ]error:* ]]; then         \
                echo -e "$(ERR_COLOR)$${line}$(RESET)";     \
            elif [[ $$line == *:[\ ]warning:* ]]; then      \
                echo -e "$(WRN_COLOR)$${line}$(RESET)";     \
            else                                            \
                echo -e "$(STD_COLOR)$${line}$(RESET)";     \
            fi;                                             \
        done; exit $${PIPESTATUS[0]};
    
    .PHONY: $(PROJECT)
    
    $(PROJECT): bin/$(PROJECT)
    
    bin/$(PROJECT): $(OBJS)
        @mkdir -p bin
        @echo g++ -o $@ $(OBJS) -Iinclude
        @g++ -o $@ $(OBJS) -Iinclude $(COLOR_OUTPUT)
    
    obj/%.o: src/%.cc
        @mkdir -p obj
        @echo g++ -o $@ -c $< -Wall -Wextra
        @g++ -o $@ -c $< -Wall -Wextra $(COLOR_OUTPUT)
    

    It assumes all C++ source files are in the src directory (extention .cc) and header files are in the include directory.

提交回复
热议问题