How can I define the default value for a variable in mkfile

177 Views Asked by At

Makefile:

XDG_CONFIG_HOME?=$HOME/.config

I want convert the line to Plan9 mkfile syntax.

I tried

XDG_CONFIG_HOME=`{if(~ $XDG_CONFIG_HOME '') echo $HOME/.config; if not echo $XDG_CONFIG_HOME}

and it worked but it's ugly. Any alternative?

2

There are 2 best solutions below

0
On BEST ANSWER

mk(1) doesn’t provide such a built-in way of defining variables only if they aren’t non-empty environment variables. Actually, neither any make(1) from Bell Labs nor POSIX make(1) provide such a way to do that, the ?= syntax is just a GNU extension.

What you usually would do is in your mkfile just set the variable normally:

XDG_CONFIG_HOME = $home/.config

and overwrite the variable in the mk(1) command:

    ; mk 'XDG_CONFIG_HOME='^$home^'/cfg'
1
On

In shell, ":-" is used to set a variable to a default value in case the variable has no value:

XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}