Skip to content

Commit 5c15a6e

Browse files
committed
Add a way for users to provide local directories for external libraries
Allow the users to provide their own directories for all the external libraries that are downloaded as part of the build process, by overriding name_SOURCE_DIR and name_BINARY_DIR. PiperOrigin-RevId: 246938364
1 parent c3c8f74 commit 5c15a6e

15 files changed

+114
-73
lines changed

CMakeLists.txt

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "")
8888

8989
# Add flatbuffers as a subdirectory, and set the directory variables for it,
9090
# so that the sub Firebase projects can depend upon it if necessary.
91-
add_external_subdirectory(flatbuffers)
92-
populate_external_source_vars(flatbuffers)
91+
add_external_library(flatbuffers)
9392

9493
# Some of the external libraries are not used for mobile.
9594
if (NOT ANDROID AND NOT IOS)
@@ -98,18 +97,15 @@ if (NOT ANDROID AND NOT IOS)
9897
if (WIN32)
9998
set(CMAKE_USE_WINSSL ON CACHE BOOL "")
10099
endif()
101-
add_external_subdirectory(curl)
102-
populate_external_source_vars(curl)
100+
add_external_library(curl)
103101

104-
add_external_subdirectory(libuv)
105-
populate_external_source_vars(libuv)
102+
add_external_library(libuv)
106103

107104
find_package(OpenSSL)
108105

109-
add_external_subdirectory(zlib)
110-
populate_external_source_vars(zlib)
106+
add_external_library(zlib)
111107

112-
populate_external_source_vars(uWebSockets)
108+
add_external_library(uWebSockets)
113109
# uWebSockets does not come with a CMakeLists file, so define the target.
114110
# Note that since it depends on OpenSSL, only do so if that was found.
115111
if (OPENSSL_FOUND)
@@ -118,18 +114,18 @@ if (NOT ANDROID AND NOT IOS)
118114
else()
119115
# Epoll is only used on Linux, otherwise LibUV is used.
120116
set(uWebSockets_extra_src
121-
${uWebSockets_SOURCE_DIR}/src/Epoll.cpp)
117+
${UWEBSOCKETS_SOURCE_DIR}/src/Epoll.cpp)
122118
endif()
123119
add_library(libuWS STATIC
124120
${uWebSockets_extra_src}
125-
${uWebSockets_SOURCE_DIR}/src/Extensions.cpp
126-
${uWebSockets_SOURCE_DIR}/src/Group.cpp
127-
${uWebSockets_SOURCE_DIR}/src/HTTPSocket.cpp
128-
${uWebSockets_SOURCE_DIR}/src/Hub.cpp
129-
${uWebSockets_SOURCE_DIR}/src/Networking.cpp
130-
${uWebSockets_SOURCE_DIR}/src/Node.cpp
131-
${uWebSockets_SOURCE_DIR}/src/Socket.cpp
132-
${uWebSockets_SOURCE_DIR}/src/WebSocket.cpp)
121+
${UWEBSOCKETS_SOURCE_DIR}/src/Extensions.cpp
122+
${UWEBSOCKETS_SOURCE_DIR}/src/Group.cpp
123+
${UWEBSOCKETS_SOURCE_DIR}/src/HTTPSocket.cpp
124+
${UWEBSOCKETS_SOURCE_DIR}/src/Hub.cpp
125+
${UWEBSOCKETS_SOURCE_DIR}/src/Networking.cpp
126+
${UWEBSOCKETS_SOURCE_DIR}/src/Node.cpp
127+
${UWEBSOCKETS_SOURCE_DIR}/src/Socket.cpp
128+
${UWEBSOCKETS_SOURCE_DIR}/src/WebSocket.cpp)
133129
if(MSVC)
134130
set(websockets_additional_defines
135131
-DWIN32_LEAN_AND_MEAN # Ensure that windows doesn't include winsock.h by
@@ -149,10 +145,10 @@ if (NOT ANDROID AND NOT IOS)
149145
)
150146
target_include_directories(libuWS
151147
PUBLIC
152-
${libuv_SOURCE_DIR}/include
153-
${uWebSockets_SOURCE_DIR}
154-
${zlib_SOURCE_DIR}
155-
${zlib_BINARY_DIR}
148+
${LIBUV_SOURCE_DIR}/include
149+
${UWEBSOCKETS_SOURCE_DIR}
150+
${ZLIB_SOURCE_DIR}
151+
${ZLIB_BINARY_DIR}
156152
PRIVATE
157153
${OPENSSL_INCLUDE_DIR}
158154
)
@@ -167,19 +163,18 @@ if (NOT ANDROID AND NOT IOS)
167163
find_package(Protobuf)
168164
if (PROTOBUF_FOUND)
169165
# NanoPB requires Protobuf to be present, so only add it if it was found.
170-
add_external_subdirectory(nanopb)
171-
populate_external_source_vars(nanopb)
166+
add_external_library(nanopb)
172167
# NanoPB has a FindNanopb which defines the function to generate files, so
173168
# add it to the module path, and use that.
174-
list(INSERT CMAKE_MODULE_PATH 0 ${nanopb_SOURCE_DIR}/extra)
169+
list(INSERT CMAKE_MODULE_PATH 0 ${NANOPB_SOURCE_DIR}/extra)
175170
find_package(Nanopb)
176171
endif()
177172
endif()
178173

179174
if(ANDROID OR IOS)
180175
# Mobile platforms build flatc externally so that it works on the platform
181176
# performing the build.
182-
set(firebase_external_flatc_build_dir "${flatbuffers_BINARY_DIR}-flatc")
177+
set(firebase_external_flatc_build_dir "${FLATBUFFERS_BINARY_DIR}-flatc")
183178
set(firebase_external_flatc "${firebase_external_flatc_build_dir}/flatc")
184179

185180
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
@@ -224,7 +219,7 @@ if(ANDROID OR IOS)
224219
add_custom_command(
225220
OUTPUT ${firebase_external_flatc}
226221
COMMAND cd ${firebase_external_flatc_build_dir} ${COMMAND_CONCAT}
227-
${ENV_COMMAND} cmake ${flatbuffers_SOURCE_DIR} ${COMMAND_CONCAT}
222+
${ENV_COMMAND} cmake ${FLATBUFFERS_SOURCE_DIR} ${COMMAND_CONCAT}
228223
${ENV_COMMAND} cmake --build . --target flatc
229224
COMMENT "Building flatc (the FlatBuffer schema compiler)")
230225

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,34 @@ cmake -G “Visual Studio 15 2017” ..
119119
More information on generators can be found at
120120
<https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html>.
121121

122+
By default, when building the SDK, the CMake process will download any third
123+
party dependencies that are needed for the build. This logic is in
124+
[cmake/external_rules.cmake](/cmake/external_rules.cmake), and the accompanying
125+
[cmake/external/CMakeLists.txt](/cmake/external/CMakeLists.txt). If you would
126+
like to provide your own directory for these dependencies, you can override
127+
`[[dependency_name]]_SOURCE_DIR` and `[[dependency_name]]_BINARY_DIR`. If the
128+
binary directory is not provided, it defaults to the given source directory,
129+
appended with `-build`.
130+
131+
For example, to provide a custom flatbuffer directory you could run:
132+
133+
``` bash
134+
cmake -DFLATBUFFERS_SOURCE_DIR=/tmp/flatbuffers ..
135+
```
136+
137+
And the binary directory would automatically be set to `/tmp/flatbuffers-build`.
138+
139+
Currently, the third party libraries that can be provided this way are:
140+
141+
| Library |
142+
| ------- |
143+
| CURL |
144+
| FLATBUFFERS |
145+
| LIBUV |
146+
| NANOPB |
147+
| UWEBSOCKETS |
148+
| ZLIB |
149+
122150
#### Building with CMake for iOS
123151
The Firebase C++ SDK comes with a CMake config file to build the library for
124152
iOS platforms, [cmake/ios.cmake](/cmake/ios.cmake). In order to build with it,

app/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ target_include_directories(firebase_app
248248
${FIREBASE_GEN_FILE_DIR}
249249
PRIVATE
250250
${FIREBASE_CPP_SDK_ROOT_DIR}
251-
${flatbuffers_SOURCE_DIR}/include
251+
${FLATBUFFERS_SOURCE_DIR}/include
252252
)
253253
target_compile_definitions(firebase_app
254254
PRIVATE

app/rest/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ add_library(firebase_rest_lib STATIC
4242
target_include_directories(firebase_rest_lib
4343
PUBLIC
4444
${FIREBASE_CPP_SDK_ROOT_DIR}
45-
${flatbuffers_SOURCE_DIR}/include
46-
${zlib_SOURCE_DIR}/..
47-
${zlib_BINARY_DIR}
45+
${FLATBUFFERS_SOURCE_DIR}/include
46+
${ZLIB_SOURCE_DIR}/..
47+
${ZLIB_BINARY_DIR}
4848
PRIVATE
49-
${curl_SOURCE_DIR}/include
49+
${CURL_SOURCE_DIR}/include
5050
)
5151

5252
if(MSVC)

auth/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ if(ANDROID OR IOS)
167167
set(additional_target_definitions)
168168
else()
169169
set(additional_include_DIR
170-
${flatbuffers_SOURCE_DIR}/include
170+
${FLATBUFFERS_SOURCE_DIR}/include
171171
${LIBSECRET_INCLUDE_DIRS})
172172
set(additional_link_LIB
173173
firebase_rest_lib

cmake/external/curl.cmake

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
include(ExternalProject)
1616

17-
if(TARGET curl)
17+
if(TARGET curl OR NOT DOWNLOAD_CURL)
1818
return()
1919
endif()
2020

21-
# TODO(b/117950963): Use a system- or user-supplied curl if available
22-
2321
ExternalProject_Add(
2422
curl
2523

@@ -32,4 +30,4 @@ ExternalProject_Add(
3230
BUILD_COMMAND ""
3331
INSTALL_COMMAND ""
3432
TEST_COMMAND ""
35-
)
33+
)

cmake/external/flatbuffers.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
include(ExternalProject)
1616

17-
if(TARGET flatbuffers)
17+
if(TARGET flatbuffers OR NOT DOWNLOAD_FLATBUFFERS)
1818
return()
1919
endif()
2020

cmake/external/libuv.cmake

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
include(ExternalProject)
1616

17-
if(TARGET libuv)
17+
if(TARGET libuv OR NOT DOWNLOAD_LIBUV)
1818
return()
1919
endif()
2020

21-
# TODO(b/117950963): Use a system- or user-supplied libuv if available
22-
2321
ExternalProject_Add(
2422
libuv
2523

@@ -32,4 +30,4 @@ ExternalProject_Add(
3230
BUILD_COMMAND ""
3331
INSTALL_COMMAND ""
3432
TEST_COMMAND ""
35-
)
33+
)

cmake/external/nanopb.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
include(ExternalProject)
1616

17-
if(TARGET nanopb)
17+
if(TARGET nanopb OR NOT DOWNLOAD_NANOPB)
1818
return()
1919
endif()
2020

@@ -31,4 +31,4 @@ ExternalProject_Add(
3131
BUILD_COMMAND ""
3232
INSTALL_COMMAND ""
3333
TEST_COMMAND ""
34-
)
34+
)

cmake/external/uWebSockets.cmake

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
include(ExternalProject)
1616

17-
if(TARGET uWebSockets)
17+
if(TARGET uWebSockets OR NOT DOWNLOAD_UWEBSOCKETS)
1818
return()
1919
endif()
2020

21-
# TODO(b/117950963): Use a system- or user-supplied openssl if available
22-
2321
ExternalProject_Add(
2422
uWebSockets
2523

@@ -32,4 +30,4 @@ ExternalProject_Add(
3230
BUILD_COMMAND ""
3331
INSTALL_COMMAND ""
3432
TEST_COMMAND ""
35-
)
33+
)

cmake/external/zlib.cmake

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
include(ExternalProject)
1616

17-
if(TARGET zlib)
17+
if(TARGET zlib OR NOT DOWNLOAD_ZLIB)
1818
return()
1919
endif()
2020

21-
# TODO(b/117950963): Use a system- or user-supplied zlib if available
22-
2321
ExternalProject_Add(
2422
zlib
2523

@@ -34,4 +32,4 @@ ExternalProject_Add(
3432
BUILD_COMMAND ""
3533
INSTALL_COMMAND ""
3634
TEST_COMMAND ""
37-
)
35+
)

cmake/external_rules.cmake

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,35 @@ function(download_external_sources)
3636
else()
3737
set(external_platform DESKTOP)
3838
endif()
39-
39+
40+
# Set variables to indicate if local versions of third party libraries should
41+
# be used instead of downloading them.
42+
function(check_use_local_directory NAME)
43+
if (EXISTS ${${NAME}_SOURCE_DIR})
44+
set(DOWNLOAD_${NAME} OFF PARENT_SCOPE)
45+
else()
46+
set(DOWNLOAD_${NAME} ON PARENT_SCOPE)
47+
endif()
48+
endfunction()
49+
check_use_local_directory(CURL)
50+
check_use_local_directory(FLATBUFFERS)
51+
check_use_local_directory(LIBUV)
52+
check_use_local_directory(NANOPB)
53+
check_use_local_directory(UWEBSOCKETS)
54+
check_use_local_directory(ZLIB)
55+
4056
execute_process(
4157
COMMAND
4258
${ENV_COMMAND} cmake
4359
-DCMAKE_INSTALL_PREFIX=${FIREBASE_INSTALL_DIR}
4460
-DFIREBASE_DOWNLOAD_DIR=${FIREBASE_DOWNLOAD_DIR}
4561
-DFIREBASE_EXTERNAL_PLATFORM=${external_platform}
62+
-DDOWNLOAD_CURL=${DOWNLOAD_CURL}
63+
-DDOWNLOAD_FLATBUFFERS=${DOWNLOAD_FLATBUFFERS}
64+
-DDOWNLOAD_LIBUV=${DOWNLOAD_LIBUV}
65+
-DDOWNLOAD_NANOPB=${DOWNLOAD_NANOPB}
66+
-DDOWNLOAD_UWEBSOCKETS=${DOWNLOAD_UWEBSOCKETS}
67+
-DDOWNLOAD_ZLIB=${DOWNLOAD_ZLIB}
4668
${PROJECT_SOURCE_DIR}/cmake/external
4769
OUTPUT_FILE ${PROJECT_BINARY_DIR}/external/output_cmake_config.txt
4870
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/external
@@ -60,20 +82,24 @@ function(download_external_sources)
6082
)
6183
endfunction()
6284

63-
# Populates directory variables for the given name, to the location that name
64-
# would be in after a call to download_external_sources.
65-
function(populate_external_source_vars NAME)
66-
set(${NAME}_SOURCE_DIR "${FIREBASE_BINARY_DIR}/external/src/${NAME}"
67-
PARENT_SCOPE)
68-
set(${NAME}_BINARY_DIR "${FIREBASE_BINARY_DIR}/external/src/${NAME}-build"
69-
PARENT_SCOPE)
70-
endfunction()
85+
# Populates directory variables for the given name to the location that name
86+
# would be in after a call to download_external_sources, if the variable is
87+
# not already a valid directory.
88+
# Adds the source directory as a subdirectory if a CMakeLists file is found.
89+
function(add_external_library NAME)
90+
string(TOUPPER ${NAME} UPPER_NAME)
91+
if (NOT EXISTS ${${UPPER_NAME}_SOURCE_DIR})
92+
set(${UPPER_NAME}_SOURCE_DIR "${FIREBASE_BINARY_DIR}/external/src/${NAME}")
93+
set(${UPPER_NAME}_SOURCE_DIR "${${UPPER_NAME}_SOURCE_DIR}" PARENT_SCOPE)
94+
endif()
7195

72-
# Adds the given library's location as a subdirectory that the caller uses.
73-
function(add_external_subdirectory NAME)
74-
add_subdirectory(
75-
${FIREBASE_BINARY_DIR}/external/src/${NAME}
76-
${FIREBASE_BINARY_DIR}/external/src/${NAME}-build
77-
EXCLUDE_FROM_ALL
78-
)
79-
endfunction()
96+
if (NOT EXISTS ${${UPPER_NAME}_BINARY_DIR})
97+
set(${UPPER_NAME}_BINARY_DIR "${${UPPER_NAME}_SOURCE_DIR}-build")
98+
set(${UPPER_NAME}_BINARY_DIR "${${UPPER_NAME}_BINARY_DIR}" PARENT_SCOPE)
99+
endif()
100+
101+
if (EXISTS "${${UPPER_NAME}_SOURCE_DIR}/CMakeLists.txt")
102+
add_subdirectory(${${UPPER_NAME}_SOURCE_DIR} ${${UPPER_NAME}_BINARY_DIR}
103+
EXCLUDE_FROM_ALL)
104+
endif()
105+
endfunction()

database/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ if(ANDROID OR IOS)
134134
set(additional_DEFINES)
135135
else()
136136
set(additional_include_DIR
137-
${flatbuffers_SOURCE_DIR}/include
137+
${FLATBUFFERS_SOURCE_DIR}/include
138138
${OPENSSL_INCLUDE_DIR}
139-
${uWebSockets_SOURCE_DIR}/..)
139+
${UWEBSOCKETS_SOURCE_DIR}/..)
140140

141141
set(additional_link_LIB
142142
flatbuffers

messaging/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ target_include_directories(firebase_messaging
9797
if(ANDROID)
9898
target_include_directories(firebase_messaging
9999
PRIVATE
100-
${flatbuffers_SOURCE_DIR}/include
100+
${FLATBUFFERS_SOURCE_DIR}/include
101101
)
102102
endif()
103103
target_compile_definitions(firebase_messaging

remote_config/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ if(ANDROID OR IOS OR use_stub)
7676
set(additional_link_LIB)
7777
else()
7878
set(additional_include_DIR
79-
${flatbuffers_SOURCE_DIR}/include
79+
${FLATBUFFERS_SOURCE_DIR}/include
8080
${NANOPB_INCLUDE_DIRS}
8181
${PROJECT_BINARY_DIR}/..)
8282
set(additional_link_LIB

0 commit comments

Comments
 (0)