Create a variable in a makefile by reading contents of another file

I'm guessing that you like to set a variable in your Makefile to the contents of another file:

FILE=test.txt
VARIABLE=`cat $(FILE)`

target:
    echo $(VARIABLE)

GNU make version 4.2 supports file reading operation, so with respect to Maxim Egorushkin's great answer there is an optional way to solve this problem now:

FILE     := test.txt
variable :=$(file < $(FILE))

Assuming GNU make:

file := whatever.txt
variable := $(shell cat ${file})

cat doesn't exist on Windows. Solution that works for Linux and Windows:

cat := $(if $(filter $(OS),Windows_NT),type,cat)
variable := $(shell $(cat) filename)

Explanation: Seems like On Windows there is always OS environment variable defined to be equal to 'Windows_NT'. This way, for Windows type command is used, for non-Windows cat is used.

Tags:

Makefile