Simple string substitution in Makefile

13,354

Found it: $(SHFLAGS) was defined as

SHFLAGS=-shared -Wl,-soname,$$(@F) blablafoo

and using any substitution on it would not work until $$(@F) is actually evaluated (in my case to libswscale.so.2).

I solved it by replacing the variable reference:

@echo $(subst $$(@F),$(SLIBNAME),$(SHFLAGS))

Small hint on assignments: VAR = $(OTHERVAR) is evaluated when used, VAR := $(OTHERVAR) is evaluated immediately.

Share:
13,354
AndiDog
Author by

AndiDog

Principal Engineer, improving engineering by means of knowledge and practice sharing, automation, tooling. Developing with C++/Python/TypeScript/Rust/Go/Kubernetes... choice doesn't matter much, as long as you stick with it.

Updated on October 12, 2022

Comments

  • AndiDog
    AndiDog over 1 year

    I want to replace the string libswscale.so.2 by libswscale.so (variables called $(SLIBNAME_WITH_MAJOR) and $(SLIBNAME), respectively). This is what I tried in the Makefile:

    $(SUBDIR)$(SLIBNAME_WITH_MAJOR): $(OBJS) $(SUBDIR)lib$(NAME).ver
        [...]
        @echo SHFLAGS=$(SHFLAGS)
        @echo SLIBNAME_WITH_MAJOR=$(SLIBNAME_WITH_MAJOR)
        @echo SLIBNAME=$(SLIBNAME)
        @echo A $(patsubst $(SLIBNAME_WITH_MAJOR),$(SLIBNAME),$(SHFLAGS))
        @echo B $(SHFLAGS:$(SLIBNAME_WITH_MAJOR)=$(SLIBNAME))
        @echo C $($(SHFLAGS):$(SLIBNAME_WITH_MAJOR)=$(SLIBNAME))
        @echo D $(SHFLAGS:$(SLIBNAME_WITH_MAJOR)=$(SLIBNAME))
        @echo E $(subst $(SLIBNAME_WITH_MAJOR),$(SLIBNAME),$(SHFLAGS))
        @echo F $(subst l,L,$(SHFLAGS))
    

    The output is

    SHFLAGS=-shared -Wl,-soname,libswscale.so.2 -Wl,-Bsymbolic -Wl,--version-script,libswscale/libswscale.ver
    SLIBNAME_WITH_MAJOR=libswscale.so.2
    SLIBNAME=libswscale.so
    A -shared -Wl,-soname,libswscale.so.2 -Wl,-Bsymbolic -Wl,--version-script,libswscale/libswscale.ver
    B -shared -Wl,-soname,libswscale.so.2 -Wl,-Bsymbolic -Wl,--version-script,libswscale/libswscale.ver
    C
    D -shared -Wl,-soname,libswscale.so.2 -Wl,-Bsymbolic -Wl,--version-script,libswscale/libswscale.ver
    E -shared -Wl,-soname,libswscale.so.2 -Wl,-Bsymbolic -Wl,--version-script,libswscale/libswscale.ver
    F -shared -WL,-soname,libswscale.so.2 -WL,-BsymboLic -WL,--version-script,LibswscaLe/LibswscaLe.ver
    

    The last one (F) is especially ridiculous. What is wrong here? Is it because $(SHFLAGS) is made up of variables as well?