1+ import argparse
2+ import os
3+ import subprocess
4+ import shutil
5+ import glob
6+
7+ def run_command (command , shell = True , cwd = None ):
8+ if os .name == 'nt' : # Check if the OS is Windows
9+ command = f'powershell.exe -Command "{ command } "'
10+ result = subprocess .run (command , shell = shell , check = True , text = True , cwd = cwd )
11+ return result
12+
13+ def main ():
14+ parser = argparse .ArgumentParser (description = "CMake Build and Test Script" )
15+ parser .add_argument ('--os' , required = True , choices = ['linux' , 'windows' , 'macos' ], help = 'Operating System' )
16+ parser .add_argument ('--build_type' , default = 'Debug' , choices = ['Release' , 'Debug' ], help = 'Build Type' )
17+ parser .add_argument ('--platform' , choices = ['linux_x64' , 'linux_x86' , 'osx' , 'win32' , 'win64' , 'uwp' ], help = 'Platform string for CMake' )
18+ parser .add_argument ('--build' , action = 'store_true' , help = 'Execute the build step' )
19+ parser .add_argument ('--test' , action = 'store_true' , help = 'Execute the test step' )
20+ args = parser .parse_args ()
21+
22+ build_output_dir = os .path .join (os .getcwd (), 'build' )
23+ os .makedirs (build_output_dir , exist_ok = True )
24+
25+ if args .os == 'windows' :
26+ c_compiler = 'cl'
27+ cpp_compiler = 'cl'
28+ elif args .os == 'linux' :
29+ c_compiler = 'gcc'
30+ cpp_compiler = 'g++'
31+ elif args .os == 'macos' :
32+ c_compiler = 'clang'
33+ cpp_compiler = 'clang++'
34+
35+ # Configure CMake
36+ cmake_command = f'cmake -B { build_output_dir } -DCMAKE_CXX_COMPILER={ cpp_compiler } -DCMAKE_C_COMPILER={ c_compiler } -DCMAKE_BUILD_TYPE={ args .build_type } -S { os .getcwd ()} '
37+ if args .os == 'macos' :
38+ cmake_command += ' -G "Xcode"'
39+ if args .platform :
40+ cmake_command += f' -DPLATFORM:STRING={ args .platform } '
41+ run_command (cmake_command )
42+
43+ # Build
44+ if args .build :
45+ run_command (f'cmake --build { build_output_dir } --config { args .build_type } ' )
46+ else :
47+ exit (0 )
48+
49+ # Test
50+ if args .test :
51+ run_command (f'ctest --build-config { args .build_type } --verbose --output-on-failure' , cwd = build_output_dir )
52+
53+
54+ # Package Build Artifacts
55+ package_dir = os .path .join (build_output_dir , 'package' )
56+ os .makedirs (package_dir , exist_ok = True )
57+ files_to_copy = glob .glob (f'{ build_output_dir } /{ args .build_type } /*GameAnalytics.*' )
58+ for file in files_to_copy :
59+ shutil .copy (file , package_dir )
60+ shutil .copytree (os .path .join (os .getcwd (), 'include' ), os .path .join (package_dir , 'include' ), dirs_exist_ok = True )
61+
62+ # Print Package Contents
63+ if args .os == 'windows' :
64+ run_command (f'dir { package_dir } ' , shell = True )
65+ else :
66+ run_command (f'ls -la { package_dir } ' , shell = True )
67+
68+ # Print architecture information
69+ #use lipo on macos and linux and dumpbin on windows
70+ if args .os == 'macos' :
71+ run_command (f'lipo -info { package_dir } /*GameAnalytics.*' )
72+ elif args .os == 'linux' :
73+ run_command (f'file { package_dir } /*GameAnalytics.*' )
74+ elif args .os == 'windows' :
75+ run_command (f'dumpbin /headers { package_dir } /GameAnalytics.lib | findstr machine' )
76+
77+
78+ if __name__ == "__main__" :
79+ main ()
0 commit comments