|
9 | 9 |
|
10 | 10 | # Python < 3.8 does not have typing.Literals |
11 | 11 | try: |
12 | | - from typing import Literal |
| 12 | + from typing import Iterable, Literal |
13 | 13 | except ImportError: |
14 | | - from typing_extensions import Literal |
| 14 | + from typing_extensions import Literal, Iterable |
15 | 15 |
|
16 | 16 | from re import Match, Pattern |
17 | 17 |
|
@@ -2097,6 +2097,14 @@ def append_multiline_macro(def_value: str | tuple, line: str): |
2097 | 2097 | return (def_args, def_value) |
2098 | 2098 | return def_value + line |
2099 | 2099 |
|
| 2100 | + def find_file_in_directories(directories: Iterable[str], filename: str) -> str: |
| 2101 | + for include_dir in directories: |
| 2102 | + file = os.path.join(include_dir, filename) |
| 2103 | + if os.path.isfile(file): |
| 2104 | + return file |
| 2105 | + msg = f"Could not locate include file: {filename} in {directories}" |
| 2106 | + raise FortranFileNotFoundError(msg) |
| 2107 | + |
2100 | 2108 | if pp_defs is None: |
2101 | 2109 | pp_defs = {} |
2102 | 2110 | if include_dirs is None: |
@@ -2250,34 +2258,21 @@ def append_multiline_macro(def_value: str | tuple, line: str): |
2250 | 2258 | if (match is not None) and ((len(pp_stack) == 0) or (pp_stack[-1][0] < 0)): |
2251 | 2259 | log.debug("%s !!! Include statement(%d)", line.strip(), i + 1) |
2252 | 2260 | include_filename = match.group(1).replace('"', "") |
2253 | | - include_path = None |
2254 | | - # Intentionally keep this as a list and not a set. There are cases |
2255 | | - # where projects play tricks with the include order of their headers |
2256 | | - # to get their codes to compile. Using a set would not permit that. |
2257 | | - for include_dir in include_dirs: |
2258 | | - include_path_tmp = os.path.join(include_dir, include_filename) |
2259 | | - if os.path.isfile(include_path_tmp): |
2260 | | - include_path = os.path.abspath(include_path_tmp) |
2261 | | - break |
2262 | | - if include_path is not None: |
2263 | | - try: |
2264 | | - include_file = FortranFile(include_path) |
2265 | | - include_file.load_from_disk() |
2266 | | - log.debug("\n!!! Parsing include file '%s'", include_path) |
2267 | | - _, _, _, defs_tmp = preprocess_file( |
2268 | | - include_file.contents_split, |
2269 | | - file_path=include_path, |
2270 | | - pp_defs=defs_tmp, |
2271 | | - include_dirs=include_dirs, |
2272 | | - debug=debug, |
2273 | | - ) |
2274 | | - log.debug("!!! Completed parsing include file") |
2275 | | - except FortranFileNotFoundError as e: |
2276 | | - log.debug("!!! Failed to parse include file: %s", str(e)) |
2277 | | - else: |
2278 | | - log.debug( |
2279 | | - "%s !!! Could not locate include file (%d)", line.strip(), i + 1 |
| 2261 | + try: |
| 2262 | + include_path = find_file_in_directories(include_dirs, include_filename) |
| 2263 | + include_file = FortranFile(include_path) |
| 2264 | + include_file.load_from_disk() |
| 2265 | + log.debug("\n!!! Parsing include file '%s'", include_path) |
| 2266 | + _, _, _, defs_tmp = preprocess_file( |
| 2267 | + include_file.contents_split, |
| 2268 | + file_path=include_path, |
| 2269 | + pp_defs=defs_tmp, |
| 2270 | + include_dirs=include_dirs, |
| 2271 | + debug=debug, |
2280 | 2272 | ) |
| 2273 | + log.debug("!!! Completed parsing include file") |
| 2274 | + except FortranFileNotFoundError as e: |
| 2275 | + log.debug("%s !!! %s - Ln:%d", line.strip(), str(e), i + 1) |
2281 | 2276 |
|
2282 | 2277 | # Substitute (if any) read in preprocessor macros |
2283 | 2278 | for def_tmp, value in defs_tmp.items(): |
|
0 commit comments