Skip to content

Commit 7ce29a0

Browse files
committed
use cmake block()
1 parent cdb6962 commit 7ce29a0

6 files changed

+79
-95
lines changed

CMakeLists.txt

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
cmake_minimum_required(VERSION 3.19...3.30)
1+
cmake_minimum_required(VERSION 3.25...3.30)
22
# 3.19 for CheckSourceCompiles
3+
# 3.25 for block()
34

45
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
56
message(FATAL_ERROR "Please use out-of-source build:
@@ -24,11 +25,6 @@ set(CMAKE_CXX_STANDARD 20)
2425

2526
message(STATUS "${PROJECT_NAME} CMake ${CMAKE_VERSION}")
2627

27-
foreach(c IN ITEMS f03abstract f03bind
28-
f08contiguous f08submod_bind)
29-
include(cmake/${c}.cmake)
30-
endforeach()
31-
3228
include(cmake/compilers.cmake)
3329

3430
option(cppcheck "Run cppcheck")

cmake/compilers.cmake

+73-40
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ include(CheckIncludeFileCXX)
44

55
# --- abi check: C++ and Fortran compiler ABI compatibility
66

7-
function(abi_check)
7+
block()
8+
89
if(NOT DEFINED abi_compile)
910

1011
message(CHECK_START "checking that C, C++, and Fortran compilers can link")
@@ -21,32 +22,30 @@ else()
2122
endif()
2223

2324
# try_run() doesn't adequately detect failed exception handling--it may pass while ctest of the same exe fails
24-
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.25)
25-
message(CHECK_START "checking that C++ exception handling works")
26-
try_compile(exception_compile
27-
PROJECT exception
28-
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/exception_check
29-
OUTPUT_VARIABLE abi_output
30-
)
31-
if(abi_output MATCHES "ld: warning: could not create compact unwind for")
32-
message(CHECK_FAIL "no")
33-
set(HAVE_CXX_TRYCATCH false CACHE BOOL "C++ exception handling broken")
34-
message(WARNING "C++ exception handling will not work reliably due to incompatible compilers")
35-
else()
36-
message(CHECK_PASS "yes")
37-
set(HAVE_CXX_TRYCATCH true CACHE BOOL "C++ exception handling works")
38-
endif()
39-
endif()
4025

26+
message(CHECK_START "checking that C++ exception handling works")
27+
try_compile(exception_compile
28+
PROJECT exception
29+
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/exception_check
30+
OUTPUT_VARIABLE abi_output
31+
)
32+
if(abi_output MATCHES "ld: warning: could not create compact unwind for")
33+
message(CHECK_FAIL "no")
34+
set(HAVE_CXX_TRYCATCH false CACHE BOOL "C++ exception handling broken")
35+
message(WARNING "C++ exception handling will not work reliably due to incompatible compilers")
36+
else()
37+
message(CHECK_PASS "yes")
38+
set(HAVE_CXX_TRYCATCH true CACHE BOOL "C++ exception handling works")
4139
endif()
4240

41+
endif()
4342

44-
endfunction(abi_check)
45-
abi_check()
43+
endblock()
4644

4745

4846
# --- ISO_Fortran_binding.h header
49-
function(check_iso_fortran_binding)
47+
block()
48+
5049
check_include_file("ISO_Fortran_binding.h" HAVE_ISO_FORTRAN_BINDING_H)
5150

5251
# here we assume C and Fortran compilers are from the same vendor
@@ -77,39 +76,73 @@ if(HAVE_ISO_FORTRAN_BINDING_H)
7776
)
7877
endif()
7978

80-
endfunction()
81-
check_iso_fortran_binding()
79+
endblock()
80+
8281

83-
# --- GCC < 12 can't do these
8482
check_source_compiles(Fortran
85-
"program cstr
86-
use, intrinsic :: iso_c_binding, only : c_char
83+
"program test
8784
implicit none
88-
interface
89-
subroutine fun(s) bind(C)
90-
import :: c_char
91-
character(kind=c_char, len=:), pointer, intent(out) :: s
92-
end subroutine
93-
end interface
85+
real :: a(1)
86+
print '(L1)', is_contiguous(a)
87+
end program"
88+
f08contiguous
89+
)
90+
91+
check_source_compiles(Fortran
92+
"program abst
93+
94+
implicit none
95+
96+
type, abstract :: L1
97+
integer, pointer :: bullseye(:,:)
98+
end type L1
99+
100+
type, extends(L1) :: L2
101+
integer, pointer :: arrow(:)
102+
end type L2
103+
104+
class(L2), allocatable :: obj
105+
94106
end program
95107
"
108+
f03abstract
109+
)
110+
111+
include(${CMAKE_CURRENT_LIST_DIR}/f08submod_bind.cmake)
112+
113+
114+
block()
115+
116+
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
117+
118+
check_source_compiles(Fortran
119+
"subroutine fun(i) bind(C)
120+
use, intrinsic :: iso_c_binding
121+
integer(C_INT), intent(in) :: i
122+
end subroutine"
123+
f03bind
124+
)
125+
126+
127+
# --- GCC < 12 can't do these
128+
check_source_compiles(Fortran
129+
"subroutine fun(s) bind(C)
130+
use, intrinsic :: iso_c_binding
131+
character(kind=c_char, len=:), pointer, intent(out) :: s
132+
end subroutine"
96133
HAVE_C_CHAR_PTR
97134
)
98135

99136
check_source_compiles(Fortran
100-
"program string_view
101-
use, intrinsic :: iso_c_binding, only : c_char
102-
implicit none
103-
interface
104-
subroutine echo_c( str ) bind(C)
105-
import :: c_char
137+
"subroutine echo_c( str ) bind(C)
138+
use, intrinsic :: iso_c_binding
106139
character(kind=c_char, len=:), allocatable, intent(in) :: str
107-
end subroutine
108-
end interface
109-
end program"
140+
end subroutine"
110141
HAVE_C_ALLOC_CHAR
111142
)
112143

144+
endblock()
145+
113146
# --- fix errors about needing -fPIE
114147
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
115148
include(CheckPIESupported)

cmake/f03abstract.cmake

-19
This file was deleted.

cmake/f03bind.cmake

-16
This file was deleted.

cmake/f08contiguous.cmake

-8
This file was deleted.

cmake/f08submod_bind.cmake

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
function(f08submod)
2-
3-
if(DEFINED f08submod_bind OR CMAKE_VERSION VERSION_LESS 3.25)
1+
if(DEFINED f08submod_bind)
42
return()
53
endif()
64

5+
block()
6+
77
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
88

99
message(CHECK_START "Checking Fortran 2008 submodule bind(C) support")
@@ -18,6 +18,4 @@ else()
1818
message(CHECK_FAIL "no")
1919
endif()
2020

21-
endfunction()
22-
23-
f08submod()
21+
endblock()

0 commit comments

Comments
 (0)