diff options
author | Matthew Krupcale <mkrupcale@matthewkrupcale.com> | 2018-04-24 01:06:49 -0400 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2018-05-21 23:17:02 +0300 |
commit | 45cc001a40ff52254a0bd27718149dce8cebe73d (patch) | |
tree | d2619f359c7ba255138ef2ea6e08a09452b2eb8b /test cases/fortran | |
parent | cf5fe1d4401505ac808b1b018a21dbcc30cfc9f5 (diff) | |
download | meson-45cc001a40ff52254a0bd27718149dce8cebe73d.zip meson-45cc001a40ff52254a0bd27718149dce8cebe73d.tar.gz meson-45cc001a40ff52254a0bd27718149dce8cebe73d.tar.bz2 |
Add support for finding libraries in Fortran projects
* mesonbuild/compilers/c.py: Make the `find_library` method more generic by allowing the user to supply the `code` for compiling and linking.
* mesonbuild/compilers/fortran.py: Use the methods inherited from `Compiler` base class where appropriate. Also reuse `CComiler` methods where applicable. This should be sufficient to get various compiler/linker arguments as well as to compile and link Fortran programs. This was tested with `gfortran` compiler, and while the other compilers ought to work for simple cases, their methods are primarily inherited from the base `FortranCompiler` class.
* test cases/fortran/10 find library/gzip.f90: Fortran module with some basic Fortran wrapper interfaces to `gzopen`, `gzwrite`, and `gzclose` C `zlib` functions.
* test cases/fortran/10 find library/main.f90: Fortran program using the `gzip` Fortran interface module to write some data to a gzip file.
* test cases/fortran/10 find library/meson.build: Meson build file for this test case. This demonstrates the ability to link the Fortran program against an external library.
Diffstat (limited to 'test cases/fortran')
-rw-r--r-- | test cases/fortran/10 find library/gzip.f90 | 32 | ||||
-rw-r--r-- | test cases/fortran/10 find library/main.f90 | 40 | ||||
-rw-r--r-- | test cases/fortran/10 find library/meson.build | 9 |
3 files changed, 81 insertions, 0 deletions
diff --git a/test cases/fortran/10 find library/gzip.f90 b/test cases/fortran/10 find library/gzip.f90 new file mode 100644 index 0000000..2a7e7df --- /dev/null +++ b/test cases/fortran/10 find library/gzip.f90 @@ -0,0 +1,32 @@ +module gzip + + interface + function gzopen(path, mode) bind(C) + use iso_c_binding, only: c_char, c_ptr + implicit none + character(c_char), intent(in) :: path(*), mode(*) + type(c_ptr) :: gzopen + end function gzopen + end interface + + interface + function gzwrite(file, buf, len) bind(C) + use iso_c_binding, only: c_int, c_ptr + implicit none + type(c_ptr), value, intent(in) :: file + type(*), intent(in) :: buf + integer(c_int), value, intent(in) :: len + integer(c_int) :: gzwrite + end function gzwrite + end interface + + interface + function gzclose(file) bind(C) + use iso_c_binding, only: c_int, c_ptr + implicit none + type(c_ptr), value, intent(in) :: file + integer(c_int) :: gzclose + end function gzclose + end interface + +end module gzip diff --git a/test cases/fortran/10 find library/main.f90 b/test cases/fortran/10 find library/main.f90 new file mode 100644 index 0000000..2550b44 --- /dev/null +++ b/test cases/fortran/10 find library/main.f90 @@ -0,0 +1,40 @@ +program main + + use iso_c_binding, only: c_int, c_char, c_null_char, c_ptr + use gzip, only: gzopen, gzwrite, gzclose + + implicit none + + character(kind=c_char,len=*), parameter :: path = & + c_char_"test.gz"//c_null_char + character(kind=c_char,len=*), parameter :: mode = & + c_char_"wb9"//c_null_char + integer(c_int), parameter :: buffer_size = 512 + + type(c_ptr) :: file + character(len=buffer_size) :: buffer + integer(c_int) :: ret + integer :: i + + ! open file + file = gzopen(path, mode) + + ! fill buffer with data + do i=1,buffer_size/4 + write(buffer(4*(i-1)+1:4*i), '(i3.3, a)') i, new_line('') + end do + ret = gzwrite(file, buffer, buffer_size) + if (ret.ne.buffer_size) then + write(*,'(a, i3, a, i3, a)') 'Error: ', ret, ' / ', buffer_size, & + ' bytes written.' + stop 1 + end if + + ! close file + ret = gzclose(file) + if (ret.ne.0) then + print *, 'Error: failure to close file with error code ', ret + stop 1 + end if + +end program main diff --git a/test cases/fortran/10 find library/meson.build b/test cases/fortran/10 find library/meson.build new file mode 100644 index 0000000..be66888 --- /dev/null +++ b/test cases/fortran/10 find library/meson.build @@ -0,0 +1,9 @@ +project('find fortran library', 'fortran') + +fortranc = meson.get_compiler('fortran') + +sources = ['main.f90', 'gzip.f90'] +zlib = fortranc.find_library('z') + +exe = executable('zlibtest', sources, dependencies : zlib) +test('testzlib', exe) |