Homebrew / homebrew-core

🍻 Default formulae for the missing package manager for macOS (or Linux)
https://brew.sh
BSD 2-Clause "Simplified" License
13.77k stars 12.45k forks source link

Upgrading to Xcode CLT 11.0 broke GCC 9 #40676

Closed crisluengo closed 5 years ago

crisluengo commented 5 years ago

I just upgraded the Xcode Command Line Tools to 11.0. Now I can no longer compile with GCC 9. I get this error with any C++ file that includes cstdlib:

In file included from /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/stdlib.h:61,
                 from /usr/local/Cellar/gcc/9.1.0/include/c++/9.1.0/cstdlib:75,
                 from /Users/cris/newdip/include/diplib/library/dimension_array.h:30,
                 from /Users/cris/newdip/include/diplib.h:26,
                 from /Users/cris/newdip/src/analysis/findshift.cpp:21:
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/Availability.h:257:22: error: missing binary operator before token "("
  257 |     #if __has_builtin(__is_target_arch)
      |                      ^

It looks like cstdlib includes stdlib.h, but GCC finds it among Clang's header files, rather than the ones that come with GCC. This version of stdlib.h uses some compiler intrinsic that is unique to Clang.

This is the output of gcc --version:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.20.17)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Homebrew is up to date and I have the latest GCC installed through Homebrew.

I have uninstalled GCC and installed it anew, with no change in behavior.

Related issues: 1, 2


Warning: The following directories do not exist:
/usr/local/sbin

You should create these directories and change their ownership to your account.
  sudo mkdir -p /usr/local/sbin
  sudo chown -R $(whoami) /usr/local/sbin

Warning: Unbrewed dylibs were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected dylibs:
  /usr/local/lib/libics.dylib

Warning: Unbrewed header files were found in /usr/local/include.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected header files:
  /usr/local/include/libics.h
  /usr/local/include/libics_intern.h
  /usr/local/include/libics_ll.h
  /usr/local/include/libics_sensor.h
  /usr/local/include/libics_test.h
  /usr/local/include/uvcode.h
  /usr/local/include/valgrind/callgrind.h
  /usr/local/include/valgrind/config.h
  /usr/local/include/valgrind/drd.h
  /usr/local/include/valgrind/helgrind.h
  /usr/local/include/valgrind/libvex.h
  /usr/local/include/valgrind/libvex_basictypes.h
  /usr/local/include/valgrind/libvex_emnote.h
  /usr/local/include/valgrind/libvex_guest_amd64.h
  /usr/local/include/valgrind/libvex_guest_arm.h
  /usr/local/include/valgrind/libvex_guest_arm64.h
  /usr/local/include/valgrind/libvex_guest_mips32.h
  /usr/local/include/valgrind/libvex_guest_mips64.h
  /usr/local/include/valgrind/libvex_guest_offsets.h
  /usr/local/include/valgrind/libvex_guest_ppc32.h
  /usr/local/include/valgrind/libvex_guest_ppc64.h
  /usr/local/include/valgrind/libvex_guest_s390x.h
  /usr/local/include/valgrind/libvex_guest_x86.h
  /usr/local/include/valgrind/libvex_ir.h
  /usr/local/include/valgrind/libvex_s390x_common.h
  /usr/local/include/valgrind/libvex_trc_values.h
  /usr/local/include/valgrind/memcheck.h
  /usr/local/include/valgrind/pub_tool_addrinfo.h
  /usr/local/include/valgrind/pub_tool_aspacehl.h
  /usr/local/include/valgrind/pub_tool_aspacemgr.h
  /usr/local/include/valgrind/pub_tool_basics.h
  /usr/local/include/valgrind/pub_tool_basics_asm.h
  /usr/local/include/valgrind/pub_tool_clientstate.h
  /usr/local/include/valgrind/pub_tool_clreq.h
  /usr/local/include/valgrind/pub_tool_debuginfo.h
  /usr/local/include/valgrind/pub_tool_deduppoolalloc.h
  /usr/local/include/valgrind/pub_tool_errormgr.h
  /usr/local/include/valgrind/pub_tool_execontext.h
  /usr/local/include/valgrind/pub_tool_gdbserver.h
  /usr/local/include/valgrind/pub_tool_guest.h
  /usr/local/include/valgrind/pub_tool_hashtable.h
  /usr/local/include/valgrind/pub_tool_libcassert.h
  /usr/local/include/valgrind/pub_tool_libcbase.h
  /usr/local/include/valgrind/pub_tool_libcfile.h
  /usr/local/include/valgrind/pub_tool_libcprint.h
  /usr/local/include/valgrind/pub_tool_libcproc.h
  /usr/local/include/valgrind/pub_tool_libcsetjmp.h
  /usr/local/include/valgrind/pub_tool_libcsignal.h
  /usr/local/include/valgrind/pub_tool_machine.h
  /usr/local/include/valgrind/pub_tool_mallocfree.h
  /usr/local/include/valgrind/pub_tool_options.h
  /usr/local/include/valgrind/pub_tool_oset.h
  /usr/local/include/valgrind/pub_tool_poolalloc.h
  /usr/local/include/valgrind/pub_tool_rangemap.h
  /usr/local/include/valgrind/pub_tool_redir.h
  /usr/local/include/valgrind/pub_tool_replacemalloc.h
  /usr/local/include/valgrind/pub_tool_seqmatch.h
  /usr/local/include/valgrind/pub_tool_signals.h
  /usr/local/include/valgrind/pub_tool_sparsewa.h
  /usr/local/include/valgrind/pub_tool_stacktrace.h
  /usr/local/include/valgrind/pub_tool_threadstate.h
  /usr/local/include/valgrind/pub_tool_tooliface.h
  /usr/local/include/valgrind/pub_tool_transtab.h
  /usr/local/include/valgrind/pub_tool_vki.h
  /usr/local/include/valgrind/pub_tool_vkiscnums.h
  /usr/local/include/valgrind/pub_tool_vkiscnums_asm.h
  /usr/local/include/valgrind/pub_tool_wordfm.h
  /usr/local/include/valgrind/pub_tool_xarray.h
  /usr/local/include/valgrind/pub_tool_xtmemory.h
  /usr/local/include/valgrind/pub_tool_xtree.h
  /usr/local/include/valgrind/valgrind.h
  /usr/local/include/valgrind/vki/vki-amd64-linux.h
  /usr/local/include/valgrind/vki/vki-arm-linux.h
  /usr/local/include/valgrind/vki/vki-arm64-linux.h
  /usr/local/include/valgrind/vki/vki-darwin.h
  /usr/local/include/valgrind/vki/vki-linux-drm.h
  /usr/local/include/valgrind/vki/vki-linux.h
  /usr/local/include/valgrind/vki/vki-mips32-linux.h
  /usr/local/include/valgrind/vki/vki-mips64-linux.h
  /usr/local/include/valgrind/vki/vki-posixtypes-amd64-linux.h
  /usr/local/include/valgrind/vki/vki-posixtypes-arm-linux.h
  /usr/local/include/valgrind/vki/vki-posixtypes-arm64-linux.h
  /usr/local/include/valgrind/vki/vki-posixtypes-mips32-linux.h
  /usr/local/include/valgrind/vki/vki-posixtypes-mips64-linux.h
  /usr/local/include/valgrind/vki/vki-posixtypes-ppc32-linux.h
  /usr/local/include/valgrind/vki/vki-posixtypes-ppc64-linux.h
  /usr/local/include/valgrind/vki/vki-posixtypes-s390x-linux.h
  /usr/local/include/valgrind/vki/vki-posixtypes-x86-linux.h
  /usr/local/include/valgrind/vki/vki-ppc32-linux.h
  /usr/local/include/valgrind/vki/vki-ppc64-linux.h
  /usr/local/include/valgrind/vki/vki-s390x-linux.h
  /usr/local/include/valgrind/vki/vki-scnums-amd64-linux.h
  /usr/local/include/valgrind/vki/vki-scnums-arm-linux.h
  /usr/local/include/valgrind/vki/vki-scnums-arm64-linux.h
  /usr/local/include/valgrind/vki/vki-scnums-darwin.h
  /usr/local/include/valgrind/vki/vki-scnums-mips32-linux.h
  /usr/local/include/valgrind/vki/vki-scnums-mips64-linux.h
  /usr/local/include/valgrind/vki/vki-scnums-ppc32-linux.h
  /usr/local/include/valgrind/vki/vki-scnums-ppc64-linux.h
  /usr/local/include/valgrind/vki/vki-scnums-s390x-linux.h
  /usr/local/include/valgrind/vki/vki-scnums-solaris.h
  /usr/local/include/valgrind/vki/vki-scnums-x86-linux.h
  /usr/local/include/valgrind/vki/vki-solaris-repcache.h
  /usr/local/include/valgrind/vki/vki-solaris.h
  /usr/local/include/valgrind/vki/vki-x86-linux.h
  /usr/local/include/valgrind/vki/vki-xen-domctl.h
  /usr/local/include/valgrind/vki/vki-xen-evtchn.h
  /usr/local/include/valgrind/vki/vki-xen-gnttab.h
  /usr/local/include/valgrind/vki/vki-xen-hvm.h
  /usr/local/include/valgrind/vki/vki-xen-memory.h
  /usr/local/include/valgrind/vki/vki-xen-mmuext.h
  /usr/local/include/valgrind/vki/vki-xen-physdev.h
  /usr/local/include/valgrind/vki/vki-xen-schedop.h
  /usr/local/include/valgrind/vki/vki-xen-sysctl.h
  /usr/local/include/valgrind/vki/vki-xen-tmem.h
  /usr/local/include/valgrind/vki/vki-xen-version.h
  /usr/local/include/valgrind/vki/vki-xen-x86.h
  /usr/local/include/valgrind/vki/vki-xen-xsm.h
  /usr/local/include/valgrind/vki/vki-xen.h

Warning: Unbrewed .pc files were found in /usr/local/lib/pkgconfig.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected .pc files:
  /usr/local/lib/pkgconfig/valgrind.pc

Warning: Unbrewed static libraries were found in /usr/local/lib.
If you didn't put them there on purpose they could cause problems when
building Homebrew formulae, and may need to be deleted.

Unexpected static libraries:
  /usr/local/lib/libics.a

Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  unbound
close-issue-app[bot] commented 5 years ago

From the issue template:

Please note we will close your issue without comment if you delete, do not read or do not fill out the issue checklist below and provide ALL the requested information. If you repeatedly fail to use the issue template, we will block you from ever submitting issues to Homebrew again.

If you add the necessary information to this issue (don't create a new one, please) then this issue may be reopened.

crisluengo commented 5 years ago

Is there no way to get this issue reopened?

GCC should never include any directory in front of its own standard library directories. In particular, the search order seems to be:

  1. /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/stdlib.h
  2. /usr/local/Cellar/gcc/9.1.0/include/c++/9.1.0/stdlib.h

but this should be the other way around.

SMillerDev commented 5 years ago

Pull requests would be appreciated but there simply aren't enough maintainers of homebrew to maintain beta versions of macOS/apple tools. If you install an unsupported version you're on your own.

crisluengo commented 5 years ago

@SMillerDev I understand that you guys are stretched thin. However, this was a normal upgrade suggested by my OS, how am I supposed to know it is an unsupported version?

I'm trying to figure out what is going on. It seems that GCC depends on the system's C library, so this is intended behaviour. It is a change to the system's C library that is not compatible with GCC.

crisluengo commented 5 years ago

This is a bug in the C library that comes with the updated Xcode. To fix it, edit the file /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/Availability.h (must be done as root). Near the point where the error is generated (line number 257), add the following two lines:

#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
  #if defined(__has_builtin)   // <=== ADD THIS LINE
    #if __has_builtin(__is_target_arch)
     #if __has_builtin(__is_target_vendor)
      #if __has_builtin(__is_target_os)
       #if __has_builtin(__is_target_environment)
        #if __has_builtin(__is_target_variant_os)
         #if __has_builtin(__is_target_variant_environment)
          #if (__is_target_arch(x86_64) && __is_target_vendor(apple) && ((__is_target_os(ios) && __is_target_environment(macabi)) || (__is_target_variant_os(ios) && __is_target_variant_environment(macabi))))
            #define __OSX_AVAILABLE_STARTING(_osx, _ios) __AVAILABILITY_INTERNAL##_osx __AVAILABILITY_INTERNAL##_ios
            #define __OSX_AVAILABLE_BUT_DEPRECATED(_osxIntro, _osxDep, _iosIntro, _iosDep) \
                                                            __AVAILABILITY_INTERNAL##_osxIntro##_DEP##_osxDep __AVAILABILITY_INTERNAL##_iosIntro##_DEP##_iosDep
            #define __OSX_AVAILABLE_BUT_DEPRECATED_MSG(_osxIntro, _osxDep, _iosIntro, _iosDep, _msg) \
                                                            __AVAILABILITY_INTERNAL##_osxIntro##_DEP##_osxDep##_MSG(_msg) __AVAILABILITY_INTERNAL##_iosIntro##_DEP##_iosDep##_MSG(_msg)
          #endif /* # if __is_target_arch... */
         #endif /* #if __has_builtin(__is_target_variant_environment) */
        #endif /* #if __has_builtin(__is_target_variant_os) */
       #endif /* #if __has_builtin(__is_target_environment) */
      #endif /* #if __has_builtin(__is_target_os) */
     #endif /* #if __has_builtin(__is_target_vendor) */
    #endif /* #if __has_builtin(__is_target_arch) */
  #endif   // <=== ADD THIS LINE

    #ifndef __OSX_AVAILABLE_STARTING
        #define __OSX_AVAILABLE_STARTING(_osx, _ios) __AVAILABILITY_INTERNAL##_osx
        #define __OSX_AVAILABLE_BUT_DEPRECATED(_osxIntro, _osxDep, _iosIntro, _iosDep) \
                                                        __AVAILABILITY_INTERNAL##_osxIntro##_DEP##_osxDep
        #define __OSX_AVAILABLE_BUT_DEPRECATED_MSG(_osxIntro, _osxDep, _iosIntro, _iosDep, _msg) \
                                                        __AVAILABILITY_INTERNAL##_osxIntro##_DEP##_osxDep##_MSG(_msg)
    #endif /* __OSX_AVAILABLE_STARTING */

#else

Needless to say, edit system files at your own risk! :)

bermudalocket commented 5 years ago

@crisluengo unfortunately the issue persists for me even after making that change. I'm on macOS 10.15 though and made the above suggested edit in the 10.15 SDK instead of the 10.14 SDK. I've been debugging this since Monday and was hoping this was the solution. Frustrating :(

fxcoudert commented 5 years ago

this was a normal upgrade suggested by my OS, how am I supposed to know it is an unsupported version?

Xcode 11 is beta, so this was not a “normal” update, or you've configured at some point your system to install beta versions (and likely forgotten about it 😄).

GCC should never include any directory in front of its own standard library directories

This is not the issue.

It is a change to the system's C library that is not compatible with GCC

Yes. The beta SDK includes in Availability.h some code that is not standard C compliant, because it uses the clang-specific __has_builtin built-in unconditionally. Please report this to Apple so they can get it fixed.

pronvit commented 5 years ago

sys/cdefs.h defines __has_builtin and others if they're not available. Including sys/cdefs.h before stdlib.h fixes the problem.

fxcoudert commented 5 years ago

Including sys/cdefs.h before stdlib.h fixes the problem

There are many such workarounds. The only real fix is to report the problem to Apple, so they can fix it. (Duplicate reports actually help them determine what is a pressing issue, too.)

QYQSDTC commented 5 years ago

@crisluengo It fixes my problem, I just updated yesterday. Thanks bro.

dgboy2000 commented 5 years ago

This same issue happens for me in Xcode 10.2.1. @crisluengo's solution of ordering import paths properly makes the most sense to me. I was able to ditch homebrew's gcc and use clang but wanted to "+1" this post.

ggouaillardet commented 5 years ago

TL;DR I faced the same issue and configure CPPFLAGS='-D__has_builtin(x)=0' ... was my workaround ([EDIT] that was good enough to pass configure but crashed at make time)

On my up-to-date (OS X and brew) Mac (with zero beta software) I tried to build Open MPI and configure failed with the following error message

ptrdiff_t type is not available, this is required by C99 standard.  Cannot continue

after some debugging, I found that this autoconf test failed

checking for ANSI C header files... no

and this is caused by the issue discussed here.

Though it is technically correct to blame Apple for breaking stdlib.h, or even Open MPI for expecting autoconf will #include <stddef.h> under the hood, I am afraid this issue has a much larger impact that some might have expected. I do not know the policy (if any) for dealing with such cases, but IMHO, a workaround should be included in the brew package in order to be (even) more user friendly.

my 0.02US$

jeffhammond commented 5 years ago

I see this with Xcode Version 10.2.1 (10E1001) but find that I can resolve it by switching

#include <stdlib.h>
#include <stdio.h>

to

#include <stdio.h>
#include <stdlib.h>

MCVE

This solution exists because stdio.h causes the definition of __has_builtin.

/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include//secure/_stdio.h:#ifndef __has_builtin
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include//secure/_stdio.h:#define _undef__has_builtin
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include//secure/_stdio.h:#define __has_builtin(x) 0

I got lucky in finding this one because I always insist on including <stdio.h> before all the other <st*.h>, for what has been, until now, nothing more than aesthetics.

ggouaillardet commented 5 years ago

FWIW, based on @jeffhammond workaround, I made the patch below for autoconf so stdio.h is always included before stdlib.h is used.

Though the patch itself should not fix anything, it is harmless and good enough (for now) to work around the broken headers.

@MikeMcQuaid FYI

in order to apply it, simply add the following lines in the autoconf formula

  patch do
    url "https://gist.githubusercontent.com/ggouaillardet/11e15a9349dc2100d98d59a5ccc3c74e/raw/34e16d46fd1f1f5cc060d15afbb3c94a385ebdcb/autoconf.diff"
    sha256 "7c415d8994fa668dc16803950c0b3e92ad8f400fbf5a7adc0f6ea59fe29a3640"
  end
diff -ruN orig/autoconf-2.69/lib/autoconf/c.m4 autoconf-2.69/lib/autoconf/c.m4
--- orig/autoconf-2.69/lib/autoconf/c.m4    2012-01-21 22:46:39.000000000 +0900
+++ autoconf-2.69/lib/autoconf/c.m4 2019-07-22 17:01:36.000000000 +0900
@@ -1204,11 +1204,11 @@
 # variable length arrays.
 AC_DEFUN([_AC_PROG_CC_C99],
 [_AC_C_STD_TRY([c99],
-[[#include <stdarg.h>
+[[#include <stdio.h>
+#include <stdarg.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <wchar.h>
-#include <stdio.h>

 // Check varargs macros.  These examples are taken from C99 6.10.3.5.
 #define debug(...) fprintf (stderr, __VA_ARGS__)
@@ -1849,8 +1849,8 @@
     ac_cv_c_flexmember,
     [AC_COMPILE_IFELSE(
        [AC_LANG_PROGRAM(
-     [[#include <stdlib.h>
-       #include <stdio.h>
+     [[#include <stdio.h>
+       #include <stdlib.h>
        #include <stddef.h>
        struct s { int n; double d[]; };]],
      [[int m = getchar ();
diff -ruN orig/autoconf-2.69/lib/autoconf/functions.m4 autoconf-2.69/lib/autoconf/functions.m4
--- orig/autoconf-2.69/lib/autoconf/functions.m4    2012-01-21 22:46:39.000000000 +0900
+++ autoconf-2.69/lib/autoconf/functions.m4 2019-07-22 17:03:44.000000000 +0900
@@ -890,7 +890,8 @@
 AC_CACHE_CHECK([for GNU libc compatible malloc], ac_cv_func_malloc_0_nonnull,
 [AC_RUN_IFELSE(
 [AC_LANG_PROGRAM(
-[[#if defined STDC_HEADERS || defined HAVE_STDLIB_H
+[[#include <stdio.h>
+#if defined STDC_HEADERS || defined HAVE_STDLIB_H
 # include <stdlib.h>
 #else
 char *malloc ();
@@ -1003,6 +1004,7 @@
 #endif

 #include <limits.h>
+#include <stdio.h>
 #include <stdlib.h>

 #ifdef HAVE_UNISTD_H
@@ -1395,7 +1397,8 @@
 AC_CACHE_CHECK([for GNU libc compatible realloc], ac_cv_func_realloc_0_nonnull,
 [AC_RUN_IFELSE(
 [AC_LANG_PROGRAM(
-[[#if defined STDC_HEADERS || defined HAVE_STDLIB_H
+[[#include <stdio.h>
+#if defined STDC_HEADERS || defined HAVE_STDLIB_H
 # include <stdlib.h>
 #else
 char *realloc ();
@@ -1595,6 +1598,7 @@
      [[/* On HP-UX before 11.23, strtold returns a struct instead of
        long double.  Reject implementations like that, by requiring
        compatibility with the C99 prototype.  */
+#            include <stdio.h>
 #       include <stdlib.h>
         static long double (*p) (char const *, char **) = strtold;
         static long double
diff -ruN orig/autoconf-2.69/lib/autoconf/headers.m4 autoconf-2.69/lib/autoconf/headers.m4
--- orig/autoconf-2.69/lib/autoconf/headers.m4  2012-04-25 11:37:26.000000000 +0900
+++ autoconf-2.69/lib/autoconf/headers.m4   2019-07-22 17:02:16.000000000 +0900
@@ -676,7 +676,8 @@
 # --------------
 AC_DEFUN([AC_HEADER_STDC],
 [AC_CACHE_CHECK(for ANSI C header files, ac_cv_header_stdc,
-[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdlib.h>
+[AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <stdio.h>
+#include <stdlib.h>
 #include <stdarg.h>
 #include <string.h>
 #include <float.h>
@@ -697,7 +698,8 @@
 if test $ac_cv_header_stdc = yes; then
   # /bin/cc in Irix-4.0.5 gets non-ANSI ctype macros unless using -ansi.
   AC_RUN_IFELSE([AC_LANG_SOURCE(
-[[#include <ctype.h>
+[[#include <stdio.h>
+#include <ctype.h>
 #include <stdlib.h>
 #if ((' ' & 0x0FF) == 0x020)
 # define ISLOWER(c) ('a' <= (c) && (c) <= 'z')
SMillerDev commented 5 years ago

Thanks for the work but I'm just gonna repeat what fx said cause it's essential in this:

There are many such workarounds. The only real fix is to report the problem to Apple, so they can fix it. (Duplicate reports actually help them determine what is a pressing issue, too.)

jeffhammond commented 5 years ago

One can dismiss essentially all good engineering solutions by identifying better ones.
https://en.wikipedia.org/wiki/Perfect_is_the_enemy_of_good

MikeMcQuaid commented 5 years ago

@jeffhammond Your meta-commentary is not welcome or necessary, thanks.