conanconan-2

Conan profile: different compiler flags per build type


I have the following Conan default profile:

[settings]
build_type=Debug
arch=x86_64
compiler=gcc
compiler.cppstd=gnu23
compiler.libcxx=libstdc++11
compiler.version=14
os=Linux

[conf]
tools.build:cxxflags=["-march=skylake", "-mtune=skylake"]

{% if build_type == "Release" %}
    tools.build:cxxflags+=["-flto=auto"]
{% endif %}

However, it seems that build_type renders as an empty string during profile evaluation. Is there a way to define different compiler flags based on the build type?

I use this profile to build the Debug build type by default, but e.g. for benchmarks I run conan build . --build=missing -s build_type=Release to build the Release one.

I know I can create a separate release profile, but my question is whether it's possible to handle this in a single file. I have per-package configuration and would prefer not to duplicate or triplicate that across multiple profiles.


Solution

  • build_type is not a jinja2 variable, it is just a string in the file, that jinja is not aware at all of it, so it is expected that evaluates to empty.

    That comparison if build_type == "Release" doesn't make much sense, because the build_type = Debug a few lines above, and that would evaluate at the time the file is loaded. So build_type will always be a constant in this file with a value of Debug. The loading of profile files is reading them from disk, then evaluating the jinja2 rendering, so anything that is external (like other command line arguments) are not involved in the evaluation of this jinja2 render.

    It wouldn't be necessary to duplicate things in different files, as profile files have both include() directives (in the Conan syntax), that allows to load other profile files and compose over them, and also the jinja2 syntax of include/import could be used to load other files and reuse logic from them to avoid duplication.