Skip to content
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

Firestore Swift Cpp Experiment #13956

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion FirebaseCombineSwift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ for internal testing only. It should not be published.

s.pod_target_xcconfig = {
'HEADER_SEARCH_PATHS' => '"${PODS_TARGET_SRCROOT}"',
'SWIFT_OBJC_INTEROP_MODE' => 'objcxx',
}

s.test_spec 'unit' do |unit_tests|
Expand All @@ -81,7 +82,8 @@ for internal testing only. It should not be published.
unit_tests.exclude_files = 'FirebaseCombineSwift/Tests/Unit/**/*Template.swift'
unit_tests.requires_app_host = true
unit_tests.pod_target_xcconfig = {
'SWIFT_OBJC_BRIDGING_HEADER' => '$(PODS_TARGET_SRCROOT)/FirebaseCombineSwift/Tests/Unit/FirebaseCombine-unit-Bridging-Header.h'
'SWIFT_OBJC_BRIDGING_HEADER' => '$(PODS_TARGET_SRCROOT)/FirebaseCombineSwift/Tests/Unit/FirebaseCombine-unit-Bridging-Header.h',
'SWIFT_OBJC_INTEROP_MODE' => 'objcxx',
}
unit_tests.dependency 'OCMock'
unit_tests.dependency 'FirebaseAuthTestingSupport'
Expand Down
9 changes: 9 additions & 0 deletions FirebaseCore/Sources/Public/FirebaseCore/FIRVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@
NS_ASSUME_NONNULL_BEGIN

/** Returns the current version of Firebase. */

#ifdef __cplusplus
extern "C" {
#endif

NS_SWIFT_NAME(FirebaseVersion())
NSString* FIRFirebaseVersion(void);

#ifdef __cplusplus
}
#endif

NS_ASSUME_NONNULL_END
6 changes: 6 additions & 0 deletions FirebaseFirestore.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ Google Cloud Firestore is a NoSQL document database built for automatic scaling,
'FirebaseFirestoreInternal/**/*.[mh]',
'Firestore/Swift/Source/**/*.swift',
]

s.pod_target_xcconfig = {
# Enables C++ <-> Swift interop (by default it's only C)
"SWIFT_OBJC_INTEROP_MODE" => "objcxx",
}

s.resource_bundles = {
"#{s.module_name}_Privacy" => 'Firestore/Swift/Source/Resources/PrivacyInfo.xcprivacy'
}
Expand Down
6 changes: 5 additions & 1 deletion FirebaseFirestoreInternal.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ Google Cloud Firestore is a NoSQL document database built for automatic scaling,
# Header files that constitute the interface to this module. Only Objective-C
# headers belong here, since FirebaseFirestore is primarily an Objective-C
# framework.
s.public_header_files = 'Firestore/Source/Public/FirebaseFirestore/*.h'
s.public_header_files = [
'Firestore/Source/Public/FirebaseFirestore/*.h',
'Firestore/core/swift/include/*.h'
]

# source_files contains most of the header and source files for the project.
# This includes files named in `public_header_files`.
Expand All @@ -52,6 +55,7 @@ Google Cloud Firestore is a NoSQL document database built for automatic scaling,
'Firestore/core/include/**/*.{cc,mm}',
'Firestore/core/src/**/*.{cc,mm}',
'FirebaseAuth/Interop/**/*.h',
'Firestore/core/swift/**/*.{cc,h}',
]

# Internal headers that aren't necessarily globally unique. Most C++ internal
Expand Down
36 changes: 36 additions & 0 deletions Firestore/Source/API/FIRInterface.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "Firestore/Source/Public/FirebaseFirestore/FIRInterface.h"

#import "Firestore/core/src/api/used_by_objective_c.h"
#include "Firestore/core/src/util/string_apple.h"

using firebase::firestore::util::MakeString;

NS_ASSUME_NONNULL_BEGIN

@implementation FIRInterface

+ (void)print:(NSString *)content {
CppInterfaceCalledByObjectiveC::print(MakeString(content));
}

@end

NS_ASSUME_NONNULL_END
29 changes: 29 additions & 0 deletions Firestore/Source/Public/FirebaseFirestore/FIRInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface FIRInterface : NSObject

#pragma mark - Create Filter

+ (void)print:(NSString *)content;

@end

NS_ASSUME_NONNULL_END
25 changes: 25 additions & 0 deletions Firestore/Swift/Source/SwiftAPI/SwiftCppAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#if SWIFT_PACKAGE
@_exported import FirebaseFirestoreCpp
#endif // SWIFT_PACKAGE

public class SwiftCallingCpp {
public init(_ value: String) {
CppInterfaceCalledBySwift.print(std.string(value))

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/SwiftCppAPI.swift

View workflow job for this annotation

GitHub Actions / platforms (catalyst)

no exact matches in call to initializer

Check failure on line 23 in Firestore/Swift/Source/SwiftAPI/SwiftCppAPI.swift

View workflow job for this annotation

GitHub Actions / platforms (catalyst)

no exact matches in call to initializer
}
}
2 changes: 2 additions & 0 deletions Firestore/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ firebase_ios_glob(
src/objc/*.h
src/remote/*.cc
src/remote/*.h
swift/include/*.h
swift/src/*.cc
EXCLUDE ${nanopb_sources}
)

Expand Down
22 changes: 22 additions & 0 deletions Firestore/core/src/api/used_by_objective_c.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "used_by_objective_c.h"
#include <iostream>

void CppInterfaceCalledByObjectiveC::print(std::string content) {
std::cout << "C++ function runs with value: " << content << std::endl;
}
27 changes: 27 additions & 0 deletions Firestore/core/src/api/used_by_objective_c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FIREBASE_USED_BY_SWIFT_H
#define FIREBASE_USED_BY_SWIFT_H

#include <string>

class CppInterfaceCalledByObjectiveC {
public:
static void print(std::string content);
};

#endif // FIREBASE_USED_BY_SWIFT_H
22 changes: 22 additions & 0 deletions Firestore/core/swift/include/FirebaseFirestoreCpp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FIREBASE_FIREBASEFIRESTORECPP_H
#define FIREBASE_FIREBASEFIRESTORECPP_H

#import "used_by_swift.h"

#endif // FIREBASE_FIREBASEFIRESTORECPP_H
27 changes: 27 additions & 0 deletions Firestore/core/swift/include/used_by_swift.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FIREBASE_USED_BY_SWIFT_H
#define FIREBASE_USED_BY_SWIFT_H

#include <string>

Check failure on line 20 in Firestore/core/swift/include/used_by_swift.h

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

'string' file not found

Check failure on line 20 in Firestore/core/swift/include/used_by_swift.h

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

'string' file not found

Check failure on line 20 in Firestore/core/swift/include/used_by_swift.h

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

'string' file not found

Check failure on line 20 in Firestore/core/swift/include/used_by_swift.h

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

'string' file not found

Check failure on line 20 in Firestore/core/swift/include/used_by_swift.h

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

'string' file not found

class CppInterfaceCalledBySwift {
public:
static void print(std::string content);
};

#endif // FIREBASE_USED_BY_SWIFT_H
22 changes: 22 additions & 0 deletions Firestore/core/swift/src/used_by_swift.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "../include/used_by_swift.h"
#include <iostream>

void CppInterfaceCalledBySwift::print(std::string content) {
std::cout << "C++ function runs with value: " << content << std::endl;
}
33 changes: 30 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,10 @@ let package = Package(
dependencies: [
"FirebaseFirestoreTarget",
],
path: "FirebaseCombineSwift/Sources/Firestore"
path: "FirebaseCombineSwift/Sources/Firestore",
swiftSettings: [
.interoperabilityMode(.Cxx), // C++ interoperability setting
]
),
.target(
name: "FirebaseStorageCombineSwift",
Expand Down Expand Up @@ -1171,7 +1174,10 @@ let package = Package(
"FirebaseStorage",
.product(name: "nanopb", package: "nanopb"),
],
path: "SwiftPMTests/swift-test"
path: "SwiftPMTests/swift-test",
swiftSettings: [
.interoperabilityMode(.Cxx), // C++ interoperability setting
]
),
.testTarget(
name: "analytics-import-test",
Expand Down Expand Up @@ -1399,6 +1405,17 @@ func firestoreWrapperTarget() -> Target {
)
}

func firebaseFirestoreCppTarget() -> Target {
return .target(
name: "FirebaseFirestoreCpp",
path: "Firestore/core/swift",
publicHeadersPath: "include", // Path to the public headers
cxxSettings: [
.headerSearchPath("include"), // Ensure the header search path is correct
]
)
}

func firestoreTargets() -> [Target] {
if ProcessInfo.processInfo.environment["FIREBASE_SOURCE_FIRESTORE"] != nil {
return [
Expand Down Expand Up @@ -1469,13 +1486,15 @@ func firestoreTargets() -> [Target] {
.linkedLibrary("c++"),
]
),
firebaseFirestoreCppTarget(),
.target(
name: "FirebaseFirestore",
dependencies: [
"FirebaseCore",
"FirebaseCoreExtension",
"FirebaseFirestoreInternalWrapper",
"FirebaseSharedSwift",
"FirebaseFirestoreCpp",
],
path: "Firestore",
exclude: [
Expand All @@ -1496,7 +1515,10 @@ func firestoreTargets() -> [Target] {
sources: [
"Swift/Source/",
],
resources: [.process("Source/Resources/PrivacyInfo.xcprivacy")]
resources: [.process("Source/Resources/PrivacyInfo.xcprivacy")],
swiftSettings: [
.interoperabilityMode(.Cxx), // C++ interoperability setting
]
),
]
}
Expand Down Expand Up @@ -1542,9 +1564,13 @@ func firestoreTargets() -> [Target] {
"FirebaseCoreExtension",
"leveldb",
"FirebaseSharedSwift",
"FirebaseFirestoreCpp",
],
path: "Firestore/Swift/Source",
resources: [.process("Resources/PrivacyInfo.xcprivacy")],
swiftSettings: [
.interoperabilityMode(.Cxx), // C++ interoperability setting
],
linkerSettings: [
.linkedFramework("SystemConfiguration", .when(platforms: [.iOS, .macOS, .tvOS])),
.linkedFramework("UIKit", .when(platforms: [.iOS, .tvOS])),
Expand All @@ -1561,5 +1587,6 @@ func firestoreTargets() -> [Target] {
publicHeadersPath: "."
),
firestoreInternalTarget,
firebaseFirestoreCppTarget(),
]
}
Loading