-
Notifications
You must be signed in to change notification settings - Fork 37
[ATFE] Package common multilib headers into target triple directory. #423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6a23199
[ATFE] Package common multilib headers into target triple directory.
simpal01 dee16cf
fixup! [ATFE] Make sure multilib-optimised directory created no earli…
simpal01 bddfb96
fixup! [ATFE] Ensure dependency on multilib-yaml as script expects th…
simpal01 c2f5ebc
fixup! Add IncludePath to multilib.yaml conditionally.
simpal01 ffdca78
fixup! Python script needs to always create the multilib-optimised fo…
simpal01 edbe90c
fixup! Make ENABLE_MULTILIB_HEADER_OPTIMISATION is OFF by default.
simpal01 2f51b28
fixup! Make ENABLE_MULTILIB_HEADER_OPTIMISATION is ON by default
simpal01 a57c95b
fixup! Using IncludeDirs instead of IncludePath.
simpal01 5444434
fixup! stdlibs has to be added irrespecitve of the header optiimisati…
simpal01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
arm-software/embedded/arm-multilib/common-headers-generate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| """ | ||
| Identifies and extracts header files that are common across multiple multilib variant directories. | ||
| This script scans all variant folders within a multilib target directory | ||
| (e.g., `arm-none-eabi/variant1/include`, `arm-none-eabi/variant2/include`, etc.) and compares all | ||
| `.h` files. If the same file (by name and content) appears in multiple variants, it will be moved | ||
| to a shared include directory at: | ||
| <CMAKE_BINARY_DIR>/multilib-optimised/<target>/include/ | ||
| for the following multilib targets: | ||
| - arm-none-eabi | ||
| - aarch64-none-elf | ||
| Arguments: | ||
| <CMAKE_BINARY_DIR>/multilib Path to the CMake build directory containing non optmised multilib. | ||
| eg: build/multilib-builds/multilib/picolibc-build/multilib | ||
| <CMAKE_BINARY_DIR>/multilib-optimised Path to the CMake build directory where optimised multilib should be generated.. | ||
| eg: build/multilib-builds/multilib/picolibc-build/multilib-optimised | ||
| This is useful to reduce duplication in the toolchain by centralising common headers | ||
| that are shared across architecture variants. | ||
| """ | ||
| import argparse | ||
| import os | ||
| import filecmp | ||
| import shutil | ||
|
|
||
| # Define the multilib target dirs which want to process | ||
| MULTILIB_TARGET_DIRS = ["arm-none-eabi", "aarch64-none-elf"] | ||
|
|
||
|
|
||
| def files_are_identical(f1, f2): | ||
| return filecmp.cmp(f1, f2, shallow=False) | ||
|
|
||
|
|
||
| def collect_variant_include_paths(input_target_dir): | ||
| """ | ||
| Extracts each multilib variant and its corresponding include path from the non-optimised multilib directory. | ||
| Stores the results to enable later comparison of header contents across different non-optimised multilib variant | ||
| include paths. | ||
| """ | ||
| variant_include_path = {} | ||
| for variant in os.listdir(input_target_dir): | ||
| include_path = os.path.join(input_target_dir, variant, "include") | ||
| if os.path.isdir(include_path): | ||
| variant_include_path[variant] = include_path | ||
| return variant_include_path | ||
|
|
||
|
|
||
| def extract_common_headers_for_targets(args): | ||
| for target in MULTILIB_TARGET_DIRS: | ||
| input_target_dir = os.path.join( | ||
| os.path.abspath(args.multilib_non_optimised_dir), target | ||
| ) | ||
| output_target_dir = os.path.join( | ||
| os.path.abspath(args.multilib_optimised_dir), target | ||
| ) | ||
| output_include_dir = os.path.join(output_target_dir, "include") | ||
|
|
||
| if not os.path.isdir(input_target_dir): | ||
| print( | ||
| f"Skipping extracting the common headers for {target}: input path {input_target_dir} not found" | ||
| ) | ||
| continue | ||
|
|
||
| variant_includes = collect_variant_include_paths(input_target_dir) | ||
| if len(variant_includes) < 2: | ||
| print( | ||
| f"Skipping extracing 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." | ||
| ) | ||
| return | ||
dcandler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| os.makedirs(output_include_dir, exist_ok=True) | ||
|
|
||
| # Step 1: compare first two variants and extract the common headers into the targets common include directory | ||
| base_dir = list(variant_includes.values())[0] | ||
| compare_dir = list(variant_includes.values())[1] | ||
| for root, sub_dirs, header_files in os.walk(base_dir): | ||
| sub_dir_root = os.path.relpath(root, base_dir) | ||
| for header in header_files: | ||
| h1 = os.path.join(base_dir, sub_dir_root, header) | ||
| h2 = os.path.join(compare_dir, sub_dir_root, header) | ||
| if os.path.exists(h2) and files_are_identical(h1, h2): | ||
| out_dir = os.path.join(output_include_dir, sub_dir_root) | ||
| os.makedirs(out_dir, exist_ok=True) | ||
| shutil.copy2(h1, os.path.join(out_dir, header)) | ||
|
|
||
| # Step 2: Compare all the variants with the new common include. Any headers that do not match | ||
| # and do not exit in common include should retain in their respective variant specific directories. | ||
| for variant, include_path in variant_includes.items(): | ||
| for root, sub_dirs, header_files in os.walk(include_path): | ||
| sub_dir_root = os.path.relpath(root, include_path) | ||
| for header in header_files: | ||
| variant_header = os.path.join(include_path, sub_dir_root, header) | ||
| common_header = os.path.join( | ||
| output_include_dir, sub_dir_root, header | ||
| ) | ||
| if not os.path.exists(common_header) or not files_are_identical( | ||
| variant_header, common_header | ||
| ): | ||
| out_dir = os.path.join( | ||
| os.path.abspath(args.multilib_optimised_dir), | ||
| target, | ||
| variant, | ||
| sub_dir_root, | ||
| "include", | ||
| ) | ||
| os.makedirs(out_dir, exist_ok=True) | ||
| shutil.copy2(variant_header, os.path.join(out_dir, header)) | ||
|
|
||
| # Step3: For each variant, the lib and share directories should be copied from the non-optimised multilib | ||
| # directory as it is. | ||
| for variant in variant_includes: | ||
| remaining_dirs = ["lib", "share"] | ||
| for folder in remaining_dirs: | ||
| src_dir = os.path.join(input_target_dir, variant, folder) | ||
| dst_dir = os.path.join(output_target_dir, variant, folder) | ||
| if os.path.exists(src_dir): | ||
| # If destination exists, remove it first | ||
| if os.path.exists(dst_dir): | ||
| shutil.rmtree(dst_dir) | ||
| os.makedirs(os.path.dirname(dst_dir), exist_ok=True) | ||
| shutil.copytree(src_dir, dst_dir) | ||
| else: | ||
| print(f"Warning: {src_dir} does not exist and will be skipped.") | ||
|
|
||
| # Step4: Copy multilib.yaml file as it is from the non-optimised multilib directoy. | ||
| src_yaml = os.path.join(args.multilib_non_optimised_dir, "multilib.yaml") | ||
| dst_yaml = os.path.join(args.multilib_optimised_dir, "multilib.yaml") | ||
| if os.path.exists(src_yaml): | ||
| shutil.copy2(src_yaml, dst_yaml) | ||
| else: | ||
| raise FileNotFoundError(f"Source yaml '{src_yaml}' does not exist.") | ||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser( | ||
| description=__doc__, | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
| parser.add_argument( | ||
| "multilib_non_optimised_dir", | ||
| help="CMake binary directory containing the non-optimised multilib headers", | ||
| ) | ||
| parser.add_argument( | ||
| "multilib_optimised_dir", | ||
| help="CMake binary directory where the optimised multilib headers should be generated", | ||
| ) | ||
| args = parser.parse_args() | ||
|
|
||
| extract_common_headers_for_targets(args) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.