Skip to content

Commit 08a2385

Browse files
committed
Added support scripts and documentation in contrib
1 parent 2d7c286 commit 08a2385

File tree

4 files changed

+290
-0
lines changed

4 files changed

+290
-0
lines changed

contrib/cmake/BuildFCSV.cmake

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
if(GIT_FOUND)
2+
# Retrieve, build, and install fortran-csv-module (aka FCSV)
3+
# distribution from GitHub
4+
set(FCSV_DIST_DIR "${CMAKE_CURRENT_BINARY_DIR}/FCSV-source")
5+
6+
set(FCSV_LOCAL_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/FCSV-artifacts")
7+
8+
# Note: "<INSTALL_DIR>" is interpolated within ExternalProject_Add to
9+
# FCSV_LOCAL_INSTALL_DIR
10+
# list(APPEND FCSV_CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>")
11+
list(APPEND FCSV_CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX:PATH=${FCSV_LOCAL_INSTALL_DIR}")
12+
13+
ExternalProject_Add(
14+
FCSV_external
15+
# Note: Use URL and URL_HASH [SHA512|SHA256|MD5]=4A54C0DE... to
16+
# download and checksum an archive. Note that URL may refer to a
17+
# local file, allowing this to work without net access.
18+
# GIT_REPOSITORY https://github.com/jacobwilliams/fortran-csv-module.git
19+
# GIT_TAG 1.2.0
20+
GIT_REPOSITORY https://github.com/apthorpe/fortran-csv-module.git
21+
GIT_TAG 2d7c286225f53c77ed113e4776953d1cf0a780ae
22+
SOURCE_DIR "${FCSV_DIST_DIR}"
23+
INSTALL_DIR "${FCSV_LOCAL_INSTALL_DIR}"
24+
CMAKE_ARGS ${FCSV_CMAKE_ARGS}
25+
BUILD_BYPRODUCTS ${FCSV_LOCAL_INSTALL_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}fcsv${CMAKE_STATIC_LIBRARY_SUFFIX}
26+
LOG_BUILD YES
27+
USES_TERMINAL_DOWNLOAD YES
28+
USES_TERMINAL_UPDATE YES
29+
)
30+
31+
# From fortran-csv-module/CMakeLists.txt:
32+
# ...
33+
# # Set default installation paths; should be invoked after setting project language(s)
34+
# include(GNUInstallDirs)
35+
# ...
36+
# # Fortran module files
37+
# install(FILES "${LIBFCSV_FORTRAN_MODULE_DIR}/csv_module.mod"
38+
# DESTINATION finclude)
39+
# ...
40+
41+
# Create ${FCSV_LOCAL_INSTALL_DIR}/finclude based on the module install location
42+
# set in fortran-csv-module/CMakeLists.txt. Creating this directory avoids a race
43+
# condition - see https://www.scivision.dev/cmake-fetchcontent-vs-external-project/
44+
file(MAKE_DIRECTORY ${FCSV_LOCAL_INSTALL_DIR}/finclude)
45+
46+
# Make the fcsv library available to the current project as an import
47+
add_library(fcsv STATIC IMPORTED GLOBAL)
48+
49+
# Set properties on fcsv target to point at the installed library location and
50+
# the module directory created above. FCSV uses `include(GNUInstallDirs)` which
51+
# typically installs libraries to ./lib which is why the IMPORTED_LOCATION below
52+
# uses the path ${FCSV_LOCAL_INSTALL_DIR}/lib
53+
set_target_properties(fcsv
54+
PROPERTIES
55+
IMPORTED_LOCATION ${FCSV_LOCAL_INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}fcsv${CMAKE_STATIC_LIBRARY_SUFFIX}
56+
INTERFACE_INCLUDE_DIRECTORIES ${FCSV_LOCAL_INSTALL_DIR}/finclude
57+
)
58+
59+
# To use this recipe, add one of the following fragments
60+
# to CMakeLists.txt after project():
61+
# find_package(Git)
62+
# include(BuildFCSV)
63+
# or
64+
# find_package(Git)
65+
# include(/path/to/BuildFCSV.cmake)
66+
67+
# To include the csv_module.mod link the fcsv library to the target
68+
# MyExecutable, add the following directives after
69+
# add_executable(MyExecutable ...):
70+
# -----
71+
# target_link_libraries(MyExecutable fcsv)
72+
# target_include_directories(MyExecutable PUBLIC $<TARGET_PROPERTY:fcsv,Fortran_MODULE_DIRECTORY>)
73+
# add_dependencies(MyExecutable FCSV_external)
74+
# -----
75+
76+
else()
77+
message(STATUS "git not available; using fallback CSV source files")
78+
79+
set(FCSV_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/contrib/fortran-csv-module/src")
80+
81+
# Full path to csv_kinds.f90
82+
set(FCSV_KINDS_SRC "${FCSV_SOURCE_DIR}/csv_kinds.f90")
83+
84+
# Full path to csv_parameters.f90
85+
set(FCSV_PARAMETERS_SRC "${FCSV_SOURCE_DIR}/csv_parameters.f90")
86+
87+
# Full path to csv_utilities.f90
88+
set(FCSV_UTILITIES_SRC "${FCSV_SOURCE_DIR}/csv_utilities.f90")
89+
90+
# Full path to csv_module.F90
91+
set(FCSV_MODULE_SRC "${FCSV_SOURCE_DIR}/csv_module.F90")
92+
93+
list(APPEND FCSV_SRC_FILES "${FCSV_KINDS_SRC}" "${FCSV_PARAMETERS_SRC}" "${FCSV_UTILITIES_SRC}" "${FCSV_MODULE_SRC}")
94+
message(STATUS "Developer Note: Append contents of FCSV_SRC_FILES to list of sources to compile")
95+
96+
message(STATUS "CSV source files are in ${FCSV_SOURCE_DIR}")
97+
98+
endif()
99+
100+
set(CSV_FOUND ON)
101+
# __END__

contrib/cmake/FindFCSV.cmake

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2+
# file LICENSE for details.
3+
4+
#[=======================================================================[.rst:
5+
FindFCSV
6+
---------
7+
8+
Find ``FCSV`` unit testing library for Fortran.
9+
10+
The module defines the following variables:
11+
12+
``FCSV_LIB_NAME``
13+
``FCSV`` library base name ('fcsv')
14+
15+
``FCSV_LIBRARY``
16+
path to the ``FCSV`` library
17+
18+
``FCSV_LIBRARY_DIR``
19+
path to the ``FCSV`` library directory
20+
21+
``FCSV_MODULE_FILE``
22+
path to the ``FCSV`` Fortran module (.mod) file
23+
24+
``FCSV_MODULE_DIR``
25+
path to the ``FCSV`` Fortran module directory
26+
27+
``FCSV_FOUND``
28+
"True" if the ``FCSV`` library and module files were found
29+
30+
Example usage:
31+
32+
.. code-block:: cmake
33+
34+
find_package(FCSV)
35+
#]=======================================================================]
36+
37+
set(FCSV_LIB_NAME fcsv)
38+
set(FCSV_FOUND OFF)
39+
40+
# Set FCSV_ROOT and FCSV_MODULE_PATH on the command line:
41+
# The following are defined in the root CMakeLists.txt file
42+
# set(FCSV_ROOT "" CACHE PATH "Installation root of FCSV library")
43+
# set(FCSV_MODULE_PATH "" CACHE PATH "Directory containing FCSV Fortran module (.mod) files")
44+
45+
# BuildFCSV.cmake sets FCSV_ROOT to
46+
# <project_root>\build\FCSV_external-prefix\src\FCSV_external-build
47+
48+
if(IS_DIRECTORY "${FCSV_ROOT}")
49+
set(SEARCH_FCSV_LIB ${FCSV_ROOT}/lib)
50+
set(SEARCH_FCSV_MOD ${FCSV_ROOT}/include ${FCSV_ROOT}/module
51+
${FCSV_ROOT}/finclude ${FCSV_ROOT}/finclude/fcsv)
52+
endif()
53+
54+
if(IS_DIRECTORY "${FCSV_MODULE_PATH}")
55+
list(APPEND SEARCH_FCSV_MOD "${FCSV_MODULE_PATH}")
56+
endif()
57+
58+
find_library(FCSV_LIBRARY
59+
NAMES "${FCSV_LIB_NAME}"
60+
PATHS ${SEARCH_FCSV_LIB}
61+
)
62+
63+
# message(STATUS "Debug: SEARCH_FCSV_MOD=${SEARCH_FCSV_MOD}")
64+
find_file(FCSV_MODULE_FILE
65+
NAMES "csv_module.mod" "${FCSV_LIB_NAME}.mod"
66+
PATHS ${SEARCH_FCSV_MOD}
67+
)
68+
69+
# Set FCSV_FOUND if both FCSV_LIBRARY and FCSV_MODULE_FILE are found
70+
include(FindPackageHandleStandardArgs)
71+
find_package_handle_standard_args(FCSV DEFAULT_MSG
72+
FCSV_LIBRARY FCSV_MODULE_FILE)
73+
74+
if(FCSV_FOUND)
75+
##### Set Output Variables #####
76+
77+
# Set the following:
78+
# - FCSV_LIB_NAME (at top; "fcsv")
79+
# - FCSV_LIBRARY_DIR
80+
# - FCSV_MODULE_DIR
81+
# - FCSV_MODULE_FILE (from find_file())
82+
get_filename_component(FCSV_LIBRARY_DIR "${FCSV_LIBRARY}" DIRECTORY)
83+
get_filename_component(FCSV_MODULE_DIR "${FCSV_MODULE_FILE}" DIRECTORY)
84+
message(STATUS "Found FCSV library under ${FCSV_LIBRARY_DIR}")
85+
else()
86+
message(STATUS "Cannot find FCSV (is FCSV_ROOT set? '${FCSV_ROOT}')")
87+
endif()
88+
89+
# These variables are set to be compatible with the naming scheme used
90+
# in original FCSV example CMake setup; see
91+
# build/FCSV-source/examples/example1/CMakeLists.txt
92+
# - FCSV_LIB_NAME (= "fcsv")
93+
# - FCSV_LIBRARY_DIR
94+
# - FCSV_MODULE_DIR
95+
96+
# Note: This needs to be manually added to the list of source files
97+
# required for unit tests
98+
# - FCSV_MODULE_FILE

contrib/cmake/IncludeFCSV.cmake

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Retrieve fortran-csv-module from external source and include into project
2+
# See https://github.com/jacobwilliams/fortran-csv-module
3+
#
4+
#!!! Verify all these!
5+
# The following output variables are set by the FCSV subproject
6+
# - FCSV_LIB_NAME --> LIBFCSV_NAME
7+
# - FCSV_LIBRARY_DIR --> ???
8+
# - FCSV_MODULE_DIR --> LIBFCSV_FORTRAN_MODULE_DIR
9+
set(FCSV_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/FCSV-source")
10+
11+
FetchContent_Declare(
12+
FCSV_external
13+
# GIT_REPOSITORY https://github.com/jacobwilliams/fortran-csv-module.git
14+
GIT_REPOSITORY https://github.com/apthorpe/fortran-csv-module.git
15+
GIT_TAG 2d7c286225f53c77ed113e4776953d1cf0a780ae
16+
SOURCE_DIR "${FCSV_SOURCE_DIR}"
17+
)
18+
19+
FetchContent_MakeAvailable(FCSV_external)
20+
FetchContent_GetProperties(FCSV_external)
21+
# FetchContent_GetProperties(FCSV_external
22+
# POPULATED FCSV_external_POPULATED
23+
# )
24+
25+
# To use this recipe, add one of the following include() lines
26+
# to CMakeLists.txt after project():
27+
# include(IncludeFCSV)
28+
# or
29+
# include(/path/to/IncludeFCSV.cmake)
30+
31+
# To include the csv_module.mod link the fcsv library to the target
32+
# MyExecutable, add the following directives after
33+
# add_executable(MyExecutable ...):
34+
# -----
35+
# target_link_libraries(MyExecutable fcsv)
36+
# target_include_directories(MyExecutable PUBLIC $<TARGET_PROPERTY:fcsv,Fortran_MODULE_DIRECTORY>)
37+
# add_dependencies(MyExecutable fcsv)
38+
# -----
39+
40+
# set(FCSV_LIB_NAME "${LIBFCSV_NAME}")
41+
# # set(FCSV_LIBRARY_DIR --generator expression giving path of TARGET:${LIBFCSV_NAME}--
42+
# set(FCSV_MODULE_DIR "${LIBFCSV_FORTRAN_MODULE_DIR}")
43+
44+
set(FCSV_FOUND "${FCSV_external_POPULATED}")
45+
# __END__

contrib/cmake/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributed CMake Support
2+
3+
The following CMake recipes are intended for developers using
4+
fortran-csv-module as a dependency in their own projects.
5+
6+
Please read through the recipes before use; they contain
7+
documentation that will help integrate fortran-csv-module
8+
with a CMake based project.
9+
10+
Each recipe has advantages and disadvantages as listed below.
11+
Only one should be needed but they may be chained such that
12+
CMake preferentially uses a local installation and falls back
13+
to pulling source code over the network or vice versa.
14+
15+
## FindFCSV.cmake
16+
17+
FindFCSV searches the host system for a previously installed fcsv library
18+
(*e.g.* `libfcsv.a`) and the Fortran module file `csv_module.mod`.
19+
No version checking is performed and there will likely be problems
20+
if fortran-csv-module and the parent project are built with
21+
different Fortran compilers.
22+
23+
## BuildFCSV.cmake
24+
25+
__Important__: Review and modify `GIT_REPOSITORY` and `GIT_TAG` before
26+
use; they currently point to a test fork of the library. Also, ensure that
27+
`include(Git)` and `include(ExternalProject)` are called in the parent
28+
project before this recipe.
29+
30+
This recipe uses `ExternalProject_Add` to retrieve the fortran-csv-module
31+
project and build it as an standalone dependency of the parent
32+
project. Tests and compiler flags of FCSV are kept separate from the
33+
parent project.
34+
35+
## IncludeFCSV.cmake
36+
37+
__Important__: Review and modify `GIT_REPOSITORY` and `GIT_TAG` before
38+
use; they currently point to a test fork of the library. Also, ensure that
39+
`include(Git)` and `include(FetchContent)` are called in the parent
40+
project before this recipe.
41+
42+
This recipe uses `FetchContent` to retrieve the fortran-csv-module
43+
project and build it as part of the parent project. This has two
44+
important effects: compiler flags of the parent and child project
45+
are merged and CTest will run FCSV's tests in addition to those of
46+
the parent project.

0 commit comments

Comments
 (0)