Skip to content

Commit c011b1e

Browse files
committed
accomodate f2008 compilers via submodule
1 parent 4d479fd commit c011b1e

File tree

6 files changed

+86
-29
lines changed

6 files changed

+86
-29
lines changed

Diff for: CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ enable_testing()
55
# this avoids stdlib and projects using stdlib from having to introspect stdlib's directory structure
66
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR})
77

8+
# compiler feature checks
9+
include(CheckFortranSourceCompiles)
10+
check_fortran_source_compiles("error stop i; end" f18errorstop SRC_EXT f90)
11+
812
add_subdirectory(src)

Diff for: src/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ set(SRC
66

77
add_library(fortran_stdlib ${SRC})
88

9+
if(f18errorstop)
10+
target_sources(fortran_stdlib PRIVATE f18estop.f90)
11+
else()
12+
target_sources(fortran_stdlib PRIVATE f08estop.f90)
13+
endif()
14+
915
add_subdirectory(tests)
1016

1117
install(TARGETS fortran_stdlib

Diff for: src/f08estop.f90

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
submodule (stdlib_experimental_error) estop
2+
3+
contains
4+
5+
module procedure error_stop
6+
! Aborts the program with nonzero exit code
7+
! this is a fallback for Fortran 2008 error stop (e.g. Intel 19.1/2020 compiler)
8+
!
9+
! The "stop <character>" statement generally has return code 0.
10+
! To allow non-zero return code termination with character message,
11+
! error_stop() uses the statement "error stop", which by default
12+
! has exit code 1 and prints the message to stderr.
13+
! An optional integer return code "code" may be specified.
14+
!
15+
! Example
16+
! -------
17+
!
18+
! call error_stop("Invalid argument")
19+
20+
write(stderr,*) msg
21+
22+
if(present(code)) then
23+
select case (code)
24+
case (1)
25+
error stop 1
26+
case (2)
27+
error stop 2
28+
case (77)
29+
error stop 77
30+
case default
31+
write(stderr,*) 'ERROR: code ',code,' was specified.'
32+
error stop
33+
end select
34+
else
35+
error stop
36+
endif
37+
end procedure
38+
39+
end submodule estop

Diff for: src/f18estop.f90

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
submodule (stdlib_experimental_error) estop
2+
3+
contains
4+
5+
module procedure error_stop
6+
! Aborts the program with nonzero exit code
7+
!
8+
! The "stop <character>" statement generally has return code 0.
9+
! To allow non-zero return code termination with character message,
10+
! error_stop() uses the statement "error stop", which by default
11+
! has exit code 1 and prints the message to stderr.
12+
! An optional integer return code "code" may be specified.
13+
!
14+
! Example
15+
! -------
16+
!
17+
! call error_stop("Invalid argument")
18+
19+
if(present(code)) then
20+
write(stderr,*) msg
21+
error stop code
22+
else
23+
error stop msg
24+
endif
25+
end procedure
26+
27+
end submodule estop

Diff for: src/stdlib_experimental_error.f90

+8-27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ module stdlib_experimental_error
22
use, intrinsic :: iso_fortran_env, only: stderr=>error_unit
33
implicit none
44
private
5+
6+
interface ! f{08,18}estop.f90
7+
module subroutine error_stop(msg, code)
8+
character(*), intent(in) :: msg
9+
integer, intent(in), optional :: code
10+
end subroutine error_stop
11+
end interface
12+
513
public :: assert, error_stop
614

715
contains
@@ -23,31 +31,4 @@ subroutine assert(condition, code)
2331
if (.not. condition) call error_stop("Assert failed.", code)
2432
end subroutine
2533

26-
subroutine error_stop(msg, code)
27-
! Aborts the program with nonzero exit code
28-
!
29-
! The "stop <character>" statement generally has return code 0.
30-
! To allow non-zero return code termination with character message,
31-
! error_stop() uses the statement "error stop", which by default
32-
! has exit code 1 and prints the message to stderr.
33-
! An optional integer return code "code" may be specified.
34-
!
35-
! Example
36-
! -------
37-
!
38-
! call error_stop("Invalid argument")
39-
40-
character(len=*) :: msg ! Message to print on stderr
41-
integer, intent(in), optional :: code
42-
43-
integer :: returncode
44-
45-
if(present(code)) then
46-
write(stderr,*) msg
47-
error stop code
48-
else
49-
error stop msg
50-
endif
51-
end subroutine
52-
5334
end module

Diff for: src/tests/loadtxt/test_loadtxt.f90

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
program test_loadtxt
22
use iso_fortran_env, only: sp=>real32, dp=>real64 ,qp=>real128
33
use stdlib_experimental_io, only: loadtxt
4+
use stdlib_experimental_error, only: error_stop
45
implicit none
56

67
real(sp), allocatable :: s(:, :)
@@ -46,8 +47,7 @@ subroutine print_array(a)
4647
print *, a(i, :)
4748
end do
4849
class default
49-
write(*,'(a)')'The proposed type is not supported'
50-
error stop
50+
call error_stop('The proposed type is not supported')
5151
end select
5252

5353
end subroutine

0 commit comments

Comments
 (0)