help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Is there any value in a lisp-like $(cond ...) function?


From: J.T. Conklin
Subject: Is there any value in a lisp-like $(cond ...) function?
Date: Wed, 15 Dec 2010 22:47:14 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, berkeley-unix)

One of my makefiles contains a rule with a recipe with a shell case
statement.  For purposes of discussion, something like this:

        t:
                case "${FOO}" in \
                foo) ${RESULTS1} ;; \
                bar) ${RESULTS2} ;; \
                baz) ${RESULTS3} ;; \
                *)   ${RESULTS4} ;; \
                esac

The actual statement is much longer, and it's often difficult to tell
exactly what command is being executed.  So I set about converting it
so it would be evaluated by GNU make rather than the shell.  My first
attempt was something like:

        t:
                $(if ${COND1},${RESULTS1},\
                        $(if ${COND2},${RESULTS2},\
                                $(if ${COND3},${RESULTS3},${RESULTS4})))

This worked as expected, but the nested $(if ...) functions needed for
the actual case statement I was using bordered on being unmaintainable.

My first impulse was to hack up a lisp-like $(cond ...) function like:

        t:
                $(cond ${COND1},${RESULTS1}, \
                       ${COND2},${RESULTS2}, \
                       ${COND3},${RESULTS3}, \
                       t, ${RESULTS4} )

If ${COND1} evaluates to true, $(cond ...) expands and returns
${RESULTS1}, otherwise it skips to ${COND2}, and so on.

While testing my implementation, I realized that using $(or ...) can
mostly approximate this:

        t:
                $(or $(if ${COND1},${RESULTS1}), \
                     $(if ${COND2},${RESULTS2}), \
                     $(if ${COND3},${RESULTS3}), \
                     ${RESULTS4})

The significant difference is that if ${CONDN} is true but ${RESULTN}
expands to false, $(or ...) will continue on where $(cond ...) would 
have stopped.  Accidently adding an else-part to an $(if ...) would
cause problems too.

But for my requirements, it turns out using $(or ...) is sufficient
and avoids the need to have a locally modified GNU make.

Before moving on, I thought I'd ask whether there was any value to
adding a $(cond ...) conditional function to GNU make.  If so, I'll
gladly contribute my func_cond implementation (even though there is
nothing special about it -- almost all of it was taken from func_if
and func_or).

    --jtc

PS: And if I've totally missed a better idiom for this task, please
let me know.

-- 
J.T. Conklin



reply via email to

[Prev in Thread] Current Thread [Next in Thread]