Repository: conan-io/conan
Pull request: Set cmake compile options based on language
Changelog: Fix: Set CMake targets compile options based on language
Docs: omit

#tags: slow

- [x] Refer to the issue that supports this Pull Request: closes #7499
- [x] If the issue has missing info, explain the purpose/use case/pain/need that covers this Pull Request.
- [x] I've read the [Contributing guide](https://github.com/conan-io/conan/blob/develop/.github/CONTRIBUTING.md).
- [x] I've followed the PEP8 style guides for Python code.
- [x] I've opened another PR in the Conan docs repo to the ``develop`` branch, documenting this one. 

<sup>**Note:** By default this PR will skip the slower tests and will use a limited set of python versions. Check [here](https://github.com/conan-io/conan/blob/develop/.github/PR_INCREASE_TESTING.md) how to increase the testing level by writing some tags in the current PR body text.</sup>


Problem statement:
[bug] Cmake targets glue C and CXX flags
<!--
  Please don't forget to update the issue title.
  Include all applicable information to help us reproduce your problem.

  To help us debug your issue please explain:
-->

### Environment Details (include every applicable attribute)
  * Operating System+version: Ubuntu 20.04 in docker
  * Compiler+version: GCC 9.3 and GCC 9.2 cross for arm
  * Conan version: 1.28.0
  * Python version:  3.8.2

### Steps to reproduce (Include if Applicable)
  1. Make package with CXX specific flags, eg. -fno-exceptions, -fno-rtti
  2. Use cmake generator with target aproach and link that package to your target.
  3. Enable languages: C and C++ and add one .c and one .cpp file
  4. Compile using cmake

### Logs (Executed commands with output) (Include/Attach if Applicable)
`[build] cc1: warning: command line option '-fno-rtti' is valid for C++/D/ObjC++ but not for C`

### Possible solution:
There are lines for every target in conanbuildinfo.cmake:
```
set_property(TARGET CONAN_PKG::PACKAGE_NAME PROPERTY INTERFACE_COMPILE_OPTIONS ${CONAN_C_FLAGS_PACKAGE_NAME_LIST} ${CONAN_CXX_FLAGS_PACKAGE_NAME_LIST}
                                                                  $<$<CONFIG:Release>:${CONAN_C_FLAGS_PACKAGE_NAME_RELEASE_LIST} ${CONAN_CXX_FLAGS_PACKAGE_NAME_RELEASE_LIST}>
                                                                  $<$<CONFIG:RelWithDebInfo>:${CONAN_C_FLAGS_PACKAGE_NAME_RELWITHDEBINFO_LIST} ${CONAN_CXX_FLAGS_PACKAGE_NAME_RELWITHDEBINFO_LIST}>
                                                                  $<$<CONFIG:MinSizeRel>:${CONAN_C_FLAGS_PACKAGE_NAME_MINSIZEREL_LIST} ${CONAN_CXX_FLAGS_PACKAGE_NAME_MINSIZEREL_LIST}>
                                                                  $<$<CONFIG:Debug>:${CONAN_C_FLAGS_PACKAGE_NAME_DEBUG_LIST}  ${CONAN_CXX_FLAGS_PACKAGE_NAME_DEBUG_LIST}>)
```

Those lines should be changed to use additinal generator expression: `<$<COMPILE_LANGUAGE:(CXX|C)>`, eg.

```
set_property(TARGET CONAN_PKG::PACKAGE_NAME PROPERTY INTERFACE_COMPILE_OPTIONS (
    $<$<COMPILE_LANGUAGE:C>:${CONAN_C_FLAGS_PACKAGE_NAME_LIST}> 
    $<$<COMPILE_LANGUAGE:CXX>:${CONAN_CXX_FLAGS_PACKAGE_NAME_LIST}>
    $<$<CONFIG:Release>:
        $<$<COMPILE_LANGUAGE:C>:${CONAN_C_FLAGS_PACKAGE_NAME_RELEASE_LIST}> 
        $<$<COMPILE_LANGUAGE:CXX>:${CONAN_CXX_FLAGS_PACKAGE_NAME_RELEASE_LIST}>>
    $<$<CONFIG:RelWithDebInfo>:
        $<$<COMPILE_LANGUAGE:C>:${CONAN_C_FLAGS_PACKAGE_NAME_RELWITHDEBINFO_LIST}> 
        $<$<COMPILE_LANGUAGE:CXX>:${CONAN_CXX_FLAGS_PACKAGE_NAME_RELWITHDEBINFO_LIST}>>
    $<$<CONFIG:MinSizeRel>:
        $<$<COMPILE_LANGUAGE:C>:${CONAN_C_FLAGS_PACKAGE_NAME_MINSIZEREL_LIST}> 
        $<$<COMPILE_LANGUAGE:CXX>:${CONAN_CXX_FLAGS_PACKAGE_NAME_MINSIZEREL_LIST}>>
    $<$<CONFIG:Debug>:
        $<$<COMPILE_LANGUAGE:C>:${CONAN_C_FLAGS_PACKAGE_NAME_DEBUG_LIST}>  
        $<$<COMPILE_LANGUAGE:CXX>:${CONAN_CXX_FLAGS_PACKAGE_NAME_DEBUG_LIST}>>)
```

I haven't check it yet but I use this expression to set rtti flags in cmake project and it works correctly:
```
   add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>)
   add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>)
```

<!--
  Your log content should be related to the bug description, it can be:
  - Conan command output
  - Server output (Artifactory, conan_server)
-->

Hints from issue discussion:
Hi @pgorgon-hem 

Thanks for reporting this issue.
Just to understand the severity of the issue, what is the typical effect, just that warning, but the flag is safely ignored by the compilers? Does it fail in some scenarios?

This might need to be changed in other generators as well. As it seems this code section is getting some complexity and would be error prone, I wonder if this could be templatized. 
Hi,
The typical effect is just warning and flag is safety ignored by gcc, but I've tested it only with gcc 9 and -fno-rtti and -fno-exception flags. I don't know how it behaves with other compilers and other flags. But I can imagine that some build parameters for c and c++ objects might be different and can't link when flags set wrongly. Eg. I have external library packaged with conan. This library is also distributed for embedded targets (compiled without RTTI) and when I link that library to my executable it can't find typeinfo for objects from library, so I need to add -fno-rtti to my executable compilation to link it properly. Because I'm always traying to achive no warnings in compilation I added these flags manually in my cmake (for whole project).

This fragment of cmake code is from [conan/conans/client/generators/cmake_common.py:203](https://github.com/conan-io/conan/blob/74548ddf0a3eabf87cdc7f1e9e38a238ed16667a/conans/client/generators/cmake_common.py#L203). This file is included in every cmake generator, but it is used in generate_targets_section method which is used in CMakeGenerator and CMakeMultiGenerator, so I think it have influence on cmake and cmake-multi generators only. 

I thought about making fix of that by myself but I have only little experience in conan so I can't predict all effects of changes.

Commit message: fix cmake code and tests

Patch to review:
diff --git a/conans/client/generators/cmake_common.py b/conans/client/generators/cmake_common.py
index 72c42d7f951..e95c6102b9b 100644
--- a/conans/client/generators/cmake_common.py
+++ b/conans/client/generators/cmake_common.py
@@ -200,11 +200,20 @@ def cmake_global_vars(deps, build_type=""):
                                                                       $<$<CONFIG:RelWithDebInfo>:${{CONAN_COMPILE_DEFINITIONS_{uname}_RELWITHDEBINFO}}>
                                                                       $<$<CONFIG:MinSizeRel>:${{CONAN_COMPILE_DEFINITIONS_{uname}_MINSIZEREL}}>
                                                                       $<$<CONFIG:Debug>:${{CONAN_COMPILE_DEFINITIONS_{uname}_DEBUG}}>)
-    set_property(TARGET {name} PROPERTY INTERFACE_COMPILE_OPTIONS ${{CONAN_C_FLAGS_{uname}_LIST}} ${{CONAN_CXX_FLAGS_{uname}_LIST}}
-                                                                  $<$<CONFIG:Release>:${{CONAN_C_FLAGS_{uname}_RELEASE_LIST}} ${{CONAN_CXX_FLAGS_{uname}_RELEASE_LIST}}>
-                                                                  $<$<CONFIG:RelWithDebInfo>:${{CONAN_C_FLAGS_{uname}_RELWITHDEBINFO_LIST}} ${{CONAN_CXX_FLAGS_{uname}_RELWITHDEBINFO_LIST}}>
-                                                                  $<$<CONFIG:MinSizeRel>:${{CONAN_C_FLAGS_{uname}_MINSIZEREL_LIST}} ${{CONAN_CXX_FLAGS_{uname}_MINSIZEREL_LIST}}>
-                                                                  $<$<CONFIG:Debug>:${{CONAN_C_FLAGS_{uname}_DEBUG_LIST}}  ${{CONAN_CXX_FLAGS_{uname}_DEBUG_LIST}}>)
+    set_property(TARGET {name} PROPERTY INTERFACE_COMPILE_OPTIONS $<$<COMPILE_LANGUAGE:C>:${{CONAN_C_FLAGS_{uname}_LIST}}>
+                                                                  $<$<COMPILE_LANGUAGE:CXX>:${{CONAN_CXX_FLAGS_{uname}_LIST}}>
+                                                                  $<$<CONFIG:Release>:
+                                                                      $<$<COMPILE_LANGUAGE:C>:${{CONAN_C_FLAGS_{uname}_RELEASE_LIST}}>
+                                                                      $<$<COMPILE_LANGUAGE:CXX>:${{CONAN_CXX_FLAGS_{uname}_RELEASE_LIST}}>>
+                                                                  $<$<CONFIG:RelWithDebInfo>:
+                                                                      $<$<COMPILE_LANGUAGE:C>:${{CONAN_C_FLAGS_{uname}_RELWITHDEBINFO_LIST}}>
+                                                                      $<$<COMPILE_LANGUAGE:CXX>:${{CONAN_CXX_FLAGS_{uname}_RELWITHDEBINFO_LIST}}>>
+                                                                  $<$<CONFIG:MinSizeRel>:
+                                                                      $<$<COMPILE_LANGUAGE:C>:${{CONAN_C_FLAGS_{uname}_MINSIZEREL_LIST}}>
+                                                                      $<$<COMPILE_LANGUAGE:CXX>:${{CONAN_CXX_FLAGS_{uname}_MINSIZEREL_LIST}}>>
+                                                                  $<$<CONFIG:Debug>:
+                                                                      $<$<COMPILE_LANGUAGE:C>:${{CONAN_C_FLAGS_{uname}_DEBUG_LIST}}>
+                                                                      $<$<COMPILE_LANGUAGE:CXX>:${{CONAN_CXX_FLAGS_{uname}_DEBUG_LIST}}>>)
 """
 
 
diff --git a/conans/client/generators/cmake_find_package.py b/conans/client/generators/cmake_find_package.py
index 350af11a40c..2f04a1b2adb 100644
--- a/conans/client/generators/cmake_find_package.py
+++ b/conans/client/generators/cmake_find_package.py
@@ -42,7 +42,8 @@ class CMakeFindPackageGenerator(Generator):
                 set_property(TARGET {name}::{name} PROPERTY INTERFACE_COMPILE_DEFINITIONS
                              ${{{name}_COMPILE_DEFINITIONS}})
                 set_property(TARGET {name}::{name} PROPERTY INTERFACE_COMPILE_OPTIONS
-                             "${{{name}_COMPILE_OPTIONS_LIST}}")
+                             $<$<COMPILE_LANGUAGE:C>:${{{name}_COMPILE_OPTIONS_C}}>
+                             $<$<COMPILE_LANGUAGE:CXX>:${{{name}_COMPILE_OPTIONS_CXX}}>)
                 {find_dependencies_block}
             endif()
         endif()
@@ -96,7 +97,8 @@ class CMakeFindPackageGenerator(Generator):
         set({{ pkg_name }}_{{ comp_name }}_RES_DIRS {{ comp.res_paths }})
         set({{ pkg_name }}_{{ comp_name }}_DEFINITIONS {{ comp.defines }})
         set({{ pkg_name }}_{{ comp_name }}_COMPILE_DEFINITIONS {{ comp.compile_definitions }})
-        set({{ pkg_name }}_{{ comp_name }}_COMPILE_OPTIONS_LIST "{{ comp.cxxflags_list }}" "{{ comp.cflags_list }}")
+        set({{ pkg_name }}_{{ comp_name }}_COMPILE_OPTIONS_C {{ comp.cflags_list }})
+        set({{ pkg_name }}_{{ comp_name }}_COMPILE_OPTIONS_CPP {{ comp.cxxflags_list }})
         set({{ pkg_name }}_{{ comp_name }}_LIBS {{ comp.libs }})
         set({{ pkg_name }}_{{ comp_name }}_SYSTEM_LIBS {{ comp.system_libs }})
         set({{ pkg_name }}_{{ comp_name }}_FRAMEWORK_DIRS {{ comp.framework_paths }})
@@ -181,7 +183,8 @@ class CMakeFindPackageGenerator(Generator):
                 set_target_properties({{ pkg_name }}::{{ comp_name }} PROPERTIES INTERFACE_COMPILE_DEFINITIONS
                                       "{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_DEFINITIONS}' }}")
                 set_target_properties({{ pkg_name }}::{{ comp_name }} PROPERTIES INTERFACE_COMPILE_OPTIONS
-                                      "{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_LIST}' }}")
+                                      "$<$<COMPILE_LANGUAGE:C>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_C}' }}>
+                                       $<$<COMPILE_LANGUAGE:CXX>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_CXX}' }}>")
             endif()
         endif()
 
@@ -266,7 +269,7 @@ def _find_for_dep(self, pkg_name, pkg_findname, pkg_filename, cpp_info):
         pkg_public_deps_filenames = [self._get_filename(self.deps_build_info[public_dep])
                                      for public_dep in cpp_info.public_deps]
         pkg_public_deps_names = [self._get_name(self.deps_build_info[public_dep])
-                                     for public_dep in cpp_info.public_deps]
+                                 for public_dep in cpp_info.public_deps]
         deps_names = ";".join(["{n}::{n}".format(n=n) for n in pkg_public_deps_names])
         if cpp_info.components:
             components = self._get_components(pkg_name, pkg_findname, cpp_info)
diff --git a/conans/client/generators/cmake_find_package_common.py b/conans/client/generators/cmake_find_package_common.py
index c4af23b4aca..deb6836df4f 100644
--- a/conans/client/generators/cmake_find_package_common.py
+++ b/conans/client/generators/cmake_find_package_common.py
@@ -15,6 +15,8 @@
 )
 set({name}_COMPILE_DEFINITIONS{build_type_suffix} {deps.compile_definitions})
 set({name}_COMPILE_OPTIONS{build_type_suffix}_LIST "{deps.cxxflags_list}" "{deps.cflags_list}")
+set({name}_COMPILE_OPTIONS_C{build_type_suffix} "{deps.cflags_list}")
+set({name}_COMPILE_OPTIONS_CXX{build_type_suffix} "{deps.cxxflags_list}")
 set({name}_LIBRARIES_TARGETS{build_type_suffix} "") # Will be filled later, if CMake 3
 set({name}_LIBRARIES{build_type_suffix} "") # Will be filled later
 set({name}_LIBS{build_type_suffix} "") # Same as {name}_LIBRARIES
diff --git a/conans/client/generators/cmake_find_package_multi.py b/conans/client/generators/cmake_find_package_multi.py
index 1576f558f0b..bb852eb7198 100644
--- a/conans/client/generators/cmake_find_package_multi.py
+++ b/conans/client/generators/cmake_find_package_multi.py
@@ -64,10 +64,18 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator):
                  $<$<CONFIG:Debug>:${{{name}_COMPILE_DEFINITIONS_DEBUG}}>)
 set_property(TARGET {name}::{name}
              PROPERTY INTERFACE_COMPILE_OPTIONS
-                 $<$<CONFIG:Release>:${{{name}_COMPILE_OPTIONS_RELEASE_LIST}}>
-                 $<$<CONFIG:RelWithDebInfo>:${{{name}_COMPILE_OPTIONS_RELWITHDEBINFO_LIST}}>
-                 $<$<CONFIG:MinSizeRel>:${{{name}_COMPILE_OPTIONS_MINSIZEREL_LIST}}>
-                 $<$<CONFIG:Debug>:${{{name}_COMPILE_OPTIONS_DEBUG_LIST}}>)
+                 $<$<CONFIG:Release>:
+                     $<$<COMPILE_LANGUAGE:C>:${{{name}_COMPILE_OPTIONS_C_RELEASE}}>
+                     $<$<COMPILE_LANGUAGE:CXX>:${{{name}_COMPILE_OPTIONS_CXX_RELEASE}}>>
+                 $<$<CONFIG:RelWithDebInfo>:
+                     $<$<COMPILE_LANGUAGE:C>:${{{name}_COMPILE_OPTIONS_C_RELWITHDEBINFO}}>
+                     $<$<COMPILE_LANGUAGE:CXX>:${{{name}_COMPILE_OPTIONS_CXX_RELWITHDEBINFO}}>>
+                 $<$<CONFIG:MinSizeRel>:
+                     $<$<COMPILE_LANGUAGE:C>:${{{name}_COMPILE_OPTIONS_C_MINSIZEREL}}>
+                     $<$<COMPILE_LANGUAGE:CXX>:${{{name}_COMPILE_OPTIONS_CXX_MINSIZEREL}}>>
+                 $<$<CONFIG:Debug>:
+                     $<$<COMPILE_LANGUAGE:C>:${{{name}_COMPILE_OPTIONS_C_DEBUG}}>
+                     $<$<COMPILE_LANGUAGE:CXX>:${{{name}_COMPILE_OPTIONS_CXX_DEBUG}}>>)
     """
 
     # https://gitlab.kitware.com/cmake/cmake/blob/master/Modules/BasicConfigVersion-SameMajorVersion.cmake.in
@@ -119,7 +127,8 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator):
         set({{ pkg_name }}_{{ comp_name }}_RES_DIRS_{{ build_type }} {{ comp.res_paths }})
         set({{ pkg_name }}_{{ comp_name }}_DEFINITIONS_{{ build_type }} {{ comp.defines }})
         set({{ pkg_name }}_{{ comp_name }}_COMPILE_DEFINITIONS_{{ build_type }} {{ comp.compile_definitions }})
-        set({{ pkg_name }}_{{ comp_name }}_COMPILE_OPTIONS_LIST_{{ build_type }} "{{ comp.cxxflags_list }}" "{{ comp.cflags_list }}")
+        set({{ pkg_name }}_{{ comp_name }}_COMPILE_OPTIONS_C_{{ build_type }} "{{ comp.cflags_list }}")
+        set({{ pkg_name }}_{{ comp_name }}_COMPILE_OPTIONS_CXX_{{ build_type }} "{{ comp.cxxflags_list }}")
         set({{ pkg_name }}_{{ comp_name }}_LIBS_{{ build_type }} {{ comp.libs }})
         set({{ pkg_name }}_{{ comp_name }}_SYSTEM_LIBS_{{ build_type }} {{ comp.system_libs }})
         set({{ pkg_name }}_{{ comp_name }}_FRAMEWORK_DIRS_{{ build_type }} {{ comp.framework_paths }})
@@ -239,10 +248,18 @@ class CMakeFindPackageMultiGenerator(CMakeFindPackageGenerator):
                          $<$<CONFIG:MinSizeRel>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_DEFINITIONS_MINSIZEREL}' }}>
                          $<$<CONFIG:Debug>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_DEFINITIONS_DEBUG}' }}>)
         set_property(TARGET {{ pkg_name }}::{{ comp_name }} PROPERTY INTERFACE_COMPILE_OPTIONS
-                         $<$<CONFIG:Release>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_LIST_RELEASE}' }}>
-                         $<$<CONFIG:RelWithDebInfo>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_LIST_RELWITHDEBINFO}' }}>
-                         $<$<CONFIG:MinSizeRel>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_LIST_MINSIZEREL}' }}>
-                         $<$<CONFIG:Debug>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_LIST_DEBUG}' }}>)
+                         $<$<CONFIG:Release>:
+                             $<$<COMPILE_LANGUAGE:C>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_C_RELEASE}' }}>
+                             $<$<COMPILE_LANGUAGE:CXX>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_CXX_RELEASE}' }}>>
+                         $<$<CONFIG:RelWithDebInfo>:
+                             $<$<COMPILE_LANGUAGE:C>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_C_RELWITHDEBINFO}' }}>
+                             $<$<COMPILE_LANGUAGE:CXX>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_CXX_RELWITHDEBINFO}' }}>>
+                         $<$<CONFIG:MinSizeRel>:
+                             $<$<COMPILE_LANGUAGE:C>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_C_MINSIZEREL}' }}>
+                             $<$<COMPILE_LANGUAGE:CXX>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_CXX_MINSIZEREL}' }}>>
+                         $<$<CONFIG:Debug>:
+                             $<$<COMPILE_LANGUAGE:C>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_C_DEBUG}' }}>
+                             $<$<COMPILE_LANGUAGE:CXX>:{{ '${'+pkg_name+'_'+comp_name+'_COMPILE_OPTIONS_CXX_DEBUG}' }}>>)
         set({{ pkg_name }}_{{ comp_name }}_TARGET_PROPERTIES TRUE)
 
         {%- endfor %}
diff --git a/conans/test/functional/build_helpers/cmake_flags_test.py b/conans/test/functional/build_helpers/cmake_flags_test.py
index 8309af28cb2..da2e3c6e25f 100644
--- a/conans/test/functional/build_helpers/cmake_flags_test.py
+++ b/conans/test/functional/build_helpers/cmake_flags_test.py
@@ -262,12 +262,18 @@ def transitive_targets_flags_test(self):
         self.assertNotIn("My", cmake_cxx_flags)
         self.assertIn("CONAN_CXX_FLAGS=MyFlag1 MyFlag2 MyChatFlag1 MyChatFlag2",
                       client.out)
-        self.assertIn("HELLO_CXX_FLAGS=-load;C:\some\path;MyFlag1;MyFlag2;"
-                      "$<$<CONFIG:Release>:;>;$<$<CONFIG:RelWithDebInfo>:;>;"
-                      "$<$<CONFIG:MinSizeRel>:;>;$<$<CONFIG:Debug>:;>", client.out)
-        self.assertIn("CHAT_CXX_FLAGS=MyChatFlag1;MyChatFlag2;"
-                      "$<$<CONFIG:Release>:;>;$<$<CONFIG:RelWithDebInfo>:;>;"
-                      "$<$<CONFIG:MinSizeRel>:;>;$<$<CONFIG:Debug>:;>", client.out)
+        self.assertIn("HELLO_CXX_FLAGS=$<$<COMPILE_LANGUAGE:C>:-load;C:\some\path>;$<$<COMPILE_LANGUAGE:CXX>:MyFlag1;MyFlag2>;"
+                      "$<$<CONFIG:Release>:;$<$<COMPILE_LANGUAGE:C>:>;$<$<COMPILE_LANGUAGE:CXX>:>>;"
+                      "$<$<CONFIG:RelWithDebInfo>:;$<$<COMPILE_LANGUAGE:C>:>;$<$<COMPILE_LANGUAGE:CXX>:>>;"
+                      "$<$<CONFIG:MinSizeRel>:;$<$<COMPILE_LANGUAGE:C>:>;$<$<COMPILE_LANGUAGE:CXX>:>>;"
+                      "$<$<CONFIG:Debug>:;$<$<COMPILE_LANGUAGE:C>:>;$<$<COMPILE_LANGUAGE:CXX>:>>",
+                      client.out)
+        self.assertIn("CHAT_CXX_FLAGS=$<$<COMPILE_LANGUAGE:C>:>;$<$<COMPILE_LANGUAGE:CXX>:MyChatFlag1;MyChatFlag2;>;"
+                      "$<$<CONFIG:Release>:;$<$<COMPILE_LANGUAGE:C>:>;$<$<COMPILE_LANGUAGE:CXX>:>>;"
+                      "$<$<CONFIG:RelWithDebInfo>:;$<$<COMPILE_LANGUAGE:C>:>;$<$<COMPILE_LANGUAGE:CXX>:>>;"
+                      "$<$<CONFIG:MinSizeRel>:;$<$<COMPILE_LANGUAGE:C>:>;$<$<COMPILE_LANGUAGE:CXX>:>>;"
+                      "$<$<CONFIG:Debug>:;$<$<COMPILE_LANGUAGE:C>:>;$<$<COMPILE_LANGUAGE:CXX>:>>",
+                      client.out)
         self.assertIn('HELLO_DEFINES=MY_DEF=My" \string;MY_DEF2=My${} other \string;', client.out)
 
     def cmake_test_needed_settings(self):
diff --git a/conans/test/functional/generators/cmake_find_package_test.py b/conans/test/functional/generators/cmake_find_package_test.py
index 1412c66fa00..beaa9d32714 100644
--- a/conans/test/functional/generators/cmake_find_package_test.py
+++ b/conans/test/functional/generators/cmake_find_package_test.py
@@ -68,7 +68,8 @@ def build(self):
                       "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:shared_link_flag>;"
                       "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,MODULE_LIBRARY>:shared_link_flag>;"
                       "$<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:>", client.out)
-        self.assertIn("Compile options: a_cxx_flag;a_flag", client.out)
+        self.assertIn("Compile options: $<$<COMPILE_LANGUAGE:C>:a_flag>;$<$<COMPILE_LANGUAGE:CXX>:a_cxx_flag>",
+                      client.out)
 
     def cmake_lock_target_redefinition_test(self):
         client = TestClient()
@@ -486,14 +487,6 @@ def build(self):
                       client.out)
 
     def cpp_info_filename_test(self):
-        def add_to_conan_file(after, add_lines, spaces_to_indent):
-            indent = '\n' + (' ' * spaces_to_indent)
-            replace = indent.join([after] + add_lines)
-            replace_in_file(os.path.join(client.current_folder, "conanfile.py"),
-                            after,
-                            replace,
-                            output=client.out)
-
         client = TestClient()
         client.run("new hello/1.0 -s")
         indent = '\n        '
@@ -562,10 +555,6 @@ def build(self):
         client.run("install .")
         client.run("build .")
 
-        print('~' * 120)
-        print(client.out)
-        print('~' * 120)
-
         self.assertIn('Found MYHELLO2: 1.0 (found version "1.0")', client.out)
         self.assertIn('Found MYHELLO: 1.0 (found version "1.0")', client.out)
         self.assertIn("Target libs (hello2): "
diff --git a/conans/test/functional/generators/cmake_test.py b/conans/test/functional/generators/cmake_test.py
index 4ca405247ec..b66547a00ce 100644
--- a/conans/test/functional/generators/cmake_test.py
+++ b/conans/test/functional/generators/cmake_test.py
@@ -380,3 +380,51 @@ def build(self):
 
         client.run('create .')
         self.assertIn("POLICY CMP0054 IS OLD", client.out)
+
+    def test_cmake_compile_options(self):
+        conanfile = textwrap.dedent("""
+            from conans import ConanFile
+
+            class ConanTest(ConanFile):
+                name = "flags"
+                version = "1.0"
+                generators = "cmake"
+                settings = "os", "compiler", "arch", "build_type"
+
+                def package_info(self):
+                    self.cpp_info.cflags = ["-fno-asm"]
+                    self.cpp_info.cxxflags = ["-fno-exceptions", "-fno-rtti"]
+                """)
+
+        client = TestClient()
+        client.save({"conanfile.py": conanfile})
+        client.run("create conanfile.py")
+
+        conanfile_consumer = textwrap.dedent("""
+                from conans import ConanFile, CMake
+
+                class ConanTest(ConanFile):
+                    name = "consumer"
+                    version = "1.0"
+                    generators = "cmake"
+                    settings = "os", "compiler", "arch", "build_type"
+                    requires = "flags/1.0"
+                    exports_sources = "CMakeLists.txt"
+
+                    def build(self):
+                        cmake = CMake(self)
+                        cmake.configure()
+                    """)
+        cmakelists = textwrap.dedent("""
+            cmake_minimum_required(VERSION 2.8)
+            PROJECT(consumer C CXX)
+            include(conanbuildinfo.cmake)
+            CONAN_BASIC_SETUP(TARGETS)
+            get_target_property(opts CONAN_PKG::flags INTERFACE_COMPILE_OPTIONS)
+            message("${opts}")
+            """)
+        client.save({"conanfile.py": conanfile_consumer,
+                     "CMakeLists.txt": cmakelists})
+        client.run('create .')
+        self.assertIn("$<$<COMPILE_LANGUAGE:C>:-fno-asm>;$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions;-fno-rtti>;",
+                      client.out)


Code review comments:
diff_hunk: @@ -486,14 +487,6 @@ def build(self):
                       client.out)
 
     def cpp_info_filename_test(self):
-        def add_to_conan_file(after, add_lines, spaces_to_indent):
path: conans/test/functional/generators/cmake_find_package_test.py
text: @author:
removed code not used