Tarick Welling
Tarick Welling

Reputation: 3255

How to read file contents as list of variables?

I have some python code which works with a C library that I compile with a Makefile. So I create some install/uninstall/installed routines to load/unload/check my library. I add the python commands to it too so everything works.

Now the problem is, not all the machines have the same python version and python might not be set to the most up to date version. I would like to execute python-{x.y} -m pip {install -e . | uninstall {libname} | show {libname}}.

My current approach was: create a configured.dat file with the following contents:

python3.4 python3.6 python3.8 

And the following Makefile routine:

CONFIGURED_FILE=configured.dat
INSTALLED_PYTHON_VERSIONS :=$(file < $(CONFIGURED_FILE))

RED := \033[0;31m
GREEN := \033[1;32m
CYAN := \033[0;36m
NO_COLOR := \033[0m

ifneq ($(VERBOSE),)
    VERBOSE := ''
else
    VERBOSE := '-q'
endif
EDITABLE := '-e'

.PHONY:  install
install:
    # make C libs

    $(foreach f,$(INSTALLED_PYTHON_VERSIONS), echo -e 'installing $(CYAN)$f$(NO_COLOR)'; $f -m pip install $(VERBOSE) $(EDITABLE) .;)

On my development machine this works. On my deployment machine it doesn't. The variable INSTALLED_PYTHON_VERSIONS is empty. How? Why? The only change I found between the environments that should impact it is the make version, 3.82 on prod, and 4.3 on dev. Prod is centos 7.9.2009 and dev is ubuntu 22.04.1.

The root problem is the wanting to invoke different python versions, the superficial problem is being unable to read the file contents into a list of variables. A good answer to either would be fine by me.

Upvotes: 0

Views: 47

Answers (1)

MadScientist
MadScientist

Reputation: 100816

You found the problem yourself: on one machine you're using GNU make 3.82 and on the other 4.3. According to the GNU make NEWS file, the $(file ...) function was added in GNU make 4.0 (released in 2013, so 9 years ago).

You can just use cat:

INSTALLED_PYTHON_VERSIONS := $(shell cat $(CONFIGURED_FILE))

I should also point out that echo -e is not portable. There are no POSIX-specified options to echo and lots of different implementations accept, or not, different options. You probably want to switch to printf which is portable and well-defined.

Upvotes: 1

Related Questions