Skip to content

Commit e1e3de3

Browse files
committed
Export SWIG interface files for downstream usage
This allows *other* C++/Fortran codes to integrate their standard library usage with Flibcpp's Fortran proxy types.
1 parent 18b2a95 commit e1e3de3

21 files changed

+76
-27
lines changed

CMakeLists.txt

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,21 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/flibcpp_version.cpp.in"
103103
include(GNUInstallDirs)
104104

105105
set(CMAKE_Fortran_MODULE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/module")
106-
set(FLIBCPP_GENERATE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/generated")
106+
set(FLIBCPP_INTERFACE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
107+
set(FLIBCPP_GENERATE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
107108
set(FLIBCPP_INSTALL_CONFIGDIR "${CMAKE_INSTALL_LIBDIR}/cmake/Flibcpp")
108109
set(FLIBCPP_INSTALL_MODULEDIR "${CMAKE_INSTALL_INCLUDEDIR}")
109110
set(FLIBCPP_NAMESPACE Flibcpp::)
110111

112+
# List of libraries exported by cmake/FlibcppConfig.cmake.in
111113
set(FLIBCPP_LIBRARIES)
112114

113115
function(swig_fortran_add_module name)
116+
set(src_file "${FLIBCPP_INTERFACE_DIR}/${name}.i")
114117
# We're using C++
115-
set_property(SOURCE src/${name}.i PROPERTY CPLUSPLUS ON)
118+
set_property(SOURCE "${src_file}" PROPERTY CPLUSPLUS ON)
116119
# We need to include the source directory
117-
set_property(SOURCE src/${name}.i PROPERTY USE_TARGET_INCLUDE_DIRECTORIES ON)
120+
set_property(SOURCE "${src_file}" PROPERTY USE_TARGET_INCLUDE_DIRECTORIES ON)
118121

119122
if (FLIBCPP_USE_SWIG)
120123
# SWIG is available; actually generate the library dynamically.
@@ -123,8 +126,22 @@ function(swig_fortran_add_module name)
123126
LANGUAGE Fortran
124127
TYPE USE_BUILD_SHARED_LIBS
125128
OUTPUT_DIR "${FLIBCPP_GENERATE_DIR}"
126-
SOURCES src/${name}.i ${ARGN}
129+
SOURCES "${src_file}" ${ARGN}
127130
)
131+
132+
# Add SWIG headers
133+
target_include_directories(${name}
134+
PUBLIC
135+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
136+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
137+
)
138+
139+
# Install the interface file for downstream libraries to use
140+
install(FILES
141+
"${src_file}"
142+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
143+
)
144+
128145
else()
129146
# SWIG is *not* being used: compile the code committed in the repository,
130147
# generated by the developer with SWIG.
@@ -142,30 +159,36 @@ function(swig_fortran_add_module name)
142159
)
143160

144161
target_include_directories(${name}
145-
PRIVATE
146-
# Internal C++ headers
147-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
148162
PUBLIC
149163
# Fortran modules
150164
$<BUILD_INTERFACE:${CMAKE_Fortran_MODULE_DIRECTORY}>
151165
$<INSTALL_INTERFACE:${FLIBCPP_INSTALL_MODULEDIR}>
152166
)
153167

154168
# Set up installation
155-
install(TARGETS ${name}
169+
install(TARGETS
170+
${name}
156171
EXPORT Flibcpp-targets
157172
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
158173
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
159174
)
160175
# Add to list of targets to export
161176
set(FLIBCPP_LIBRARIES ${FLIBCPP_LIBRARIES} ${FLIBCPP_NAMESPACE}${name}
162-
PARENT_SCOPE
177+
PARENT_SCOPE
163178
)
164179
endfunction()
165180

166-
# Four SWIG libraries
181+
# Install primary flc module, compiling version info as well
167182
swig_fortran_add_module(flc "${FLIBCPP_VERSION_CPP}")
168183

184+
# Also install 'import_flc' if using SWIG
185+
if (FLIBCPP_USE_SWIG)
186+
install(FILES
187+
"${CMAKE_CURRENT_SOURCE_DIR}/include/import_flc.i"
188+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
189+
)
190+
endif()
191+
169192
swig_fortran_add_module(flc_algorithm)
170193
target_link_libraries(flc_algorithm flc_random flc)
171194

@@ -183,7 +206,8 @@ target_link_libraries(flc_vector flc flc_string)
183206
#---------------------------------------------------------------------------#
184207

185208
# Install module files
186-
install(DIRECTORY "${CMAKE_Fortran_MODULE_DIRECTORY}/"
209+
install(DIRECTORY
210+
"${CMAKE_Fortran_MODULE_DIRECTORY}/"
187211
DESTINATION "${FLIBCPP_INSTALL_MODULEDIR}"
188212
)
189213

@@ -197,20 +221,20 @@ install(EXPORT Flibcpp-targets
197221
include(CMakePackageConfigHelpers)
198222

199223
configure_package_config_file(
200-
${CMAKE_CURRENT_LIST_DIR}/cmake/FlibcppConfig.cmake.in
201-
${CMAKE_CURRENT_BINARY_DIR}/FlibcppConfig.cmake
224+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/FlibcppConfig.cmake.in"
225+
"${CMAKE_CURRENT_BINARY_DIR}/FlibcppConfig.cmake"
202226
INSTALL_DESTINATION ${FLIBCPP_INSTALL_CONFIGDIR}
203227
)
204228

205229
write_basic_package_version_file(
206-
${CMAKE_CURRENT_BINARY_DIR}/FlibcppConfigVersion.cmake
230+
"${CMAKE_CURRENT_BINARY_DIR}/FlibcppConfigVersion.cmake"
207231
VERSION ${PROJECT_VERSION}
208232
COMPATIBILITY SameMajorVersion
209233
)
210234

211-
install(
212-
FILES ${CMAKE_CURRENT_BINARY_DIR}/FlibcppConfig.cmake
213-
${CMAKE_CURRENT_BINARY_DIR}/FlibcppConfigVersion.cmake
235+
install(FILES
236+
"${CMAKE_CURRENT_BINARY_DIR}/FlibcppConfig.cmake"
237+
"${CMAKE_CURRENT_BINARY_DIR}/FlibcppConfigVersion.cmake"
214238
DESTINATION ${FLIBCPP_INSTALL_CONFIGDIR}
215239
)
216240

doc/interface.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,37 @@ flc
1515

1616
The primary file defines typemaps.
1717

18-
.. literalinclude:: ../src/flc.i
18+
.. literalinclude:: ../include/flc.i
1919
:linenos:
2020

2121
flc_algorithm
2222
=============
2323

24-
.. literalinclude:: ../src/flc_algorithm.i
24+
.. literalinclude:: ../include/flc_algorithm.i
2525
:linenos:
2626

2727
flc_chrono
2828
=============
2929

30-
.. literalinclude:: ../src/flc_chrono.i
30+
.. literalinclude:: ../include/flc_chrono.i
3131
:linenos:
3232

3333
flc_random
3434
=============
3535

36-
.. literalinclude:: ../src/flc_random.i
36+
.. literalinclude:: ../include/flc_random.i
3737
:linenos:
3838

3939
flc_string
4040
=============
4141

42-
.. literalinclude:: ../src/flc_string.i
42+
.. literalinclude:: ../include/flc_string.i
4343
:linenos:
4444

4545
flc_vector
4646
=============
4747

48-
.. literalinclude:: ../src/flc_vector.i
48+
.. literalinclude:: ../include/flc_vector.i
4949
:linenos:
5050

5151
.. ############################################################################

doc/introduction.rst

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ By default, Flibcpp builds shared libraries. Add the CMake argument
3434
.. _YUM: https://access.redhat.com/solutions/9934
3535
.. _Flibcpp source code: https://github.com/swig-fortran/flibcpp/releases
3636

37-
Downstream usage
38-
================
37+
Downstream usage as a library
38+
=============================
3939

4040
The Flibcpp library is most easily used when your downstream app is built with
4141
CMake. It should require a single line to initialize::
@@ -63,6 +63,32 @@ library names. Depending on your system configuration, you might have to
6363
also explicitly link your app against the compiler's C++ standard libraries
6464
using ``-lstdc++``.
6565

66+
Downstream usage as a component
67+
===============================
68+
69+
Flibcpp's SWIG interface files can be used with your Fortran-accessible
70+
C++ project to seamlessly integrate the Flibcpp Fortran wrapper code with
71+
yours. To start, you must have the latest version of the `SWIG+Fortran`_ tool
72+
installed on your machine: the version of SWIG used by your installation of
73+
Flibcpp *must* match the version used by your downstream library/app. When you
74+
build Flibcpp for downstream SWIG usage, you must configure using ``cmake
75+
-DFLIBCPP_USE_SWIG=ON ..``. This will cause the SWIG interface files to be
76+
installed to ``${CMAKE_PREFIX_PATH}/include`` to make them available
77+
downstream. Finally, in your downstream SWIG interface code, instead of calling
78+
``%import <flc.i>`` you must use ``%include <import_flc.i>``. This is necessary
79+
to inject function declarations and other internal macros into your wrapper
80+
code.
81+
82+
At that point, all you have to do is (for example) ``%import <flc_vector>`` to
83+
allow ``std::vector<double>`` in your library headers to be wrapped by
84+
Flibcpp's ``VectorReal8`` Fortran proxy derived type.
85+
86+
An `example C++/Fortran library`_ that integrates with Flibcpp will be made
87+
available on Github.
88+
89+
.. _SWIG+Fortran: https://github.com/swig-fortran
90+
.. _example C++/Fortran library: https://github.com/swig-fortran/flibcpp-example-lib).
91+
6692
Developing
6793
==========
6894

@@ -74,8 +100,6 @@ examples, and documentation can be independently enabled using the
74100
``FLIBCPP_BUILD_TESTS``, ``FLIBCPP_BUILD_EXAMPLES``, and ``FLIBCPP_BUILD_DOCS``
75101
options.
76102

77-
.. _SWIG+Fortran: https://github.com/swig-fortran
78-
79103
.. ############################################################################
80104
.. end of doc/introduction.rst
81105
.. ############################################################################

src/flc.i renamed to include/flc.i

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)