2727import tempfile
2828import threading
2929import sys
30+ import json
3031
3132from absl import app , flags , logging
3233
8283g_cpp_sdk_realpath = ""
8384
8485FLAGS = 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+ )
8596flags .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+
145201def 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