From 4569cc994f727d3cbb90c5cce318c49bf8d075a5 Mon Sep 17 00:00:00 2001 From: Dhiogo Brustolin Date: Thu, 24 Oct 2024 15:06:36 +0200 Subject: [PATCH] fix: Build visionOS project with static Sentry SDK (#4462) We need to enable Swift interoperability with c++ for our visionOS SDK, in order to make the static SDK work with visionOS projects --- CHANGELOG.md | 4 ++++ Sources/Configuration/SDK.xcconfig | 1 + Sources/Sentry/SentryHttpTransport.m | 3 ++- Sources/Sentry/SentryLevelHelper.m | 5 +++-- Sources/Sentry/SentryLogC.m | 6 ++++-- Sources/Sentry/SentrySDK.m | 1 + Sources/Sentry/include/SentryLevelHelper.h | 4 +++- Sources/Sentry/include/SentryLogC.h | 7 ++++++- Sources/Swift/Helper/Log/SentryLevel.swift | 2 +- Sources/Swift/Tools/SentryLog.swift | 2 +- develop-docs/DECISIONS.md | 10 ++++++++++ 11 files changed, 36 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14dbce83628..22f31f1cdc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixes + +- Build visionOS project with static Sentry SDK (#4462) + ### Improvements - Log a warning when dropping envelopes due to rate-limiting (#4463) diff --git a/Sources/Configuration/SDK.xcconfig b/Sources/Configuration/SDK.xcconfig index 57a86caf22c..4270c685e30 100644 --- a/Sources/Configuration/SDK.xcconfig +++ b/Sources/Configuration/SDK.xcconfig @@ -47,3 +47,4 @@ GCC_PREPROCESSOR_DEFINITIONS_TestCI = DEBUG=1 TEST=1 TESTCI=1 GCC_PREPROCESSOR_DEFINITIONS_Release = RELEASE=1 GCC_PREPROCESSOR_DEFINITIONS_ReleaseWithoutUIKit = RELEASE=1 SENTRY_NO_UIKIT=1 GCC_PREPROCESSOR_DEFINITIONS = $(GCC_PREPROCESSOR_DEFINITIONS_$(CONFIGURATION)) +SWIFT_OBJC_INTEROP_MODE[sdk=xr*]=objcxx diff --git a/Sources/Sentry/SentryHttpTransport.m b/Sources/Sentry/SentryHttpTransport.m index 4c5a5defddb..6ec6866380e 100644 --- a/Sources/Sentry/SentryHttpTransport.m +++ b/Sources/Sentry/SentryHttpTransport.m @@ -232,7 +232,8 @@ - (SentryFlushResult)flush:(NSTimeInterval)timeout - (void)envelopeItemDropped:(SentryEnvelopeItem *)envelopeItem withCategory:(SentryDataCategory)dataCategory; { - SENTRY_LOG_WARN(@"Envelope item dropped due to exceeding rate limit. Category: %@", nameForSentryDataCategory(dataCategory)); + SENTRY_LOG_WARN(@"Envelope item dropped due to exceeding rate limit. Category: %@", + nameForSentryDataCategory(dataCategory)); [self recordLostEvent:dataCategory reason:kSentryDiscardReasonRateLimitBackoff]; [self recordLostSpans:envelopeItem reason:kSentryDiscardReasonRateLimitBackoff]; } diff --git a/Sources/Sentry/SentryLevelHelper.m b/Sources/Sentry/SentryLevelHelper.m index 104092d5d15..8455fb4c4bf 100644 --- a/Sources/Sentry/SentryLevelHelper.m +++ b/Sources/Sentry/SentryLevelHelper.m @@ -1,8 +1,9 @@ #import "SentryLevelHelper.h" #import "SentryBreadcrumb+Private.h" -NSUInteger -sentry_breadcrumbLevel(SentryBreadcrumb *breadcrumb) +@implementation SentryLevelBridge : NSObject ++ (NSUInteger)breadcrumbLevel:(SentryBreadcrumb *)breadcrumb { return breadcrumb.level; } +@end diff --git a/Sources/Sentry/SentryLogC.m b/Sources/Sentry/SentryLogC.m index 79f616b8747..918e0b70686 100644 --- a/Sources/Sentry/SentryLogC.m +++ b/Sources/Sentry/SentryLogC.m @@ -1,3 +1,4 @@ +#import "SentryLogC.h" #import "SentryAsyncSafeLog.h" #import "SentryFileManager.h" #import "SentryInternalCDefines.h" @@ -6,8 +7,8 @@ NS_ASSUME_NONNULL_BEGIN -void -sentry_initializeAsyncLogFile(void) +@implementation SentryAsyncLogWrapper ++ (void)initializeAsyncLogFile { const char *asyncLogPath = [[sentryStaticCachesPath() stringByAppendingPathComponent:@"async.log"] UTF8String]; @@ -25,5 +26,6 @@ @"Could not open a handle to specified path for async logging %s", asyncLogPath); }; } +@end NS_ASSUME_NONNULL_END diff --git a/Sources/Sentry/SentrySDK.m b/Sources/Sentry/SentrySDK.m index a54aa922eae..4a5c504216f 100644 --- a/Sources/Sentry/SentrySDK.m +++ b/Sources/Sentry/SentrySDK.m @@ -14,6 +14,7 @@ #import "SentryHub+Private.h" #import "SentryInternalDefines.h" #import "SentryLog.h" +#import "SentryLogC.h" #import "SentryMeta.h" #import "SentryOptions+Private.h" #import "SentryProfilingConditionals.h" diff --git a/Sources/Sentry/include/SentryLevelHelper.h b/Sources/Sentry/include/SentryLevelHelper.h index 1211dd6486b..13368a76d61 100644 --- a/Sources/Sentry/include/SentryLevelHelper.h +++ b/Sources/Sentry/include/SentryLevelHelper.h @@ -7,6 +7,8 @@ NS_ASSUME_NONNULL_BEGIN /** * This is a workaround to access SentryLevel value from swift */ -NSUInteger sentry_breadcrumbLevel(SentryBreadcrumb *breadcrumb); +@interface SentryLevelBridge : NSObject ++ (NSUInteger)breadcrumbLevel:(SentryBreadcrumb *)breadcrumb; +@end NS_ASSUME_NONNULL_END diff --git a/Sources/Sentry/include/SentryLogC.h b/Sources/Sentry/include/SentryLogC.h index a6d2057c11a..a7e84525c59 100644 --- a/Sources/Sentry/include/SentryLogC.h +++ b/Sources/Sentry/include/SentryLogC.h @@ -1,4 +1,9 @@ #ifndef SentryLogC_h #define SentryLogC_h -void sentry_initializeAsyncLogFile(void); +#import + +@interface SentryAsyncLogWrapper : NSObject ++ (void)initializeAsyncLogFile; +@end + #endif diff --git a/Sources/Swift/Helper/Log/SentryLevel.swift b/Sources/Swift/Helper/Log/SentryLevel.swift index 80ed88a7190..da4cd4c8f48 100644 --- a/Sources/Swift/Helper/Log/SentryLevel.swift +++ b/Sources/Swift/Helper/Log/SentryLevel.swift @@ -47,6 +47,6 @@ class SentryLevelHelper: NSObject { } static func breadcrumbLevel(_ breadcrumb: Breadcrumb) -> SentryLevel? { - SentryLevel(rawValue: sentry_breadcrumbLevel(breadcrumb)) + SentryLevel(rawValue: SentryLevelBridge.breadcrumbLevel(breadcrumb)) } } diff --git a/Sources/Swift/Tools/SentryLog.swift b/Sources/Swift/Tools/SentryLog.swift index 85bb4a75994..bbe71328018 100644 --- a/Sources/Swift/Tools/SentryLog.swift +++ b/Sources/Swift/Tools/SentryLog.swift @@ -15,7 +15,7 @@ class SentryLog: NSObject { self.isDebug = isDebug self.diagnosticLevel = diagnosticLevel } - sentry_initializeAsyncLogFile() + SentryAsyncLogWrapper.initializeAsyncLogFile() } @objc diff --git a/develop-docs/DECISIONS.md b/develop-docs/DECISIONS.md index 6e53bf2c39f..1d1b0c8d5ab 100644 --- a/develop-docs/DECISIONS.md +++ b/develop-docs/DECISIONS.md @@ -226,3 +226,13 @@ please refer to this [PR](https://github.com/getsentry/sentry-cocoa/pull/3623). When coding with Swift be aware of two things: 1. If you want to use swift code in an Objc file: `#import "SentrySwift.h"` 2. If you want to use Objc code from Swift, first add the desired header file to `SentryInternal.h`, then, in your Swift file, `@_implementationOnly import _SentryPrivate` (the underscore makes auto-complete ignore it since we dont want users importing this module). + +## Enabling C++/Objective-c++ interoperability for visionOS + +Date: October 23, 2024 +Contributors: @brustolin, @philipphofmann + +To enable visionOS support with the Sentry static framework, you need to set the `SWIFT_OBJC_INTEROP_MODE` build setting to `objcxx`. This setting will only be applied for visionOS, but because much of the codebase is shared across platforms, this change introduces a limitation: we won’t be able to call C functions directly from Swift code. + +However, C functions can still be accessed from code that is conditionally compiled using directives, such as `#if os(iOS)`. +