c++wafgcc-attribute

How can I create a platform-independent macro to wrap a compiler extension?


I have some code like this that I need to make cross-platform:

int __attribute__((noinline)) fn() { return 0; }

I want to use Waf to write a config.h that contains

#define ATTRIBUTE(attr) __attribute__((attr))

if the compiler supports this extension, and

#define ATTRIBUTE(attr)

otherwise, so I can rewrite the function like this:

int ATTRIBUTE(noinline) fn() { return 0; }

Looking at the configuration documentation I don't see an obvious way to do this. The best I could do is define some HAS_ATTRIBUTES macro if a code snippet using this feature fails to compile.

Does waf support configuring the project in this manner, or will I have to write a second config header manually?

(I am specifically looking for a waf answer. If I could use something else, I would.)


Solution

  • Here is how I solved it.

    In my wscript:

    attribute_noinline_prg = '''
        int __attribute__((noinline)) fn() { return 0; }
        int main()
        {
            return fn();
        }
        '''
    conf.check_cxx(fragment=attribute_noinline_prg,
                   msg='Checking for __attribute__(noinline)',
                   define_name='HAVE_ATTRIBUTE_NOINLINE',
                   mandatory=False)
    

    And then in a manually-created config header:

    #ifdef HAVE_ATTRIBUTE_NOINLINE
    #define ATTRIBUTE_NOINLINE __attribute__((noinline))
    #else
    #define ATTRIBUTE_NOINLINE
    #endif
    

    I would have hoped to avoid manually creating the header like this, but it does the job.