|
| 1 | +import sys |
| 2 | +import subprocess |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +# Read the system arguments. |
| 6 | +args = sys.argv[1:] |
| 7 | + |
| 8 | +# Set the output directory. |
| 9 | +output_dir = Path("build" , "preprocessed_files") |
| 10 | + |
| 11 | +# If output directory does not exist, create it. |
| 12 | +if not output_dir.exists(): |
| 13 | + output_dir.mkdir(parents=True) |
| 14 | + |
| 15 | +# Get the filenames with .fypp extension and convert to string. |
| 16 | +filenames = [str(f) for f in Path(".").glob("**/*.fypp")] |
| 17 | + |
| 18 | +# Filter out the macro definitions. |
| 19 | +macros = [arg for arg in args if arg.startswith("-D")] |
| 20 | + |
| 21 | +# Filter out the include paths with -I prefix. |
| 22 | +include_paths = [arg for arg in args if arg.startswith("-I")] |
| 23 | + |
| 24 | +# Declare preprocessed array. |
| 25 | +preprocessed = [] |
| 26 | + |
| 27 | +# Preprocess the .fypp files. |
| 28 | +for filename in filenames: |
| 29 | + |
| 30 | + # Get the output filename only without extension and path. |
| 31 | + output_filename = Path(filename).stem |
| 32 | + |
| 33 | + # Save the output_file to build directory. |
| 34 | + output_file = str(Path(output_dir, f"{output_filename}.f90")) |
| 35 | + |
| 36 | + subprocess.run( |
| 37 | + [ |
| 38 | + "fypp", |
| 39 | + filename, |
| 40 | + *include_paths, |
| 41 | + *macros, |
| 42 | + output_file, |
| 43 | + ], |
| 44 | + ) |
| 45 | + |
| 46 | + # Append the output_file to preprocessed array. |
| 47 | + # Along with the path to store object files. |
| 48 | + # This will save the object files in preprocessed_files directory. |
| 49 | + preprocessed.append(f"-c {output_file} -o {str(Path(output_dir, f'{output_filename}.o'))}") |
| 50 | + |
| 51 | +# Run gfortran for each preprocessed file. |
| 52 | +for file in preprocessed : |
| 53 | + file_args = file.split() |
| 54 | + subprocess.run( |
| 55 | + ["gfortran"] + file_args, |
| 56 | + check=True, |
| 57 | + ) |
| 58 | + |
| 59 | +subprocess.run(["gfortran"] + args) |
0 commit comments