m4

Propagate m4 command line define to included files


I have one m4 file that gets included by several other m4 files, call it inc.m4. It defines some macros. I now want to have a command line flag which changes those definitions, e.g.

inc.m4

ifdef(`FLAG',dnl
define(`VAR',foo)dnl
,dnl
define(`VAR',bar)dnl
)dnl

test.m4

include(inc.m4)dnl
VAR

When I run m4 test.m4 I get bar as expected.

But when I run m4 -DFLAG test.m4, I get bar as well instead of foo as I'd have hoped. It seems the definition of FLAG from the command line doesn't get propagated to the included file.

Is there another way around this? I'd prefer not to do this in all the files that include inc.m4:

include(inc.m4)dnl
ifdef(`FLAG`,,define(`VAR',foo))dnl override inc.m4 for FLAG mode

Solution

  • ifdef(`FLAG',dnl
    `define(`VAR',foo)'dnl
    ,dnl
    `define(`VAR',bar)'dnl
    )dnl
    

    Please note that defines are between ticks.