Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Compiler Wrapper script for Preprocessing using fypp #729

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions example_packages/preprocess_fypp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# preprocess_fypp
My cool new project!
2 changes: 2 additions & 0 deletions example_packages/preprocess_fypp/fpm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name = "preprocess_fypp"

15 changes: 15 additions & 0 deletions example_packages/preprocess_fypp/src/file1.fypp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#: set SHOW = 1

module my_mod1
implicit none

real, parameter :: pi = 3.1415926536
real, parameter :: e = 2.7182818285

contains

subroutine show_consts()
print *, "pi = ", pi
if (${SHOW}$ == 1) print *, "e = ", e
end subroutine show_consts
end module my_mod1
15 changes: 15 additions & 0 deletions example_packages/preprocess_fypp/src/file2.fypp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#: set SHOW = 2

module my_mod2
implicit none

real, parameter :: pi = 3.1415926536
real, parameter :: e = 2.7182818285

contains

subroutine show_consts2()
print *, "pi = ", pi
if (${SHOW}$ == 1) print *, "e = ", e
end subroutine show_consts2
end module my_mod2
10 changes: 10 additions & 0 deletions example_packages/preprocess_fypp/src/preprocess_fypp.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module preprocess_fypp
implicit none
private

public :: say_hello
contains
subroutine say_hello
print *, "Hello, preprocess_fypp!"
end subroutine say_hello
end module preprocess_fypp
33 changes: 33 additions & 0 deletions fypp-gfortran.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3
import sys
import subprocess
from pathlib import Path

# Read the system arguments.
args = sys.argv[1:]

# Get the filenames with .fypp extension and convert to string.
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)
jvdp1 marked this conversation as resolved.
Show resolved Hide resolved
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")]

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
5 changes: 3 additions & 2 deletions src/fpm_targets.f90
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,9 @@ subroutine build_target_list(targets,model)

call add_target(targets,package=model%packages(j)%name,type = FPM_TARGET_OBJECT,&
output_name = get_object_name(sources(i)), &
source = sources(i) &
)
source = sources(i), &
macros = model%packages(j)%macros, &
version = model%packages(j)%version)

if (sources(i)%unit_scope == FPM_SCOPE_APP) then

Expand Down