11

Currently I am working with Makefiles that have definitions like

MYLIB=/.../mylib-1.2.34

The problem is that these are different for different developers, and it is a pain having to re-edit the file after every checkout.

So I tried exporting a specific environment variable, and then doing

MYLIBX:=$(MYLIB_ENV)
MYLIBX?=MYLIB

Trouble is that if MYLIB_ENV is not defined, it still creates an empty MYLIBX, so the ?= does not work. Is there a clean way to do this very basic thing?

I am working with a "rich" set of make files developed over many years that do all sorts of things like make and call each other, so changing things deeply is not an option.

SOLUTION

Double shuffle. MYLIB already defined.

MYLIB_ENV?=MYLIB
MYLIB:=MYLIB_ENV
3
  • First of all, Why is it different for each and every developer? If you really want to read the environment variables then use export for the variables you want to get in your Makefile
    – Thushi
    Mar 9, 2017 at 4:20
  • @Thushi, it just is different, do not go there. Yes, of course export, that is not the issue. Thanks.
    – Tuntable
    Mar 9, 2017 at 4:22
  • So exporting environment variable is working for you now?
    – Thushi
    Mar 9, 2017 at 4:24

2 Answers 2

13

The problem with

MYLIB:=$(MYLIB_ENV)
MYLIB?=/.../mylib-1.2.34

is that MYLIB is always defined in the first line, so the second never applies.

The typical approach in this situation is just

MYLIB?=/.../mylib-1.2.34

That way individual developers can specify their own value from the shell, either on the make command line

make MYLIB=...

or in their environment before running make

export MYLIB=...
make

(so they can set it once, e.g. in their shell startup scripts, and forget about it).

If you just run

make

without specifying a value for MYLIB, the default /.../mylib-1.2.34 is used.

Another option is to determine where the Makefile is stored, but that doesn't work in all cases (in particular if the path to the Makefile contains spaces).

3
  • Thanks, but does not answer the question. There must be a default withing the make files. There are many makefiles in a complex arrangement. So neither of your approaches is useful (and my question actually contains them.)
    – Tuntable
    Mar 10, 2017 at 7:27
  • The approach I describe does contain a default: if MYLIB isn't defined externally, it takes the value /.../mylib-1.2.34. Mar 10, 2017 at 10:24
  • Yes, of course. My situation is slightly different, I updated the question.
    – Tuntable
    Mar 12, 2017 at 4:31
2

It's not very clear what it is exactly that you are after. But in case you want that you need to override the settings of the make variable MYLIB to something different from what it is specified in the Makefile WITHOUT having to edit the Makefile for that, then it is very simple: In your make invocations, specify MYLIB on the commandline with a value of your choice , like as:

make -f yourmakefile yourtarget MYLIB="I_need_my_personal_value"

Another method :

MYLIB := ORIGINAL_VALUE

ifneq '$(MYLIB_ENV)' ''
MYLIB := $(MYLIB_ENV)
endif
1
  • Thanks. First solution is impractical, there are many make files in a complex arrangement. Second would work -- it is what I was trying to avoid though.
    – Tuntable
    Mar 10, 2017 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.