|
| 1 | +#!/usr/bin/env python3 |
1 | 2 | import sys
|
2 | 3 | import subprocess
|
3 | 4 | from pathlib import Path
|
|
13 | 14 | output_dir.mkdir(parents=True)
|
14 | 15 |
|
15 | 16 | # Get the filenames with .fypp extension and convert to string.
|
16 |
| -filenames = [str(f) for f in Path(".").glob("**/*.fypp")] |
| 17 | +source_file = [arg for arg in args if arg.endswith(".fypp")] |
| 18 | +output_file = [arg for arg in args if arg.endswith(".o")] |
| 19 | + |
| 20 | +if len(source_file) != len(output_file): |
| 21 | + subprocess.run(["gfortran"] + args, check=True) |
| 22 | + sys.exit(0) |
| 23 | + |
| 24 | +source_file = source_file[0] |
| 25 | +output_file = output_file[0] |
| 26 | +preprocessed = output_file.replace(".o", ".f90") |
17 | 27 |
|
18 | 28 | # Filter out the macro definitions.
|
19 | 29 | macros = [arg for arg in args if arg.startswith("-D")]
|
20 | 30 |
|
21 | 31 | # Filter out the include paths with -I prefix.
|
22 | 32 | include_paths = [arg for arg in args if arg.startswith("-I")]
|
23 | 33 |
|
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) |
| 34 | +subprocess.run( |
| 35 | + ["fypp", "-n", source_file, preprocessed] + macros + include_paths, |
| 36 | + check=True |
| 37 | +) |
| 38 | + |
| 39 | +args = [arg for arg in args if arg != source_file and not arg in macros] + [preprocessed] |
| 40 | +subprocess.run(["gfortran"] + args, check=True) |
0 commit comments