Skip to content

Commit

Permalink
Only preprocess current files
Browse files Browse the repository at this point in the history
  • Loading branch information
awvwgk authored and arteevraina committed Sep 1, 2022
1 parent 243b1dd commit 502a0d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 39 deletions.
55 changes: 18 additions & 37 deletions fypp-gfortran.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python3
import sys
import subprocess
from pathlib import Path
Expand All @@ -13,47 +14,27 @@
output_dir.mkdir(parents=True)

# Get the filenames with .fypp extension and convert to string.
filenames = [str(f) for f in Path(".").glob("**/*.fypp")]
source_file = [arg for arg in args if arg.endswith(".fypp")]
output_file = [arg for arg in args if arg.endswith(".o")]

if len(source_file) != len(output_file):
subprocess.run(["gfortran"] + args, check=True)
sys.exit(0)

source_file = source_file[0]
output_file = output_file[0]
preprocessed = output_file.replace(".o", ".f90")

# Filter out the macro definitions.
macros = [arg for arg in args if arg.startswith("-D")]

# Filter out the include paths with -I prefix.
include_paths = [arg for arg in args if arg.startswith("-I")]

# Declare preprocessed array.
preprocessed = []

# Preprocess the .fypp files.
for filename in filenames:

# Get the output filename only without extension and path.
output_filename = Path(filename).stem

# Save the output_file to build directory.
output_file = str(Path(output_dir, f"{output_filename}.f90"))

subprocess.run(
[
"fypp",
filename,
*include_paths,
*macros,
output_file,
],
)

# Append the output_file to preprocessed array.
# Along with the path to store object files.
# This will save the object files in preprocessed_files directory.
preprocessed.append(f"-c {output_file} -o {str(Path(output_dir, f'{output_filename}.o'))}")

# Run gfortran for each preprocessed file.
for file in preprocessed :
file_args = file.split()
subprocess.run(
["gfortran"] + file_args,
check=True,
)

subprocess.run(["gfortran"] + args)
subprocess.run(
["fypp", "-n", source_file, preprocessed] + macros + include_paths,
check=True
)

args = [arg for arg in args if arg != source_file and not arg in macros] + [preprocessed]
subprocess.run(["gfortran"] + args, check=True)
4 changes: 2 additions & 2 deletions src/fpm_sources.f90
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module fpm_sources
private
public :: add_sources_from_dir, add_executable_sources

character(4), parameter :: fortran_suffixes(2) = [".f90", &
".f "]
character(5), parameter :: fortran_suffixes(3) = [".f90 ", ".fypp", &
".f "]
character(4), parameter :: c_suffixes(4) = [".c ", ".h ", ".cpp", ".hpp"]

contains
Expand Down

0 comments on commit 502a0d5

Please sign in to comment.