Skip to content

Turn existing ifdefs around backtrace() API into a CMake SWIFT_SUPPORTS_BACKTRACE_REPORTING flag #39598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ else()
set(SWIFT_STDLIB_ENABLE_PRESPECIALIZATION_default FALSE)
endif()

if("${SWIFT_HOST_VARIANT_SDK}" MATCHES "CYGWIN")
set(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING_default FALSE)
elseif("${SWIFT_HOST_VARIANT_SDK}" MATCHES "HAIKU")
set(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING_default FALSE)
elseif("${SWIFT_HOST_VARIANT_SDK}" MATCHES "WASI")
set(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING_default FALSE)
else()
set(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING_default TRUE)
endif()

#
# User-configurable options for the standard library.
#
Expand All @@ -75,6 +85,10 @@ option(SWIFT_STDLIB_HAS_DLADDR
"Build stdlib assuming the runtime environment runtime environment provides dladdr API."
TRUE)

option(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING
"Build stdlib assuming the runtime environment provides the backtrace(3) API."
"${SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING_default}")

option(SWIFT_RUNTIME_STATIC_IMAGE_INSPECTION
"Build stdlib assuming the runtime environment runtime environment only supports a single runtime image with Swift code."
FALSE)
Expand Down
4 changes: 4 additions & 0 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ function(_add_target_variant_c_compile_flags)
list(APPEND result "-DSWIFT_RUNTIME_OS_VERSIONING")
endif()

if(SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING)
list(APPEND result "-DSWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING")
endif()

set("${CFLAGS_RESULT_VAR_NAME}" "${result}" PARENT_SCOPE)
endfunction()

Expand Down
14 changes: 4 additions & 10 deletions stdlib/public/runtime/Errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
//
//===----------------------------------------------------------------------===//

#if defined(__CYGWIN__) || defined(__HAIKU__) || defined(__wasi__)
#define SWIFT_SUPPORTS_BACKTRACE_REPORTING 0
#else
#define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1
#endif

#if defined(_WIN32)
#include <mutex>
#endif
Expand Down Expand Up @@ -73,7 +67,7 @@ enum: uint32_t {

using namespace swift;

#if SWIFT_SUPPORTS_BACKTRACE_REPORTING && SWIFT_STDLIB_HAS_DLADDR
#if SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING && SWIFT_STDLIB_HAS_DLADDR
static bool getSymbolNameAddr(llvm::StringRef libraryName,
const SymbolInfo &syminfo,
std::string &symbolName, uintptr_t &addrOut) {
Expand Down Expand Up @@ -136,7 +130,7 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName,

void swift::dumpStackTraceEntry(unsigned index, void *framePC,
bool shortOutput) {
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING && SWIFT_STDLIB_HAS_DLADDR
#if SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING && SWIFT_STDLIB_HAS_DLADDR
SymbolInfo syminfo;

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

SWIFT_ALWAYS_INLINE
static bool withCurrentBacktraceImpl(std::function<void(void **, int)> call) {
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
#if SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING
constexpr unsigned maxSupportedStackDepth = 128;
void *addrs[maxSupportedStackDepth];
#if defined(_WIN32)
Expand Down Expand Up @@ -325,7 +319,7 @@ reportNow(uint32_t flags, const char *message)
#elif defined(__ANDROID__)
__android_log_print(ANDROID_LOG_FATAL, "SwiftRuntime", "%s", message);
#endif
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
#if SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING
if (flags & FatalErrorFlags::ReportBacktrace) {
fputs("Current stack trace:\n", stderr);
printCurrentBacktrace();
Expand Down
1 change: 1 addition & 0 deletions utils/build-presets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,7 @@ swift-objc-interop=0
swift-enable-compatibility-overrides=0
swift-enable-reflection=0
swift-stdlib-has-dladdr=0
swift-stdlib-supports-backtrace-reporting=0
swift-stdlib-has-darwin-libmalloc=0
swift-stdlib-has-stdin=0
swift-runtime-static-image-inspection=1
Expand Down
8 changes: 8 additions & 0 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ KNOWN_SETTINGS=(
swift-enable-compatibility-overrides "1" "whether to support back-deploying compatibility fixes for newer apps running on older runtimes"
swift-enable-reflection "1" "whether to support reflection and mirrors"
swift-stdlib-has-dladdr "1" "whether to build stdlib assuming the runtime environment provides dladdr API"
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"
swift-runtime-static-image-inspection "0" "whether to build stdlib assuming the runtime environment only supports a single runtime image with Swift code"
swift-stdlib-single-threaded-runtime "0" "whether to build stdlib as a single-threaded runtime only"
swift-stdlib-os-versioning "1" "whether to build stdlib with availability based on OS versions (Darwin only)"
Expand Down Expand Up @@ -2103,6 +2104,13 @@ for host in "${ALL_HOSTS[@]}"; do
)
fi

if [[ "${SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING}" ]] ; then
cmake_options=(
"${cmake_options[@]}"
-DSWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING:BOOL=$(true_false "${SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING}")
)
fi

if [ "${SWIFT_INSTALL_COMPONENTS}" ] ; then
cmake_options=(
"${cmake_options[@]}"
Expand Down
2 changes: 1 addition & 1 deletion utils/check_freestanding_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
]
common_expected_dependencies = [
"___bzero", "___divti3", "___error", "___stderrp", "___stdoutp",
"___truncsfhf2", "___udivti3", "_abort", "_arc4random_buf", "_backtrace",
"___truncsfhf2", "___udivti3", "_abort", "_arc4random_buf",
"_calloc", "_close", "_environ", "_flockfile", "_floorl", "_fprintf",
"_fputc", "_fputs", "_free", "_funlockfile", "_fwrite", "_malloc",
"_malloc_size", "_memchr", "_memcmp", "_memcpy", "_memmove", "_memset",
Expand Down