Skip to content

Commit c9d01f0

Browse files
cynthiajianga-maurice
authored andcommitted
[OpenSource][CPP][Android] reduce redundant building process
Instead of each gradle project repeatedly build firebase_app in its externalNativeBuild, refer to the already built one from :app project. PiperOrigin-RevId: 297437024
1 parent 5d83ee4 commit c9d01f0

38 files changed

+167
-55
lines changed

CMakeLists.txt

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,43 @@ set (CMAKE_CXX_STANDARD 11)
2020
# Turn on virtual folders for visual studio
2121
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
2222

23+
# Top level option that determines the default behavior of the include options
24+
# below. Useful for turning off all at once, and then turning on a specific one.
25+
option(FIREBASE_INCLUDE_LIBRARY_DEFAULT
26+
"Should each library be included by default." ON)
2327
# Different options to enable/disable each library being included during
2428
# configuration.
25-
option(FIREBASE_INCLUDE_ADMOB "Include the AdMob library." ON)
29+
option(FIREBASE_INCLUDE_ADMOB "Include the AdMob library."
30+
${FIREBASE_INCLUDE_LIBRARY_DEFAULT})
2631
option(FIREBASE_INCLUDE_ANALYTICS
27-
"Include the Google Analytics for Firebase library." ON)
28-
option(FIREBASE_INCLUDE_AUTH "Include the Firebase Authentication library." ON)
32+
"Include the Google Analytics for Firebase library."
33+
${FIREBASE_INCLUDE_LIBRARY_DEFAULT})
34+
option(FIREBASE_INCLUDE_AUTH "Include the Firebase Authentication library."
35+
${FIREBASE_INCLUDE_LIBRARY_DEFAULT})
2936
option(FIREBASE_INCLUDE_DATABASE
30-
"Include the Firebase Realtime Database library." ON)
37+
"Include the Firebase Realtime Database library."
38+
${FIREBASE_INCLUDE_LIBRARY_DEFAULT})
3139
option(FIREBASE_INCLUDE_DYNAMIC_LINKS
32-
"Include the Firebase Dynamic Links library." ON)
40+
"Include the Firebase Dynamic Links library."
41+
${FIREBASE_INCLUDE_LIBRARY_DEFAULT})
3342
option(FIREBASE_INCLUDE_FUNCTIONS
34-
"Include the Cloud Functions for Firebase library." ON)
43+
"Include the Cloud Functions for Firebase library."
44+
${FIREBASE_INCLUDE_LIBRARY_DEFAULT})
3545
option(FIREBASE_INCLUDE_INSTANCE_ID
36-
"Include the Firebase Instance ID library." ON)
46+
"Include the Firebase Instance ID library."
47+
${FIREBASE_INCLUDE_LIBRARY_DEFAULT})
3748
option(FIREBASE_INCLUDE_MESSAGING
38-
"Include the Firebase Cloud Messaging library." ON)
49+
"Include the Firebase Cloud Messaging library."
50+
${FIREBASE_INCLUDE_LIBRARY_DEFAULT})
3951
option(FIREBASE_INCLUDE_REMOTE_CONFIG
40-
"Include the Firebase Remote Config library." ON)
52+
"Include the Firebase Remote Config library."
53+
${FIREBASE_INCLUDE_LIBRARY_DEFAULT})
4154
option(FIREBASE_INCLUDE_STORAGE
42-
"Include the Cloud Storage for Firebase library." ON)
43-
option(FIREBASE_CPP_BUILD_TESTS
44-
"Enable the Firebase C++ Build Tests." OFF)
45-
option(FIREBASE_FORCE_FAKE_SECURE_STORAGE
46-
"Disable use of platform secret store and use fake impl." OFF)
47-
option(FIREBASE_CPP_BUILD_PACKAGE
48-
"Bundle the Firebase C++ libraries into a zip file." OFF)
55+
"Include the Cloud Storage for Firebase library."
56+
${FIREBASE_INCLUDE_LIBRARY_DEFAULT})
57+
58+
option(FIREBASE_CPP_USE_PRIOR_GRADLE_BUILD
59+
"When building with Gradle, use the previously built libraries." OFF)
4960

5061
# Define this directory to be the root of the C++ SDK, which the libraries can
5162
# then refer to.
@@ -94,10 +105,18 @@ set(FIREBASE_BINARY_DIR ${PROJECT_BINARY_DIR})
94105
set(FIREBASE_INSTALL_DIR ${PROJECT_BINARY_DIR}/opt)
95106
set(FIREBASE_DOWNLOAD_DIR ${PROJECT_BINARY_DIR}/downloads)
96107

97-
# Run the CMake build logic that will download all the external dependencies.
98-
message(STATUS "Downloading external project dependencies...")
99-
download_external_sources()
100-
message(STATUS "Download complete.")
108+
if(FIREBASE_CPP_USE_PRIOR_GRADLE_BUILD)
109+
# Figure out where app's binary_dir was.
110+
string(REGEX REPLACE
111+
"${CMAKE_CURRENT_LIST_DIR}/[^/]+/(.*)"
112+
"${CMAKE_CURRENT_LIST_DIR}/app/\\1"
113+
APP_BINARY_DIR "${FIREBASE_BINARY_DIR}")
114+
else()
115+
# Run the CMake build logic that will download all the external dependencies.
116+
message(STATUS "Downloading external project dependencies...")
117+
download_external_sources()
118+
message(STATUS "Download complete.")
119+
endif()
101120

102121
# Disable the Flatbuffer build tests, install and flathash
103122
set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "")
@@ -109,9 +128,13 @@ if(IOS OR ANDROID)
109128
set(FLATBUFFERS_BUILD_FLATC OFF CACHE BOOL "")
110129
endif()
111130

112-
# Add flatbuffers as a subdirectory, and set the directory variables for it,
113-
# so that the sub Firebase projects can depend upon it if necessary.
114-
add_external_library(flatbuffers)
131+
if(FIREBASE_CPP_USE_PRIOR_GRADLE_BUILD)
132+
message(STATUS "flatbuffers is added with APP_BINARY_DIR ${APP_BINARY_DIR}")
133+
add_external_library(flatbuffers BINARY_DIR "${APP_BINARY_DIR}")
134+
else()
135+
message(STATUS "flatbuffers is added normally")
136+
add_external_library(flatbuffers)
137+
endif()
115138

116139
if(FIREBASE_CPP_BUILD_TESTS)
117140
add_external_library(googletest)
@@ -290,8 +313,24 @@ if(FIREBASE_CPP_BUILD_TESTS)
290313
add_subdirectory(testing)
291314
endif()
292315

293-
# App needs to come first, since other libraries will depend upon it.
294-
add_subdirectory(app)
316+
if(NOT FIREBASE_CPP_USE_PRIOR_GRADLE_BUILD)
317+
add_subdirectory(app)
318+
else()
319+
# Add firebase_app as a target on the previously built app.
320+
add_library(firebase_app STATIC IMPORTED GLOBAL)
321+
file(MAKE_DIRECTORY "${APP_BINARY_DIR}/generated")
322+
file(MAKE_DIRECTORY "${FIREBASE_BINARY_DIR}/generated")
323+
set(app_include_dirs
324+
"${CMAKE_CURRENT_LIST_DIR}/app/src/include"
325+
"${APP_BINARY_DIR}/generated"
326+
"${FIREBASE_BINARY_DIR}/generated"
327+
)
328+
set_target_properties(firebase_app PROPERTIES
329+
IMPORTED_LOCATION "${APP_BINARY_DIR}/libfirebase_app.a"
330+
INTERFACE_INCLUDE_DIRECTORIES "${app_include_dirs}"
331+
)
332+
endif()
333+
295334
if (FIREBASE_INCLUDE_ADMOB)
296335
add_subdirectory(admob)
297336
endif()

admob/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ android {
6262
externalNativeBuild {
6363
cmake {
6464
targets 'firebase_admob'
65+
// Args are: Re-use app library prebuilt by app gradle project.
66+
// Don't configure all the cmake subprojects.
67+
// Only include needed project.
68+
arguments '-DFIREBASE_CPP_USE_PRIOR_GRADLE_BUILD=ON',
69+
'-DFIREBASE_INCLUDE_LIBRARY_DEFAULT=OFF',
70+
'-DFIREBASE_INCLUDE_ADMOB=ON'
6571
}
6672
}
6773
}

analytics/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ android {
6262
externalNativeBuild {
6363
cmake {
6464
targets 'firebase_analytics'
65+
// Args are: Re-use app library prebuilt by app gradle project.
66+
// Don't configure all the cmake subprojects.
67+
// Only include needed project.
68+
arguments '-DFIREBASE_CPP_USE_PRIOR_GRADLE_BUILD=ON',
69+
'-DFIREBASE_INCLUDE_LIBRARY_DEFAULT=OFF',
70+
'-DFIREBASE_INCLUDE_ANALYTICS=ON'
6571
}
6672
}
6773
}

android_build_files/generate_proguard.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def generateFinalProguard(Set<File> proguardSet, String outputProguard) {
6464
def defineGenerateProguardFile(String subproject, String buildType,
6565
Action<File> callback) {
6666
Task t = tasks.getByName("externalNativeBuild$buildType").with {
67-
String outputProguard = "$buildDir/${subproject}.pro"
67+
String outputProguard = "$buildDir/$buildType/${subproject}.pro"
6868

6969
outputs.file "$outputProguard"
7070
doLast {

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ android {
6262
externalNativeBuild {
6363
cmake {
6464
targets 'firebase_app'
65+
// Don't configure all the cmake subprojects.
66+
arguments '-DFIREBASE_INCLUDE_LIBRARY_DEFAULT=OFF'
6567
}
6668
}
6769
}

auth/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ android {
6262
externalNativeBuild {
6363
cmake {
6464
targets 'firebase_auth'
65+
// Args are: Re-use app library prebuilt by app gradle project.
66+
// Don't configure all the cmake subprojects.
67+
// Only include needed project.
68+
arguments '-DFIREBASE_CPP_USE_PRIOR_GRADLE_BUILD=ON',
69+
'-DFIREBASE_INCLUDE_LIBRARY_DEFAULT=OFF',
70+
'-DFIREBASE_INCLUDE_AUTH=ON'
6571
}
6672
}
6773
}

cmake/external_rules.cmake

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,17 @@ endfunction()
9191
# not already a valid directory.
9292
# Adds the source directory as a subdirectory if a CMakeLists file is found.
9393
function(add_external_library NAME)
94+
cmake_parse_arguments(optional "" "BINARY_DIR" "" ${ARGN})
95+
96+
if(optional_BINARY_DIR)
97+
set(BINARY_DIR "${optional_BINARY_DIR}")
98+
else()
99+
set(BINARY_DIR "${FIREBASE_BINARY_DIR}")
100+
endif()
101+
94102
string(TOUPPER ${NAME} UPPER_NAME)
95103
if (NOT EXISTS ${${UPPER_NAME}_SOURCE_DIR})
96-
set(${UPPER_NAME}_SOURCE_DIR "${FIREBASE_BINARY_DIR}/external/src/${NAME}")
104+
set(${UPPER_NAME}_SOURCE_DIR "${BINARY_DIR}/external/src/${NAME}")
97105
set(${UPPER_NAME}_SOURCE_DIR "${${UPPER_NAME}_SOURCE_DIR}" PARENT_SCOPE)
98106
endif()
99107

@@ -102,8 +110,11 @@ function(add_external_library NAME)
102110
set(${UPPER_NAME}_BINARY_DIR "${${UPPER_NAME}_BINARY_DIR}" PARENT_SCOPE)
103111
endif()
104112

113+
message(STATUS "add_ext... ${UPPER_NAME}_SOURCE_DIR: ${${UPPER_NAME}_SOURCE_DIR}")
114+
message(STATUS "add_ext... ${UPPER_NAME}_BINARY_DIR: ${${UPPER_NAME}_BINARY_DIR}")
115+
105116
if (EXISTS "${${UPPER_NAME}_SOURCE_DIR}/CMakeLists.txt")
106117
add_subdirectory(${${UPPER_NAME}_SOURCE_DIR} ${${UPPER_NAME}_BINARY_DIR}
107-
EXCLUDE_FROM_ALL)
118+
EXCLUDE_FROM_ALL)
108119
endif()
109120
endfunction()

cmake/firebase_cpp_gradle.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function(firebase_cpp_proguard_file LIBRARY_NAME)
4444
string(TOUPPER "${LIBRARY_NAME}" upper_name)
4545
set(proguard_var "FIREBASE_CPP_${upper_name}_PROGUARD")
4646
set(${proguard_var}
47-
"${FIREBASE_SOURCE_DIR}/${LIBRARY_NAME}/build/${LIBRARY_NAME}.pro"
47+
"${FIREBASE_SOURCE_DIR}/${LIBRARY_NAME}/build/${CMAKE_BUILD_TYPE}/${LIBRARY_NAME}.pro"
4848
CACHE FILEPATH "Proguard file for ${LIBRARY_NAME}" FORCE)
4949

5050
firebase_cpp_gradle(":${LIBRARY_NAME}:externalNativeBuildRelease"

database/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ android {
6262
externalNativeBuild {
6363
cmake {
6464
targets 'firebase_database'
65+
// Args are: Re-use app library prebuilt by app gradle project.
66+
// Don't configure all the cmake subprojects.
67+
// Only include needed project.
68+
arguments '-DFIREBASE_CPP_USE_PRIOR_GRADLE_BUILD=ON',
69+
'-DFIREBASE_INCLUDE_LIBRARY_DEFAULT=OFF',
70+
'-DFIREBASE_INCLUDE_DATABASE=ON'
6571
}
6672
}
6773
}

dynamic_links/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ android {
6262
externalNativeBuild {
6363
cmake {
6464
targets 'firebase_dynamic_links'
65+
// Args are: Re-use app library prebuilt by app gradle project.
66+
// Don't configure all the cmake subprojects.
67+
// Only include needed project.
68+
arguments '-DFIREBASE_CPP_USE_PRIOR_GRADLE_BUILD=ON',
69+
'-DFIREBASE_INCLUDE_LIBRARY_DEFAULT=OFF',
70+
'-DFIREBASE_INCLUDE_DYNAMIC_LINKS=ON'
6571
}
6672
}
6773
}

0 commit comments

Comments
 (0)