giampaolo / psutil

Cross-platform lib for process and system monitoring in Python
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.38k forks source link

[macOS] v. 5.9.7 broken on macOS < 10.13: `if_dl.h: error: unknown type name 'u_char'; did you mean 'char'?` #2360

Closed barracuda156 closed 8 months ago

barracuda156 commented 8 months ago

5.9.7 fails to build now on multiple systems.

On buildbots with clang, x86_64:

/usr/bin/clang -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -pipe -Os -arch x86_64 -isysroot/ -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=597 -DPy_LIMITED_API=0x03060000 -DPSUTIL_OSX=1 -I/opt/local/Library/Frameworks/Python.framework/Versions/3.11/include/python3.11 -c psutil/arch/osx/net.c -o build/temp.macosx-10.12-x86_64-cpython-311/psutil/arch/osx/net.o
In file included from psutil/arch/osx/net.c:12:
/usr/include/net/if_dl.h:90:2: error: unknown type name 'u_char'; did you mean 'char'?
        u_char  sdl_len;        /* Total length of sockaddr */
        ^
/usr/include/net/if_dl.h:91:2: error: unknown type name 'u_char'; did you mean 'char'?
        u_char  sdl_family;     /* AF_LINK */
        ^
/usr/include/net/if_dl.h:92:2: error: unknown type name 'u_short'; did you mean 'short'?
        u_short sdl_index;      /* if != 0, system given index for interface */
        ^
/usr/include/net/if_dl.h:93:2: error: unknown type name 'u_char'; did you mean 'char'?
        u_char  sdl_type;       /* interface type */
        ^
/usr/include/net/if_dl.h:94:2: error: unknown type name 'u_char'; did you mean 'char'?
        u_char  sdl_nlen;       /* interface name length, no trailing 0 reqd. */
        ^
/usr/include/net/if_dl.h:95:2: error: unknown type name 'u_char'; did you mean 'char'?
        u_char  sdl_alen;       /* link level address length */
        ^
/usr/include/net/if_dl.h:96:2: error: unknown type name 'u_char'; did you mean 'char'?
        u_char  sdl_slen;       /* link layer selector length */
        ^
psutil/arch/osx/net.c:68:21: warning: array subscript is of type 'char' [-Wchar-subscripts]
            ifc_name[sdl->sdl_nlen] = 0;
                    ^~~~~~~~~~~~~~
1 warning and 7 errors generated.
XCode (https://developer.apple.com/xcode/) is not installed
error: command '/usr/bin/clang' failed with exit code 1

Locally with gcc, on ppc:

/usr/bin/gcc-4.2 -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -pipe -Os -arch ppc -isysroot/ -DPSUTIL_POSIX=1 -DPSUTIL_SIZEOF_PID_T=4 -DPSUTIL_VERSION=597 -DPy_LIMITED_API=0x03060000 -DPSUTIL_OSX=1 -I/opt/local/Library/Frameworks/Python.framework/Versions/3.11/include/python3.11 -c psutil/arch/osx/net.c -o build/temp.macosx-10.6-ppc-cpython-311/psutil/arch/osx/net.o
In file included from psutil/arch/osx/net.c:12:
//usr/include/net/if_dl.h:90: error: expected specifier-qualifier-list before ‘u_char’
psutil/arch/osx/net.c: In function ‘psutil_net_io_counters’:
psutil/arch/osx/net.c:67: error: ‘struct sockaddr_dl’ has no member named ‘sdl_data’
psutil/arch/osx/net.c:67: error: ‘struct sockaddr_dl’ has no member named ‘sdl_nlen’
psutil/arch/osx/net.c:67: error: ‘struct sockaddr_dl’ has no member named ‘sdl_data’
psutil/arch/osx/net.c:67: error: ‘struct sockaddr_dl’ has no member named ‘sdl_nlen’
psutil/arch/osx/net.c:68: error: ‘struct sockaddr_dl’ has no member named ‘sdl_nlen’
XCode (https://developer.apple.com/xcode/) is not installed
error: command '/usr/bin/gcc-4.2' failed with exit code 1

ERROR Backend subprocess exited when trying to invoke build_wheel

It does something strange. First of all, Xcode is installed. Then, why it needs Xcode at all? Finally, something got badly broken with headers, apparently. 5.9.5 built fine.

ryandesign commented 8 months ago

Sergey also filed this with MacPorts where he noted that it builds fine on macOS 10.13 and later, but not on 10.12 and earlier. This suggested to me that maybe some system header that is automatically included on 10.13 and later that provides the definition of u_char needed to be included manually.

I found that old macOS system headers have a bug that net/if_df.h does not include sys/types.h, but net/if.h does, so including that headers before, rather than after, net/if_dl.h works around the problem; I submitted a PR for that.

More generally, following the convention of including system headers in alphabetical order should minimize these kinds of problems.

There are further build failures on old macOS systems after fixing this. I'll investigate and report that separately.

barracuda156 commented 4 months ago

@ryandesign @giampaolo I see in notes elsewhere https://github.com/sahlberg/libnfs/commit/381883da909ffb68b599bf83bc47d90e6b40f6cd that sys/socket.h must be included before net/if.h, so the fix above might not have been correct: https://github.com/giampaolo/psutil/pull/2361/files

Though py-psutil 5.9.8 builds fine for me on 10.6, I just checked.