Skip to content

Commit e6e9084

Browse files
authored
[flang] Put ISO_Fortran_binding.h where it can be easily used (#69121)
The update stems from the discussion in https://discourse.llvm.org/t/adding-flang-specific-header-files-to-clang/72442 This is my second attempt at this. My first attempt was in pull request #68756. I decided to put ISO_Fortran_binding.h in a place where it would be accessible with the include: "#include<ISO_Fortran_binding.h>" rather than "#include<fortran/ISO_Fortran_binding.h>" because this is what gfortran implements. Note that the file is also installed into ".../include/flang", so if a user wanted to access the file from a compiler other than clang, it would be available. I added a test in ".../flang/test/Examples". To make the test work, I also needed to put ISO_Fortran_binding.h into the build area. Although the flang project depends on clang, clang may not always be available in a flang build. For example, when building just the "check-flang" target, the "clang" executable may not be available at the time the new test gets run. To account for this, I made the test's script check for the existence of the "clang" executable. If "clang" is not available, it simply prints "PASS". If it is available, it fully builds and executes the test. On success, this will also print "PASS"
1 parent b781c7a commit e6e9084

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

flang/CMakeLists.txt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,10 @@ if (NOT(FLANG_DEFAULT_RTLIB STREQUAL ""))
273273
"Default runtime library to use (empty for platform default)" FORCE)
274274
endif()
275275

276-
277-
278276
set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
279-
277+
if (NOT PACKAGE_VERSION)
278+
set(PACKAGE_VERSION ${LLVM_VERSION_MAJOR})
279+
endif()
280280

281281
if (NOT DEFINED FLANG_VERSION_MAJOR)
282282
set(FLANG_VERSION_MAJOR ${LLVM_VERSION_MAJOR})
@@ -490,3 +490,17 @@ if (NOT LLVM_INSTALL_TOOLCHAIN_ONLY)
490490
PATTERN "*.inc"
491491
)
492492
endif()
493+
494+
# Put ISO_Fortran_binding.h into the include files of the build area now
495+
# so that we can run tests before installing
496+
include(GetClangResourceDir)
497+
get_clang_resource_dir(HEADER_BINARY_DIR PREFIX ${LLVM_LIBRARY_OUTPUT_INTDIR}/.. SUBDIR include)
498+
configure_file(
499+
${FLANG_SOURCE_DIR}/include/flang/ISO_Fortran_binding.h
500+
${HEADER_BINARY_DIR}/ISO_Fortran_binding.h)
501+
502+
# And also install it into the install area
503+
get_clang_resource_dir(HEADER_INSTALL_DIR PREFIX ${CMAKE_INSTALL_PREFIX} SUBDIR include)
504+
install(
505+
FILES ${CMAKE_INSTALL_PREFIX}/include/flang/ISO_Fortran_binding.h
506+
DESTINATION ${HEADER_INSTALL_DIR})

flang/test/Examples/ctofortran.f90

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
! UNSUPPORTED: system-windows
2+
! RUN: split-file %s %t
3+
! RUN: chmod +x %t/runtest.sh
4+
! RUN: %t/runtest.sh %t %flang $t/ffile.f90 $t/cfile.c | FileCheck %s
5+
6+
!--- ffile.f90
7+
subroutine foo(a) bind(c)
8+
integer :: a(:)
9+
if (lbound(a, 1) .ne. 1) then
10+
print *, 'FAIL expected 1 for lbound but got ',lbound(a, 1)
11+
stop 1
12+
endif
13+
14+
if (ubound(a, 1) .ne. 10) then
15+
print *, 'FAIL expected 10 for ubound but got ',ubound(a, 1)
16+
stop 1
17+
endif
18+
19+
do i = lbound(a,1),ubound(a,1)
20+
!print *, a(i)
21+
if (a(i) .ne. i) then
22+
print *, 'FAIL expected', i, ' for index ',i, ' but got ',a(i)
23+
stop 1
24+
endif
25+
enddo
26+
print *, 'PASS'
27+
end subroutine foo
28+
29+
! CHECK: PASS
30+
!--- cfile.c
31+
#include <stdio.h>
32+
#include <stdlib.h>
33+
#include <ISO_Fortran_binding.h>
34+
35+
void foo(CFI_cdesc_t*);
36+
37+
int a[10];
38+
39+
int main() {
40+
int i, res;
41+
static CFI_CDESC_T(1) r1;
42+
CFI_cdesc_t *desc = (CFI_cdesc_t*)&r1;
43+
CFI_index_t extent[1] = {10};
44+
45+
for(i=0; i<10; ++i) {
46+
a[i] = i+1;
47+
}
48+
49+
res = CFI_establish(desc, (void*)a, CFI_attribute_other, CFI_type_int32_t,
50+
sizeof(int), 1, extent);
51+
if (res != 0) {
52+
printf("FAIL CFI_establish returned %d instead of 0.\n",res);
53+
exit(1);
54+
}
55+
56+
foo(desc);
57+
return 0;
58+
}
59+
!--- runtest.sh
60+
#!/bin/bash
61+
export CCOMP=`dirname $2`/clang
62+
if [ -x $CCOMP ]
63+
then
64+
$CCOMP -c $1/$4 -o $1/cfile.o
65+
$2 $1/$3 $1/cfile.o -o $1/ctofortran
66+
$1/ctofortran # should print "PASS"
67+
else
68+
# No clang compiler, just pass by default
69+
echo "PASS"
70+
fi

0 commit comments

Comments
 (0)