Following #7890, this patch introduces the ability to read the contents of a file to the fs module. This patch introduces the ability to read files at configure time, but has some restrictions: - binary files are not supported (I don't think this will prove a problem, and if people are wanting to do something with binary files, they should probably be shelling out to their own script). - Only files outside the build directory allowed. This limitation should prevent build loops. Given that reading an arbitrary file at configure time can affect the configuration in almost arbitrary ways, meson should force a reconfigure when the given file changes. This is non-configurable, but this can easily be changed with a future keyword argument.pull/8303/head
parent
95c0790711
commit
46e3480f7c
10 changed files with 159 additions and 2 deletions
@ -0,0 +1,40 @@ |
||||
## Support for reading files at configuration time with the `fs` module |
||||
|
||||
Reading text files during configuration is now supported. This can be done at |
||||
any time after `project` has been called |
||||
|
||||
```meson |
||||
project(myproject', 'c') |
||||
license_text = run_command( |
||||
find_program('python3'), '-c', 'print(open("COPYING").read())' |
||||
).stdout().strip() |
||||
about_header = configuration_data() |
||||
about_header.add('COPYRIGHT', license_text) |
||||
about_header.add('ABOUT_STRING', meson.project_name()) |
||||
... |
||||
``` |
||||
|
||||
There are several problems with the above approach: |
||||
1. It's ugly and confusing |
||||
2. If `COPYING` changes after configuration, Meson won't correctly rebuild when |
||||
configuration data is based on the data in COPYING |
||||
3. It has extra overhead |
||||
|
||||
`fs.read` replaces the above idiom thus: |
||||
```meson |
||||
project(myproject', 'c') |
||||
fs = import('fs') |
||||
license_text = fs.read('COPYING').strip() |
||||
about_header = configuration_data() |
||||
about_header.add('COPYRIGHT', license_text) |
||||
about_header.add('ABOUT_STRING', meson.project_name()) |
||||
... |
||||
``` |
||||
|
||||
They are not equivalent, though. Files read with `fs.read` create a |
||||
configuration dependency on the file, and so if the `COPYING` file is modified, |
||||
Meson will automatically reconfigure, guaranteeing the build is consistent. It |
||||
can be used for any properly encoded text files. It supports specification of |
||||
non utf-8 encodings too, so if you're stuck with text files in a different |
||||
encoding, it can be passed as an argument. See the [`meson` |
||||
object](Reference-manual.md#meson-object) documentation for details. |
@ -0,0 +1 @@ |
||||
utf-16-text binary |
@ -0,0 +1 @@ |
||||
1.2.0 |
@ -0,0 +1,21 @@ |
||||
project( |
||||
'meson-fs-read-file', |
||||
[], |
||||
version: files('VERSION') |
||||
) |
||||
fs = import('fs') |
||||
|
||||
assert(fs.read('VERSION').strip() == meson.project_version(), 'file misread') |
||||
|
||||
expected = ( |
||||
'∮ E⋅da = Q, n → ∞, ∑ f(i) = ∏ g(i), ∀x∈ℝ: ⌈x⌉ = −⌊−x⌋, α ∧ ¬β = ¬(¬α ∨ β)' |
||||
) |
||||
assert( |
||||
fs.read('utf-16-text', encoding: 'utf-16').strip() == expected, |
||||
'file was not decoded correctly' |
||||
) |
||||
|
||||
# Make sure we handle `files()` objects properly, too |
||||
version_file = files('VERSION') |
||||
|
||||
subdir('other') |
@ -0,0 +1,3 @@ |
||||
fs = import('fs') |
||||
assert(fs.read(version_file).strip() == '1.2.0') |
||||
assert(fs.read('../VERSION').strip() == '1.2.0') |
Binary file not shown.
Loading…
Reference in new issue