Search code examples
makefilegnu-make

Set in Makefile the value of a variable previously set in via included file


I have a Makefile that includes a .env file, and this .env sets default value of some variables. One of the variables default has relative path to file. In Makefile I want to obtain the full path, because this variable could be passed via command line (hence, bypassing .env's value) and I want to make sure I have use it as full path.

What I tried:

.env file

FILE := relative/path/to/file

Makefile file

include .env
export

FILE := $(shell realpath $(FILE))

.PHONY: sometarget
sometarget:
    @echo "FILE = $(FILE)"

I also tried to replace the assignment operator to =, ?= and others, but all failed.

I expected to achieve FILE as /full/path/to/file.

EDIT:

Updated to add a sample target to the Makefile to use as example.

Also, to add that I learned that actually the issue is when passing the value via command line, e.g. make FILE=relative. In this particular case is that I get simply "relative" as output when I expected its full path. That's my main issue.


Solution

  • Use override:

    include .env
    override FILE := $(shell realpath $(FILE))
    
    $(info Here $$FILE is [${FILE}])
    
    all:
            @echo '$$FILE' in recipe $(FILE)