Skip to content

Commit

Permalink
Add --preset arg to generated cmake CLI
Browse files Browse the repository at this point in the history
* Add --generator flag that adds -G arg to cmake CLI
  • Loading branch information
mfdeveloper committed Mar 11, 2024
1 parent 0b87599 commit 4de4230
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ output_unpack*/
vcpkg

# Visual Studio Code
.vscode/
.vscode/*
!.vscode/extensions.json
!.vscode/settings.json

# Downloaded NDK
ndk/
ndk_zip

# Downloaded EDM4U
unity_jar_resolver/

# Cmake
CMakeUserPresets.json
8 changes: 8 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"recommendations": [
"garytyler.darcula-pycharm",
"jsaulou.theme-by-language",
"njpwerner.autodocstring",
"cstrap.python-snippets"
]
}
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"json.schemas": [
{
"fileMatch": [
"/CMakePresets.json",
"/CMakeUserPresets.json"
],
"url": "https://cmake.org/cmake/help/latest/_downloads/3e2d73bff478d88a7de0de736ba5e361/schema.json"
}
],
"workbench.colorTheme": "Visual Studio 2019 Dark"
}
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Firebase Unity SDK can be found at <https://github.com/firebase/quickstart-unity
- [Install Unity](#install-unity)
- [Building](#building)
- [Building for certain library](#building-for-certain-library)
- [Building with --preset name](#building-with---preset-name)
- [Building with custom generator](#building-with-custom-generator)
- [Packaging](#packaging)
- [Packaging unitypackage](#packaging-unitypackage)
- [Packaging tgz](#packaging-tgz)
Expand Down Expand Up @@ -88,6 +90,34 @@ python scripts/build_scripts/build_zips.py --platform=<target platform> --target

> **Note:** Supported library names: analytics, app_check, auth, crashlytics, database, dynamic_links, firestore, functions, installations, messaging, remote_config, storage
### Building with --preset name

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:

1. Create a `.json` file with your presets. You can find full examples below:

- [Example CMakePresets.json file](https://learn.microsoft.com/en-us/cpp/build/cmake-presets-vs?view=msvc-170&source=recommendations#example-cmakepresetsjson-file)
- [cmake-presets: Format](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html#id4)
2. Build with any of commands described above. The generated cmake command should be:

```bash
cmake_setup_args is: cmake <repository root path> --preset firebase-unity-sdk-<something> ...
```

Optionally, it's possible pass a custom preset name with: `--preset` arg:
```bash
python ./scripts/build_scripts/build_zips.py --preset=my-custom-preset --platform=windows
```
### Building with custom generator
For custom cmake generator (e.g define as **Visual Studio 2022** or newest), it's possible override the default generator with `--generator` flag:

```bash
python scripts/build_scripts/build_zips.py --generator='Visual Studio 17 2022' --platform=windows
```

## Packaging

We can package the built artifacts to better imported by Unity Editor.
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Python dependencis

absl-extra~=0.1.3
64 changes: 63 additions & 1 deletion scripts/build_scripts/build_zips.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import tempfile
import threading
import sys
import json

from absl import app, flags, logging

Expand Down Expand Up @@ -82,6 +83,16 @@
g_cpp_sdk_realpath = ""

FLAGS = flags.FLAGS

#
flags.DEFINE_string(
'generator', "Visual Studio 16 2019",
"The cmake generator passed with -G flag"
)
flags.DEFINE_string(
'preset', None,
"The cmake --preset name arg from CMakeSettings.json or CMakeUserSettings.json"
)
flags.DEFINE_string(
'platform', None,
'Which platform to build SDK on. Required one entry from ({})'.format(
Expand Down Expand Up @@ -142,6 +153,51 @@ def get_build_path(platform, clean_build=False):
return platform_path


def get_presets_file_path(source_path: str):
"""Get the cmake args to pass as --preset name from a CMakePresets.json
from that root project folder
Args:
source_path: root source folder to find CMakePresets.json or CMakeUserPresets.json files.
Returns:
camke args with the --preset name that contains variables and others cmake configurations
"""

if FLAGS.preset:
return f"--preset {FLAGS.preset}"

preset_files = [
source_path + "CMakePresets.json",
source_path + "CMakeUserPresets.json"
]

for pfile in preset_files:

if not os.path.exists(pfile):
continue

try:
presets_file = open(pfile)

if presets_file:
presets_data = json.load(presets_file)

# List comprehension filter
matches = [x for x in presets_data['configurePresets'] if x['name'].startswith("firebase-unity-sdk")]
if matches and matches[0]:
preset = matches[0]

return f"--preset {preset['name']}"

except OSError as error:
print(
f"Error on load file: '{pfile}'",
"Reason => ",
f'[{type(error).__name__}]: {error.strerror}'
)


def get_cpp_folder_args(source_path):
"""Get the cmake args to pass in local Firebase C++ SDK folder.
If not found, will download from Firebase C++ git repo.
Expand Down Expand Up @@ -430,7 +486,7 @@ def get_windows_args():
cmake args for windows platform.
"""
result_args = []
result_args.append('-G Visual Studio 16 2019')
result_args.append("-G %s" % FLAGS.generator) # Default: -G Visual Studio 16 2019
result_args.append('-A x64') # TODO flexibily for x32
result_args.append("-DFIREBASE_PYTHON_HOST_EXECUTABLE:FILEPATH=%s" % sys.executable)
# Use a newer version of the Windows SDK, as the default one has build issues with grpc
Expand Down Expand Up @@ -734,6 +790,9 @@ def main(argv):
platform, ",".join(SUPPORT_PLATFORMS)))

source_path = os.getcwd()
relative_path = "." + os.path.sep

cmake_presets_file_args = get_presets_file_path(relative_path)
cmake_cpp_folder_args = get_cpp_folder_args(source_path)
build_path = get_build_path(platform, FLAGS.clean_build)
if is_android_build() and g_cpp_sdk_realpath:
Expand All @@ -746,6 +805,9 @@ def main(argv):
"cmake",
source_path
]

if cmake_presets_file_args:
cmake_setup_args.append(cmake_presets_file_args)

if FLAGS.verbose:
cmake_setup_args.append('-DCMAKE_VERBOSE_MAKEFILE=1')
Expand Down

0 comments on commit 4de4230

Please sign in to comment.