Can you glob source code with meson?
Asked Answered
A

4

13

Is it possible to glob source code files in a meson build?

Age answered 23/12, 2015 at 18:30 Comment(0)
S
7

Globbing source files is discouraged and is bad practice, and not only on Meson. It causes weird errors, makes it hard to have some developement files aside for development but that you don't want to build or ship, and can cause problems with incremental builds.

Explicit is better than implicit.

2021-03-02 EDIT:

Read also Why can't I specify target files with a wildcard? in the Meson FAQ.

Meson does not support this syntax and the reason for this is simple. This can not be made both reliable and fast.

If after all the warnings, you still want to do it at your own risk, the FAQ tells you how in But I really want to use wildcards!. You just use an external script to do the globbing and return the list of files (that script is called grabber.sh in that example).

c = run_command('grabber.sh')
sources = c.stdout().strip().split('\n')
e = executable('prog', sources)

Splashboard answered 27/2, 2018 at 17:16 Comment(0)
A
5

I found an example in the meson unit tests showing how to glob source, but in the comments it says this is not recommended.

if build_machine.system() == 'windows'
  c = run_command('grabber.bat')
  grabber = find_program('grabber2.bat')
else
  c = run_command('grabber.sh')
  grabber = find_program('grabber.sh')
endif


# First test running command explicitly.
if c.returncode() != 0
  error('Executing script failed.')
endif

newline = '''
'''

sources = c.stdout().strip().split(newline)

e = executable('prog', sources)

The reason this is not recommended: attempting to add files by glob'ing a directory will NOT make those files automatically appear in the build. You have to manually re-invoke meson for the files to be added to the build. Re-invoking ninja or other back-ends is not sufficient, you must reinvoke meson itself.

Age answered 28/12, 2015 at 17:44 Comment(0)
S
5

meson.build

glob = run_command('python', 'glob')
sources = glob.stdout().strip().split('\n')

glob:

import glob

sources = glob.glob('./src/*.cpp') + glob.glob('./src/**/*.cpp')
for i in sources:
    print(i)
Sinewy answered 15/8, 2021 at 14:33 Comment(0)
A
0

No it's not possible. Every sources have to be explicitly stated to build a target.

Actinia answered 18/9, 2016 at 3:7 Comment(1)
The only way I found was to call out to an external process that globs source and returns a list of files which can be iterated over in Meson.Age

© 2022 - 2024 — McMap. All rights reserved.