I'm trying to achieve something like this: If user will run make build_x version=1.0 then show Building version 1.0, else (make build_x without version param) show Building version latest. But what is the key here - I need to version be - by default value (to be able to use it properly with git). This is why I use additional variable version_info for replacing - with latest.

So my code looks like this:

    $(eval version ?= -)        
    $(eval version_info = ${if ["${version}" == "-"], "latest", "${version}"})

    ${INFO} "Doing checkout according to version $(version_info)..."

I know that probably issue is with this condition given to if.

Any ideas?

  • What kind of makefile? GNU? – Michael Homer Jul 5 '17 at 8:02
  • Yes, sorry for not mentioning it. – marverix Jul 5 '17 at 8:24

This works for me:

version ?= -
ifeq (-,$(version))
  version_info = latest
  version_info = $(version)

    @echo version_info = $(version_info)

It sets version to - if unset, then fills in version_info appropriately.

Using target-specific variables, and a one-liner variant:

build_x: version ?= -
build_x: version_info = $(if $(version:-=),$(version),latest)
    @echo version_info = $(version_info)

This works as follows (see the overall GNU Make documentation):

  • version ?= - sets version to - if it’s not already set
  • if checks its first argument, evaluates it to see if it’s empty or not, and is replaced with the second argument if the first is non-empty, and the third if it is
  • $(version:-=) evaluates version, replacing - with the empty string (: introduces the replacement, the search key is the text before =, the replacement is the text after =)

You could put the target-specific variable value feature to good use here:

build_x: version := latest
     @echo "Building version $(version)"

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.