I am struggling with the following problem :
I would like to use tput in my Makefile for color but it needs the environment variable $TERM ( and the echo alternative with \033[0;31m is not portable and will no behave the same way on all computers ).
When on an environment that does not have the variable, make fails and the program is not compiled.
I already tried searching for environment variable testing in a Makefile but with no success so far.
This is an example of what I would like to do :
@tput setaf $2
CC = gcc
CFLAGS = -Wall -Wextra
SRCS = my_super_file.c
OBJS = $(SRCS:.c=.o)
NAME = super_programm
$(call my_color, " Compiling $<", 2)
$(CC) -o $(NAME) $(OBJS)
rm -rf $(OBJS)
rm -rf $(NAME)
re: fclean all
.PHONY: all re clean fclean
I think this makefile illustrates what you want:
have_term := $(shell echo $$TERM) ifdef have_term define my_color = @tput setaf $2 @tput bold @echo $1 @tput sgr0 endef else my_color = @echo $1 endif all: $(call my_color, "Hello world", 2)
TERM is defined in the environment then
Hello World in
green, otherwise in plain white.