Skip to content

Commit 03888e6

Browse files
committed
Add --preset arg to generated cmake CLI
* Add --generator flag that adds -G arg to cmake CLI
1 parent 63eedac commit 03888e6

File tree

6 files changed

+122
-2
lines changed

6 files changed

+122
-2
lines changed

.gitignore

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ output_unpack*/
2929
vcpkg
3030

3131
# Visual Studio Code
32-
.vscode/
32+
.vscode/*
33+
!.vscode/extensions.json
34+
!.vscode/settings.json
3335

3436
# Downloaded NDK
3537
ndk/
3638
ndk_zip
3739

3840
# Downloaded EDM4U
3941
unity_jar_resolver/
42+
43+
# Cmake
44+
CMakeUserPresets.json

.vscode/extensions.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"recommendations": [
3+
"garytyler.darcula-pycharm",
4+
"jsaulou.theme-by-language",
5+
"njpwerner.autodocstring",
6+
"cstrap.python-snippets"
7+
]
8+
}

.vscode/settings.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"json.schemas": [
3+
{
4+
"fileMatch": [
5+
"/CMakePresets.json",
6+
"/CMakeUserPresets.json"
7+
],
8+
"url": "https://cmake.org/cmake/help/latest/_downloads/3e2d73bff478d88a7de0de736ba5e361/schema.json"
9+
}
10+
],
11+
"workbench.colorTheme": "Visual Studio 2019 Dark"
12+
}

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Firebase Unity SDK can be found at <https://github.com/firebase/quickstart-unity
3333
- [Install Unity](#install-unity)
3434
- [Building](#building)
3535
- [Building for certain library](#building-for-certain-library)
36+
- [Building with --preset name](#building-with---preset-name)
37+
- [Building with custom generator](#building-with-custom-generator)
3638
- [Packaging](#packaging)
3739
- [Packaging unitypackage](#packaging-unitypackage)
3840
- [Packaging tgz](#packaging-tgz)
@@ -88,6 +90,34 @@ python scripts/build_scripts/build_zips.py --platform=<target platform> --target
8890

8991
> **Note:** Supported library names: analytics, app_check, auth, crashlytics, database, dynamic_links, firestore, functions, installations, messaging, remote_config, storage
9092
93+
### Building with --preset name
94+
95+
Cmake allows use a configuration file (`CMakePresets.json` or `CMakeUserPresets.json`) with custom local environment settings, such as **cache** and/or **environment variable**s. To do that, follow the steps below:
96+
97+
1. Create a `.json` file with your presets. You can find full examples below:
98+
99+
- [Example CMakePresets.json file](https://learn.microsoft.com/en-us/cpp/build/cmake-presets-vs?view=msvc-170&source=recommendations#example-cmakepresetsjson-file)
100+
- [cmake-presets: Format](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#id4)
101+
2. Build with any of commands described above. The generated cmake command should be:
102+
103+
```bash
104+
cmake_setup_args is: cmake <repository root path> --preset firebase-unity-sdk-<something> ...
105+
```
106+
107+
Optionally, it's possible pass a custom preset name with: `--preset` arg:
108+
109+
```bash
110+
python ./scripts/build_scripts/build_zips.py --preset=my-custom-preset --platform=windows
111+
```
112+
113+
### Building with custom generator
114+
115+
For custom cmake generator (e.g define as **Visual Studio 2022** or newest), it's possible override the default generator with `--generator` flag:
116+
117+
```bash
118+
python scripts/build_scripts/build_zips.py --generator='Visual Studio 17 2022' --platform=windows
119+
```
120+
91121
## Packaging
92122

93123
We can package the built artifacts to better imported by Unity Editor.

requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python dependencis
2+
3+
absl-extra~=0.1.3

scripts/build_scripts/build_zips.py

+63-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import tempfile
2828
import threading
2929
import sys
30+
import json
3031

3132
from absl import app, flags, logging
3233

@@ -82,6 +83,16 @@
8283
g_cpp_sdk_realpath = ""
8384

8485
FLAGS = flags.FLAGS
86+
87+
#
88+
flags.DEFINE_string(
89+
'generator', "Visual Studio 16 2019",
90+
"The cmake generator passed with -G flag"
91+
)
92+
flags.DEFINE_string(
93+
'preset', None,
94+
"The cmake --preset name arg from CMakeSettings.json or CMakeUserSettings.json"
95+
)
8596
flags.DEFINE_string(
8697
'platform', None,
8798
'Which platform to build SDK on. Required one entry from ({})'.format(
@@ -142,6 +153,51 @@ def get_build_path(platform, clean_build=False):
142153
return platform_path
143154

144155

156+
def get_presets_file_path(source_path: str):
157+
"""Get the cmake args to pass as --preset name from a CMakePresets.json
158+
from that root project folder
159+
160+
Args:
161+
source_path: root source folder to find CMakePresets.json or CMakeUserPresets.json files.
162+
163+
Returns:
164+
camke args with the --preset name that contains variables and others cmake configurations
165+
"""
166+
167+
if FLAGS.preset:
168+
return f"--preset {FLAGS.preset}"
169+
170+
preset_files = [
171+
source_path + "CMakePresets.json",
172+
source_path + "CMakeUserPresets.json"
173+
]
174+
175+
for pfile in preset_files:
176+
177+
if not os.path.exists(pfile):
178+
continue
179+
180+
try:
181+
presets_file = open(pfile)
182+
183+
if presets_file:
184+
presets_data = json.load(presets_file)
185+
186+
# List comprehension filter
187+
matches = [x for x in presets_data['configurePresets'] if x['name'].startswith("firebase-unity-sdk")]
188+
if matches and matches[0]:
189+
preset = matches[0]
190+
191+
return f"--preset {preset['name']}"
192+
193+
except OSError as error:
194+
print(
195+
f"Error on load file: '{pfile}'",
196+
"Reason => ",
197+
f'[{type(error).__name__}]: {error.strerror}'
198+
)
199+
200+
145201
def get_cpp_folder_args(source_path):
146202
"""Get the cmake args to pass in local Firebase C++ SDK folder.
147203
If not found, will download from Firebase C++ git repo.
@@ -430,7 +486,7 @@ def get_windows_args():
430486
cmake args for windows platform.
431487
"""
432488
result_args = []
433-
result_args.append('-G Visual Studio 16 2019')
489+
result_args.append("-G %s" % FLAGS.generator) # Default: -G Visual Studio 16 2019
434490
result_args.append('-A x64') # TODO flexibily for x32
435491
result_args.append("-DFIREBASE_PYTHON_HOST_EXECUTABLE:FILEPATH=%s" % sys.executable)
436492
# Use a newer version of the Windows SDK, as the default one has build issues with grpc
@@ -734,6 +790,9 @@ def main(argv):
734790
platform, ",".join(SUPPORT_PLATFORMS)))
735791

736792
source_path = os.getcwd()
793+
relative_path = "." + os.path.sep
794+
795+
cmake_presets_file_args = get_presets_file_path(relative_path)
737796
cmake_cpp_folder_args = get_cpp_folder_args(source_path)
738797
build_path = get_build_path(platform, FLAGS.clean_build)
739798
if is_android_build() and g_cpp_sdk_realpath:
@@ -746,6 +805,9 @@ def main(argv):
746805
"cmake",
747806
source_path
748807
]
808+
809+
if cmake_presets_file_args:
810+
cmake_setup_args.append(cmake_presets_file_args)
749811

750812
if FLAGS.verbose:
751813
cmake_setup_args.append('-DCMAKE_VERBOSE_MAKEFILE=1')

0 commit comments

Comments
 (0)