delavnog delavnog - 1 month ago 7x
Linux Question

Specify makefile with env variable

I have the following alias, which simplifies my workflow when moving from project to project:

alias mk='make -f ${ACTIVE_MAKEFILE}'

Some background: the makefiles are in a separate directory, all together, with names related to the project that they refer to. I do not keep them in the project directory because:

  1. They are my private makefiles, and I do not want to commit them to the repo

  2. I actually want to keep track of the changes, so they must be in a repo. For that I have an

This is working fine ... but: I lose completion. Doing
mk <TAB>
does not show me the available Makefile targets, so that having the alias is actually making my workflow less efficient.

I thought that instead of telling make to use a specific makefile with the
flag, I could set an env variable to tell make just that, something like:

export MAKEFILE=my-makefile

And then the command:


Will automagically use my
. Since the command has not changed, autocompletion will keep working.

But either that option does not exist, or I am unable to find the name of the env variable that make is using to select the

What env variable can I set (if any) to tell
to use a specific makefile?


Two options originally suggested in comments:

  1. Have you considered creating a symlink in each directory with a fixed, uniform name (it might be called makefileor Makefile, or even that points to the relevant out-of-directory makefile? Your alias then simply uses this fixed name, and you make sure that your VCS ignores the local symlink so it isn't pushed to the central repositories?

  2. As an alternative, use a shell script (perhaps dlnmk) instead of an alias, and have that apply the rules you have to locate the makefiles for you (so the script determines the correct name and location of the makefile and then invokes make with the correct -f option). The overhead will be minuscule and you encapsulate your thinking into a script, so you don't have to think about it any more.

Of the two, I'd probably go with the script, but I write scripts at the drop of a hat. I currently have about 400 scripts in my $HOME/bin directory, and almost 900 scripts saved in the associated RCS directory — I've been doing this for a while and Git was not an option for the first twenty years or so (because it didn't exist yet).