Skip to content

Commit d4e86ba

Browse files
authored
Fixes for compatibility with builds in firebase-cpp-sdk (#4984)
* Don't modify CMAKE_CXX_FLAGS * Avoid conflict on RANDOM_FILE * Only conditionally build tests * Remove alias provided by the upstream build * Set SOURCE_DIR and BINARY_DIR for each external library * Compute repo root without git This supports building Firestore from a source archive * Make empty files C++ This makes it possible to compile with a single set of flags for every cc_library.
1 parent 54fb849 commit d4e86ba

File tree

5 files changed

+93
-80
lines changed

5 files changed

+93
-80
lines changed

CMakeLists.txt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ endif()
6060
enable_language(C)
6161
enable_language(CXX)
6262

63+
option(
64+
FIREBASE_IOS_BUILD_TESTS
65+
"Enable building of C++ and Objective-C tests for this project"
66+
ON
67+
)
68+
6369

6470
list(INSERT CMAKE_MODULE_PATH 0 ${PROJECT_SOURCE_DIR}/cmake)
6571
include(compiler_id)
@@ -140,6 +146,12 @@ endif()
140146

141147

142148
# gRPC
149+
150+
# libcurl and c-ares conflict in their usage of this variable. Prevent
151+
# libcurl's setting of this variable from affecting the c-ares build that's
152+
# pulled in indirectly via gRPC.
153+
unset(RANDOM_FILE CACHE)
154+
143155
find_package(OpenSSL QUIET)
144156
if(OPENSSL_FOUND)
145157
set(gRPC_SSL_PROVIDER package CACHE STRING "Use external OpenSSL")
@@ -187,7 +199,6 @@ else()
187199
)
188200
endif()
189201

190-
add_alias(protobuf::libprotobuf libprotobuf)
191202
if(CXX_CLANG OR CXX_GNU)
192203
target_compile_options(
193204
libprotobuf
@@ -245,7 +256,9 @@ if(APPLE)
245256
endif()
246257

247258

248-
enable_testing()
259+
if(FIREBASE_IOS_BUILD_TESTS)
260+
enable_testing()
261+
endif()
249262
include(compiler_setup)
250263

251264
add_subdirectory(FirebaseCore)

cmake/cc_rules.cmake

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function(cc_library name)
4545
${FIREBASE_SOURCE_DIR}
4646
)
4747

48+
target_compile_options(${name} PRIVATE ${FIREBASE_CXX_FLAGS})
4849
target_link_libraries(${name} PUBLIC ${ccl_DEPENDS})
4950

5051
if(ccl_EXCLUDE_FROM_ALL)
@@ -135,6 +136,10 @@ endfunction()
135136
# Defines a new test executable target with the given target name, sources, and
136137
# dependencies. Implicitly adds DEPENDS on GTest::GTest and GTest::Main.
137138
function(cc_test name)
139+
if(NOT FIREBASE_IOS_BUILD_TESTS)
140+
return()
141+
endif()
142+
138143
set(multi DEPENDS SOURCES)
139144
cmake_parse_arguments(cct "" "" "${multi}" ${ARGN})
140145

@@ -184,6 +189,8 @@ function(cc_fuzz_test name)
184189
DEPENDS ${ccf_DEPENDS}
185190
)
186191

192+
target_compile_options(${name} PRIVATE ${FIREBASE_CXX_FLAGS})
193+
187194
# Copy the dictionary file and corpus directory, if they are defined.
188195
if(DEFINED ccf_DICTIONARY)
189196
add_custom_command(
@@ -314,6 +321,7 @@ function(objc_framework target)
314321
INTERFACE
315322
-F${CMAKE_CURRENT_BINARY_DIR}
316323
PRIVATE
324+
${FIREBASE_CXX_FLAGS}
317325
${OBJC_FLAGS}
318326
-fno-autolink
319327
-Wno-unused-parameter
@@ -356,56 +364,55 @@ function(objc_framework target)
356364
endfunction()
357365

358366
function(objc_test target)
359-
if(APPLE)
360-
set(flag EXCLUDE_FROM_ALL)
361-
set(single HOST VERSION WORKING_DIRECTORY)
362-
set(multi DEPENDS DEFINES HEADERS INCLUDES SOURCES)
363-
cmake_parse_arguments(ot "${flag}" "${single}" "${multi}" ${ARGN})
367+
if(NOT APPLE OR NOT FIREBASE_IOS_BUILD_TESTS)
368+
return()
369+
endif()
364370

365-
xctest_add_bundle(
366-
${target}
367-
${ot_HOST}
368-
${ot_SOURCES}
369-
)
371+
set(flag EXCLUDE_FROM_ALL)
372+
set(single HOST VERSION WORKING_DIRECTORY)
373+
set(multi DEPENDS DEFINES HEADERS INCLUDES SOURCES)
374+
cmake_parse_arguments(ot "${flag}" "${single}" "${multi}" ${ARGN})
375+
376+
xctest_add_bundle(
377+
${target}
378+
${ot_HOST}
379+
${ot_SOURCES}
380+
)
370381

371-
add_objc_flags(
372-
${target}
373-
${ot_SOURCES}
374-
)
382+
add_objc_flags(
383+
${target}
384+
${ot_SOURCES}
385+
)
375386

376-
target_link_libraries(
377-
${target}
378-
PRIVATE
379-
${ot_DEPENDS}
380-
)
387+
target_compile_options(${target} PRIVATE ${FIREBASE_CXX_FLAGS})
388+
target_link_libraries(${target} PRIVATE ${ot_DEPENDS})
381389

382-
xctest_add_test(
383-
${target}
384-
${target}
385-
)
390+
xctest_add_test(
391+
${target}
392+
${target}
393+
)
386394

387-
if(ot_WORKING_DIRECTORY)
388-
set_property(
389-
TEST ${target} PROPERTY
390-
WORKING_DIRECTORY ${ot_WORKING_DIRECTORY}
391-
)
392-
endif()
395+
if(ot_WORKING_DIRECTORY)
396+
set_property(
397+
TEST ${target} PROPERTY
398+
WORKING_DIRECTORY ${ot_WORKING_DIRECTORY}
399+
)
400+
endif()
393401

394-
if(APPLE AND WITH_ASAN)
395-
set_property(
396-
TEST ${target} APPEND PROPERTY
397-
ENVIRONMENT
398-
DYLD_INSERT_LIBRARIES=${CLANG_ASAN_DYLIB}
399-
)
400-
endif()
402+
if(WITH_ASAN)
403+
set_property(
404+
TEST ${target} APPEND PROPERTY
405+
ENVIRONMENT
406+
DYLD_INSERT_LIBRARIES=${CLANG_ASAN_DYLIB}
407+
)
408+
endif()
401409

402-
if(APPLE AND WITH_TSAN)
403-
set_property(
404-
TEST ${target} APPEND PROPERTY
405-
ENVIRONMENT
406-
DYLD_INSERT_LIBRARIES=${CLANG_TSAN_DYLIB}
407-
)
408-
endif()
410+
if(WITH_TSAN)
411+
set_property(
412+
TEST ${target} APPEND PROPERTY
413+
ENVIRONMENT
414+
DYLD_INSERT_LIBRARIES=${CLANG_TSAN_DYLIB}
415+
)
409416
endif()
410417
endfunction()
411418

@@ -416,14 +423,14 @@ endfunction()
416423
#
417424
# Appends the generated source file name to the list named by sources_list.
418425
macro(generate_dummy_source name sources_list)
419-
set(__empty_header_only_file "${CMAKE_CURRENT_BINARY_DIR}/${name}_header_only_empty.c")
426+
set(__empty_header_only_file "${CMAKE_CURRENT_BINARY_DIR}/${name}_header_only_empty.cc")
420427

421428
if(NOT EXISTS ${__empty_header_only_file})
422429
file(WRITE ${__empty_header_only_file}
423430
"// Generated file that keeps header-only CMake libraries happy.
424431
425432
// single meaningless symbol
426-
void ${name}_header_only_fakesym(void) {}
433+
void ${name}_header_only_fakesym() {}
427434
"
428435
)
429436
endif()

cmake/compiler_setup.cmake

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,6 @@ if(MSVC)
116116
)
117117
endif()
118118

119-
foreach(flag ${common_flags} ${c_flags})
120-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
121-
endforeach()
122-
123-
foreach(flag ${common_flags} ${cxx_flags})
124-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}")
125-
endforeach()
126-
127119
foreach(flag ${common_flags} ${c_flags})
128120
list(APPEND FIREBASE_C_FLAGS ${flag})
129121
endforeach()

cmake/external_rules.cmake

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,22 @@ function(download_external_sources)
3636
endfunction()
3737

3838
function(add_external_subdirectory NAME)
39-
add_subdirectory(
40-
${FIREBASE_BINARY_DIR}/external/src/${NAME}
41-
${FIREBASE_BINARY_DIR}/external/src/${NAME}-build
42-
EXCLUDE_FROM_ALL
43-
)
39+
string(TOUPPER ${NAME} UPPER_NAME)
40+
if (NOT EXISTS ${${UPPER_NAME}_SOURCE_DIR})
41+
set(${UPPER_NAME}_SOURCE_DIR "${FIREBASE_BINARY_DIR}/external/src/${NAME}")
42+
set(${UPPER_NAME}_SOURCE_DIR "${${UPPER_NAME}_SOURCE_DIR}" PARENT_SCOPE)
43+
endif()
44+
45+
if (NOT EXISTS ${${UPPER_NAME}_BINARY_DIR})
46+
set(${UPPER_NAME}_BINARY_DIR "${${UPPER_NAME}_SOURCE_DIR}-build")
47+
set(${UPPER_NAME}_BINARY_DIR "${${UPPER_NAME}_BINARY_DIR}" PARENT_SCOPE)
48+
endif()
49+
50+
if (EXISTS "${${UPPER_NAME}_SOURCE_DIR}/CMakeLists.txt")
51+
add_subdirectory(
52+
${${UPPER_NAME}_SOURCE_DIR}
53+
${${UPPER_NAME}_BINARY_DIR}
54+
EXCLUDE_FROM_ALL
55+
)
56+
endif()
4457
endfunction()

scripts/binary_to_array.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python2
22

3-
# Copyright 2018 Google
3+
# Copyright 2018 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -48,8 +48,6 @@
4848
import logging
4949
import os
5050

51-
from lib import git
52-
5351
arg_parser = argparse.ArgumentParser()
5452

5553
arg_parser.add_argument("input",
@@ -223,26 +221,16 @@ def source(namespaces, array_name, array_size_name, fileid, filename,
223221
return data
224222

225223

226-
def _get_repo_root(filename):
227-
"""Returns the root of the repository containing the given file.
228-
229-
Args:
230-
filename: A source file or directory in the repository.
224+
def _get_repo_root():
225+
"""Returns the root of the source repository.
231226
"""
232227

233-
# CMake builds can be run out of source tree, so use the directory containing
234-
# a source file as the starting point for the calculation.
235-
if os.path.isdir(filename):
236-
dir_in_repo = filename
237-
else:
238-
dir_in_repo = os.path.dirname(filename)
239-
240-
starting_dir = os.getcwd()
228+
scripts_dir = os.path.abspath(os.path.dirname(__file__))
229+
assert os.path.basename(scripts_dir) == 'scripts'
241230

242-
os.chdir(dir_in_repo)
243-
root_dir = git.get_repo_root()
231+
root_dir = os.path.dirname(scripts_dir)
232+
assert os.path.isdir(os.path.join(root_dir, '.github'))
244233

245-
os.chdir(starting_dir)
246234
return root_dir
247235

248236

@@ -265,8 +253,8 @@ def main():
265253
output_header = input_file_base + ".h"
266254
logging.debug("Using default --output_header='%s'", output_header)
267255

256+
root_dir = _get_repo_root()
268257
absolute_dir = path.dirname(output_header)
269-
root_dir = _get_repo_root(absolute_dir)
270258

271259
relative_dir = path.relpath(absolute_dir, root_dir)
272260
relative_header_path = path.join(relative_dir, path.basename(output_header))

0 commit comments

Comments
 (0)