Skip to content

Commit ebbd629

Browse files
committed
[ATFE] Improve error handling and header optimisation in common header generation logic. (#456)
This patch introduces additional error checking and handle edge cases in common header generation script. 1. Adds a check to handle cases where the multilib folder is either empty or does not exist, avoiding unhandled exceptions. 2. Improves logic by allowing header optimisation to proceed for targets that have more than two variants, even if another target lacks sufficient variants for a comparison. These changes ensure the script can still performing valid optimisations where possible. 3. Copying multilib.yaml can be moved outside the loop, as it only needs to be done once after all target folders are generated. (cherry picked from commit 256be48)
1 parent 6f03014 commit ebbd629

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

arm-software/embedded/arm-multilib/common-headers-generate.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ def extract_common_headers_for_targets(args):
5555
if os.path.exists(args.multilib_optimised_dir):
5656
shutil.rmtree(args.multilib_optimised_dir)
5757

58+
if os.path.isdir(args.multilib_non_optimised_dir):
59+
existing_target_dirs = [
60+
dir_name
61+
for dir_name in MULTILIB_TARGET_DIRS
62+
if os.path.isdir(os.path.join(args.multilib_non_optimised_dir, dir_name))
63+
]
64+
if not existing_target_dirs:
65+
raise Exception(
66+
f"Error: Expected to find either arm-none-eabi or aarch64-none-elf in '{args.multilib_non_optimised_dir}', but folder is empty."
67+
)
68+
src_yaml = os.path.join(args.multilib_non_optimised_dir, "multilib.yaml")
69+
if not os.path.exists(src_yaml):
70+
raise FileNotFoundError(f"Source yaml '{src_yaml}' does not exist.")
71+
else:
72+
raise FileNotFoundError(
73+
f"Error: Expected folder '{args.multilib_non_optimised_dir}' does not exist"
74+
)
75+
5876
for target in MULTILIB_TARGET_DIRS:
5977
input_target_dir = os.path.join(
6078
os.path.abspath(args.multilib_non_optimised_dir), target
@@ -73,17 +91,18 @@ def extract_common_headers_for_targets(args):
7391
variant_includes = collect_variant_include_paths(input_target_dir)
7492
if len(variant_includes) < 2:
7593
print(
76-
f"Skipping extracting the common headers for {target}: not enough variants to compare.At least two variants must be enabled for the multilib header optimisation phase to proceed."
94+
f"Skipping extracting the common headers for {target}: not enough variants to compare. "
95+
"At least two variants must be enabled for the multilib header optimisation phase to proceed."
7796
)
7897
# The script always creates the multilib-optimised folder, even when there's only one variant and no
7998
# optimization is applied. In that case, multilib-optimised will just contain a copy of the
8099
# single variant from the non-optimised multilib directory.
81-
if os.path.exists(args.multilib_non_optimised_dir):
82-
shutil.copytree(args.multilib_non_optimised_dir, args.multilib_optimised_dir)
83-
return
100+
if os.path.exists(input_target_dir):
101+
shutil.copytree(input_target_dir, output_target_dir, dirs_exist_ok=False)
102+
continue
84103

85104
# Creating the common include headers for each target
86-
os.makedirs(output_include_dir, exist_ok=True)
105+
os.makedirs(output_include_dir, exist_ok=False)
87106

88107
# Step 1: compare first two variants and extract the common headers into the targets common include directory
89108
base_dir = list(variant_includes.values())[0]
@@ -137,13 +156,10 @@ def extract_common_headers_for_targets(args):
137156
else:
138157
print(f"Warning: {src_dir} does not exist and will be skipped.")
139158

140-
# Step4: Copy multilib.yaml file as it is from the non-optimised multilib directoy.
141-
src_yaml = os.path.join(args.multilib_non_optimised_dir, "multilib.yaml")
142-
dst_yaml = os.path.join(args.multilib_optimised_dir, "multilib.yaml")
143-
if os.path.exists(src_yaml):
144-
shutil.copy2(src_yaml, dst_yaml)
145-
else:
146-
raise FileNotFoundError(f"Source yaml '{src_yaml}' does not exist.")
159+
# Step4: Copy multilib.yaml file as it is from the non-optimised multilib directoy.
160+
src_yaml = os.path.join(args.multilib_non_optimised_dir, "multilib.yaml")
161+
dst_yaml = os.path.join(args.multilib_optimised_dir, "multilib.yaml")
162+
shutil.copy2(src_yaml, dst_yaml)
147163

148164

149165
def main():

0 commit comments

Comments
 (0)