diff --git a/example_packages/preprocess_fypp/README.md b/example_packages/preprocess_fypp/README.md new file mode 100644 index 0000000000..8f3d323eaa --- /dev/null +++ b/example_packages/preprocess_fypp/README.md @@ -0,0 +1,2 @@ +# preprocess_fypp +My cool new project! diff --git a/example_packages/preprocess_fypp/fpm.toml b/example_packages/preprocess_fypp/fpm.toml new file mode 100644 index 0000000000..3bb9141fed --- /dev/null +++ b/example_packages/preprocess_fypp/fpm.toml @@ -0,0 +1,2 @@ +name = "preprocess_fypp" + diff --git a/example_packages/preprocess_fypp/src/file1.fypp b/example_packages/preprocess_fypp/src/file1.fypp new file mode 100644 index 0000000000..582006a7bd --- /dev/null +++ b/example_packages/preprocess_fypp/src/file1.fypp @@ -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 \ No newline at end of file diff --git a/example_packages/preprocess_fypp/src/file2.fypp b/example_packages/preprocess_fypp/src/file2.fypp new file mode 100644 index 0000000000..5501da58c5 --- /dev/null +++ b/example_packages/preprocess_fypp/src/file2.fypp @@ -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 \ No newline at end of file diff --git a/example_packages/preprocess_fypp/src/preprocess_fypp.f90 b/example_packages/preprocess_fypp/src/preprocess_fypp.f90 new file mode 100644 index 0000000000..57ad3cb8eb --- /dev/null +++ b/example_packages/preprocess_fypp/src/preprocess_fypp.f90 @@ -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 diff --git a/fypp-gfortran.py b/fypp-gfortran.py new file mode 100755 index 0000000000..28768a6a19 --- /dev/null +++ b/fypp-gfortran.py @@ -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) + 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) diff --git a/src/fpm_sources.f90 b/src/fpm_sources.f90 index b821362c55..16586da9ef 100644 --- a/src/fpm_sources.f90 +++ b/src/fpm_sources.f90 @@ -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 diff --git a/src/fpm_targets.f90 b/src/fpm_targets.f90 index ca88bb5e35..9119f66d8a 100644 --- a/src/fpm_targets.f90 +++ b/src/fpm_targets.f90 @@ -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