1- #---------------------------------------------------------------------------#
2- # \file CMakeLists.txt
3- #
4- # Copyright (c) 2019 Oak Ridge National Laboratory, UT-Battelle, LLC.
5- # Distributed under an MIT open source license: see LICENSE for details.
1+ #---------------------------------*-CMake-*----------------------------------#
2+ # Copyright (c) 2019-2021 Oak Ridge National Laboratory, UT-Battelle, LLC.
3+ # License-Filename: LICENSE
4+ # SPDX-License-Identifier: MIT
65#---------------------------------------------------------------------------#
76
87cmake_minimum_required (VERSION 3.8)
@@ -14,6 +13,9 @@ cgv_find_version(Flibcpp)
1413project (Flibcpp VERSION "${Flibcpp_VERSION} " LANGUAGES CXX Fortran)
1514cmake_policy (VERSION 3.8...3.19)
1615
16+ list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR} /cmake" )
17+ include (FlibUtils)
18+
1719#---------------------------------------------------------------------------#
1820# OPTIONS
1921#---------------------------------------------------------------------------#
@@ -40,23 +42,8 @@ endif()
4042# FLAGS
4143#---------------------------------------------------------------------------#
4244
43- # Fortran standard
44- set (FLIBCPP_FORTRAN_STD "03" CACHE STRING
45- "Fortran standard for compiling generated code (options: 03/08/15/18/none)" )
46- if (FLIBCPP_FORTRAN_STD AND NOT FLIBCPP_FORTRAN_STD STREQUAL "none" )
47- if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU" )
48- set (_FLIBCPP_STD_FLAGS "-std=f20${FLIBCPP_FORTRAN_STD} " )
49- elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel" )
50- set (_FLIBCPP_STD_FLAGS "-std${FLIBCPP_FORTRAN_STD} " )
51- else ()
52- message (WARNING "Fortran standard flags are not known for "
53- "compilier '${CMAKE_Fortran_COMPILER_ID} ': ignoring"
54- "FLIBCPP_FORTRAN_STD=${FLIBCPP_FORTRAN_STD} . Configure with "
55- "the FFLAGS environment variable or explicitly specify "
56- "CMAKE_Fortran_FLAGS" )
57- set (FLIBCPP_FORTRAN_STD "none" CACHE FORCE STRING "" )
58- endif ()
59- endif ()
45+ # Define FLIBCPP_Fortran_STANDARD and add as Fortran compile options
46+ flib_fortran_standard(FLIBCPP "03" )
6047
6148# Build type
6249if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES )
@@ -73,34 +60,8 @@ endif()
7360# MODULES TO LOAD
7461#---------------------------------------------------------------------------#
7562
76- list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR} /cmake" )
77- if (CMAKE_VERSION VERSION_LESS 3.18)
78- list (APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR} /cmake/backport-cmake-318" )
79- endif ()
80-
81- if (FLIBCPP_USE_SWIG)
82- if (NOT SWIG_fortran_FOUND)
83- find_package (SWIG COMPONENTS fortran REQUIRED)
84- endif ()
85-
86- if (CMAKE_VERSION VERSION_LESS 3.12)
87- message (FATAL_ERROR "CMake 3.12 or higher is required to regenerate the "
88- "Fortran bindings using SWIG. Set FLIBCPP_USE_SWIG to OFF "
89- "and reconfigure." )
90- endif ()
91-
92- if (CMAKE_VERSION GREATER_EQUAL 3.13)
93- cmake_policy (SET CMP0078 "NEW" )
94- else ()
95- set (FLIBCPP_CMP0078 "NEW" )
96- endif ()
97- if (CMAKE_VERSION GREATER_EQUAL 3.14)
98- cmake_policy (SET CMP0086 "NEW" )
99- else ()
100- set (FLIBCPP_CMP0086 "NEW" )
101- endif ()
102- include (UseSWIG)
103- endif ()
63+ # Load SWIG if FLIBCPP_USE_SWIG is true
64+ flib_find_and_use_swig(FLIBCPP)
10465
10566# Load CTest if applicable
10667if (FLIBCPP_BUILD_TESTS OR FLIBCPP_BUILD_EXAMPLES)
@@ -113,60 +74,14 @@ endif()
11374
11475include (GNUInstallDirs)
11576
116- set (CMAKE_Fortran_MODULE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR} /module" )
117- set (FLIBCPP_INTERFACE_DIR "${CMAKE_CURRENT_SOURCE_DIR} /include" )
118- set (FLIBCPP_GENERATE_DIR "${CMAKE_CURRENT_SOURCE_DIR} /src" )
119- set (FLIBCPP_INSTALL_CONFIGDIR "${CMAKE_INSTALL_LIBDIR} /cmake/Flibcpp" )
120- set (FLIBCPP_INSTALL_MODULEDIR "${CMAKE_INSTALL_INCLUDEDIR} " )
12177set (FLIBCPP_NAMESPACE "Flibcpp::" )
78+ flib_dir_variables(FLIBCPP)
12279
12380# List of libraries exported by cmake/FlibcppConfig.cmake.in
12481set (FLIBCPP_LIBRARIES)
12582
12683function (flibcpp_add_module name )
127- if (FLIBCPP_USE_SWIG)
128- # SWIG is available; actually generate the library dynamically.
129-
130- set (src_file "${FLIBCPP_INTERFACE_DIR} /${name} .i" )
131- # We're using C++
132- set_property (SOURCE "${src_file} " PROPERTY CPLUSPLUS ON )
133- # We need to include the source directory
134- set_property (SOURCE "${src_file} " PROPERTY USE_TARGET_INCLUDE_DIRECTORIES ON )
135-
136- # Create the library
137- swig_add_library(${name}
138- LANGUAGE Fortran
139- TYPE USE_BUILD_SHARED_LIBS
140- OUTPUT_DIR "${FLIBCPP_GENERATE_DIR} "
141- SOURCES "${src_file} " ${ARGN}
142- )
143-
144- # Add SWIG headers
145- target_include_directories (${name}
146- PUBLIC
147- $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR} /include >
148- $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >
149- )
150-
151- # Install the interface file for downstream libraries to use
152- install (FILES
153- "${src_file} "
154- DESTINATION "${CMAKE_INSTALL_INCLUDEDIR} "
155- )
156-
157- else ()
158- # SWIG is *not* being used: compile the code committed in the repository,
159- # generated by the developer with SWIG.
160- add_library (${name}
161- "${FLIBCPP_GENERATE_DIR} /${name} .f90"
162- "${FLIBCPP_GENERATE_DIR} /${name} FORTRAN_wrap.cxx"
163- ${ARGN}
164- )
165- endif ()
166-
167- # Allow the library to be referred to by its namespaced version, for use by
168- # downstream projects that *directly* compile flibcpp
169- add_library (${FLIBCPP_NAMESPACE}${name} ALIAS ${name} )
84+ flib_add_fortran_module(FLIBCPP ${name} "cxx" "f90" ${ARGN} )
17085
17186 # Compile C++ code with C++11
17287 target_compile_features (${name}
@@ -176,20 +91,9 @@ function(flibcpp_add_module name)
17691 # Compile with e.g. std=c++11 instead of =gnu++11
17792 set_property (TARGET ${name} PROPERTY CXX_EXTENSIONS OFF )
17893
179- if (_FLIBCPP_STD_FLAGS)
180- # Compile Fortran code with a particular standard
181- target_compile_options (${name}
182- PUBLIC $<$<COMPILE_LANGUAGE:Fortran>:${_FLIBCPP_STD_FLAGS} >
183- )
184- endif ()
185-
186- target_include_directories (${name}
187- PUBLIC
188- # Fortran modules
189- $<BUILD_INTERFACE:${CMAKE_Fortran_MODULE_DIRECTORY} >
190- $<INSTALL_INTERFACE:${FLIBCPP_INSTALL_MODULEDIR} >
191- )
192-
94+ # Allow the library to be referred to by its namespaced version, for use by
95+ # downstream projects that *directly* compile flibcpp
96+ add_library (${FLIBCPP_NAMESPACE}${name} ALIAS ${name} )
19397 # Set up installation
19498 install (TARGETS
19599 ${name}
@@ -239,6 +143,9 @@ target_link_libraries(flc_vector flc flc_string)
239143# INSTALLATION
240144#---------------------------------------------------------------------------#
241145
146+ set (FLIBCPP_INSTALL_CONFIGDIR
147+ "${CMAKE_INSTALL_LIBDIR} /cmake/${PROJECT_NAME} " )
148+
242149# Install module files
243150install (DIRECTORY
244151 "${CMAKE_Fortran_MODULE_DIRECTORY} /"
0 commit comments