Skip to content

Commit 002d3a3

Browse files
committed
meson/cmake share generated input files
1 parent b680ec3 commit 002d3a3

File tree

6 files changed

+83
-135
lines changed

6 files changed

+83
-135
lines changed

src/CMakeLists.txt

Lines changed: 3 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,14 @@
11

2+
set(is_windows .false.)
23
if(WIN32)
34
set(is_windows .true.)
4-
else()
5-
set(is_windows .false.)
65
endif()
76
configure_file(pathlib.in.f90 pathlib.f90)
87

9-
set(reader_template "
10-
integer :: varid, ier, drank
11-
12-
13-
if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open'
14-
15-
ier = nf90_inq_varid(self%ncid, dname, varid)
16-
17-
if(ier == NF90_NOERR) then
18-
ier = nf90_inquire_variable(self%ncid, varid, ndims=drank)
19-
if(drank /= rank(value)) then
20-
ier = NF90_EDIMSIZE
21-
write(stderr,*) 'ERROR: ' // dname // ' rank ', drank, ' /= variable rank ',rank(value)
22-
return
23-
endif
24-
endif
25-
26-
if(ier == NF90_NOERR) then
27-
select type (value)
28-
type is (real(real64))
29-
ier = nf90_get_var(self%ncid, varid, value)
30-
type is (real(real32))
31-
ier = nf90_get_var(self%ncid, varid, value)
32-
type is (integer(int64))
33-
ier = nf90_get_var(self%ncid, varid, value)
34-
type is (integer(int32))
35-
ier = nf90_get_var(self%ncid, varid, value)
36-
class default
37-
ier = NF90_EBADTYPE
38-
end select
39-
endif
40-
41-
if (present(ierr)) ierr = ier
42-
if (check_error(ier, dname)) then
43-
if (present(ierr)) return
44-
error stop
45-
endif
46-
")
8+
file(READ reader_template.in.f90 reader_template)
479
configure_file(reader.in.f90 reader.f90)
4810

49-
set(writer_template "
50-
integer :: varid, dimids(rank(value)), ier
51-
52-
call self%def_dims(dname, dimnames=dims, dims=shape(value), dimids=dimids, ierr=ier)
53-
54-
select type (value)
55-
type is (real(real64))
56-
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_DOUBLE, dimids=dimids, varid=varid)
57-
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
58-
type is (real(real32))
59-
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_FLOAT, dimids=dimids, varid=varid)
60-
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
61-
type is (integer(int64))
62-
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_INT64, dimids=dimids, varid=varid)
63-
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
64-
type is (integer(int32))
65-
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_INT, dimids=dimids, varid=varid)
66-
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
67-
class default
68-
ierr = NF90_EBADTYPE
69-
end select
70-
71-
if (present(ierr)) ierr = ier
72-
if (check_error(ier, dname)) then
73-
if (present(ierr)) return
74-
error stop
75-
endif
76-
")
11+
file(READ writer_template.in.f90 writer_template)
7712
configure_file(writer.in.f90 writer.f90)
7813

7914
target_sources(nc4fortran PRIVATE

src/meson.build

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,20 @@ configure_file(
88
output : 'pathlib.f90',
99
configuration : conf_data)
1010

11-
reader_template = configuration_data()
12-
reader_template.set('reader_template',
13-
'''
14-
integer :: varid, ier
15-
16-
if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open'
17-
18-
ier = nf90_inq_varid(self%ncid, dname, varid)
19-
20-
if(ier == NF90_NOERR) then
21-
select type (value)
22-
type is (real(real64))
23-
ier = nf90_get_var(self%ncid, varid, value)
24-
type is (real(real32))
25-
ier = nf90_get_var(self%ncid, varid, value)
26-
type is (integer(int64))
27-
ier = nf90_get_var(self%ncid, varid, value)
28-
type is (integer(int32))
29-
ier = nf90_get_var(self%ncid, varid, value)
30-
class default
31-
ier = NF90_EBADTYPE
32-
end select
33-
endif
11+
python = find_program('python')
3412

35-
if (present(ierr)) ierr = ier
36-
if (check_error(ier, dname)) then
37-
if (present(ierr)) return
38-
error stop
39-
endif
40-
''')
13+
ret = run_command(python, '-c', 'print(open("' + meson.current_source_dir() / 'reader_template.in.f90' + '").read())')
14+
reader_template = configuration_data()
15+
reader_template.set('reader_template', ret.stdout())
4116
configure_file(
4217
input : 'reader.in.f90',
4318
output : 'reader.f90',
4419
configuration : reader_template
4520
)
4621

22+
ret = run_command(python, '-c', 'print(open("' + meson.current_source_dir() / 'writer_template.in.f90' + '").read())')
4723
writer_template = configuration_data()
48-
writer_template.set('writer_template',
49-
'''
50-
integer :: varid, dimids(rank(value)), ier
51-
52-
call self%def_dims(dname, dims, shape(value), dimids, ier)
53-
54-
select type (value)
55-
type is (real(real64))
56-
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_DOUBLE, dimids=dimids, varid=varid)
57-
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
58-
type is (real(real32))
59-
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_FLOAT, dimids=dimids, varid=varid)
60-
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
61-
type is (integer(int64))
62-
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_INT64, dimids=dimids, varid=varid)
63-
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
64-
type is (integer(int32))
65-
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_INT, dimids=dimids, varid=varid)
66-
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
67-
class default
68-
ierr = NF90_EBADTYPE
69-
end select
70-
71-
if (present(ierr)) ierr = ier
72-
if (check_error(ier, dname)) then
73-
if (present(ierr)) return
74-
error stop
75-
endif
76-
''')
24+
writer_template.set('writer_template', ret.stdout())
7725
configure_file(
7826
input : 'writer.in.f90',
7927
output : 'writer.f90',

src/reader_template.in.f90

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
integer :: varid, ier, drank
2+
3+
4+
if(.not.self%is_open) error stop 'ERROR:nc4fortran:reader file handle not open'
5+
6+
ier = nf90_inq_varid(self%ncid, dname, varid)
7+
8+
if(ier == NF90_NOERR) then
9+
ier = nf90_inquire_variable(self%ncid, varid, ndims=drank)
10+
if(drank /= rank(value)) then
11+
ier = NF90_EDIMSIZE
12+
write(stderr,*) 'ERROR: ' // dname // ' rank ', drank, ' /= variable rank ',rank(value)
13+
return
14+
endif
15+
endif
16+
17+
if(ier == NF90_NOERR) then
18+
select type (value)
19+
type is (real(real64))
20+
ier = nf90_get_var(self%ncid, varid, value)
21+
type is (real(real32))
22+
ier = nf90_get_var(self%ncid, varid, value)
23+
type is (integer(int64))
24+
ier = nf90_get_var(self%ncid, varid, value)
25+
type is (integer(int32))
26+
ier = nf90_get_var(self%ncid, varid, value)
27+
class default
28+
ier = NF90_EBADTYPE
29+
end select
30+
endif
31+
32+
if (present(ierr)) ierr = ier
33+
if (check_error(ier, dname)) then
34+
if (present(ierr)) return
35+
error stop
36+
endif

src/tests/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ add_test(NAME nc4fortran:attributes COMMAND $<TARGET_FILE:test_attributes>
1717
add_executable(test_error test_error.f90)
1818
target_link_libraries(test_error nc4fortran::nc4fortran)
1919
add_test(NAME nc4fortran:error COMMAND $<TARGET_FILE:test_error>
20-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
20+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
2121

2222
add_executable(test_exist test_exist.f90)
2323
target_link_libraries(test_exist PRIVATE nc4fortran::nc4fortran)

src/tests/meson.build

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
test_io = executable('test_io', 'test_io.f90',
2-
fortran_args: quiet,
3-
dependencies: netcdf_interface)
4-
test('ReadWrite', test_io, args: meson.current_build_dir(), timeout: 10, suite: 'nc4basic')
1+
test_array = executable('test_array', 'test_array.f90', dependencies: netcdf_interface)
2+
test('array', test_array, suite: 'nc4basic', timeout: 10)
53

64
test_attr = executable('test_attributes', 'test_attributes.f90',
75
fortran_args: quiet,
@@ -18,12 +16,17 @@ test_exist = executable('test_exist', 'test_exist.f90',
1816
dependencies: netcdf_interface)
1917
test('exist', test_exist, args: meson.current_build_dir(), timeout: 10, suite: 'nc4basic')
2018

21-
test_str = executable('test_string', 'test_string.f90',
19+
test_scalar = executable('test_scalar', 'test_scalar.f90', dependencies: netcdf_interface)
20+
test('scalar', test_scalar, suite: 'nc4basic', timeout: 10)
21+
22+
test_shape = executable('test_shape', 'test_shape.f90',
2223
fortran_args: quiet,
2324
dependencies: netcdf_interface)
24-
test('string', test_str, args: meson.current_build_dir(), timeout: 10, suite: 'nc4basic')
25+
test('shape', test_shape,
26+
is_parallel : false,
27+
timeout: 10, suite: 'nc4basic')
2528

26-
test_shape = executable('test_shape', 'test_shape.f90',
29+
test_str = executable('test_string', 'test_string.f90',
2730
fortran_args: quiet,
2831
dependencies: netcdf_interface)
29-
test('shape', test_shape, args: meson.current_build_dir(), timeout: 10, suite: 'nc4basic')
32+
test('string', test_str, args: meson.current_build_dir(), timeout: 10, suite: 'nc4basic')

src/writer_template.in.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
integer :: varid, dimids(rank(value)), ier
2+
3+
call self%def_dims(dname, dimnames=dims, dims=shape(value), dimids=dimids, ierr=ier)
4+
5+
select type (value)
6+
type is (real(real64))
7+
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_DOUBLE, dimids=dimids, varid=varid)
8+
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
9+
type is (real(real32))
10+
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_FLOAT, dimids=dimids, varid=varid)
11+
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
12+
type is (integer(int64))
13+
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_INT64, dimids=dimids, varid=varid)
14+
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
15+
type is (integer(int32))
16+
if(ier == NF90_NOERR) ier = nf90_def_var(self%ncid, dname, NF90_INT, dimids=dimids, varid=varid)
17+
if(ier == NF90_NOERR) ier = nf90_put_var(self%ncid, varid, value)
18+
class default
19+
ierr = NF90_EBADTYPE
20+
end select
21+
22+
if (present(ierr)) ierr = ier
23+
if (check_error(ier, dname)) then
24+
if (present(ierr)) return
25+
error stop
26+
endif

0 commit comments

Comments
 (0)