1

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.)

  • 2
    Notice that the attributes supported depends of the compiler (and version), not only the way to add attribute. So a MACRO `MY_NOINLINE` seems more appropriate than `ATTRIBUTE(noinline)`. In addition standard has also some [attributes](https://en.cppreference.com/w/cpp/language/attributes) now. – Jarod42 Dec 02 '19 at 15:28

1 Answers1

0

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.