Skip to content

feat(core): switch FirebaseCore to Swift and port leaf types (Phases 2-3) - #17156

Draft
paulb777 wants to merge 14 commits into
pb-core-swift-link-gatefrom
pb-core-swift
Draft

paulb777 wants to merge 14 commits into
pb-core-swift-link-gatefrom
pb-core-swift

Conversation

@paulb777

Copy link
Copy Markdown
Member

Switches FirebaseCore to a Swift target, folds FirebaseCoreInternal into FirebaseCore, and ports all Phase 3 leaf types to Swift while preserving the public Swift/Objective-C APIs and the Objective-C runtime/binary-link ABI.

This is a draft PR covering Phases 2 and 3 of the FirebaseCore Swift port, stacked on #17149 (pb-core-swift-link-gate) with #17151 and #17152 cherry-picked underneath until those Phase 1 PRs merge to main.

Changes

Phase 2 — Swift target & FirebaseCoreInternal consolidation

  • Converted FirebaseCore in Package.swift to a Swift target (FirebaseCore/Swift/Sources), re-exporting remaining Objective-C public classes from FirebaseCoreObjC via FirebaseCoreExports.swift and the transitional FIRCoreObjCAnchor protocol.
  • Consolidated FirebaseCoreInternal into FirebaseCore, exposing UnfairLock via @_spi(FirebaseCoreInternal) (with an NSLock fallback for non-Apple platforms), moving heartbeat unit tests to FirebaseCore/Tests/HeartbeatUnit, and porting FIRHeartbeatLogger and FIRHeaderValueFromHeartbeatsPayload to Swift (HeartbeatLogger.swift).
  • Added FirebaseCoreSwiftShim so Swift targets importing Clang modules that do @import FirebaseCore; (such as FirebaseCoreExtension) resolve native Swift types cleanly, and added direct FirebaseCore dependencies on Objective-C test targets that import FirebaseCoreExtension headers.
  • Updated all in-repo FirebaseCoreInternal consumers (FirebaseAI, FirebaseAuth, FirebaseFunctions, FirebaseSessions, FirebaseAppDistributionInternal, FirebaseInstallations tests, and CoreUnit).

Phase 3 — Leaf-type Swift ports

  • Timestamp and header snapshot infrastructure: Ported Timestamp (FIRTimestamp) and raiseCoreException to Swift (Timestamp.swift, CoreExceptions.swift), converted FIRTimestamp.h to a forwarding header, added FirebaseCore-SwiftImport.h and checked-in FirebaseCore-SwiftSnapshot.h (for non-modular ObjC++ under Swift Build and transitive ObjC dependents of FirebaseCoreSwiftShim), and added scripts/core_api/{gen_header,update_snapshot,check_snapshot}.sh.
  • SceneFinder: Ported FIRSceneFinder (SceneFinder.swift) for iOS/tvOS/visionOS using private non-FIR Objective-C proxy protocols so UIKit objects, test fakes, and OCMock proxies behave identically.
  • Core utilities: Ported FIRFirebaseUserAgent (FirebaseUserAgent.swift), FIRBundleUtil (BundleUtil.swift), and FIRAnalyticsConfiguration (AnalyticsConfiguration.swift), preserving #if DEBUG test seams.
  • Logger and FirebaseConfiguration: Ported FirebaseLoggerLevel (@nonexhaustive @objc(FIRLoggerLevel)), FirebaseLogger (@_spi(FirebaseCoreInternal) plus @_cdecl C entry points and FIRLoggerCore), and FirebaseConfiguration (FIRConfiguration). Moved kFIRLogger* constants and variadic FIRLog* / FIRLogBasic* shims into FirebaseCore/Extension/FIRLogger.m (checking FIRIsLoggableLevel before formatting) and migrated in-repo Swift FirebaseLogger.log call sites to @_spi(FirebaseCoreInternal).
  • Component system: Ported FIRComponent, FIRComponentType, and FIRComponentContainer (Component.swift, ComponentContainer.swift) with a portable ObjectIdentifier-keyed Swift component registry alongside the Apple Protocol-keyed Objective-C bridge (NSRecursiveLock for reentrant dependency resolution and appWillBeDeleted: callbacks, plus ObjC proxy protocols for OCMock and fake apps such as FIRFakeApp).
  • UnfairLock ThreadSanitizer fix: Stored UnfairLock's value out-of-line via UnsafeMutablePointer<Value> and made lockPointer immutable (let) so zero-sized Value types (Void) do not alias offset 0 (isa) of self and trigger a Swift access race under TSan.
  • Parity & coverage gates: Updated scripts/core_api/core_api.py to build FirebaseCore.swiftmodule and FirebaseCore-Swift.h across all 5 Apple platforms, skip @_spi nodes in surface_decls(), and normalize Swift-generated Objective-C USRs/default attributes; added reviewed Phase 3 entries to FirebaseCore/Tests/APIBaselines/allowlist.json; updated scripts/core_api/check_binary_link.py for Swift __PROTOCOL_* symbols and FirebaseCore/Swift/Sources; updated scripts/core_api/coverage.py and LogCodeTests.swift for FirebaseCore/Swift/Sources; added SwiftComponentRegistryTests.swift and FIRLogBasic* tests in LoggerTests.swift.

Not changed / known limitations

  • FIRVersion, FIROptions, and FIRApp remain in Objective-C (FirebaseCoreObjC) and will be ported in Phase 4, along with planned deprecations and removal of FirebaseCoreObjC and FIRCoreObjCAnchor.
  • CocoaPods podspecs (FirebaseCore.podspec, FirebaseCoreInternal.podspec), CI workflow updates, and the FirebaseCore/CHANGELOG.md entry are deferred to Phases 4–5 (so CocoaPods lint jobs are not expected to pass on this draft PR yet).

API Changes

No breaking public Swift or Objective-C API changes. Reviewed differences allowlisted in FirebaseCore/Tests/APIBaselines/allowlist.json:

  • -[FIRTimestamp init] is now callable from Objective-C (returning the current time); +[FIRTimestamp timestamp] remains unchanged.
  • FirebaseLoggerLevel carries @nonexhaustive on Swift 6.2+ to preserve NS_ENUM switch exhaustiveness (@unknown default), and FIRLoggerLevelMin / FIRLoggerLevelMax are provided as Objective-C compatibility macros in FirebaseCoreObjC.h.
  • Swift surfaces explicit NSObject/NSCopying overrides on Timestamp and inherited -[FIRConfiguration init] in the generated header, plus the transitional FIRCoreObjCAnchor (_FirebaseCoreObjCAnchor) protocol removed in Phase 4.

Testing

  • API parity (scripts/core_api/core_api.py): Two-step dump --out + check --current with Xcode 26.5 (Swift 6.3.2) passed across ios, catalyst, macos, tvos, watchos, and Objective-C (clang -extract-api).
  • Header snapshots (scripts/core_api/check_snapshot.sh): Passed on Xcode 26.2 (full), Xcode 26.5 (--decls-only), and Xcode 27.2 (--decls-only) for both macosx and iphoneos.
  • ThreadSanitizer (scripts/core_api/run_tsan.sh): Passed all CoreSwiftUnit tests on macOS (Xcode 27.2) with zero TSan reports. (Prior to the UnfairLock fix, ConcurrencyTests.testConcurrentConfigureFindAndDelete, testConcurrentDataCollectionFlag, and testConcurrentDuplicateConfigure failed with a Swift access race in UnfairLock.withLock.)
  • Line coverage (scripts/core_api/coverage.py check): Passed on macOS (Xcode 27.2) at 88.67% total line coverage (baseline 88.53%).
  • Core suites (macOS Xcode 27.2, macOS Xcode 26.5, iOS Simulator iPhone 17 OS 27.2): CoreSwiftUnit, CoreUnit, CoreRuntimeABI (Debug & Release, verified zero test symbols in Release nm -a), CoreSwiftAPI, CoreObjCAPI, and FirebaseCoreInternalTests passed with zero warnings in FirebaseCore.
  • Binary link gate (macOS Xcode 27.2): CoreAnalyticsLink, CoreFirestoreLink, dynamic CoreBinaryLink-Package, and scripts/core_api/check_binary_link.py check passed.
  • Downstream & SPM suites (macOS Xcode 27.2): DatabaseUnit, DatabaseUnitSwift, AuthUnit, FirebaseFunctionsUnit, FirebaseSessionsUnit, FirebaseStorageUnit, FirebaseFirestoreTests, FirebaseAppCheckUnit, ABTestingUnit, FirebaseCrashlyticsUnit, MessagingUnit, AnalyticsObjCAPI, AnalyticsSwiftUnit, objc-import-test, swift-test, and version-test passed; clang -arch x86_64 -Wall -Wextra -Werror -fsyntax-only passed on FirebaseCore/Extension/FIRLogger.m.
  • Style & lint: scripts/style.sh test-only (all changed Swift/ObjC files), scripts/check_whitespace.sh, scripts/check_imports.swift, python3 -m py_compile, and bash -n all passed.
  • Not run locally: CocoaPods specs/lint, source Firestore (FIREBASE_SOURCE_FIRESTORE), and Linux builds.

#no-changelog

This PR was prepared with AI assistance.

Every SwiftPM target and podspec that uses FirebaseCore's internal API
(the FirebaseCore/Extension headers, or the internal symbols they declare)
now depends on FirebaseCoreExtension explicitly.

Today the Objective-C SDKs reach the Extension headers by path, through a
header search path to the repository root, and link the internal symbols
from FirebaseCore. Once FirebaseCore is reimplemented in Swift,
FirebaseCoreExtension becomes an Objective-C target with its own
implementation file: the FIRLog* functions and the kFIR* and
FIRAuthStateDidChangeInternalNotification* constants move there. Targets
that use them must depend on it, or they won't link. While FirebaseCore is
still Objective-C, the new dependencies change nothing: FirebaseCoreExtension
only has headers and an empty file.

Package.swift:
- SDK targets: FirebaseABTesting, FirebaseAppCheck, FirebaseAppDistribution,
  FirebaseCrashlytics, FirebaseDatabaseInternal,
  FirebaseInAppMessagingInternal, FirebaseInstallations, FirebaseMessaging,
  FirebasePerformance, FirebaseRemoteConfigInternal, and
  FirebaseFirestoreInternalWrapper (both the source and binary variants).
- Wrappers of closed-source binaries: FirebaseAnalyticsWrapper,
  FirebaseAnalyticsCoreWrapper and FirebaseAnalyticsIdentitySupportTarget,
  because the FirebaseAnalytics binary uses kFIRLoggerAnalytics; and the
  binary FirebaseFirestoreInternalWrapper, because the
  FirebaseFirestoreInternal binary uses FIRLogBasic*, FIRSetLoggerLevel*,
  FIRIsLoggableLevel* and FIRAuthStateDidChangeInternalNotification*.
- Test targets that import the Extension headers or module directly:
  CoreUnit, ABTestingUnit, AppDistributionUnit, AuthUnit,
  FirebaseCrashlyticsUnit, DatabaseUnit, FirebaseFunctionsObjCIntegration,
  MessagingUnit, PerformanceUnit, RemoteConfigUnit, FirebaseAppCheckUnit and
  FirebaseAILogicUnit.

Podspecs: FirebaseABTesting, FirebaseAppCheck,
FirebaseAppDistribution, FirebaseCrashlytics, FirebaseDatabase,
FirebaseFirestoreInternal, FirebaseInAppMessaging, FirebaseInstallations,
FirebaseMessaging, FirebasePerformance and FirebaseRemoteConfig, with the
same version requirement as their FirebaseCore dependency.

No source files or header search paths change.

FirebaseAnalytics.podspec is left alone for now: it describes the
closed-source binary, and adding the dependency would change the layout of the
zip distribution. It's needed only once kFIRLoggerAnalytics moves to
FirebaseCoreExtension, and will be coordinated with the release then.
Add FirebaseCore/Swift/Sources/FirebaseVersion.swift with
`let firebaseVersion = "13.3.0"`, written like the declaration in
Package.swift. It becomes the source of the version in code when FirebaseCore
is implemented in Swift, replacing the Firebase_VERSION build setting. It is
not compiled yet.

The release tooling updates it with the same sed command as Package.swift
(removing the temporary .bak files), and version-test checks that the two
literals match.
@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@danger-firebase-ios

Copy link
Copy Markdown
1 Warning
⚠️ New public headers were added, did you remember to add them to the umbrella header?

Generated by 🚫 Danger

@paulb777

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request migrates key components of FirebaseCore from Objective-C to Swift, including configuration, logging, timestamps, bundle utilities, and the component container, while introducing a Clang module shim (FirebaseCoreSwiftShim) to maintain backward compatibility. The review feedback highlights several opportunities to improve type safety and idiomatic Swift usage. Specifically, it recommends replacing unsafe unsafeBitCast operations with safer Swift casting (as?) or selector checks in FirebaseComponentContainer, inheriting from NSObjectProtocol in private proxy protocols within SceneFinder to avoid manual method redeclarations, and properly deinitializing lockPointer before deallocation in UnfairLock.

- Emit Apache 2.0 copyright header in FirebaseCore-SwiftSnapshot.h via update_snapshot.sh and ignore leading comments up to #ifndef FIREBASECORE_SWIFT_H in check_snapshot.sh.
- Match FIRLogger.h enum types in FirebaseLogger.swift @_cdecl functions (FIRGetLoggerLevel, FIRSetLoggerLevel, FIRIsLoggableLevel) to fix Xcode 26.2 MandatorySILLinker type mismatch.
- Import FIRApp.h under !__has_feature(modules) in FirebaseCore-SwiftImport.h so standalone non-modular ObjC++ includes (e.g. FIRTimestamp.h in source Firestore) have FIRApp declared for the FIRCoreObjCAnchor category, and add ObjCPPStandaloneTimestampTests.mm.
- Wire Firebase -> [FirebaseCore, FirebaseCoreSwiftShim], add Firebase to FirebaseCoreExtension, and expose FirebaseCore via FirebaseCoreTarget so Swift 'import Firebase' works for every product (including analytics-import-test) while keeping FirebaseCore portable to Linux; remove redundant downstream FirebaseCoreSwiftShim entries.
- Guard populateComponents(from:for:) with responds(to: #selector(_CoreLibraryClassProxy.componentsToRegister)) before unsafeBitCast.
- Store component.creationBlock as AnyObject instead of unsafeBitCast.
- Guard unsafeBitCast to ObjCComponentCreationBlock with an NSBlock isKind(of:) check (Swift's runtime as? cast returns nil for Objective-C NSBlocks), and add unit tests for non-conforming registrants and non-block entries.
- Inherit NSObjectProtocol in _CoreSceneProxy instead of redeclaring isKindOfClass:.
- Call lockPointer.deinitialize(count: 1) before deallocate() in UnfairLock.deinit.
…sions

- Match Objective-C BOOL (`ObjCBool` on x86_64 macOS/Mac Catalyst, `Bool` elsewhere) in `FirebaseLogger.swift` `@_cdecl` signatures so Xcode 26.2 `MandatorySILLinker` succeeds on x86_64 macOS and Catalyst.
- Nest internal `@objc(FIR...)` compatibility classes inside `_FirebaseCoreObjCImpl` and mark internal `@objc` mirror protocols `private` so `swiftc -application-extension` (`APPLICATION_EXTENSION_API_ONLY=YES`) does not emit duplicate `@interface`/`@protocol` declarations into `FirebaseCore-Swift.h` while preserving exported `_OBJC_CLASS_$_FIR...` symbols and protocol metadata in Release builds.
- Dispatch `FIRLoggerCore` via a private protocol in `FirebaseCore/Extension/FIRLogger.m` instead of redeclaring `@interface FIRLoggerCore`.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant