Skip to content

Commit

Permalink
Add a newline at the end of test files
Browse files Browse the repository at this point in the history
When running in some settings, a C compiler may demand newlines at the
end of each file.  Instead of modifying everywhere that writes out test
files to incorporate newlines in each indivudual string, simply add a
newline when writing it out.

Only add a newline to the end of the file if there isn't one already
there.

An examples of when this is a problem is running with `CC=clang` and
`CFLAGS="--std=c99 -pedantic-errors"` and meson.build contains (for
example) the following:

```
project('myproject', 'c')
executable('myexecutable', 'main.c')
cc = meson.get_compiler('c')
sizeof_int = cc.sizeof('int')

```

sizeof_int will be -1, because the compile failed.  The meson logs
contain the error `testfile.c:7:10: error: no newline at end of file`
  • Loading branch information
mjgarton authored and eli-schwartz committed Sep 23, 2024
1 parent b1abfa8 commit 5102f43
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mesonbuild/compilers/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,8 @@ def compile(self, code: 'mesonlib.FileOrString',
'testfile.' + self.default_suffix)
with open(srcname, 'w', encoding='utf-8') as ofile:
ofile.write(code)
if not code.endswith('\n'):
ofile.write('\n')
# ccache would result in a cache miss
no_ccache = True
code_debug = f'Code:\n{code}'
Expand Down

0 comments on commit 5102f43

Please sign in to comment.