1

I'm trying to organize a Makefile with complex commands into something more readable.

I'm trying to break down a long list of parameters, but even single parameters are too bad, hence I wanted to break them too. But I cannot find a way to avoid unwanted spaces for these cases

# network and forward ports
QEMU_NET_FLAGS=-nic user
QEMU_NET_FLAGS+=,id=n1,restrict=on,ipv6=off,hostname=$(VMNAME)
# port forwards:
# hostfwd=[tcp|udp]:[hostaddr]:hostport-[guestaddr]:guestport
QEMU_NET_FLAGS+=,hostfwd=tcp::$(VMSSH)-:22
QEMU_NET_FLAGS+=,hostfwd=tcp::8000-:8000
QEMU_NET_FLAGS+=,hostfwd=tcp::8080-:8080
QEMU_NET_FLAGS+=,hostfwd=tcp::8081-:8081

Will cause -nic user ,id=1... instead of -nic user,id=1...

Tried ::=

First suggestion by @steeldriver:

QEMU_NET_FLAGS::=-nic user
QEMU_NET_FLAGS::=$(QEMU_NET_FLAGS),id=n1,restrict=on,ipv6=off,hostname=$(VMNAME)
QEMU_NET_FLAGS::=$(QEMU_NET_FLAGS),hostfwd=tcp::$(VMSSH)-:22

causes another problem since these are "templates", and variables like VMNAME and VMSSH should resolve late (recursively) when the resulting command line is used. And if I use :::= to then += values recursively, we are back to the unwanted space problem.

3
  • 4
    The KISS solution would probably be a simple (non-recursive i.e. :=) assignment of the form QEMU_NET_FLAGS := $(QEMU_NET_FLAGS),id=n1,... Commented May 14, 2024 at 21:31
  • @steeldriver that's actually a good overal solution. I'd rather repeat the assignment part like so instead of mashing everything in a single line. Commented May 15, 2024 at 7:36
  • @steeldriver almost, this only creates a problem since those are "template" commands that require late substitution for things like $(VMNAME)... trying to fiddle a solution to answer here Commented May 15, 2024 at 9:02

1 Answer 1

1
# helper for subst
null :=
space := ${null} ${null}

QEMU_NET_FLAGS = user
QEMU_NET_FLAGS += ,id=n1,restrict=on,ipv6=off,hostname=$(VMNAME)
QEMU_NET_FLAGS += ,hostfwd=tcp::$(VMSSH)-:22

QEMU_RUN_FLAGS+=-nic $(subst ${space},${null},$(QEMU_NET_FLAGS))

now when later I use this by setting the VMNAME and VMSSH variables and use $(QMEU_RUN_FLAGS) i will get the correct value, late/recursively substituted, and without spaces.

But I will leave this question open as it is not the best syntax experience. Notably, i had to change QEMU_NET_FLAGS = -nic user (with an intentional space) to add the parts with intentional space on the final concatenation after the subst in QEMU_RUN_FLAGS+=-nic $(subst ...

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.