From 1487a92f6ebfa06bd2a8b251c248c71e03152fcf Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sun, 14 Aug 2022 18:35:51 +0530 Subject: [PATCH 1/9] feat: added basic script for preprocessing and saving generated files --- fypp-gfortran.sh | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100755 fypp-gfortran.sh diff --git a/fypp-gfortran.sh b/fypp-gfortran.sh new file mode 100755 index 0000000000..5655cc1cbc --- /dev/null +++ b/fypp-gfortran.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +set -ex + +# Target directory for saving preprocessed project. +destdir="${DESTDIR:-build}" + +# Get fypp preprocessor +fypp="${FYPP:-$(which fypp)}" + +# Number of parallel jobs for preprocessing +if [ $(uname) = "Darwin" ]; then + njob="$(sysctl -n hw.ncpu)" +else + njob="$(nproc)" +fi + +# Create directories for the fpm project. +mkdir -p "$destdir/preprocessed_files" + +# Preprocess all files with .fypp extension in source directory. +# Save generated files in build directory. +find src -iname "*.fypp" \ + -exec basename {} .fypp ';' \ + | cut -f1 -d. | xargs -P "$njob" -I{} "$fypp" "src/{}.fypp" "$destdir/preprocessed_files/{}.f90" \ + +args=("$@") + +file=$(printf "%s\n" "${args[@]}" | grep -P '^.+\.[fF]90') + +exec gfortran "${args[@]}" + + From 9bcc10c43750980b3958cf0ebad20e376d4b5963 Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sun, 14 Aug 2022 18:40:27 +0530 Subject: [PATCH 2/9] docs: added example package --- example_packages/preprocess_fypp/README.md | 2 ++ example_packages/preprocess_fypp/fpm.toml | 2 ++ example_packages/preprocess_fypp/src/file1.fypp | 2 ++ example_packages/preprocess_fypp/src/file2.fypp | 2 ++ .../preprocess_fypp/src/preprocess_fypp.f90 | 10 ++++++++++ .../preprocess_fypp/src/preprocess_fypp.mod | Bin 0 -> 198 bytes 6 files changed, 18 insertions(+) create mode 100644 example_packages/preprocess_fypp/README.md create mode 100644 example_packages/preprocess_fypp/fpm.toml create mode 100644 example_packages/preprocess_fypp/src/file1.fypp create mode 100644 example_packages/preprocess_fypp/src/file2.fypp create mode 100644 example_packages/preprocess_fypp/src/preprocess_fypp.f90 create mode 100644 example_packages/preprocess_fypp/src/preprocess_fypp.mod 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..d4f8be3bd3 --- /dev/null +++ b/example_packages/preprocess_fypp/src/file1.fypp @@ -0,0 +1,2 @@ +#:set LOGLEVEL = 1 +print *, "LOGLEVEL: ${LOGLEVEL}$" \ 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..e28e4643f2 --- /dev/null +++ b/example_packages/preprocess_fypp/src/file2.fypp @@ -0,0 +1,2 @@ +#:set LOGLEVEL = 2 +print *, "LOGLEVEL: ${LOGLEVEL}$" \ 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/example_packages/preprocess_fypp/src/preprocess_fypp.mod b/example_packages/preprocess_fypp/src/preprocess_fypp.mod new file mode 100644 index 0000000000000000000000000000000000000000..86fdfebb0a36587f01c945afd575882ee3ae1a39 GIT binary patch literal 198 zcmV;%06G63iwFP!000001FetG3xY5d$M5?q-fiw+sL-Kf81Fs!{7)(l!*JsSltW%`@G0}huMN~4O0s-QNyh0dtX=u-84UJqTj zs8<2m%7#%6*otw!!Jm*K);PZeD338Zo$N(aO-q*BdcV7Vj0A~?g Avj6}9 literal 0 HcmV?d00001 From dcd7832532100e5dd185df059dcde36209e28352 Mon Sep 17 00:00:00 2001 From: arteevraina Date: Thu, 18 Aug 2022 07:48:52 +0530 Subject: [PATCH 3/9] fix: changed the comments and removed unused code --- fypp-gfortran.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/fypp-gfortran.sh b/fypp-gfortran.sh index 5655cc1cbc..41dd1ff710 100755 --- a/fypp-gfortran.sh +++ b/fypp-gfortran.sh @@ -15,7 +15,7 @@ else njob="$(nproc)" fi -# Create directories for the fpm project. +# Create directory for saving preprocessed files. mkdir -p "$destdir/preprocessed_files" # Preprocess all files with .fypp extension in source directory. @@ -26,8 +26,6 @@ find src -iname "*.fypp" \ args=("$@") -file=$(printf "%s\n" "${args[@]}" | grep -P '^.+\.[fF]90') - exec gfortran "${args[@]}" From baaef3601be4f58d49d9bedc4889e8e0e5f3c522 Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sat, 20 Aug 2022 14:09:03 +0530 Subject: [PATCH 4/9] refactor: remove .mod file --- .../preprocess_fypp/src/preprocess_fypp.mod | Bin 198 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 example_packages/preprocess_fypp/src/preprocess_fypp.mod diff --git a/example_packages/preprocess_fypp/src/preprocess_fypp.mod b/example_packages/preprocess_fypp/src/preprocess_fypp.mod deleted file mode 100644 index 86fdfebb0a36587f01c945afd575882ee3ae1a39..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 198 zcmV;%06G63iwFP!000001FetG3xY5d$M5?q-fiw+sL-Kf81Fs!{7)(l!*JsSltW%`@G0}huMN~4O0s-QNyh0dtX=u-84UJqTj zs8<2m%7#%6*otw!!Jm*K);PZeD338Zo$N(aO-q*BdcV7Vj0A~?g Avj6}9 From 38686a7b6757cde7ed627b719258a1a347e972de Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sat, 20 Aug 2022 14:10:27 +0530 Subject: [PATCH 5/9] feat: added python script for fypp --- fypp-gfortran.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ fypp-gfortran.sh | 31 ------------------------- 2 files changed, 59 insertions(+), 31 deletions(-) create mode 100755 fypp-gfortran.py delete mode 100755 fypp-gfortran.sh diff --git a/fypp-gfortran.py b/fypp-gfortran.py new file mode 100755 index 0000000000..f44625ff8d --- /dev/null +++ b/fypp-gfortran.py @@ -0,0 +1,59 @@ +import sys +import subprocess +from pathlib import Path + +# Read the system arguments. +args = sys.argv[1:] + +# Set the output directory. +output_dir = Path("build" , "preprocessed_files") + +# If output directory does not exist, create it. +if not output_dir.exists(): + output_dir.mkdir(parents=True) + +# Get the filenames with .fypp extension and convert to string. +filenames = [str(f) for f in Path(".").glob("**/*.fypp")] + +# 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) diff --git a/fypp-gfortran.sh b/fypp-gfortran.sh deleted file mode 100755 index 41dd1ff710..0000000000 --- a/fypp-gfortran.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env bash - -set -ex - -# Target directory for saving preprocessed project. -destdir="${DESTDIR:-build}" - -# Get fypp preprocessor -fypp="${FYPP:-$(which fypp)}" - -# Number of parallel jobs for preprocessing -if [ $(uname) = "Darwin" ]; then - njob="$(sysctl -n hw.ncpu)" -else - njob="$(nproc)" -fi - -# Create directory for saving preprocessed files. -mkdir -p "$destdir/preprocessed_files" - -# Preprocess all files with .fypp extension in source directory. -# Save generated files in build directory. -find src -iname "*.fypp" \ - -exec basename {} .fypp ';' \ - | cut -f1 -d. | xargs -P "$njob" -I{} "$fypp" "src/{}.fypp" "$destdir/preprocessed_files/{}.f90" \ - -args=("$@") - -exec gfortran "${args[@]}" - - From 689eed7e967212616cd272a763910eca0e0662b7 Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sat, 20 Aug 2022 14:10:48 +0530 Subject: [PATCH 6/9] docs: improved the file logic --- example_packages/preprocess_fypp/src/file1.fypp | 17 +++++++++++++++-- example_packages/preprocess_fypp/src/file2.fypp | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/example_packages/preprocess_fypp/src/file1.fypp b/example_packages/preprocess_fypp/src/file1.fypp index d4f8be3bd3..582006a7bd 100644 --- a/example_packages/preprocess_fypp/src/file1.fypp +++ b/example_packages/preprocess_fypp/src/file1.fypp @@ -1,2 +1,15 @@ -#:set LOGLEVEL = 1 -print *, "LOGLEVEL: ${LOGLEVEL}$" \ No newline at end of file +#: 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 index e28e4643f2..5501da58c5 100644 --- a/example_packages/preprocess_fypp/src/file2.fypp +++ b/example_packages/preprocess_fypp/src/file2.fypp @@ -1,2 +1,15 @@ -#:set LOGLEVEL = 2 -print *, "LOGLEVEL: ${LOGLEVEL}$" \ No newline at end of file +#: 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 From 47ee6225b195c0468b4526602d659adee24411df Mon Sep 17 00:00:00 2001 From: Sebastian Ehlert <28669218+awvwgk@users.noreply.github.com> Date: Sat, 20 Aug 2022 11:38:18 +0200 Subject: [PATCH 7/9] Only preprocess current files --- fypp-gfortran.py | 55 +++++++++++++++------------------------------ src/fpm_sources.f90 | 4 ++-- 2 files changed, 20 insertions(+), 39 deletions(-) diff --git a/fypp-gfortran.py b/fypp-gfortran.py index f44625ff8d..08dee63f80 100755 --- a/fypp-gfortran.py +++ b/fypp-gfortran.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import sys import subprocess from pathlib import Path @@ -13,7 +14,16 @@ 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")] @@ -21,39 +31,10 @@ # 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) 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 From 82d368c4e6bb328e43cd1bfc2534964c42a984a5 Mon Sep 17 00:00:00 2001 From: arteevraina Date: Sat, 20 Aug 2022 21:31:56 +0530 Subject: [PATCH 8/9] refactor: removed unused output dir --- fypp-gfortran.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/fypp-gfortran.py b/fypp-gfortran.py index 08dee63f80..28768a6a19 100755 --- a/fypp-gfortran.py +++ b/fypp-gfortran.py @@ -6,13 +6,6 @@ # Read the system arguments. args = sys.argv[1:] -# Set the output directory. -output_dir = Path("build" , "preprocessed_files") - -# If output directory does not exist, create it. -if not output_dir.exists(): - output_dir.mkdir(parents=True) - # 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")] From a49a97a7027c2ac6553ec5174e17917a8fe1ed93 Mon Sep 17 00:00:00 2001 From: arteevraina Date: Tue, 23 Aug 2022 22:15:39 +0530 Subject: [PATCH 9/9] fix: macros available while running tests --- src/fpm_targets.f90 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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