|
| 1 | +from pathlib import Path |
| 2 | +import re |
| 3 | +import argparse |
| 4 | + |
| 5 | + |
| 6 | +def get_path_relative_to_root(root_path: Path, file_path: Path): |
| 7 | + """Returns the relative path of the file path to the root |
| 8 | + path, appended with the root path. |
| 9 | +
|
| 10 | + Args: |
| 11 | + root_path (Path): Root path directory |
| 12 | + file_path (Path): File path in the root directory |
| 13 | +
|
| 14 | + Returns: |
| 15 | + Path: The full path to the file relative to the root path |
| 16 | + """ |
| 17 | + return Path((root_path / file_path.relative_to(root_path)).as_posix()) |
| 18 | + |
| 19 | + |
| 20 | +def get_include_paths_relative_to_root(root_path: Path): |
| 21 | + """Fetches every .h file in a directory to build a list of include paths. |
| 22 | +
|
| 23 | + Args: |
| 24 | + root_path (Path): The root path of the directory |
| 25 | +
|
| 26 | + Returns: |
| 27 | + list: List of paths to the include files |
| 28 | + """ |
| 29 | + |
| 30 | + include_paths = [] |
| 31 | + |
| 32 | + for path in root_path.rglob("*.h"): |
| 33 | + include_paths.append(get_path_relative_to_root(root_path, path)) |
| 34 | + |
| 35 | + return include_paths |
| 36 | + |
| 37 | + |
| 38 | +def find_matching_root_include_path(include_path: str, include_paths_relative_to_root): |
| 39 | + """Given a include path, finds the corresponding include path relative to the root. |
| 40 | +
|
| 41 | + Args: |
| 42 | + include_path (str): The include path in the file. |
| 43 | + include_paths (list[Path]): The list of include paths which are |
| 44 | + relative to the root directory. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + Path: The matched path relative to root, or None if not found |
| 48 | + """ |
| 49 | + |
| 50 | + for include_path_relative_to_root in include_paths_relative_to_root: |
| 51 | + |
| 52 | + if Path(include_path).name == include_path_relative_to_root.name: |
| 53 | + return include_path_relative_to_root |
| 54 | + |
| 55 | + return None |
| 56 | + |
| 57 | + |
| 58 | +def get_update_line(line: str, include_paths_relative_to_root): |
| 59 | + """Given a line in a file, replaces a #include <...> or #include "..." with |
| 60 | + a path relative to the root folder given in the include paths relative |
| 61 | + to root list. |
| 62 | +
|
| 63 | + Args: |
| 64 | + line (str): The line in the file |
| 65 | + include_paths_relative_to_root (list[Path]): List of the include paths |
| 66 | + relative to the root. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + str: The updated line if "#include" was found. |
| 70 | + """ |
| 71 | + |
| 72 | + # Find includes with <> or "" |
| 73 | + r = re.search('["<](.*)[">]', line) |
| 74 | + |
| 75 | + if r is not None and r.group(1).endswith(".h"): |
| 76 | + include_path = r.group(1) |
| 77 | + |
| 78 | + include_path_relative_to_root = find_matching_root_include_path( |
| 79 | + include_path, |
| 80 | + include_paths_relative_to_root |
| 81 | + ) |
| 82 | + |
| 83 | + # If the match was not found, the include is likely a system header |
| 84 | + if include_path_relative_to_root is not None: |
| 85 | + return line.replace(include_path, include_path_relative_to_root.as_posix()) |
| 86 | + |
| 87 | + return line |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + |
| 92 | + parser = argparse.ArgumentParser( |
| 93 | + prog="update_include_paths", |
| 94 | + description="Updates the include paths of a library so that all are relative to a root folder", |
| 95 | + ) |
| 96 | + |
| 97 | + parser.add_argument("input_directory") |
| 98 | + parser.add_argument("output_directory") |
| 99 | + parser.add_argument("-v", "--verbose", action="store_true") |
| 100 | + |
| 101 | + args = parser.parse_args() |
| 102 | + |
| 103 | + root_path = Path(args.input_directory) |
| 104 | + output_root_path = Path(args.output_directory) |
| 105 | + |
| 106 | + # Fetch every .h file to build a reference for the include paths |
| 107 | + include_paths_relative_to_root = get_include_paths_relative_to_root(root_path) |
| 108 | + |
| 109 | + # Find every .h and .c file |
| 110 | + paths = [] |
| 111 | + |
| 112 | + for path in root_path.rglob("*.c"): |
| 113 | + paths.append(path) |
| 114 | + |
| 115 | + for path in root_path.rglob("*.h"): |
| 116 | + paths.append(path) |
| 117 | + |
| 118 | + # Loop through every file and update includes so that every include is |
| 119 | + # relative (and including) the root path |
| 120 | + for path in paths: |
| 121 | + |
| 122 | + if args.verbose: |
| 123 | + print(f"Opening {path}") |
| 124 | + |
| 125 | + with open(path) as input_file: |
| 126 | + |
| 127 | + # We need to remove the directories leading to the input directory |
| 128 | + output_path = output_root_path / path.relative_to(Path(args.input_directory)) |
| 129 | + output_path.parent.mkdir(exist_ok=True, parents=True) |
| 130 | + |
| 131 | + with open(output_path, "w") as output_file: |
| 132 | + |
| 133 | + for line in input_file: |
| 134 | + updated_line = get_update_line(line, include_paths_relative_to_root) |
| 135 | + |
| 136 | + if args.verbose: |
| 137 | + if line != updated_line: |
| 138 | + print(f"\tChanged {line.strip()} to {updated_line.strip()}") |
| 139 | + |
| 140 | + output_file.write(updated_line) |
0 commit comments