Skip to content

Commit ca0dff8

Browse files
authored
Turn existing ifdefs around backtrace() API into a CMake SWIFT_SUPPORTS_BACKTRACE_REPORTING flag (#39598)
1 parent 1c0a306 commit ca0dff8

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

stdlib/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ else()
5252
set(SWIFT_STDLIB_ENABLE_PRESPECIALIZATION_default FALSE)
5353
endif()
5454

55+
if("${SWIFT_HOST_VARIANT_SDK}" MATCHES "CYGWIN")
56+
set(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING_default FALSE)
57+
elseif("${SWIFT_HOST_VARIANT_SDK}" MATCHES "HAIKU")
58+
set(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING_default FALSE)
59+
elseif("${SWIFT_HOST_VARIANT_SDK}" MATCHES "WASI")
60+
set(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING_default FALSE)
61+
else()
62+
set(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING_default TRUE)
63+
endif()
64+
5565
#
5666
# User-configurable options for the standard library.
5767
#
@@ -75,6 +85,10 @@ option(SWIFT_STDLIB_HAS_DLADDR
7585
"Build stdlib assuming the runtime environment runtime environment provides dladdr API."
7686
TRUE)
7787

88+
option(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING
89+
"Build stdlib assuming the runtime environment provides the backtrace(3) API."
90+
"${SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING_default}")
91+
7892
option(SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION
7993
"Build stdlib assuming the runtime environment runtime environment only supports a single runtime image with Swift code."
8094
FALSE)

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,10 @@ function(_add_target_variant_c_compile_flags)
347347
list(APPEND result "-DSWIFT_RUNTIME_OS_VERSIONING")
348348
endif()
349349

350+
if(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING)
351+
list(APPEND result "-DSWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING")
352+
endif()
353+
350354
set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
351355
endfunction()
352356

stdlib/public/runtime/Errors.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#if defined(__CYGWIN__) || defined(__HAIKU__) || defined(__wasi__)
18-
#define SWIFT_SUPPORTS_BACKTRACE_REPORTING 0
19-
#else
20-
#define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1
21-
#endif
22-
2317
#if defined(_WIN32)
2418
#include <mutex>
2519
#endif
@@ -73,7 +67,7 @@ enum: uint32_t {
7367

7468
using namespace swift;
7569

76-
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING && SWIFT_STDLIB_HAS_DLADDR
70+
#if SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING && SWIFT_STDLIB_HAS_DLADDR
7771
static bool getSymbolNameAddr(llvm::StringRef libraryName,
7872
const SymbolInfo &syminfo,
7973
std::string &symbolName, uintptr_t &addrOut) {
@@ -136,7 +130,7 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName,
136130

137131
void swift::dumpStackTraceEntry(unsigned index, void *framePC,
138132
bool shortOutput) {
139-
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING && SWIFT_STDLIB_HAS_DLADDR
133+
#if SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING && SWIFT_STDLIB_HAS_DLADDR
140134
SymbolInfo syminfo;
141135

142136
// 0 is failure for lookupSymbol
@@ -224,7 +218,7 @@ static _Unwind_Reason_Code SwiftUnwindFrame(struct _Unwind_Context *context, voi
224218

225219
SWIFT_ALWAYS_INLINE
226220
static bool withCurrentBacktraceImpl(std::function<void(void **, int)> call) {
227-
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
221+
#if SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING
228222
constexpr unsigned maxSupportedStackDepth = 128;
229223
void *addrs[maxSupportedStackDepth];
230224
#if defined(_WIN32)
@@ -325,7 +319,7 @@ reportNow(uint32_t flags, const char *message)
325319
#elif defined(__ANDROID__)
326320
__android_log_print(ANDROID_LOG_FATAL, "SwiftRuntime", "%s", message);
327321
#endif
328-
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
322+
#if SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING
329323
if (flags & FatalErrorFlags::ReportBacktrace) {
330324
fputs("Current stack trace:\n", stderr);
331325
printCurrentBacktrace();

utils/build-presets.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,6 +2464,7 @@ swift-objc-interop=0
24642464
swift-enable-compatibility-overrides=0
24652465
swift-enable-reflection=0
24662466
swift-stdlib-has-dladdr=0
2467+
swift-stdlib-supports-backtrace-reporting=0
24672468
swift-stdlib-has-darwin-libmalloc=0
24682469
swift-stdlib-has-stdin=0
24692470
swift-runtime-static-image-inspection=1

utils/build-script-impl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ KNOWN_SETTINGS=(
207207
swift-enable-compatibility-overrides "1" "whether to support back-deploying compatibility fixes for newer apps running on older runtimes"
208208
swift-enable-reflection "1" "whether to support reflection and mirrors"
209209
swift-stdlib-has-dladdr "1" "whether to build stdlib assuming the runtime environment provides dladdr API"
210+
swift-stdlib-supports-backtrace-reporting "" "whether to build stdlib assuming the runtime environment provides the backtrace(3) API, if not set defaults to true on all platforms except for Cygwin, Haiku and wasm"
210211
swift-runtime-static-image-inspection "0" "whether to build stdlib assuming the runtime environment only supports a single runtime image with Swift code"
211212
swift-stdlib-single-threaded-runtime "0" "whether to build stdlib as a single-threaded runtime only"
212213
swift-stdlib-os-versioning "1" "whether to build stdlib with availability based on OS versions (Darwin only)"
@@ -2103,6 +2104,13 @@ for host in "${ALL_HOSTS[@]}"; do
21032104
)
21042105
fi
21052106

2107+
if [[ "${SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING}" ]] ; then
2108+
cmake_options=(
2109+
"${cmake_options[@]}"
2110+
-DSWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING:BOOL=$(true_false "${SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING}")
2111+
)
2112+
fi
2113+
21062114
if [ "${SWIFT_INSTALL_COMPONENTS}" ] ; then
21072115
cmake_options=(
21082116
"${cmake_options[@]}"

utils/check_freestanding_dependencies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
]
5151
common_expected_dependencies = [
5252
"___bzero", "___divti3", "___error", "___stderrp", "___stdoutp",
53-
"___truncsfhf2", "___udivti3", "_abort", "_arc4random_buf", "_backtrace",
53+
"___truncsfhf2", "___udivti3", "_abort", "_arc4random_buf",
5454
"_calloc", "_close", "_environ", "_flockfile", "_floorl", "_fprintf",
5555
"_fputc", "_fputs", "_free", "_funlockfile", "_fwrite", "_malloc",
5656
"_malloc_size", "_memchr", "_memcmp", "_memcpy", "_memmove", "_memset",

0 commit comments

Comments
 (0)