summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSeth R Johnson <johnsonsr@ornl.gov>2020-02-22 15:02:47 (GMT)
committerSeth R Johnson <johnsonsr@ornl.gov>2020-02-24 17:15:48 (GMT)
commita1909e26accc234de814deb641751bdac3db1e92 (patch)
tree51e86a61dd6dc1e6afa65f92ba3ebd9d0c29e055
parent25ca8e5ce5019c0fb8648f7408b864add6b19d83 (diff)
downloadCMake-a1909e26accc234de814deb641751bdac3db1e92.zip
CMake-a1909e26accc234de814deb641751bdac3db1e92.tar.gz
CMake-a1909e26accc234de814deb641751bdac3db1e92.tar.bz2
UseSWIG: Enable SWIG Fortran target language
-rw-r--r--Help/release/dev/useswig-fortran.rst7
-rw-r--r--Modules/UseSWIG.cmake20
-rw-r--r--Tests/UseSWIG/BasicConfiguration.cmake3
-rw-r--r--Tests/UseSWIG/BasicFortran/CMakeLists.txt23
-rw-r--r--Tests/UseSWIG/CMakeLists.txt15
-rw-r--r--Tests/UseSWIG/runme.f9077
6 files changed, 145 insertions, 0 deletions
diff --git a/Help/release/dev/useswig-fortran.rst b/Help/release/dev/useswig-fortran.rst
new file mode 100644
index 0000000..17baf96
--- /dev/null
+++ b/Help/release/dev/useswig-fortran.rst
@@ -0,0 +1,7 @@
+useswig-fortran
+---------------
+
+* The :module:`UseSWIG` module now supports Fortran as a target language if
+ the ``SWIG_EXECUTABLE`` is SWIG-Fortran_.
+
+.. _`SWIG-Fortran`: https://github.com/swig-fortran/swig
diff --git a/Modules/UseSWIG.cmake b/Modules/UseSWIG.cmake
index 78522da..f6a20f8 100644
--- a/Modules/UseSWIG.cmake
+++ b/Modules/UseSWIG.cmake
@@ -341,6 +341,23 @@ function(SWIG_GET_EXTRA_OUTPUT_FILES language outfiles generatedpath infile)
list(APPEND files "${extra_file}")
endforeach()
+ if (language STREQUAL "FORTRAN" AND CMAKE_Fortran_COMPILER_LOADED)
+ # Process possible user-supplied extension in flags (obtained via parent
+ # scope variable) to determine the source file name.
+ list(FIND SWIG_COMPILATION_FLAGS "-fext" fext_idx)
+ if (fext_idx EQUAL -1)
+ # Default Fortran generated extension
+ set(fext "f90")
+ else()
+ # Get extension from user-provided flag
+ math(EXPR fext_idx "${fext_idx} + 1")
+ list(GET SWIG_COMPILATION_FLAGS "${fext_idx}" fext)
+ endif()
+ set(extra_file "${generatedpath}/${module_basename}.${fext}")
+ set_source_files_properties("${extra_file}" PROPERTIES LANGUAGE "Fortran")
+ list(APPEND files "${extra_file}")
+ endif()
+
set (${outfiles} ${files} PARENT_SCOPE)
endfunction()
@@ -415,6 +432,7 @@ function(SWIG_ADD_SOURCE_TO_MODULE name outfiles infile)
get_filename_component(swig_source_file_fullname "${infile}" ABSOLUTE)
if (NOT SWIG_MODULE_${name}_NOPROXY)
+ set(SWIG_COMPILATION_FLAGS ${swig_source_file_flags})
SWIG_GET_EXTRA_OUTPUT_FILES(${SWIG_MODULE_${name}_LANGUAGE}
swig_extra_generated_files
"${outdir}"
@@ -787,6 +805,8 @@ function(SWIG_ADD_LIBRARY name)
if (APPLE)
set_target_properties (${target_name} PROPERTIES SUFFIX ".dylib")
endif ()
+ elseif (swig_lowercase_language STREQUAL "fortran")
+ # Do *not* override the target's library prefix
else()
# assume empty prefix because we expect the module to be dynamically loaded
set_target_properties (${target_name} PROPERTIES PREFIX "")
diff --git a/Tests/UseSWIG/BasicConfiguration.cmake b/Tests/UseSWIG/BasicConfiguration.cmake
index fd3ac40..d054953 100644
--- a/Tests/UseSWIG/BasicConfiguration.cmake
+++ b/Tests/UseSWIG/BasicConfiguration.cmake
@@ -18,6 +18,9 @@ unset(SWIG_LANG_LIBRARIES)
if(${language} MATCHES csharp)
set(SWIG_LANG_TYPE TYPE SHARED)
endif()
+if(${language} MATCHES fortran)
+ set(SWIG_LANG_TYPE TYPE SHARED)
+endif()
if(${language} MATCHES python)
find_package(Python REQUIRED COMPONENTS Interpreter Development)
set(SWIG_LANG_INCLUDE_DIRECTORIES ${Python_INCLUDE_DIRS})
diff --git a/Tests/UseSWIG/BasicFortran/CMakeLists.txt b/Tests/UseSWIG/BasicFortran/CMakeLists.txt
new file mode 100644
index 0000000..e81fb85
--- /dev/null
+++ b/Tests/UseSWIG/BasicFortran/CMakeLists.txt
@@ -0,0 +1,23 @@
+cmake_minimum_required(VERSION 3.12...3.13)
+
+project(TestBasicFortran CXX Fortran)
+
+include(CTest)
+
+set(language "fortran")
+
+include (../BasicConfiguration.cmake)
+get_target_property(EXAMPLE_PREFIX example PREFIX)
+if (NOT EXAMPLE_PREFIX STREQUAL "${CMAKE_SHARED_LIBRARY_PREFIX}"
+ AND NOT EXAMPLE_PREFIX STREQUAL "EXAMPLE_PREFIX-NOTFOUND")
+ message(FATAL_ERROR "Unexpected library prefix on target: got "
+ "'${EXAMPLE_PREFIX}' but expected '${CMAKE_SHARED_LIBRARY_PREFIX}'")
+endif()
+
+
+add_executable(runme ${CMAKE_CURRENT_SOURCE_DIR}/../runme.f90)
+target_link_libraries(runme example)
+set_target_properties(runme PROPERTIES LINKER_LANGUAGE Fortran)
+
+add_test (NAME BasicFortran
+ COMMAND $<TARGET_FILE:runme>)
diff --git a/Tests/UseSWIG/CMakeLists.txt b/Tests/UseSWIG/CMakeLists.txt
index 3cc910f..d102846 100644
--- a/Tests/UseSWIG/CMakeLists.txt
+++ b/Tests/UseSWIG/CMakeLists.txt
@@ -55,6 +55,21 @@ add_test(NAME UseSWIG.BasicPerl COMMAND
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
)
+if (CMake_TEST_UseSWIG_Fortran)
+ check_language(Fortran)
+ if (CMAKE_Fortran_COMPILER)
+ add_test(NAME UseSWIG.BasicFortran COMMAND
+ ${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
+ --build-and-test
+ "${CMake_SOURCE_DIR}/Tests/UseSWIG/BasicFortran"
+ "${CMake_BINARY_DIR}/Tests/UseSWIG/BasicFortran"
+ ${build_generator_args}
+ --build-project TestBasicFortran
+ --build-options ${build_options}
+ --test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
+ )
+ endif()
+endif()
add_test(NAME UseSWIG.MultipleModules COMMAND
${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
diff --git a/Tests/UseSWIG/runme.f90 b/Tests/UseSWIG/runme.f90
new file mode 100644
index 0000000..1d985d3
--- /dev/null
+++ b/Tests/UseSWIG/runme.f90
@@ -0,0 +1,77 @@
+! File : runme.f90
+program runme
+ use ISO_FORTRAN_ENV
+ implicit none
+ integer, parameter :: STDOUT = OUTPUT_UNIT
+
+ call run()
+contains
+
+subroutine run()
+ use example
+ use iso_c_binding
+ implicit none
+
+ type(Circle) :: c
+ type(Square), target :: s ! 'target' allows it to be pointed to
+ class(Shape), pointer :: sh
+ integer(C_INT) :: n_shapes
+
+ ! ----- Object creation -----
+
+ write(STDOUT,*) "Creating some objects"
+ c = Circle(10.0d0)
+ s = Square(10.0d0)
+
+ ! ----- Access a static member -----
+ write(STDOUT,'(a,i2,a)')"A total of", s%get_nshapes(), " shapes were created"
+
+ ! ----- Member data access -----
+
+ ! Notice how we can do this using functions specific to
+ ! the 'Circle' class.
+ call c%set_x(20.0d0)
+ call c%set_y(30.0d0)
+
+ ! Now use the same functions in the base class
+ sh => s
+ call sh%set_x(-10.0d0)
+ call sh%set_y( 5.0d0)
+
+ write(STDOUT,*)"Here is their current position:"
+ write(STDOUT,'(a,f5.1,a,f5.1,a)')" Circle = (", c%get_x(), ",", c%get_y(), " )"
+ write(STDOUT,'(a,f5.1,a,f5.1,a)')" Square = (", s%get_x(), ",", s%get_y(), " )"
+
+ ! ----- Call some methods -----
+
+ write(STDOUT,*)"Here are some properties of the shapes:"
+ call print_shape(c)
+ call print_shape(s)
+
+ ! ----- Delete everything -----
+
+ ! Note: this invokes the virtual destructor
+ call c%release()
+ call s%release()
+
+ n_shapes = c%get_nshapes()
+ write(STDOUT,*) n_shapes, "shapes remain"
+ if (n_shapes /= 0) then
+ write(STDOUT,*) "Shapes were not freed properly!"
+ stop 1
+ endif
+
+ write(STDOUT,*) "Goodbye"
+end subroutine
+
+subroutine print_shape(s)
+ use example, only : Shape
+ use iso_c_binding
+ implicit none
+ class(Shape), intent(in) :: s
+
+ write(STDOUT,*)" area = ",s%area()
+ write(STDOUT,*)" perimeter = ",s%perimeter()
+end subroutine
+
+end program