Repository: conan-io/conan

Issue:
[Enhancement] make conanbuildinfo.cmake silent
Hello there,

I have browsed through similar requests / issues where the user wants to control the amount of output. This time it is about the generated CMake file. The "Library ... found:  <awful long path and filename here>" message appears many many times in my CMake output and it comes from conanbuildinfo.cmake.

Can we please suppress certain outputs like the above one?

ideally, conan install would look like:

conan install <arguments> -g cmake -quiet_cmake_files

there are also warnings that might be acceptable under certain circumstances. Eg. we deploy precompiled packages with compiler = gcc. If someone compiles the product with Clang, we will get a compiler mismatch warning (or the warning that we disabled the check). The product compiles and the linker can link everything fine. So in this case we don't need the warning.

So please consider allowing us to make the CMake script silent!

Thanks



Discussion:
HI @dzolee!

AFAIK it's not possible to configure cmake log level through the environment. So, in your case, we would need to provide a new generator suppressing all messages.

WDYT @danimtb ?
My friend, thanks for the response.
U got it wrong. I do not want to configure the CMake log level.

I want conanbuildinfo.cmake NOT print anything, NOT call message() if I do not want to.
I would like some IF's around message() calls in conanbuildinfo.cmake.
I would like that the Conan CMake generator(s) would produce such CMake code.

Clear now? :)
Hi @dzolee 

Are you using the ``conan_basic_setup()`` macro in your *CMakeLists.txt* ?

If so, maybe we could add a new parameter to that macro to avoid the messages. I will take a look.

Thanks
Thanks guys
Yes I am using conan_basic_setup(TARGETS)

I think the following example illustrates my use case: I have 6 lines printed, per package that I include with Conan:
![grafik](https://user-images.githubusercontent.com/10893838/49219476-b7142580-f3d3-11e8-81b9-39b24380b3f0.png)



Hi @dzolee 

Small question: are you seeing the above once, for each package you have in your dependencies? Because the compiler checks, for example, should be displayed just once. Including and running ``conan_basic_setup()`` once is enough, maybe you are calling it many times?

Resolving patch:
diff --git a/conans/client/generators/cmake_common.py b/conans/client/generators/cmake_common.py
--- a/conans/client/generators/cmake_common.py
+++ b/conans/client/generators/cmake_common.py
@@ -169,16 +169,22 @@ def generate_targets_section(dependencies):
 
 _cmake_common_macros = """
 
+function(conan_message MESSAGE_OUTPUT)
+    if(NOT CONAN_CMAKE_SILENT_OUTPUT)
+        message(${ARGV${0}})
+    endif()
+endfunction()
+
 function(conan_find_libraries_abs_path libraries package_libdir libraries_abs_path)
     foreach(_LIBRARY_NAME ${libraries})
         unset(CONAN_FOUND_LIBRARY CACHE)
         find_library(CONAN_FOUND_LIBRARY NAME ${_LIBRARY_NAME} PATHS ${package_libdir}
                      NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
         if(CONAN_FOUND_LIBRARY)
-            message(STATUS "Library ${_LIBRARY_NAME} found ${CONAN_FOUND_LIBRARY}")
+            conan_message(STATUS "Library ${_LIBRARY_NAME} found ${CONAN_FOUND_LIBRARY}")
             set(CONAN_FULLPATH_LIBS ${CONAN_FULLPATH_LIBS} ${CONAN_FOUND_LIBRARY})
         else()
-            message(STATUS "Library ${_LIBRARY_NAME} not found in package, might be system one")
+            conan_message(STATUS "Library ${_LIBRARY_NAME} not found in package, might be system one")
             set(CONAN_FULLPATH_LIBS ${CONAN_FULLPATH_LIBS} ${_LIBRARY_NAME})
         endif()
     endforeach()
@@ -192,7 +198,7 @@ def generate_targets_section(dependencies):
         find_library(CONAN_FOUND_LIBRARY NAME ${_LIBRARY_NAME} PATHS ${package_libdir}
                      NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
         if(CONAN_FOUND_LIBRARY)
-            message(STATUS "Library ${_LIBRARY_NAME} found ${CONAN_FOUND_LIBRARY}")
+            conan_message(STATUS "Library ${_LIBRARY_NAME} found ${CONAN_FOUND_LIBRARY}")
             set(_LIB_NAME CONAN_LIB::${package_name}_${_LIBRARY_NAME}${build_type})
             add_library(${_LIB_NAME} UNKNOWN IMPORTED)
             set_target_properties(${_LIB_NAME} PROPERTIES IMPORTED_LOCATION ${CONAN_FOUND_LIBRARY})
@@ -200,7 +206,7 @@ def generate_targets_section(dependencies):
             set_property(TARGET ${_LIB_NAME} PROPERTY INTERFACE_LINK_LIBRARIES ${deps_list})
             set(CONAN_FULLPATH_LIBS ${CONAN_FULLPATH_LIBS} ${_LIB_NAME})
         else()
-            message(STATUS "Library ${_LIBRARY_NAME} not found in package, might be system one")
+            conan_message(STATUS "Library ${_LIBRARY_NAME} not found in package, might be system one")
             set(CONAN_FULLPATH_LIBS ${CONAN_FULLPATH_LIBS} ${_LIBRARY_NAME})
         endif()
     endforeach()
@@ -210,7 +216,7 @@ def generate_targets_section(dependencies):
 
 macro(conan_set_libcxx)
     if(DEFINED CONAN_LIBCXX)
-        message(STATUS "Conan: C++ stdlib: ${CONAN_LIBCXX}")
+        conan_message(STATUS "Conan: C++ stdlib: ${CONAN_LIBCXX}")
         if(CONAN_COMPILER STREQUAL "clang" OR CONAN_COMPILER STREQUAL "apple-clang")
             if(CONAN_LIBCXX STREQUAL "libstdc++" OR CONAN_LIBCXX STREQUAL "libstdc++11" )
                 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++")
@@ -238,21 +244,21 @@ def generate_targets_section(dependencies):
 endmacro()
 
 macro(conan_set_std)
-  # Do not warn "Manually-specified variables were not used by the project"
-  set(ignorevar "${CONAN_STD_CXX_FLAG}${CONAN_CMAKE_CXX_STANDARD}${CONAN_CMAKE_CXX_EXTENSIONS}")
-  if (CMAKE_VERSION VERSION_LESS "3.1" OR 
-     (CMAKE_VERSION VERSION_LESS "3.12" AND ("${CONAN_CMAKE_CXX_STANDARD}" STREQUAL "20" OR "${CONAN_CMAKE_CXX_STANDARD}" STREQUAL "gnu20")))
+    # Do not warn "Manually-specified variables were not used by the project"
+    set(ignorevar "${CONAN_STD_CXX_FLAG}${CONAN_CMAKE_CXX_STANDARD}${CONAN_CMAKE_CXX_EXTENSIONS}")
+    if (CMAKE_VERSION VERSION_LESS "3.1" OR 
+        (CMAKE_VERSION VERSION_LESS "3.12" AND ("${CONAN_CMAKE_CXX_STANDARD}" STREQUAL "20" OR "${CONAN_CMAKE_CXX_STANDARD}" STREQUAL "gnu20")))
     if(CONAN_STD_CXX_FLAG)
-      message(STATUS "Conan setting CXX_FLAGS flags: ${CONAN_STD_CXX_FLAG}")
-      set(CMAKE_CXX_FLAGS "${CONAN_STD_CXX_FLAG} ${CMAKE_CXX_FLAGS}")
+        conan_message(STATUS "Conan setting CXX_FLAGS flags: ${CONAN_STD_CXX_FLAG}")
+        set(CMAKE_CXX_FLAGS "${CONAN_STD_CXX_FLAG} ${CMAKE_CXX_FLAGS}")
     endif()
-  else()
-    if(CONAN_CMAKE_CXX_STANDARD)
-      message(STATUS "Conan setting CPP STANDARD: ${CONAN_CMAKE_CXX_STANDARD} WITH EXTENSIONS ${CONAN_CMAKE_CXX_EXTENSIONS}")
-      set(CMAKE_CXX_STANDARD ${CONAN_CMAKE_CXX_STANDARD})
-      set(CMAKE_CXX_EXTENSIONS ${CONAN_CMAKE_CXX_EXTENSIONS})
+    else()
+        if(CONAN_CMAKE_CXX_STANDARD)
+            conan_message(STATUS "Conan setting CPP STANDARD: ${CONAN_CMAKE_CXX_STANDARD} WITH EXTENSIONS ${CONAN_CMAKE_CXX_EXTENSIONS}")
+            set(CMAKE_CXX_STANDARD ${CONAN_CMAKE_CXX_STANDARD})
+            set(CMAKE_CXX_EXTENSIONS ${CONAN_CMAKE_CXX_EXTENSIONS})
+        endif()
     endif()
-  endif()
 endmacro()
 
 macro(conan_set_rpath)
@@ -275,7 +281,7 @@ def generate_targets_section(dependencies):
 
 macro(conan_set_fpic)
     if(DEFINED CONAN_CMAKE_POSITION_INDEPENDENT_CODE)
-        message(STATUS "Conan: Adjusting fPIC flag (${CONAN_CMAKE_POSITION_INDEPENDENT_CODE})")
+        conan_message(STATUS "Conan: Adjusting fPIC flag (${CONAN_CMAKE_POSITION_INDEPENDENT_CODE})")
         set(CMAKE_POSITION_INDEPENDENT_CODE ${CONAN_CMAKE_POSITION_INDEPENDENT_CODE})
     endif()
 endmacro()
@@ -319,9 +325,9 @@ def generate_targets_section(dependencies):
 
 set(_CONAN_CURRENT_DIR ${CMAKE_CURRENT_LIST_DIR})
 function(conan_get_compiler CONAN_INFO_COMPILER CONAN_INFO_COMPILER_VERSION)
-    MESSAGE(STATUS "Current conanbuildinfo.cmake directory: " ${_CONAN_CURRENT_DIR})
+    conan_message(STATUS "Current conanbuildinfo.cmake directory: " ${_CONAN_CURRENT_DIR})
     if(NOT EXISTS ${_CONAN_CURRENT_DIR}/conaninfo.txt)
-        message(STATUS "WARN: conaninfo.txt not found")
+        conan_message(STATUS "WARN: conaninfo.txt not found")
         return()
     endif()
 
@@ -357,26 +363,26 @@ def generate_targets_section(dependencies):
     elseif(CONAN_COMPILER STREQUAL "gcc")
         set(_CHECK_VERSION ${VERSION_MAJOR}.${VERSION_MINOR})
         if(NOT ${CONAN_COMPILER_VERSION} VERSION_LESS 5.0)
-            message(STATUS "Conan: Compiler GCC>=5, checking major version ${CONAN_COMPILER_VERSION}")
+            conan_message(STATUS "Conan: Compiler GCC>=5, checking major version ${CONAN_COMPILER_VERSION}")
             conan_split_version(${CONAN_COMPILER_VERSION} CONAN_COMPILER_MAJOR CONAN_COMPILER_MINOR)
             if("${CONAN_COMPILER_MINOR}" STREQUAL "")
                 set(_CHECK_VERSION ${VERSION_MAJOR})
             endif()
         endif()
-        message(STATUS "Conan: Checking correct version: ${_CHECK_VERSION}")
+        conan_message(STATUS "Conan: Checking correct version: ${_CHECK_VERSION}")
         if(NOT ${_CHECK_VERSION} VERSION_EQUAL CONAN_COMPILER_VERSION)
             conan_error_compiler_version()
         endif()
     elseif(CONAN_COMPILER STREQUAL "clang")
         set(_CHECK_VERSION ${VERSION_MAJOR}.${VERSION_MINOR})
         if(NOT ${CONAN_COMPILER_VERSION} VERSION_LESS 8.0)
-            message(STATUS "Conan: Compiler Clang>=8, checking major version ${CONAN_COMPILER_VERSION}")
+            conan_message(STATUS "Conan: Compiler Clang>=8, checking major version ${CONAN_COMPILER_VERSION}")
             conan_split_version(${CONAN_COMPILER_VERSION} CONAN_COMPILER_MAJOR CONAN_COMPILER_MINOR)
             if("${CONAN_COMPILER_MINOR}" STREQUAL "")
                 set(_CHECK_VERSION ${VERSION_MAJOR})
             endif()
         endif()
-        message(STATUS "Conan: Checking correct version: ${_CHECK_VERSION}")
+        conan_message(STATUS "Conan: Checking correct version: ${_CHECK_VERSION}")
         if(NOT ${_CHECK_VERSION} VERSION_EQUAL CONAN_COMPILER_VERSION)
             conan_error_compiler_version()
         endif()
@@ -386,14 +392,14 @@ def generate_targets_section(dependencies):
            conan_error_compiler_version()
         endif()
     else()
-        message(STATUS "WARN: Unknown compiler '${CONAN_COMPILER}', skipping the version check...")
+        conan_message(STATUS "WARN: Unknown compiler '${CONAN_COMPILER}', skipping the version check...")
     endif()
 endfunction()
 
 function(conan_check_compiler)
     if(NOT DEFINED CMAKE_CXX_COMPILER_ID)
         if(DEFINED CMAKE_C_COMPILER_ID)
-            message(STATUS "This project seems to be plain C, using '${CMAKE_C_COMPILER_ID}' compiler")
+            conan_message(STATUS "This project seems to be plain C, using '${CMAKE_C_COMPILER_ID}' compiler")
             set(CMAKE_CXX_COMPILER_ID ${CMAKE_C_COMPILER_ID})
             set(CMAKE_CXX_COMPILER_VERSION ${CMAKE_C_COMPILER_VERSION})
         else()
@@ -401,20 +407,20 @@ def generate_targets_section(dependencies):
         endif()
     endif()
     if(CONAN_DISABLE_CHECK_COMPILER)
-        message(STATUS "WARN: Disabled conan compiler checks")
+        conan_message(STATUS "WARN: Disabled conan compiler checks")
         return()
     endif()
     if(NOT CMAKE_CXX_COMPILER_ID AND NOT CMAKE_C_COMPILER_ID)
         # This use case happens when compiler is not identified by CMake, but the compilers are there and work
-        message(STATUS "*** WARN: CMake was not able to identify a C or C++ compiler ***")
-        message(STATUS "*** WARN: Disabling compiler checks. Please make sure your settings match your environment ***")
+        conan_message(STATUS "*** WARN: CMake was not able to identify a C or C++ compiler ***")
+        conan_message(STATUS "*** WARN: Disabling compiler checks. Please make sure your settings match your environment ***")
         return()
     endif()
     if(NOT DEFINED CONAN_COMPILER)
         conan_get_compiler(CONAN_COMPILER CONAN_COMPILER_VERSION)
         if(NOT DEFINED CONAN_COMPILER)
-            message(STATUS "WARN: CONAN_COMPILER variable not set, please make sure yourself that "
-                       "your compiler and version matches your declared settings")
+            conan_message(STATUS "WARN: CONAN_COMPILER variable not set, please make sure yourself that "
+                          "your compiler and version matches your declared settings")
             return()
         endif()
     endif()
@@ -450,8 +456,8 @@ def generate_targets_section(dependencies):
 
 
     if(NOT DEFINED CONAN_COMPILER_VERSION)
-        message(STATUS "WARN: CONAN_COMPILER_VERSION variable not set, please make sure yourself "
-                       "that your compiler version matches your declared settings")
+        conan_message(STATUS "WARN: CONAN_COMPILER_VERSION variable not set, please make sure yourself "
+                             "that your compiler version matches your declared settings")
         return()
     endif()
     check_compiler_version()
@@ -518,10 +524,10 @@ def generate_targets_section(dependencies):
     set(options TARGETS NO_OUTPUT_DIRS SKIP_RPATH KEEP_RPATHS SKIP_STD SKIP_FPIC)
     cmake_parse_arguments(ARGUMENTS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
     if(CONAN_EXPORTED)
-        message(STATUS "Conan: called by CMake conan helper")
+        conan_message(STATUS "Conan: called by CMake conan helper")
     endif()
     if(CONAN_IN_LOCAL_CACHE)
-        message(STATUS "Conan: called inside local cache")
+        conan_message(STATUS "Conan: called inside local cache")
     endif()
     conan_check_compiler()
     if(NOT ARGUMENTS_NO_OUTPUT_DIRS)
@@ -529,23 +535,23 @@ def generate_targets_section(dependencies):
     endif()
     conan_set_find_library_paths()
     if(NOT ARGUMENTS_TARGETS)
-        message(STATUS "Conan: Using cmake global configuration")
+        conan_message(STATUS "Conan: Using cmake global configuration")
         conan_global_flags()
     else()
-        message(STATUS "Conan: Using cmake targets configuration")
+        conan_message(STATUS "Conan: Using cmake targets configuration")
         conan_define_targets()
     endif()
     if(ARGUMENTS_SKIP_RPATH)
         # Change by "DEPRECATION" or "SEND_ERROR" when we are ready
-        message(WARNING "Conan: SKIP_RPATH is deprecated, it has been renamed to KEEP_RPATHS")
+        conan_message(WARNING "Conan: SKIP_RPATH is deprecated, it has been renamed to KEEP_RPATHS")
     endif()
     if(NOT ARGUMENTS_SKIP_RPATH AND NOT ARGUMENTS_KEEP_RPATHS)
         # Parameter has renamed, but we keep the compatibility with old SKIP_RPATH
-        message(STATUS "Conan: Adjusting default RPATHs Conan policies")
+        conan_message(STATUS "Conan: Adjusting default RPATHs Conan policies")
         conan_set_rpath()
     endif()
     if(NOT ARGUMENTS_SKIP_STD)
-        message(STATUS "Conan: Adjusting language standard")
+        conan_message(STATUS "Conan: Adjusting language standard")
         conan_set_std()
     endif()
     if(NOT ARGUMENTS_SKIP_FPIC)
@@ -631,18 +637,18 @@ def generate_targets_section(dependencies):
     set(options TARGETS)
     cmake_parse_arguments(ARGUMENTS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
     if(CONAN_EXPORTED)
-        message(STATUS "Conan: called by CMake conan helper")
+        conan_message(STATUS "Conan: called by CMake conan helper")
     endif()
     if(CONAN_IN_LOCAL_CACHE)
-        message(STATUS "Conan: called inside local cache")
+        conan_message(STATUS "Conan: called inside local cache")
     endif()
     conan_check_compiler()
     # conan_output_dirs_setup()
     if(NOT ARGUMENTS_TARGETS)
-        message(STATUS "Conan: Using cmake global configuration")
+        conan_message(STATUS "Conan: Using cmake global configuration")
         conan_global_flags()
     else()
-        message(STATUS "Conan: Using cmake targets configuration")
+        conan_message(STATUS "Conan: Using cmake targets configuration")
         conan_define_targets()
     endif()
     conan_set_rpath()


Test patch:
diff --git a/conans/test/generators/cmake_test.py b/conans/test/generators/cmake_test.py
--- a/conans/test/generators/cmake_test.py
+++ b/conans/test/generators/cmake_test.py
@@ -3,15 +3,19 @@
 import unittest
 
 from collections import namedtuple
+from nose.plugins.attrib import attr
+
 from conans.client.conf import default_settings_yml
 from conans.client.generators.cmake import CMakeGenerator
 from conans.client.generators.cmake_multi import CMakeMultiGenerator
+from conans.client.tools import replace_in_file
 from conans.model.build_info import CppInfo
 from conans.model.conan_file import ConanFile
 from conans.model.env_info import EnvValues
 from conans.model.ref import ConanFileReference
 from conans.model.settings import Settings
 from conans.test.utils.test_files import temp_folder
+from conans.test.utils.tools import TestClient
 from conans.util.files import save
 
 
@@ -169,10 +173,10 @@ def aux_cmake_test_setup_test(self):
     set(options TARGETS NO_OUTPUT_DIRS SKIP_RPATH KEEP_RPATHS SKIP_STD SKIP_FPIC)
     cmake_parse_arguments(ARGUMENTS "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
     if(CONAN_EXPORTED)
-        message(STATUS "Conan: called by CMake conan helper")
+        conan_message(STATUS "Conan: called by CMake conan helper")
     endif()
     if(CONAN_IN_LOCAL_CACHE)
-        message(STATUS "Conan: called inside local cache")
+        conan_message(STATUS "Conan: called inside local cache")
     endif()
     conan_check_compiler()
     if(NOT ARGUMENTS_NO_OUTPUT_DIRS)
@@ -180,23 +184,23 @@ def aux_cmake_test_setup_test(self):
     endif()
     conan_set_find_library_paths()
     if(NOT ARGUMENTS_TARGETS)
-        message(STATUS "Conan: Using cmake global configuration")
+        conan_message(STATUS "Conan: Using cmake global configuration")
         conan_global_flags()
     else()
-        message(STATUS "Conan: Using cmake targets configuration")
+        conan_message(STATUS "Conan: Using cmake targets configuration")
         conan_define_targets()
     endif()
     if(ARGUMENTS_SKIP_RPATH)
         # Change by "DEPRECATION" or "SEND_ERROR" when we are ready
-        message(WARNING "Conan: SKIP_RPATH is deprecated, it has been renamed to KEEP_RPATHS")
+        conan_message(WARNING "Conan: SKIP_RPATH is deprecated, it has been renamed to KEEP_RPATHS")
     endif()
     if(NOT ARGUMENTS_SKIP_RPATH AND NOT ARGUMENTS_KEEP_RPATHS)
         # Parameter has renamed, but we keep the compatibility with old SKIP_RPATH
-        message(STATUS "Conan: Adjusting default RPATHs Conan policies")
+        conan_message(STATUS "Conan: Adjusting default RPATHs Conan policies")
         conan_set_rpath()
     endif()
     if(NOT ARGUMENTS_SKIP_STD)
-        message(STATUS "Conan: Adjusting language standard")
+        conan_message(STATUS "Conan: Adjusting language standard")
         conan_set_std()
     endif()
     if(NOT ARGUMENTS_SKIP_FPIC)
@@ -262,3 +266,33 @@ def settings_are_generated_tests(self):
         self.assertIn('set(CONAN_SETTINGS_COMPILER_VERSION "12")', cmake_lines)
         self.assertIn('set(CONAN_SETTINGS_COMPILER_RUNTIME "MD")', cmake_lines)
         self.assertIn('set(CONAN_SETTINGS_OS "Windows")', cmake_lines)
+
+    @attr('slow')
+    def no_output_test(self):
+        client = TestClient()
+        client.run("new Test/1.0 --sources")
+        cmakelists_path = os.path.join(client.current_folder, "src", "CMakeLists.txt")
+
+        # Test output works as expected
+        client.run("create . danimtb/testing")
+        self.assertIn("Conan: Using cmake global configuration", client.out)
+        self.assertIn("Conan: Adjusting default RPATHs Conan policies", client.out)
+        self.assertIn("Conan: Adjusting language standard", client.out)
+
+        # Silence output
+        replace_in_file(cmakelists_path,
+                        "conan_basic_setup()",
+                        "set(CONAN_CMAKE_SILENT_OUTPUT True)\nconan_basic_setup()",
+                        output=client.user_io.out)
+        client.run("create . danimtb/testing")
+        self.assertNotIn("Conan: Using cmake global configuration", client.out)
+        self.assertNotIn("Conan: Adjusting default RPATHs Conan policies", client.out)
+        self.assertNotIn("Conan: Adjusting language standard", client.out)
+
+        # Use TARGETS
+        replace_in_file(cmakelists_path, "conan_basic_setup()", "conan_basic_setup(TARGETS)",
+                        output=client.user_io.out)
+        client.run("create . danimtb/testing")
+        self.assertNotIn("Conan: Using cmake targets configuration", client.out)
+        self.assertNotIn("Conan: Adjusting default RPATHs Conan policies", client.out)
+        self.assertNotIn("Conan: Adjusting language standard", client.out)