3

Using meson build, is it possible to test for the existence of a directory in my project?

For example, I typically put acceptance tests next to my unit tests in a folder structure like this:

library/
        header.hp
        src/
            lib.cpp
        tests/
              acceptance_test/
              unit_test/ 

I don't always have acceptance tests, and I'd like to avoid having to have a meson.build file there if it isn't necessary. I'd much rather have a conditional subdir('acceptance_test') if the directory acceptance_test/ exists.

oz10
  • 153,307
  • 27
  • 93
  • 128

2 Answers2

5

Looking through the reference manual, I don't see any direct support for this.

You can use run_command, doing something like

if run_command('[', '-d', dirname, ']').returncode() == 0
    message('directory exists')
endif

but, of course, that has the disadvantage of not working across platforms.

Ryan Burn
  • 2,126
  • 1
  • 14
  • 35
  • I didn't think `run_command` will work because the commands are not actually run while generating the configuration, they are run by ninja after the fact. – oz10 Jan 07 '16 at 23:47
  • 1
    I just tried it. It prints 'directory exists' when you run the the meson command, and I didn't have any trouble running subdir within the if condition either. – Ryan Burn Jan 08 '16 at 00:34
  • @mickb My comment was based on what the wiki & unit test cases said, I appreciate you taking the time to try it out. – oz10 Jan 08 '16 at 21:08
  • 1
    @mickb, I imagine I could call a python script in run_command, and since meson is python3 that's already a dependency. – oz10 Jan 09 '16 at 19:22
5

Update: Starting with Meson v0.53.0, you can use the Filesystem module:

fs = import('fs')
if fs.is_dir('<dirname>')
  message('directory exists')
endif

This is portable, and does not depend on a shell.

ManuelAtWork
  • 2,198
  • 29
  • 34