From 03888e6ed7f8a013a7d5838b99f9c8343afe6eea Mon Sep 17 00:00:00 2001 From: Felipe Ferreira Date: Sun, 10 Mar 2024 17:34:08 -0300 Subject: [PATCH] Add --preset arg to generated cmake CLI * Add --generator flag that adds -G arg to cmake CLI --- .gitignore | 7 +++- .vscode/extensions.json | 8 ++++ .vscode/settings.json | 12 ++++++ README.md | 30 ++++++++++++++ requirements.txt | 3 ++ scripts/build_scripts/build_zips.py | 64 ++++++++++++++++++++++++++++- 6 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index b42162e71..8f56e6ef4 100644 --- a/.gitignore +++ b/.gitignore @@ -29,7 +29,9 @@ output_unpack*/ vcpkg # Visual Studio Code -.vscode/ +.vscode/* +!.vscode/extensions.json +!.vscode/settings.json # Downloaded NDK ndk/ @@ -37,3 +39,6 @@ ndk_zip # Downloaded EDM4U unity_jar_resolver/ + +# Cmake +CMakeUserPresets.json diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 000000000..cd443bfe9 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,8 @@ +{ + "recommendations": [ + "garytyler.darcula-pycharm", + "jsaulou.theme-by-language", + "njpwerner.autodocstring", + "cstrap.python-snippets" + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..61c1cff9d --- /dev/null +++ b/.vscode/settings.json @@ -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" +} \ No newline at end of file diff --git a/README.md b/README.md index af0db598b..a992233fa 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,8 @@ Firebase Unity SDK can be found at --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 --preset firebase-unity-sdk- ... + ``` + + 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. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..1922708c7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +# Python dependencis + +absl-extra~=0.1.3 \ No newline at end of file diff --git a/scripts/build_scripts/build_zips.py b/scripts/build_scripts/build_zips.py index 936f8da16..abd7cc55f 100644 --- a/scripts/build_scripts/build_zips.py +++ b/scripts/build_scripts/build_zips.py @@ -27,6 +27,7 @@ import tempfile import threading import sys +import json from absl import app, flags, logging @@ -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( @@ -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. @@ -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 @@ -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: @@ -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')