This is related to fypp
which has been brought up a few time around here. I’ve been reading about this implementation, and it got me thinking if it’s possible to design a simple language wrapper. I have been considering a few options, but I am having troubles getting either to work:
Common prepare
Either in CMakeDetermineFyppCompiler.cmake
or somewhere in an included path, the following are imported:
find_package(Python3 REQUIRED)
cmake_path(GET Python3_EXECUTABLE PARENT_PATH Python3_BINDIR)
find_program(
CMAKE_Fypp_COMPILER
NAMES fypp
HINTS "${Python3_BINDIR}"
DOC "Fypp preprocessor"
)
mark_as_advanced(CMAKE_Fypp_COMPILER)
set(CMAKE_Fypp_SOURCE_FILE_EXTENSIONS fypp)
set(CMAKE_Fypp_OUTPUT_EXTENSION .f90)
set(CMAKE_Fypp_COMPILER_ENV_VAR "")
Other boilerplates like configure_file(CMakeFyppCompiler.cmake)
are left out for brevity
Option A: overwriting CMAKE_Fortran_PREPROCESS_SOURCE
as a module
Within the included path:
set(CMAKE_Fortran_SOURCE_FILE_EXTENSIONS "${CMAKE_Fortran_SOURCE_FILE_EXTENSIONS};fypp")
set(CMAKE_Fortran_PREPROCESS_SOURCE "<CMAKE_Fypp_COMPILER> <SOURCE> <PREPROCESSED_SOURCE>")
This does not seem to work because the file suffix in <PREPROCESSED_SOURCE>
is not changed and the compiler is unable to detect that it is a Fortran source file.
Is there a way to alter <PREPROCESSED_SOURCE>
(as a rule, not individually) so that it is properly consumed down the line, e.g. by CMAKE_Fortran_COMPILE_OBJECT
.
Note: it is surprising that that one is picked up, but CMAKE_Fortran_CREATE_PREPROCESS_SOURCE
which is defined in the source files is not.
Option B: defining Fypp
language
The CMakeFyppInformation.cmake
file looks generally like this
set(CMAKE_Fypp_CREATE_PREPROCESS_SOURCE "<CMAKE_Fypp_COMPILER> <DEFINES> <INCLUDES> <SOURCE> <PREPROCESSED_SOURCE>")
if (NOT CMAKE_Fypp_CREATE_SHARED_LIBRARY)
set(CMAKE_Fypp_CREATE_SHARED_LIBRARY "${CMAKE_Fortran_CREATE_SHARED_LIBRARY}")
endif ()
if (NOT CMAKE_Fypp_CREATE_SHARED_MODULE)
set(CMAKE_Fypp_CREATE_SHARED_MODULE "${CMAKE_Fortran_CREATE_SHARED_MODULE}")
endif ()
if (NOT CMAKE_Fypp_CREATE_STATIC_LIBRARY)
set(CMAKE_Fypp_CREATE_STATIC_LIBRARY "${CMAKE_Fortran_CREATE_STATIC_LIBRARY}")
endif ()
if (NOT CMAKE_Fypp_COMPILE_OBJECT)
set(CMAKE_Fypp_COMPILE_OBJECT
"<CMAKE_Fypp_COMPILER> <DEFINES> <INCLUDES> <SOURCE> <OBJECT>"
"${CMAKE_Fortran_COMPILE_OBJECT}")
endif ()
if (NOT CMAKE_Fypp_LINK_EXECUTABLE)
set(CMAKE_Fypp_LINK_EXECUTABLE "${CMAKE_Fortran_LINK_EXECUTABLE}")
endif ()
set(CMAKE_Fypp_INFORMATION_LOADED 1)
I have tried a few variations around it but with no success. Some of the issues I encountered:
- When expanding
CMAKE_Fortran_COMPILE_OBJECT
, the<OBJECT>
etc. variables are expanded with respect to the definition ofFypp
language. Is there a way to generator-expression the variables to a different language? - Is there a way to inject the “object” files of
Fypp
to be source files for the Fotran language? Ideally that would be cleanest since it would only need to define aCMAKE_Fypp_COMPILE_OBJECT
command structure. - What is missing that
CMAKE_Fypp_CREATE_PREPROCESS_SOURCE
is not being picked up? It seems that it is hard-coded?
A similar usecase for this is in swig
in order to simplify the interface.