Skip to content

Commit 8e35623

Browse files
committed
Expand Fortran interface generation to accept a list of files
1 parent a1b6fe0 commit 8e35623

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

‎cmake/ecbuild_generate_fortran_interfaces.cmake‎

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#
1717
# ecbuild_generate_fortran_interfaces( TARGET <name>
1818
# DESTINATION <path>
19-
# DIRECTORIES <directory1> [<directory2> ...]
19+
# { DIRECTORIES <directory1> [<directory2> ...] | FILES <file1> [<file2> ...] }
2020
# [ PARALLEL <integer> ]
2121
# [ INCLUDE_DIRS <name> ]
2222
# [ GENERATED <name> ]
@@ -34,8 +34,9 @@
3434
# DESTINATION : required
3535
# sub-directory of ``CMAKE_CURRENT_BINARY_DIR`` to install target to
3636
#
37-
# DIRECTORIES : required
38-
# list of directories in ``SOURCE_DIR`` in which to search for Fortran files to be processed
37+
# DIRECTORIES | FILES : required
38+
# | list of directories in ``SOURCE_DIR`` in which to search for Fortran files to be processed, *or*
39+
# | list of Fortran files in ``SOURCE_DIR`` to be processed
3940
#
4041
# PARALLEL : optional, defaults to 1
4142
# number of processes to use (always 1 on Darwin systems)
@@ -47,7 +48,7 @@
4748
# name of CMake variable to store the list of generated interface files, including the full path to each
4849
#
4950
# SOURCE_DIR : optional, defaults to ``CMAKE_CURRENT_SOURCE_DIR``
50-
# directory in which to look for the sub-directories given as arguments to ``DIRECTORIES``
51+
# directory in which to look for the sub-directories or source files given as arguments to ``DIRECTORIES`` or ``FILES``
5152
#
5253
# SUFFIX : optional, defaults to ".intfb.h"
5354
# suffix to apply to name of each interface file
@@ -58,12 +59,15 @@
5859
# Usage
5960
# _____
6061
#
61-
# The listed directories will be recursively searched for Fortran files of the
62-
# form ``<fname>.[fF]``, ``<fname>.[fF]90``, ``<fname>.[fF]03`` or
63-
# ``<fname>.[fF]08``. For each matching file, a file ``<fname><suffix>`` will be
64-
# created containing the interface blocks for all external subprograms within
65-
# it, where ``<suffix>`` is the value given to the ``SUFFIX`` option. If a file
66-
# contains no such subprograms, no interface file will be generated for it.
62+
# Given a list of directories, they will be recursively searched for Fortran
63+
# files of the form ``<fname>.[fF]``, ``<fname>.[fF]90``, ``<fname>.[fF]03`` or
64+
# ``<fname>.[fF]08``. Given a list of files, these must be an exact match and
65+
# contained within ``SOURCE_DIR``. Either ``DIRECTORIES`` or ``FILES`` (or
66+
# both) must be provided. For each matching file, a file ``<fname><suffix>``
67+
# will be created containing the interface blocks for all external subprograms
68+
# within it, where ``<suffix>`` is the value given to the ``SUFFIX`` option. If
69+
# a file contains no such subprograms, no interface file will be generated for
70+
# it.
6771
#
6872
##############################################################################
6973

@@ -95,7 +99,7 @@ function( ecbuild_generate_fortran_interfaces )
9599

96100
set( options )
97101
set( single_value_args TARGET DESTINATION PARALLEL INCLUDE_DIRS GENERATED SOURCE_DIR SUFFIX FCM_CONFIG_FILE )
98-
set( multi_value_args DIRECTORIES )
102+
set( multi_value_args DIRECTORIES FILES )
99103

100104
cmake_parse_arguments( P "${options}" "${single_value_args}" "${multi_value_args}" ${_FIRST_ARG} ${ARGN} )
101105

@@ -107,8 +111,8 @@ function( ecbuild_generate_fortran_interfaces )
107111
ecbuild_error( "ecbuild_generate_fortran_interfaces: DESTINATION argument missing" )
108112
endif()
109113

110-
if( NOT DEFINED P_DIRECTORIES )
111-
ecbuild_error( "ecbuild_generate_fortran_interfaces: DIRECTORIES argument missing" )
114+
if( NOT DEFINED P_DIRECTORIES AND NOT DEFINED P_FILES )
115+
ecbuild_error( "ecbuild_generate_fortran_interfaces: Neither DIRECTORIES nor FILES argument provided" )
112116
endif()
113117

114118
if( NOT DEFINED P_PARALLEL OR (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") )
@@ -151,15 +155,28 @@ function( ecbuild_generate_fortran_interfaces )
151155
ecbuild_error( "ecbuild_generate_fortran_interfaces: needs fcm configuration in ${FCM_CONFIG_FILE}" )
152156
endif()
153157

154-
foreach( _srcdir ${P_DIRECTORIES} )
155-
if( _srcdir MATCHES "/$" )
156-
ecbuild_critical("ecbuild_generate_fortran_interfaces: directory ${_srcdir} must not end with /")
157-
endif()
158-
ecbuild_list_add_pattern( LIST fortran_files SOURCE_DIR ${P_SOURCE_DIR}
159-
GLOB ${_srcdir}/*.[fF] ${_srcdir}/*.[fF]90 ${_srcdir}/*.[fF]03 ${_srcdir}/*.[fF]08 QUIET )
160-
endforeach()
158+
if( DEFINED P_DIRECTORIES )
159+
foreach( _srcdir ${P_DIRECTORIES} )
160+
if( _srcdir MATCHES "/$" )
161+
ecbuild_critical("ecbuild_generate_fortran_interfaces: directory ${_srcdir} must not end with /")
162+
endif()
163+
ecbuild_list_add_pattern( LIST fortran_files SOURCE_DIR ${P_SOURCE_DIR}
164+
GLOB ${_srcdir}/*.[fF] ${_srcdir}/*.[fF]90 ${_srcdir}/*.[fF]03 ${_srcdir}/*.[fF]08 QUIET )
165+
endforeach()
166+
167+
string( REPLACE ";" " " _srcdirs "${P_DIRECTORIES}" )
168+
endif()
169+
170+
if( DEFINED P_FILES )
171+
foreach( _srcfile ${P_FILES} )
172+
ecbuild_list_add_pattern( LIST fortran_files SOURCE_DIR ${P_SOURCE_DIR}
173+
GLOB ${_srcfile} QUIET )
174+
endforeach()
175+
176+
string( REPLACE ";" " " _srcfiles "${P_FILES}" )
177+
endif()
161178

162-
string( REPLACE ";" " " _srcdirs "${P_DIRECTORIES}" )
179+
string(JOIN " " _srcs "${_srcdirs}" "${_srcfiles}")
163180

164181
set( _cnt 0 )
165182
set( interface_files "" )
@@ -190,7 +207,7 @@ function( ecbuild_generate_fortran_interfaces )
190207
add_custom_command(
191208
OUTPUT ${_timestamp}
192209
COMMAND ${CMAKE_COMMAND} -E remove_directory ${_fcm_lock}
193-
COMMAND ${FCM_EXECUTABLE} make -j ${P_PARALLEL} --config-file=${FCM_CONFIG_FILE} interfaces.ns-incl=${_srcdirs} interfaces.source=${P_SOURCE_DIR}
210+
COMMAND ${FCM_EXECUTABLE} make -j ${P_PARALLEL} --config-file=${FCM_CONFIG_FILE} interfaces.ns-incl=${_srcs} interfaces.source=${P_SOURCE_DIR}
194211
COMMAND ${CMAKE_COMMAND} -E touch ${_timestamp}
195212
DEPENDS ${fortran_files}
196213
COMMENT "[fcm] Generating ${_cnt} Fortran interface files for target ${P_TARGET} in ${CMAKE_CURRENT_BINARY_DIR}/${P_DESTINATION}/interfaces/include"

0 commit comments

Comments
 (0)