cmake: work around a mysterious macOS dyld problem.

For some reason, there appears to be no way to build Wireshark on macOS
Ventura with Xcode 15 and have the resulting program run without getting
an error due to failing to find pcap_createsrcstr() at startup.

In particular, there appears to be no way to use __builtin_available()
to protect accesses to the routines that showed up in Sonoma, so that
the run-time linker doesn't fail if the routine in question isn't
present.  Perhaps it requires more compiler command-line arguments.

So, instead, only check for pcap_open() if 1) this isn't macOS or 2) the
pcap library pathname doesn't begin with /usr/lib/ or
/Application/Xcode.app/, i.e. if it's not the system libpcap.  That way,
we won't every try to use those APIs for a built with the system
libpcap.

Fixes #19436.
This commit is contained in:
Guy Harris 2023-10-30 16:55:19 -07:00
parent 6f4a9a9c92
commit d300f30469
1 changed files with 44 additions and 1 deletions

View File

@ -218,7 +218,50 @@ if(PCAP_FOUND)
check_function_exists( "pcap_freecode" HAVE_PCAP_FREECODE )
check_function_exists( "pcap_create" HAVE_PCAP_CREATE )
check_function_exists( "pcap_free_datalinks" HAVE_PCAP_FREE_DATALINKS )
check_function_exists( "pcap_open" HAVE_PCAP_OPEN )
#
# macOS Sonoma's libpcap includes stub versions of the remote-
# capture APIs. They are exported as "weakly linked symbols".
#
# Xcode 15 offers only a macOS Sonoma SDK, which has a .tbd
# file for libpcap that claims it includes those APIs. (Newer
# versions of macOS don't provide the system shared libraries,
# they only provide the dyld shared cache containing those
# libraries, so the OS provides SDKs that include a .tbd file
# to use when linking.)
#
# This means that check_function_exists() will think that
# the remote-capture APIs are present, including pcap_open().
#
# However, they are *not* present in macOS Ventura and earlier,
# which means that building on Ventura with Xcode 15 produces
# executables that fail to start because one of those APIs
# isn't found in the system libpcap.
#
# Protecting calls to those APIs with __builtin_available()
# does not appear to prevent this, for some unknown reason,
# and it doesn't even allow the program to compile with
# versions of Xcode prior to Xcode 15, as the pcap.h file
# doesn't specify minimum OS versions for those functions.
#
# Given all that, and given that the versions of the
# remote-capture APIs in Sonoma are stubs that always fail,
# there doesn't seem to be any point in checking for pcap_open()
# if we're linking against the Apple libpcap.
#
# However, if we're *not* linking against the Apple libpcap,
# we should check for it, so that we can use it if it's present.
#
# So we check for pcap_open if 1) this isn't macOS or 2) the
# the libpcap we found is not a system library, meaning that
# its path begins neither with /usr/lib (meaning it's a system
# dylib) nor /Application/Xcode.app (meaning it's a file in
# the Xcode SDK).
#
if( NOT APPLE OR NOT
(PCAP_LIBRARY MATCHES "/usr/lib/.*" OR
PCAP_LIBRARY MATCHES "/Application/Xcode.app/.*"))
check_function_exists( "pcap_open" HAVE_PCAP_OPEN )
endif()
if( HAVE_PCAP_OPEN )
#
# XXX - this *should* be checked for independently of checking