Skip to content

Fypp cleanup #129

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

Merged
merged 4 commits into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ build/

# Build directory for out-of-tree builds
/build

# Emacs backup files
*~
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.14.0)
project(stdlib Fortran)
enable_testing()

include(${CMAKE_SOURCE_DIR}/cmake/stdlib.cmake)

# --- compiler options
if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU)
add_compile_options(-fimplicit-none)
Expand All @@ -22,4 +24,10 @@ if(DEFINED CMAKE_MAXIMUM_RANK)
set(CMAKE_MAXIMUM_RANK ${CMAKE_MAXIMUM_RANK})
endif()

# --- find preprocessor
find_program(FYPP fypp)
if(NOT FYPP)
message(FATAL_ERROR "Preprocessor fypp not found!")
endif()

add_subdirectory(src)
42 changes: 42 additions & 0 deletions cmake/stdlib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Preprocesses a list of files with given preprocessor and preprocessor options
#
# Args:
# preproc [in]: Preprocessor program
# preprocopts [in]: Preprocessor options
# srcext [in]: File extension of the source files
# trgext [in]: File extension of the target files
# srcfiles [in]: List of the source files
# trgfiles [out]: Contains the list of the preprocessed files on exit
#
function(preprocess preproc preprocopts srcext trgext srcfiles trgfiles)

set(_trgfiles)
foreach(srcfile IN LISTS srcfiles)
string(REGEX REPLACE "\\.${srcext}$" ".${trgext}" trgfile ${srcfile})
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${trgfile}
COMMAND ${preproc} ${preprocopts} ${CMAKE_CURRENT_SOURCE_DIR}/${srcfile} ${CMAKE_CURRENT_BINARY_DIR}/${trgfile}
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${srcfile})
list(APPEND _trgfiles ${CMAKE_CURRENT_BINARY_DIR}/${trgfile})
endforeach()
set(${trgfiles} ${_trgfiles} PARENT_SCOPE)

endfunction()



# Preprocesses fortran files with fypp.
#
# It assumes that source files have the ".fypp" extension. Target files will be
# created the extension ".f90". The FYPP variable must contain the path to the
# fypp-preprocessor.
#
# Args:
# fyppopts [in]: Options to pass to fypp.
# fyppfiles [in]: Files to be processed by fypp
# f90files [out]: List of created f90 files on exit
#
function (fypp_f90 fyppopts fyppfiles f90files)
preprocess("${FYPP}" "${fyppopts}" "fypp" "f90" "${fyppfiles}" _f90files)
set(${f90files} ${_f90files} PARENT_SCOPE)
endfunction()
46 changes: 9 additions & 37 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,17 @@ set(fppFiles
stdlib_experimental_stats_mean.fypp
)

# Pre-process
foreach(infileName IN LISTS fppFiles)

# Generate output file name
string(REGEX REPLACE ".fypp\$" ".f90" outfileName "${infileName}")

# Create the full path for the new file
set(outfile "${CMAKE_CURRENT_BINARY_DIR}/${outfileName}")

# Generate input file name
set(infile "${CMAKE_CURRENT_SOURCE_DIR}/${infileName}")

# Custom command to do the processing
if(DEFINED CMAKE_MAXIMUM_RANK)
add_custom_command(
OUTPUT "${outfile}"
COMMAND fypp -DMAXRANK=${CMAKE_MAXIMUM_RANK} "${infile}" "${outfile}"
MAIN_DEPENDENCY "${infile}"
VERBATIM)
elseif(f03rank)
add_custom_command(
OUTPUT "${outfile}"
COMMAND fypp "${infile}" "${outfile}"
MAIN_DEPENDENCY "${infile}"
VERBATIM)
else()
add_custom_command(
OUTPUT "${outfile}"
COMMAND fypp -DVERSION90 "${infile}" "${outfile}"
MAIN_DEPENDENCY "${infile}"
VERBATIM)
endif()

# Finally add output file to a list
set(outFiles ${outFiles} "${outfile}")

endforeach(infileName)

# Custom preprocessor flags
if(DEFINED CMAKE_MAXIMUM_RANK)
set(fyppFlags "-DMAXRANK=${CMAKE_MAXIMUM_RANK}")
elseif(f03rank)
set(fyppFlags)
else()
set(fyppFlags "-DVERSION90")
endif()

fypp_f90("${fyppFlags}" "${fppFiles}" outFiles)

set(SRC
stdlib_experimental_ascii.f90
Expand Down
49 changes: 49 additions & 0 deletions src/common.fypp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#:mute

#! Real kinds to be considered during templating
#:set REAL_KINDS = ["sp", "dp", "qp"]

#! Real types to be considere during templating
#:set REAL_TYPES = ["real({})".format(k) for k in REAL_KINDS]

#! Collected (kind, type) tuples for real types
#:set REAL_KINDS_TYPES = list(zip(REAL_KINDS, REAL_TYPES))


#! Integer kinds to be considered during templating
#:set INT_KINDS = ["int8", "int16", "int32", "int64"]

#! Integer types to be considere during templating
#:set INT_TYPES = ["integer({})".format(k) for k in INT_KINDS]

#! Collected (kind, type) tuples for integer types
#:set INT_KINDS_TYPES = list(zip(INT_KINDS, INT_TYPES))


#! Whether Fortran 90 compatible code should be generated
#:set VERSION90 = defined('VERSION90')


#! Ranks to be generated when templates are created
#:if not defined('MAXRANK')
#:if VERSION90
#:set MAXRANK = 7
#:else
#:set MAXRANK = 15
#:endif
#:endif


#! Generates an array rank suffix.
#!
#! Args:
#! rank [in]: Integer with rank.
#!
#! Returns:
#! Array rank suffix string (e.g. (:,:) if rank = 2)
#!
#:def ranksuffix(rank)
#{if rank > 0}#(${":" + ",:" * (rank - 1)}$)#{endif}#
#:enddef

#:endmute
Loading