Skip to content

Commit

Permalink
Fix handling of tools.build:defines for multiconfig CMake (#15921) (#…
Browse files Browse the repository at this point in the history
…15924)

* Fix handling of `tools.build:defines` for multiconfig CMake (#15921)

* add test checks

* fix

* fix test

---------

Co-authored-by: memsharded <james@conan.io>
  • Loading branch information
stevenwdv and memsharded authored Mar 25, 2024
1 parent 0ef9278 commit 80f8b7d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
6 changes: 4 additions & 2 deletions conan/tools/cmake/toolchain/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def context(self):

if not config_dict:
return None

vs_debugger_path = ""
for config, value in config_dict.items():
vs_debugger_path += f"$<$<CONFIG:{config}>:{value}>"
Expand Down Expand Up @@ -613,7 +613,9 @@ class ExtraFlagsBlock(Block):
{% endif %}
{% if defines %}
{% if config %}
add_compile_definitions($<$<CONFIG:{{config}}>:{% for define in defines %}" {{ define }}"{% endfor %}>)
{% for define in defines %}
add_compile_definitions($<$<CONFIG:{{config}}>:"{{ define }}">)
{% endfor %}
{% else %}
add_compile_definitions({% for define in defines %} "{{ define }}"{% endfor %})
{% endif %}
Expand Down
17 changes: 11 additions & 6 deletions conans/test/functional/toolchains/cmake/test_cmake_toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1833,15 +1833,15 @@ def test_cmake_toolchain_cxxflags_multi_config():
profile_release = textwrap.dedent(r"""
include(default)
[conf]
tools.build:defines=["answer=42"]
tools.build:defines=["conan_test_answer=42", "conan_test_other=24"]
tools.build:cxxflags=["/Zc:__cplusplus"]
""")
profile_debug = textwrap.dedent(r"""
include(default)
[settings]
build_type=Debug
[conf]
tools.build:defines=["answer=123"]
tools.build:defines=["conan_test_answer=123"]
tools.build:cxxflags=["/W4"]
""")

Expand All @@ -1868,10 +1868,13 @@ def build(self):
#include <stdio.h>
#define STR(x) #x
#define SHOW_DEFINE(x) printf("%s=%s\n", #x, STR(x))
#define SHOW_DEFINE(x) printf("DEFINE %s=%s!\n", #x, STR(x))
int main() {
SHOW_DEFINE(answer);
SHOW_DEFINE(conan_test_answer);
#ifdef conan_test_other
SHOW_DEFINE(conan_test_other);
#endif
char a = 123L; // to trigger warnings
#if __cplusplus
Expand Down Expand Up @@ -1902,9 +1905,11 @@ def build(self):
assert "warning C4189" in c.out

c.run_command(r"build\Release\example.exe")
assert 'answer=42' in c.out
assert 'DEFINE conan_test_answer=42!' in c.out
assert 'DEFINE conan_test_other=24!' in c.out
assert "CPLUSPLUS: __cplusplus20" in c.out

c.run_command(r"build\Debug\example.exe")
assert 'answer=123' in c.out
assert 'DEFINE conan_test_answer=123' in c.out
assert 'other=' not in c.out
assert "CPLUSPLUS: __cplusplus19" in c.out

0 comments on commit 80f8b7d

Please sign in to comment.