Skip to content

Commit 11002bd

Browse files
authored
Merge pull request #129 from aradi/fypp-cleanup
Fypp cleanup
2 parents 61129cd + 3834e2c commit 11002bd

8 files changed

+616
-562
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ build/
3535

3636
# Build directory for out-of-tree builds
3737
/build
38+
39+
# Emacs backup files
40+
*~

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.14.0)
22
project(stdlib Fortran)
33
enable_testing()
44

5+
include(${CMAKE_SOURCE_DIR}/cmake/stdlib.cmake)
6+
57
# --- compiler options
68
if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU)
79
add_compile_options(-fimplicit-none)
@@ -22,4 +24,10 @@ if(DEFINED CMAKE_MAXIMUM_RANK)
2224
set(CMAKE_MAXIMUM_RANK ${CMAKE_MAXIMUM_RANK})
2325
endif()
2426

27+
# --- find preprocessor
28+
find_program(FYPP fypp)
29+
if(NOT FYPP)
30+
message(FATAL_ERROR "Preprocessor fypp not found!")
31+
endif()
32+
2533
add_subdirectory(src)

cmake/stdlib.cmake

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Preprocesses a list of files with given preprocessor and preprocessor options
2+
#
3+
# Args:
4+
# preproc [in]: Preprocessor program
5+
# preprocopts [in]: Preprocessor options
6+
# srcext [in]: File extension of the source files
7+
# trgext [in]: File extension of the target files
8+
# srcfiles [in]: List of the source files
9+
# trgfiles [out]: Contains the list of the preprocessed files on exit
10+
#
11+
function(preprocess preproc preprocopts srcext trgext srcfiles trgfiles)
12+
13+
set(_trgfiles)
14+
foreach(srcfile IN LISTS srcfiles)
15+
string(REGEX REPLACE "\\.${srcext}$" ".${trgext}" trgfile ${srcfile})
16+
add_custom_command(
17+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${trgfile}
18+
COMMAND ${preproc} ${preprocopts} ${CMAKE_CURRENT_SOURCE_DIR}/${srcfile} ${CMAKE_CURRENT_BINARY_DIR}/${trgfile}
19+
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/${srcfile})
20+
list(APPEND _trgfiles ${CMAKE_CURRENT_BINARY_DIR}/${trgfile})
21+
endforeach()
22+
set(${trgfiles} ${_trgfiles} PARENT_SCOPE)
23+
24+
endfunction()
25+
26+
27+
28+
# Preprocesses fortran files with fypp.
29+
#
30+
# It assumes that source files have the ".fypp" extension. Target files will be
31+
# created the extension ".f90". The FYPP variable must contain the path to the
32+
# fypp-preprocessor.
33+
#
34+
# Args:
35+
# fyppopts [in]: Options to pass to fypp.
36+
# fyppfiles [in]: Files to be processed by fypp
37+
# f90files [out]: List of created f90 files on exit
38+
#
39+
function (fypp_f90 fyppopts fyppfiles f90files)
40+
preprocess("${FYPP}" "${fyppopts}" "fypp" "f90" "${fyppfiles}" _f90files)
41+
set(${f90files} ${_f90files} PARENT_SCOPE)
42+
endfunction()

src/CMakeLists.txt

+9-37
Original file line numberDiff line numberDiff line change
@@ -7,45 +7,17 @@ set(fppFiles
77
stdlib_experimental_stats_mean.fypp
88
)
99

10-
# Pre-process
11-
foreach(infileName IN LISTS fppFiles)
12-
13-
# Generate output file name
14-
string(REGEX REPLACE ".fypp\$" ".f90" outfileName "${infileName}")
15-
16-
# Create the full path for the new file
17-
set(outfile "${CMAKE_CURRENT_BINARY_DIR}/${outfileName}")
18-
19-
# Generate input file name
20-
set(infile "${CMAKE_CURRENT_SOURCE_DIR}/${infileName}")
21-
22-
# Custom command to do the processing
23-
if(DEFINED CMAKE_MAXIMUM_RANK)
24-
add_custom_command(
25-
OUTPUT "${outfile}"
26-
COMMAND fypp -DMAXRANK=${CMAKE_MAXIMUM_RANK} "${infile}" "${outfile}"
27-
MAIN_DEPENDENCY "${infile}"
28-
VERBATIM)
29-
elseif(f03rank)
30-
add_custom_command(
31-
OUTPUT "${outfile}"
32-
COMMAND fypp "${infile}" "${outfile}"
33-
MAIN_DEPENDENCY "${infile}"
34-
VERBATIM)
35-
else()
36-
add_custom_command(
37-
OUTPUT "${outfile}"
38-
COMMAND fypp -DVERSION90 "${infile}" "${outfile}"
39-
MAIN_DEPENDENCY "${infile}"
40-
VERBATIM)
41-
endif()
42-
43-
# Finally add output file to a list
44-
set(outFiles ${outFiles} "${outfile}")
45-
46-
endforeach(infileName)
4710

11+
# Custom preprocessor flags
12+
if(DEFINED CMAKE_MAXIMUM_RANK)
13+
set(fyppFlags "-DMAXRANK=${CMAKE_MAXIMUM_RANK}")
14+
elseif(f03rank)
15+
set(fyppFlags)
16+
else()
17+
set(fyppFlags "-DVERSION90")
18+
endif()
4819

20+
fypp_f90("${fyppFlags}" "${fppFiles}" outFiles)
4921

5022
set(SRC
5123
stdlib_experimental_ascii.f90

src/common.fypp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#:mute
2+
3+
#! Real kinds to be considered during templating
4+
#:set REAL_KINDS = ["sp", "dp", "qp"]
5+
6+
#! Real types to be considere during templating
7+
#:set REAL_TYPES = ["real({})".format(k) for k in REAL_KINDS]
8+
9+
#! Collected (kind, type) tuples for real types
10+
#:set REAL_KINDS_TYPES = list(zip(REAL_KINDS, REAL_TYPES))
11+
12+
13+
#! Integer kinds to be considered during templating
14+
#:set INT_KINDS = ["int8", "int16", "int32", "int64"]
15+
16+
#! Integer types to be considere during templating
17+
#:set INT_TYPES = ["integer({})".format(k) for k in INT_KINDS]
18+
19+
#! Collected (kind, type) tuples for integer types
20+
#:set INT_KINDS_TYPES = list(zip(INT_KINDS, INT_TYPES))
21+
22+
23+
#! Whether Fortran 90 compatible code should be generated
24+
#:set VERSION90 = defined('VERSION90')
25+
26+
27+
#! Ranks to be generated when templates are created
28+
#:if not defined('MAXRANK')
29+
#:if VERSION90
30+
#:set MAXRANK = 7
31+
#:else
32+
#:set MAXRANK = 15
33+
#:endif
34+
#:endif
35+
36+
37+
#! Generates an array rank suffix.
38+
#!
39+
#! Args:
40+
#! rank [in]: Integer with rank.
41+
#!
42+
#! Returns:
43+
#! Array rank suffix string (e.g. (:,:) if rank = 2)
44+
#!
45+
#:def ranksuffix(rank)
46+
#{if rank > 0}#(${":" + ",:" * (rank - 1)}$)#{endif}#
47+
#:enddef
48+
49+
#:endmute

0 commit comments

Comments
 (0)