cmake logo

Fypp is a python based preprocessor to generate source code. It is usefull to overcome some Fortran weaknesses regarding array dimensions, types (and kind).

Follow the instructions below to integrate into your CMake project.


The following exmaple assumes the current project structure as:

  • yourproject/
    • cmake/
      • utils.cmake
    • src/
      • CMakelists.txt
      • main.f90
      • subr.fpp
      • common.fpp
    • bin/
    • CMakelists.txt

Create utils.cmake file into cmake/ folder as you see below:

# Register custom commands for processing source files with fypp (.fpp -> .f90)
#
# Args:
#     oldfiles [in]: List of files to preprocess (must have .fpp suffix)
#     newfiles [out]: List of preprocessed files (will have .F90 suffix).
#
#     All files have full path
#
# Source:
#     https://github.com/dftbplus/mpifx/blob/master/cmake/MpiFxUtils.cmake
#
function(fypp_preprocess oldfiles newfiles)

  set(_newfiles)
  foreach(oldfile IN LISTS oldfiles)
    string(REGEX REPLACE "\\.fpp" ".F90" newfile ${oldfile})
    add_custom_command(
      OUTPUT ${newfile}
      COMMAND ${FYPP} ${FYPP_FLAGS} ${oldfile} ${newfile}
      MAIN_DEPENDENCY ${oldfile})
    list(APPEND _newfiles ${newfile})
  endforeach()
  set(${newfiles} ${_newfiles} PARENT_SCOPE)

endfunction()

Include into your main yourproject/CMakeLists.txt. It enables CMake to find and make the function available across the project:

# include cmake modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
include(soulioUtils)

At this point, it requires to integrate into your src/CMakeLists.txt file. It takes care to find all those fpp files so they can be processed. Once done, new files will be created but with f90 extension.

...
# fypp to F90 (preprocess)
file(GLOB_RECURSE sources-fpp *.fpp)
fypp_preprocess("${sources-fpp}" sources-postpp)

# scan generic source code files
file(GLOB_RECURSE sources  *.f90 *.F90 *.h)

# compile both types
add_library(${BINARY}_lib STATIC ${sources} ${sources-postpp})
...

Compilation should resume as usual.

3 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *