Skip to content

Commit 605a70d

Browse files
committed
Add CMake logic for defining a future library for playbilling
Adds a cmake script that allows the definition of a version of the future library that uses the namespace playbillingclient instead of firebase. Adds logic to the source files that if playbilling is being used, it includes the generated public header for it instead. Because of the source libraries needing to include the generated header, it will currently only work for playbilling, but could potentially be generalized in the future. PiperOrigin-RevId: 251918893
1 parent 0fd36a5 commit 605a70d

File tree

8 files changed

+157
-31
lines changed

8 files changed

+157
-31
lines changed

app/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ set(log_common_SRCS
8686
src/log.cc)
8787
set(log_android_SRCS
8888
src/log_android.cc
89+
src/log_android_callback.cc
8990
src/util_android.cc)
9091
set(log_ios_SRCS
9192
src/log_ios.mm)

app/src/future_manager.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@
1919

2020
#include <map>
2121
#include <set>
22-
#include "app/src/include/firebase/future.h"
2322
#include "app/src/mutex.h"
2423
#include "app/src/reference_counted_future_impl.h"
2524

2625
#if !defined(FIREBASE_NAMESPACE)
2726
#define FIREBASE_NAMESPACE firebase
2827
#endif
2928

29+
#ifdef USE_PLAYBILLING_FUTURE
30+
#include "playbillingclient/future.h"
31+
#else
32+
#include "app/src/include/firebase/future.h"
33+
#endif
34+
3035
namespace FIREBASE_NAMESPACE {
3136

3237
// Class for handling Future APIs. These all work directly with

app/src/include/firebase/internal/future_impl.h

+4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
// You shouldn't include future_impl.h directly, since its just the inline
2323
// implementation of the functions in future.h. Include future.h instead.
2424
// This is here to ensure that presubmit tests pass.
25+
#ifdef USE_PLAYBILLING_FUTURE
26+
#include "playbillingclient/future.h"
27+
#else
2528
#include "firebase/future.h"
29+
#endif
2630

2731
#if defined(FIREBASE_USE_MOVE_OPERATORS)
2832
#include <utility>

app/src/include/firebase/util.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
#define FIREBASE_APP_CLIENT_CPP_SRC_INCLUDE_FIREBASE_UTIL_H_
1919

2020
#include "firebase/app.h"
21-
#include "firebase/future.h"
2221

2322
#if !defined(FIREBASE_NAMESPACE)
2423
#define FIREBASE_NAMESPACE firebase
2524
#endif
2625

26+
#ifdef USE_PLAYBILLING_FUTURE
27+
#include "playbillingclient/future.h"
28+
#else
29+
#include "firebase/future.h"
30+
#endif
31+
2732
namespace FIREBASE_NAMESPACE {
2833

2934
struct ModuleInitializerData;

app/src/log_android.cc

-28
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@
2727
#define STR_EXPAND(x) #x
2828
#define STR(x) STR_EXPAND(x)
2929

30-
#include <assert.h>
31-
#include <jni.h>
3230
#include <stdarg.h>
33-
#include <stdio.h>
34-
35-
#include "app/src/util_android.h"
3631

3732
namespace FIREBASE_NAMESPACE {
3833

@@ -75,28 +70,5 @@ void LogMessageV(LogLevel log_level, const char* format, va_list args) {
7570
}
7671
#endif // defined(FIREBASE_ANDROID_FOR_DESKTOP)
7772

78-
// Called from com.google.firebase.app.internal.cpp.Log.
79-
extern "C" JNIEXPORT void JNICALL
80-
Java_com_google_firebase_app_internal_cpp_Log_nativeLog(
81-
JNIEnv* env, jobject instance, jint priority, jstring tag, jstring msg) {
82-
std::string ctag = util::JStringToString(env, tag);
83-
std::string cmsg = util::JStringToString(env, msg);
84-
static const LogLevel kLogPriorityToLogLevel[] = {
85-
kLogLevelDebug, // 0 = undocumented
86-
kLogLevelDebug, // 1 = undocumented
87-
kLogLevelVerbose, // 2 = android.util.Log.VERBOSE
88-
kLogLevelDebug, // 3 = android.util.Log.DEBUG
89-
kLogLevelInfo, // 4 = android.util.Log.INFO
90-
kLogLevelWarning, // 5 = android.util.Log.WARN
91-
kLogLevelError, // 6 = android.util.Log.ERROR
92-
kLogLevelAssert, // 7 = android.util.Log.ASSERT
93-
};
94-
assert(priority <
95-
sizeof(kLogPriorityToLogLevel) / sizeof(kLogPriorityToLogLevel[0]));
96-
assert(priority >= 0);
97-
LogMessage(kLogPriorityToLogLevel[priority], "(%s) %s", ctag.c_str(),
98-
cmsg.c_str());
99-
}
100-
10173
// NOLINTNEXTLINE - allow namespace overridden
10274
} // namespace FIREBASE_NAMESPACE

app/src/log_android_callback.cc

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <assert.h>
18+
#include <jni.h>
19+
20+
#include <string>
21+
22+
#include "app/src/log.h"
23+
#include "app/src/util_android.h"
24+
25+
namespace firebase {
26+
27+
// Called from com.google.firebase.app.internal.cpp.Log.
28+
extern "C" JNIEXPORT void JNICALL
29+
Java_com_google_firebase_app_internal_cpp_Log_nativeLog(
30+
JNIEnv* env, jobject instance, jint priority, jstring tag, jstring msg) {
31+
std::string ctag = util::JStringToString(env, tag);
32+
std::string cmsg = util::JStringToString(env, msg);
33+
static const LogLevel kLogPriorityToLogLevel[] = {
34+
kLogLevelDebug, // 0 = undocumented
35+
kLogLevelDebug, // 1 = undocumented
36+
kLogLevelVerbose, // 2 = android.util.Log.VERBOSE
37+
kLogLevelDebug, // 3 = android.util.Log.DEBUG
38+
kLogLevelInfo, // 4 = android.util.Log.INFO
39+
kLogLevelWarning, // 5 = android.util.Log.WARN
40+
kLogLevelError, // 6 = android.util.Log.ERROR
41+
kLogLevelAssert, // 7 = android.util.Log.ASSERT
42+
};
43+
assert(priority <
44+
sizeof(kLogPriorityToLogLevel) / sizeof(kLogPriorityToLogLevel[0]));
45+
assert(priority >= 0);
46+
LogMessage(kLogPriorityToLogLevel[priority], "(%s) %s", ctag.c_str(),
47+
cmsg.c_str());
48+
}
49+
50+
} // namespace firebase

app/src/reference_counted_future_impl.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222
#include <vector>
2323

2424
#include "app/src/cleanup_notifier.h"
25-
#include "app/src/include/firebase/future.h"
2625
#include "app/src/include/firebase/internal/common.h"
2726
#include "app/src/mutex.h"
2827

2928
#if !defined(FIREBASE_NAMESPACE)
3029
#define FIREBASE_NAMESPACE firebase
3130
#endif
3231

32+
#ifdef USE_PLAYBILLING_FUTURE
33+
#include "playbillingclient/future.h"
34+
#else
35+
#include "app/src/include/firebase/future.h"
36+
#endif
37+
3338
namespace FIREBASE_NAMESPACE {
3439

3540
// Predeclarations.

cmake/future_lib.cmake

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright 2019 Google
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
set(FUTURE_LIB_SRC_DIR ${CMAKE_CURRENT_LIST_DIR} CACHE INTERNAL "")
16+
17+
# Defines a Future library for the given namespace
18+
function(define_future_lib CPP_NAMESPACE)
19+
set(library_name "${CPP_NAMESPACE}_future")
20+
21+
# Modify the public Future header with the new namespace
22+
file(READ
23+
${FUTURE_LIB_SRC_DIR}/../app/src/include/firebase/future.h
24+
old_future_header)
25+
string(TOUPPER ${CPP_NAMESPACE} upper_namespace)
26+
set(new_header_guard "FUTURE_INCLUDE_${upper_namespace}_FUTURE_H_")
27+
string(REPLACE
28+
FIREBASE_APP_CLIENT_CPP_SRC_INCLUDE_FIREBASE_FUTURE_H_
29+
${new_header_guard}
30+
future_header_guard_changed
31+
"${old_future_header}")
32+
string(REPLACE
33+
"namespace FIREBASE_NAMESPACE {"
34+
"namespace ${CPP_NAMESPACE} {"
35+
future_header_namespace_changed
36+
"${future_header_guard_changed}")
37+
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/future_include/${CPP_NAMESPACE})
38+
set(generated_future_header_path
39+
"${PROJECT_BINARY_DIR}/future_include/${CPP_NAMESPACE}/future.h")
40+
#TODO Change to use a configure_file, will need to change future.h, and the
41+
# Firebase library to use it as well.
42+
file(WRITE
43+
${generated_future_header_path}
44+
"${future_header_namespace_changed}")
45+
46+
if(ANDROID OR DEFINED ANDROID_NDK)
47+
set(log_SRCS
48+
${FUTURE_LIB_SRC_DIR}/../app/src/log.cc
49+
${FUTURE_LIB_SRC_DIR}/../app/src/log_android.cc)
50+
elseif(IOS OR "${CMAKE_OSX_SYSROOT}" MATCHES "iphoneos")
51+
set(log_SRCS
52+
${FUTURE_LIB_SRC_DIR}/../app/src/log.cc
53+
${FUTURE_LIB_SRC_DIR}/../app/src/log_ios.mm)
54+
else()
55+
set(log_SRCS
56+
${FUTURE_LIB_SRC_DIR}/../app/src/log.cc
57+
${FUTURE_LIB_SRC_DIR}/../app/src/log_stdio.cc)
58+
endif()
59+
60+
add_library("${library_name}" STATIC
61+
${FUTURE_LIB_SRC_DIR}/../app/src/cleanup_notifier.cc
62+
${FUTURE_LIB_SRC_DIR}/../app/src/future_manager.cc
63+
${FUTURE_LIB_SRC_DIR}/../app/src/reference_counted_future_impl.cc
64+
${generated_future_header_path}
65+
${log_SRCS})
66+
67+
target_compile_definitions("${library_name}"
68+
PRIVATE
69+
-DFIREBASE_NAMESPACE=${CPP_NAMESPACE}
70+
)
71+
if("${CPP_NAMESPACE}" STREQUAL "playbillingclient")
72+
target_compile_definitions("${library_name}"
73+
PRIVATE
74+
-DUSE_PLAYBILLING_FUTURE=1
75+
)
76+
endif()
77+
target_include_directories("${library_name}"
78+
PUBLIC
79+
${PROJECT_BINARY_DIR}/future_include
80+
PRIVATE
81+
${FUTURE_LIB_SRC_DIR}/..
82+
${FUTURE_LIB_SRC_DIR}/../app/src/include
83+
)
84+
endfunction()

0 commit comments

Comments
 (0)