Skip to content

Commit b7d69f9

Browse files
committed
documentation
1 parent 848c6fe commit b7d69f9

File tree

1 file changed

+85
-25
lines changed

1 file changed

+85
-25
lines changed

scripts/build_desktop_app_with_firebase.py

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,56 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16-
16+
"""
17+
Standalone self-sufficient (no external dependencies) python script that can ease
18+
building desktop apps depending on firebase, by either using the firebase cpp
19+
source (firebase-cpp-sdk github repo) or the prebuilt release firebase libraries.
20+
21+
Note that this script works with only Python3 (3.6+).
22+
Known side effects:
23+
If building against firebase cpp source, this script might checkout a specific
24+
branch on github containing vcpkg. This will not be required once vcpkg is in the
25+
main branch.
26+
27+
Example usage:
28+
Let's say we want to build the quickstart cpp example for firebase database.
29+
As specified above, there are 2 options - build against the firebase source or
30+
prebuilt firebase libraries.
31+
32+
# Build against the firebase cpp sdk source
33+
python3 scripts/build_desktop_app_with_firebase.py --app_dir ~/quickstart-cpp/database/testapp
34+
--sdk_dir . --build_dir build_source
35+
36+
(or)
37+
38+
# Build against the prebuilt released firebase libraries
39+
python3 scripts/build_desktop_app_with_firebase.py --app_dir ~/quickstart-cpp/database/testapp
40+
--sdk_dir ~/prebuilt/firebase_cpp_sdk_6.15.1/
41+
--build_dir build_prebuilt
42+
43+
# If the script ran successfully, it will print the path to the build directory.
44+
Build successful!
45+
Please find your executables in build directory: /Users/<user>/quickstart-cpp/database/testapp/build_source
46+
47+
# Running the built example
48+
$ ./Users/<user>/quickstart-cpp/database/testapp/build_source/desktop_testapp
49+
"""
1750
import argparse
18-
import sys
51+
import distutils.spawn
1952
import os
2053
import platform
2154
import subprocess
22-
import distutils.spawn
55+
import sys
2356

24-
def is_path_valid(path):
57+
def is_path_valid_for_cmake(path):
58+
"""Check if specified path is setup for cmake"""
2559
return os.path.exists(os.path.join(path, 'CMakeLists.txt'))
2660

27-
def is_sdk_path_source(path):
61+
def is_sdk_path_source(sdk_dir):
62+
"""Validate if firebase sdk dir is firebase cpp source dir"""
2863
# Not the most reliable way to detect if the sdk path is source or prebuilt but
2964
# should work for our purpose.
30-
return os.path.exists(os.path.join(path, 'build_tools'))
65+
return os.path.exists(os.path.join(sdk_dir, 'build_tools'))
3166

3267
def get_vcpkg_triplet(arch, msvc_runtime_library='static'):
3368
""" Get vcpkg target triplet (platform definition).
@@ -62,37 +97,47 @@ def get_vcpkg_triplet(arch, msvc_runtime_library='static'):
6297
return triplet_name
6398

6499
def build_source_vcpkg_dependencies(sdk_source_dir, arch, msvc_runtime_library):
65-
# TODO: Remove this once dev has been merged onto master
66-
# This is required because vcpkg lives only in dev branch right now.
67-
subprocess.run(['git', 'checkout', 'feature/python-tool-build-apps-with-firebase'], cwd=sdk_source_dir)
68-
subprocess.run(['git', 'pull'], cwd=sdk_source_dir)
100+
"""Build C++ dependencies for firebase source SDK using vcpkg.
101+
102+
Args:
103+
sdk_source_dir (str): Path to Firebase C++ source directory.
104+
arch (str): Platform Architecture (example: 'x64').
105+
msvc_runtime_library (str): Runtime library for MSVC (eg: 'static', 'dynamic').
106+
"""
107+
# TODO: Remove this once dev branch of firebase-cpp-sdk repo has been merged
108+
# onto main branch. This is required because vcpkg lives only in dev branch currently.
109+
subprocess.run(['git', 'checkout', 'feature/python-tool-build-apps-with-firebase'],
110+
cwd=sdk_source_dir, check=True)
111+
subprocess.run(['git', 'pull'], cwd=sdk_source_dir, check=True)
69112

70113
python_exe = 'python3' if distutils.spawn.find_executable('python3') else 'python'
71-
subprocess.run([python_exe, "scripts/gha/install_prereqs_desktop.py"], cwd=sdk_source_dir)
114+
subprocess.run([python_exe, "scripts/gha/install_prereqs_desktop.py"],
115+
cwd=sdk_source_dir, check=True)
72116
subprocess.run([python_exe, "scripts/gha/build_desktop.py", "--arch", arch,
73117
"--msvc_runtime_library", msvc_runtime_library,
74-
"--vcpkg_step_only"], cwd=sdk_source_dir)
118+
"--vcpkg_step_only"], cwd=sdk_source_dir, check=True)
75119

76-
def build_app_with_source(app_dir, sdk_dir, build_dir, arch,
120+
def build_app_with_source(app_dir, sdk_source_dir, build_dir, arch,
77121
msvc_runtime_library='static', config=None,
78122
target_format=None):
79-
""" CMake configure.
123+
"""Build desktop app directly against the firebase C++ SDK source.
80124
81-
If you are seeing problems when running this multiple times,
82-
make sure to clean/delete previous build directory.
125+
Since this invovles a cmake configure, it is advised to run this on a clean
126+
build directory.
83127
84128
Args:
129+
app_dir (str): Path to directory containing application's CMakeLists.txt.
130+
sdk_source_dir (str): Path to firebase C++ SDK source directory (root of github repo).
85131
build_dir (str): Output build directory.
86132
arch (str): Platform Architecture (example: 'x64').
87133
msvc_runtime_library (str): Runtime library for MSVC (eg: 'static', 'dynamic').
88-
build_tests (bool): Build cpp unit tests.
89134
config (str): Release/Debug config.
90135
If its not specified, cmake's default is used (most likely Debug).
91136
target_format (str): If specified, build for this targetformat ('frameworks' or 'libraries').
92137
"""
93138
# Cmake configure
94139
cmd = ['cmake', '-S', '.', '-B', build_dir]
95-
cmd.append('-DFIREBASE_CPP_SDK_DIR={0}'.format(sdk_dir))
140+
cmd.append('-DFIREBASE_CPP_SDK_DIR={0}'.format(sdk_source_dir))
96141

97142
# If generator is not specifed, default for platform is used by cmake, else
98143
# use the specified value
@@ -103,10 +148,10 @@ def build_app_with_source(app_dir, sdk_dir, build_dir, arch,
103148

104149
if platform.system() == 'Linux' and arch == 'x86':
105150
# Use a separate cmake toolchain for cross compiling linux x86 builds
106-
vcpkg_toolchain_file_path = os.path.join(sdk_dir, 'external', 'vcpkg',
151+
vcpkg_toolchain_file_path = os.path.join(sdk_source_dir, 'external', 'vcpkg',
107152
'scripts', 'buildsystems', 'linux_32.cmake')
108153
else:
109-
vcpkg_toolchain_file_path = os.path.join(sdk_dir, 'external',
154+
vcpkg_toolchain_file_path = os.path.join(sdk_source_dir, 'external',
110155
'vcpkg', 'scripts',
111156
'buildsystems', 'vcpkg.cmake')
112157

@@ -124,7 +169,7 @@ def build_app_with_source(app_dir, sdk_dir, build_dir, arch,
124169

125170
# Use our special cmake option for /MD (dynamic).
126171
# If this option is not specified, the default value is /MT (static).
127-
if msvc_runtime_library == "dynamic":
172+
if msvc_runtime_library == "static":
128173
cmd.append('-DMSVC_RUNTIME_LIBRARY_STATIC=ON')
129174

130175
if (target_format):
@@ -137,10 +182,25 @@ def build_app_with_source(app_dir, sdk_dir, build_dir, arch,
137182
print("Running {0}".format(' '.join(cmd)))
138183
subprocess.run(cmd, cwd=app_dir, check=True)
139184

140-
def build_app_with_prebuilt(app_dir, sdk_dir, build_dir, arch,
185+
def build_app_with_prebuilt(app_dir, sdk_prebuilt_dir, build_dir, arch,
141186
msvc_runtime_library='static', config=None):
187+
"""Build desktop app directly against the prebuilt firebase C++ libraries.
188+
189+
Since this invovles a cmake configure, it is advised to run this on a clean
190+
build directory.
191+
192+
Args:
193+
app_dir (str): Path to directory containing application's CMakeLists.txt.
194+
sdk_prebuilt_dir (str): Path to prebuilt firebase C++ libraries.
195+
build_dir (str): Output build directory.
196+
arch (str): Platform Architecture (example: 'x64').
197+
msvc_runtime_library (str): Runtime library for MSVC (eg: 'static', 'dynamic').
198+
config (str): Release/Debug config.
199+
If its not specified, cmake's default is used (most likely Debug).
200+
"""
201+
142202
cmd = ['cmake', '-S', '.', '-B', build_dir]
143-
cmd.append('-DFIREBASE_CPP_SDK_DIR={0}'.format(sdk_dir))
203+
cmd.append('-DFIREBASE_CPP_SDK_DIR={0}'.format(sdk_prebuilt_dir))
144204

145205
if platform.system() == 'Windows':
146206
if arch == 'x64':
@@ -164,12 +224,12 @@ def build_app_with_prebuilt(app_dir, sdk_dir, build_dir, arch,
164224
def main():
165225
args = parse_cmdline_args()
166226

167-
if not is_path_valid(args.sdk_dir):
227+
if not is_path_valid_for_cmake(args.sdk_dir):
168228
print ("SDK path provided is not valid. Could not find a CMakeLists.txt at the root level.\n"
169229
"Please check the argument to '--sdk_dir'")
170230
sys.exit(1)
171231

172-
if not is_path_valid(args.app_dir):
232+
if not is_path_valid_for_cmake(args.app_dir):
173233
print ("App path provided is not valid. Could not find a CMakeLists.txt at the root level.\n"
174234
"Please check the argument to '--app_dir'")
175235
sys.exit(1)

0 commit comments

Comments
 (0)