Skip to content

Commit e64c6ea

Browse files
committed
Refactor fypp-preprocessing in CMake
1 parent 61129cd commit e64c6ea

File tree

4 files changed

+62
-37
lines changed

4 files changed

+62
-37
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

0 commit comments

Comments
 (0)