summaryrefslogtreecommitdiffstats
path: root/Tests
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 /Tests
parent25ca8e5ce5019c0fb8648f7408b864add6b19d83 (diff)
downloadCMake-a1909e26accc234de814deb641751bdac3db1e92.zip
CMake-a1909e26accc234de814deb641751bdac3db1e92.tar.gz
CMake-a1909e26accc234de814deb641751bdac3db1e92.tar.bz2
UseSWIG: Enable SWIG Fortran target language
Diffstat (limited to 'Tests')
-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
4 files changed, 118 insertions, 0 deletions
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