Skip to content

Commit b9f2d69

Browse files
committed
Support building from extant generated source
1 parent 0657186 commit b9f2d69

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

CMakeLists.txt

+37-17
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,55 @@ enable_testing()
1313
# SWIG setup
1414
##---------------------------------------------------------------------------##
1515

16-
find_package(SWIG REQUIRED)
17-
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
18-
include(CheckSWIGFortran)
19-
if (CMAKE_VERSION VERSION_LESS 3.20)
20-
# TODO: This is until Fortran support gets added to the upstream cmake script
21-
include(UseSWIGFortran)
16+
option(USE_SWIG "Enable SWIG generation" ON)
17+
if (USE_SWIG)
18+
find_package(SWIG)
19+
endif()
20+
21+
if (USE_SWIG AND SWIG_FOUND)
22+
# SWIG is requested and available; make sure it's the Fortran fork.
23+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
24+
include(CheckSWIGFortran)
25+
if (CMAKE_VERSION VERSION_LESS 3.20)
26+
# TODO: This is until Fortran support gets added to the upstream cmake script
27+
include(UseSWIGFortran)
28+
else()
29+
cmake_policy(SET CMP0078 "NEW")
30+
cmake_policy(SET CMP0086 "NEW")
31+
include(UseSWIG)
32+
endif()
2233
else()
23-
cmake_policy(SET CMP0078 "NEW")
24-
cmake_policy(SET CMP0086 "NEW")
25-
include(UseSWIG)
34+
set(USE_SWIG FALSE)
2635
endif()
2736

2837
##---------------------------------------------------------------------------##
2938
# LIBRARY
3039
##---------------------------------------------------------------------------##
40+
set(GENERATE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/generated)
3141

32-
macro(swig_fortran_add_library name)
42+
function(swig_fortran_add_library name)
3343
# We're using C++
3444
set_property(SOURCE src/${name}.i PROPERTY CPLUSPLUS ON)
3545
# We need to include the source directory
3646
set_property(SOURCE src/${name}.i
3747
PROPERTY USE_TARGET_INCLUDE_DIRECTORIES ON)
3848

39-
# Create the library
40-
swig_add_library(${name}
41-
LANGUAGE Fortran
42-
TYPE USE_BUILD_SHARED_LIBS
43-
OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/generated
44-
SOURCES src/${name}.i ${ARGN})
49+
if (USE_SWIG)
50+
# SWIG is available; actually generate the library dynamically.
51+
# Create the library
52+
swig_add_library(${name}
53+
LANGUAGE Fortran
54+
TYPE USE_BUILD_SHARED_LIBS
55+
OUTPUT_DIR "${GENERATE_DIR}"
56+
SOURCES src/${name}.i ${ARGN})
57+
else()
58+
# SWIG is *not* being used: compile the code committed in the repository,
59+
# generated by the developer with SWIG.
60+
add_library(${name}
61+
"${GENERATE_DIR}/${name}.f90"
62+
"${GENERATE_DIR}/${name}FORTRAN_wrap.cxx"
63+
${ARGN})
64+
endif()
4565

4666
# Enable C++11
4767
target_compile_features(${name} PRIVATE cxx_std_11)
@@ -61,7 +81,7 @@ macro(swig_fortran_add_library name)
6181
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
6282
$<INSTALL_INTERFACE:modules>
6383
)
64-
endmacro()
84+
endfunction()
6585

6686
# Four SWIG libraries
6787
swig_fortran_add_library(utils)

0 commit comments

Comments
 (0)