[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
How to use m4_require with arguments?
From: |
Paolo Bonzini |
Subject: |
How to use m4_require with arguments? |
Date: |
Thu, 16 Oct 2008 11:55:32 +0200 |
User-agent: |
Thunderbird 2.0.0.17 (Macintosh/20080914) |
(Note: I wanted use this to provide different implementations of
AC_COMPILE_IFELSE for different languages. I think I can also do
without it, but I thought I'd write nevertheless about a more general
m4sugar problem).
The m4sugar.m4 file mentions the possibility to use:
m4_require([AC_CHECK_HEADERS(limits.h)])
but this does not work because there is no matching m4_provide anywhere:
m4_init
m4_defun([ttt_prepare], [[$0: foo=]foo])
m4_defun([ttt], [m4_require([$0_prepare(foo)])[$0: foo=]foo])
m4_divert_push(0)dnl
m4_define([foo], [1])ttt
m4_define([foo], [2])ttt
m4_define([foo], [1])ttt
ttt
m4_divert_pop(0)
=> ttt_prepare: foo=1
ttt: foo=1
ttt_prepare: foo=2
ttt: foo=2
ttt_prepare: foo=1
ttt: foo=1
ttt_prepare: foo=1
ttt: foo=1
I then tried:
m4_defun([ttt_prepare], [m4_provide([$0(foo)])[$0: foo=]foo])
m4_defun([ttt], [m4_require([$0_prepare(foo)])[$0: foo=]foo])
but it did not work at all:
=> ttt_prepare: foo=1
ttt: foo=1
ttt: foo=2
ttt: foo=1
ttt: foo=1
The only ways I found to make it work are:
m4_defun([ttt_prepare], [m4_provide([$0(]foo[)])[$0: foo=]foo])
m4_defun([ttt], [m4_require([$0_prepare(]foo[)])[$0: foo=]foo])
m4_defun([ttt_prepare], [m4_provide([$0($*)])[$0: foo=]foo])
m4_defun([ttt], [m4_require([$0_prepare(]foo[)])[$0: foo=]foo])
m4_defun([ttt_prepare], [m4_provide([$0($@)])[$0: foo=]foo])
m4_defun([ttt],
[m4_require([$0][_prepare](m4_dquote(foo)))[$0: foo=]foo])
=> ttt_prepare: foo is: 1
ttt: foo is: 1
ttt_prepare: foo is: 2
ttt: foo is: 2
ttt: foo is: 1
ttt: foo is: 1
The last one is ugly because of quoting problems (couldn't just use
[[$0_prepare]]), but it actually suggested a decent solution:
m4_define([m4_require_with_args],
[m4_require([$1](m4_dquote(m4_shift($@))))])
to be used as:
m4_defun([ttt_prepare], [m4_provide([$0($@)])[$0: foo=]foo])
m4_defun([ttt],
[m4_require_with_args([$0_prepare], foo)[$0: foo=]foo])
This is quite robust, as I tried on more examples. The problems are:
1) that this requires a contract between the macros: if the definition
uses m4_provide([$0($@)]), the caller should use m4_require_with_args;
otherwise, it should use m4_require.
2) that if "foo" might expand to something that includes commas, you
have to use m4_expand(foo) in the arguments of m4_require_with_args.
This is ugly.
Any better ideas?
Paolo
- How to use m4_require with arguments?,
Paolo Bonzini <=