Search code examples
optimizationcompilationwaf

Checking for compiler optimization flags in waf


I have begun using the waf build system as an alternative to CMake. So far, it has been a great experience. However, I cannot figure out a good way to conditionally pass optimization flags to the compiler. Some of the code I write benefits from specific optimization flags that are only available in recent gcc releases, but I want my code to be as portable as possible. Thus I want to check whether the compiler supports these flags before I add them to the list of cxx flags used for compiling. What's the easiest way to do this?


Solution

  • You can use the stash feature of the ConfigSet cfg.env to test CFLAGS with code chunks:

    • stash cfg.env
    • add CFLAGS
    • perform check, if check fail, restore cfg.env

    And keep in mind that specific CFLAGS are not portable, if you change the compiler or target architecture they won't work. You probably want your code to work with a different compiler, so you should enable (add) specific CFLAGS only when you can identify the compiler.

    If your code is opensource, don't bother imposing CFLAGS if your code is not performance-critical. Most Linux distributions will add global optimization flags depending on the target.