Repository: conan-io/conan

Issue:
Conan on Mac can't recognize compiler correctly
Hello, 
https://travis-ci.org/procxx/kepka/jobs/322618396

on the link you can see that Conan recognize compiler as gcc 4.2 on Travis, but it's not true.


Discussion:
I think it might be because conan detects the compiler version executing: ``gcc --version``, and in your case it might be that only ``g++`` is configured as the default one. Can you output in your script the output of ``gcc --version``, or better, configure gcc 8.1 as the default gcc too?
@memsharded on osx, gcc and g++ are system aliases for clang.
To further clarify why not g++ is used in the detection, the rationale is that there are many projects that contain a mixture of C and C++, so both compilers are used. Sometimes in different libraries, but sometimes also in the same library or package. As it seems quite important to maintain consistency, there is single ``settings.compiler=gcc`` setting (not g++), and a single ``settings.compiler.version`` value in the default settings. 
I tried passing `-s compiler.version=7.2` to force detection override, subsequently cmake fails because conan generates invalid compiler identification:

```
-- Current conanbuildinfo.cmake directory: /Users/travis/build/procxx/kepka/_build_
CMake Error at _build_/conanbuildinfo.cmake:474 (message):
  Incorrect 'gcc', is not the one detected by CMake: 'AppleClang'
Call Stack (most recent call first):
  _build_/conanbuildinfo.cmake:173 (conan_check_compiler)
  CMakeLists.txt:30 (conan_basic_setup)
```
Ok, I think I get the issue. This code is in ``detect.py``:

```python
cc = os.environ.get("CC", "")
cxx = os.environ.get("CXX", "")
if cc or cxx:  # Env defined, use them
        output.info("CC and CXX: %s, %s " % (cc or "None", cxx or "None"))
        command = cc or cxx
        if "clang" in command.lower():
              return _clang_compiler(output, command)
```

Setting the CXX env var to "clang++" instead of "g++" might workaround the issue, while better detection is developed.
If you are overriding in command line, you have also to override the compiler, that is not gcc:

```bash
$ conan install . -s compiler=apple-clang -s compiler.version=7.3 -s ...
```
Beware 7.2 is not a default compiler.version for apple-clang. You can check the default settings values in ``settings.yml`` file.
So I reckon there's no portable way to set this up... ok, i will use different per-platform invocations then.
I am defining this as an improvement, lets aim to do this for 1.1, but also low priority, if 1.1 is packed, it might be delayed. Thanks very much for the feedback!
Hi @ZaMaZaN4iK ,

We've been investigating the issue and it was happening in conan ``0.30.3`` see https://travis-ci.org/procxx/kepka/jobs/322618395#L1364. I've tried to reproduce the issue with latest conan version ``1.0.4`` https://travis-ci.org/pvicente/kepka/jobs/342041385#L1959 but the behaviour detecting the compiler is ok.

Now it looks like you have another problem but not related with conan,

```
-- Performing Test WNCE_FLAG_SUPPORTED - Success
-- Found framework /System/Library/Frameworks/Cocoa.framework
-- Found framework /System/Library/Frameworks/CoreFoundation.framework
-- Found framework /System/Library/Frameworks/CoreServices.framework
-- Found framework /System/Library/Frameworks/ApplicationServices.framework
-- Found framework /System/Library/Frameworks/CoreText.framework
-- Found framework /System/Library/Frameworks/CoreGraphics.framework
-- Found framework /System/Library/Frameworks/Foundation.framework
-- Found framework /System/Library/Frameworks/OpenGL.framework
-- Found framework /System/Library/Frameworks/QuartzCore.framework
-- Found framework /System/Library/Frameworks/AGL.framework
-- Found framework /System/Library/Frameworks/Security.framework
-- Found framework /System/Library/Frameworks/SystemConfiguration.framework
-- Found framework /System/Library/Frameworks/Carbon.framework
-- Found framework /System/Library/Frameworks/CoreAudio.framework
-- Found framework /System/Library/Frameworks/AudioToolbox.framework
-- Found framework /System/Library/Frameworks/AudioUnit.framework
-- Found framework /System/Library/Frameworks/AppKit.framework
-- Found framework /System/Library/Frameworks/CoreWLAN.framework
-- Found framework /System/Library/Frameworks/IOKit.framework
-- C CXX target Telegram cotired without unity build excluding 18 files.
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/travis/build/pvicente/kepka/_build_
+cmake --build . -- -v
ninja: error: '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/AudioToolbox.framework', needed by 'bin/Telegram.app/Contents/MacOS/Telegram', missing and no known rule to make it
+exit 1
```
If you don't mind I'm going to close the issue but feel free to reopen it if you can see any problem with conan.

Cheers,
Pedro



Sorry @ZaMaZaN4iK ,

I've reproduced the same error with the latest version of conan ``1.0.4``.

https://travis-ci.org/pvicente/kepka/jobs/342447099

Working on a proper fix to make it more "portable". 



Resolving patch:
diff --git a/conans/client/conf/detect.py b/conans/client/conf/detect.py
--- a/conans/client/conf/detect.py
+++ b/conans/client/conf/detect.py
@@ -24,7 +24,15 @@ def _execute(command):
 
 
 def _gcc_compiler(output, compiler_exe="gcc"):
+
     try:
+        if platform.system() == "Darwin":
+            # In Mac OS X check if gcc is a fronted using apple-clang
+            _, out = _execute("%s --version" % compiler_exe)
+            out = out.lower()
+            if "clang" in out:
+                return None
+
         _, out = _execute('%s -dumpversion' % compiler_exe)
         compiler = "gcc"
         installed_version = re.search("([0-9](\.[0-9])?)", out).group()
@@ -147,7 +155,12 @@ def _get_default_compiler(output):
         output.info("CC and CXX: %s, %s " % (cc or "None", cxx or "None"))
         command = cc or cxx
         if "gcc" in command:
-            return _gcc_compiler(output, command)
+            gcc = _gcc_compiler(output, command)
+            if platform.system() == "Darwin" and gcc is None:
+                output.error(
+                    "%s detected as a frontend using apple-clang. Compiler not supported" % command
+                )
+            return gcc
         if "clang" in command.lower():
             return _clang_compiler(output, command)
         if platform.system() == "SunOS" and command.lower() == "cc":


Test patch:
diff --git a/conans/test/util/detect_test.py b/conans/test/util/detect_test.py
--- a/conans/test/util/detect_test.py
+++ b/conans/test/util/detect_test.py
@@ -1,15 +1,53 @@
+import platform
+import subprocess
 import unittest
-from conans.test.utils.tools import TestBufferConanOutput
+
+from conans import tools
 from conans.client.conf.detect import detect_defaults_settings
-import platform
+from conans.test.utils.tools import TestBufferConanOutput
 
 
 class DetectTest(unittest.TestCase):
 
-    def detect_test(self):
+    def detect_default_compilers_test(self):
+        platform_default_compilers = {
+            "Linux": "gcc",
+            "Darwin": "apple-clang",
+            "Windows": "Visual Studio"
+        }
         output = TestBufferConanOutput()
         result = detect_defaults_settings(output)
-        if platform.system() == "Linux":
-            for (name, value) in result:
-                if name == "compiler":
-                    self.assertEquals(value, "gcc")
+        # result is a list of tuples (name, value) so converting it to dict
+        result = dict(result)
+        platform_compiler = platform_default_compilers.get(platform.system(), None)
+        if platform_compiler is not None:
+            self.assertEquals(result.get("compiler", None), platform_compiler)
+
+    def detect_default_in_mac_os_using_gcc_as_default_test(self):
+        """
+        Test if gcc in Mac OS X is using apple-clang as frontend
+        """
+        # See: https://github.com/conan-io/conan/issues/2231
+        if platform.system() != "Darwin":
+            return
+
+        try:
+            output = subprocess.check_output(["gcc", "--version"], stderr=subprocess.STDOUT)
+        except subprocess.CalledProcessError:
+            # gcc is not installed or there is any error (no test scenario)
+            return
+
+        if b"clang" not in output:
+            # Not test scenario gcc should display clang in output
+            # see: https://stackoverflow.com/questions/19535422/os-x-10-9-gcc-links-to-clang
+            raise Exception("Apple gcc doesn't point to clang with gcc frontend anymore! please check")
+
+        output = TestBufferConanOutput()
+        with tools.environment_append({"CC": "gcc"}):
+            result = detect_defaults_settings(output)
+        # result is a list of tuples (name, value) so converting it to dict
+        result = dict(result)
+        # No compiler should be detected
+        self.assertIsNone(result.get("compiler", None))
+        self.assertIn("gcc detected as a frontend using apple-clang", output)
+        self.assertIsNotNone(output.error)