Skip to content

Commit ddd2747

Browse files
authored
[flang] Put ISO_Fortran_binding.h where it can be easily used (#70129)
The update stems from the discussion in https://discourse.llvm.org/t/adding-flang-specific-header-files-to-clang/72442 This is my third attempt at this. My second attempt was in pull request #69121. This is my second attempt at this. My first attempt was in pull request #68756. This pull request has three changes from the second one: - I put the test into the Driver directory rather than Examples so that it would get run without require the define LLVM_BUILD_EXAMPLES. - When installing ISO_Fortran_binding.h, I changed the location where it was installed from. - I changed the test so that it would work when flang was built with shared libraries. Here's the information from my previous attempts: 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/Driver". 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 7090422 commit ddd2747

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

flang/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ endif()
276276

277277

278278
set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
279+
if (NOT PACKAGE_VERSION)
280+
set(PACKAGE_VERSION ${LLVM_VERSION_MAJOR})
281+
endif()
279282

280283

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

flang/test/Driver/ctofortran.f90

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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
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 BINDIR=`dirname $2`
62+
export CCOMP=$BINDIR/clang
63+
if [ -x $CCOMP ]
64+
then
65+
export LIBDIR=$BINDIR/../lib
66+
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBDIR
67+
$CCOMP -c $1/$4 -o $1/cfile.o
68+
$2 $1/$3 $1/cfile.o -o $1/ctofortran
69+
$1/ctofortran # should print "PASS"
70+
else
71+
# No clang compiler, just pass by default
72+
echo "PASS"
73+
fi

0 commit comments

Comments
 (0)