Skip to content

Commit 40eb3b3

Browse files
committed
script to control burst
1 parent 01b1bc9 commit 40eb3b3

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# An example usage would be "- python Tools/CI/Netcode/BuildAutomations/disable-enable-burst.py --project-path {{ project.path }} --platform WebGL"
2+
# This file aims to modify BurstAotSettings file which should be present under ProjectSettings folder.
3+
# Note that this requires Burst package to be installed as well as you need to specify the platform for which you are building since there are different settings for each. (this is taken from environment variable)
4+
# This script is not overriding existing settings file but completely replacing it
5+
6+
import argparse
7+
import json
8+
import os
9+
10+
# Function that parses arguments of the script
11+
def parse_args():
12+
global args
13+
parser = argparse.ArgumentParser(description="Enable or disable Burst compilation and specify Unity project details.")
14+
15+
# Add the mutually exclusive group for --disable-burst and --enable-burst
16+
group = parser.add_mutually_exclusive_group(required=True)
17+
group.add_argument('--disable-burst', action='store_true', help='Disable Burst compilation.')
18+
group.add_argument('--enable-burst', action='store_true', help='Enable Burst compilation.')
19+
20+
# Add additional arguments
21+
parser.add_argument('--project-path', required=True, help='Specify the location of the Unity project.')
22+
23+
args = parser.parse_args()
24+
25+
26+
# This function creates a new burst settings file with default values. Notice that this should almost always not be used since assumption is that in our case we have projects with Burst preinstalled
27+
# For the "default" values I used values from NetcodeSamples project in DOTS-monorepo
28+
def create_config(settings_path):
29+
config_name = os.path.join(settings_path, 'BurstAotSettings_{}.json'.format(resolve_target()))
30+
monobehaviour = {
31+
'Version': 4,
32+
'EnableBurstCompilation': True,
33+
'EnableOptimisations': True,
34+
'EnableSafetyChecks': False,
35+
'EnableDebugInAllBuilds': False,
36+
'CpuMinTargetX32': 0,
37+
'CpuMaxTargetX32': 0,
38+
'CpuMinTargetX64': 0,
39+
'CpuMaxTargetX64': 0,
40+
'CpuTargetsX32': 6,
41+
'CpuTargetsX64': 72,
42+
'OptimizeFor': 0
43+
}
44+
45+
data = {'MonoBehaviour': monobehaviour}
46+
with open(config_name, 'w') as f:
47+
json.dump(data, f)
48+
return config_name
49+
50+
51+
# Burst has specific files for each platform, so we need to resolve the target platform to get the correct settings file.
52+
# Note that this jobs uses environment variables to pass parameters to the script.
53+
def resolve_target():
54+
# Get the platform value from the environment variable
55+
platform_key = os.environ.get('PLATFORM_WIN64_MAC_ANDROID')
56+
57+
resolved_target = platform_key
58+
if 'win64' == platform_key:
59+
resolved_target = 'StandaloneWindows'
60+
elif 'mac' == platform_key:
61+
resolved_target = 'StandaloneOSX'
62+
elif 'android' == platform_key:
63+
resolved_target = 'Android'
64+
else:
65+
raise ValueError("Unsupported platform: {}".format(platform) + "Check if you are passing correct argument for one of the supported platforms: StandaloneWindows or StandaloneLinux")
66+
67+
return resolved_target
68+
69+
70+
# This function either returns existing burst settings or creates new if a file was not found
71+
def get_or_create_burst_AOT_config():
72+
settings_path = os.path.join(args.project_path, 'ProjectSettings')
73+
if not os.path.isdir(settings_path):
74+
os.mkdir(settings_path)
75+
config_names = [os.path.join(settings_path, filename) for filename in os.listdir(settings_path) if filename.startswith("BurstAotSettings_{}".format(resolve_target()))]
76+
if not config_names:
77+
return [create_config(settings_path)]
78+
return config_names
79+
80+
81+
# Function that sets the AOT status in the burst settings file (essentially enables or disables burst compilation)
82+
def set_burst_AOT(config_file, status):
83+
config = None
84+
with open(config_file, 'r') as f:
85+
config = json.load(f)
86+
87+
assert config is not None, 'AOT settings not found; did the burst-enabled build finish successfully?'
88+
89+
config['MonoBehaviour']['EnableBurstCompilation'] = status
90+
with open(config_file, 'w') as f:
91+
json.dump(config, f)
92+
93+
94+
def main():
95+
parse_args()
96+
config_names = get_or_create_burst_AOT_config()
97+
98+
platform_key = os.environ.get('PLATFORM_WIN64_MAC_ANDROID')
99+
print(f"Burst compilation script: Unity project path is {args.project_path}")
100+
print(f"Burst compilation script: Target platform is {platform_key}")
101+
102+
if args.disable_burst:
103+
print('BURST COMPILATION: DISABLED')
104+
105+
for config_name in config_names:
106+
set_burst_AOT(config_name, False)
107+
108+
elif args.enable_burst:
109+
print('BURST COMPILATION: ENABLED')
110+
111+
for config_name in config_names:
112+
set_burst_AOT(config_name, True)
113+
114+
else:
115+
sys.exit('BURST COMPILATION: unexpected value: {}'.format(args.enable_burst))
116+
117+
118+
119+
if __name__ == '__main__':
120+
main()

0 commit comments

Comments
 (0)