oz10
oz10

Reputation: 158514

Can you glob source code with meson?

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

Upvotes: 14

Views: 8838

Answers (4)

GlobLord
GlobLord

Reputation: 51

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)

Upvotes: 5

liberforce
liberforce

Reputation: 11454

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)

Upvotes: 7

oz10
oz10

Reputation: 158514

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.

Upvotes: 5

arteymix
arteymix

Reputation: 431

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

Upvotes: -1

Related Questions