Beginner Meson questions.

737 views
Skip to first unread message

Stephen Morton

unread,
Nov 23, 2016, 1:21:06 PM11/23/16
to The Meson Build System
I'm investigating alternatives to gnu make for a new project. I've been looking at Meson and have some very beginner Meson questions. I have really tried to RTFM myself first, and to do some figuring out, but I'm still left with these questions.

As an exercise to understand Meson, I'm converting an real existing Makefile. I've got this that works. (Names have been changed but this is otherwise a working example.)

project('foo','c')

add_global_arguments(['-Wno-error=unused-but-set-variable'], language: 'c')

executable('foo_server', 
           ['foo_server.c', 'foo_utils.c'], 
           c_args: '-m32', 
           link_args: '-m32' )
executable('foo_client',
           ['foo_dist.c', 'foo_client.c', 'foo_utils.c', 'foo_nonblock_client.c'],
           c_args: '-m32',
           link_args: '-m32')


c=compiler.get_compiler('c')
pth = c.find_library('pthread')
executable('bar', 'bar.c', 
           link_args: ['-m64'],
           c_args: '-m64',
           dependency: pth )

#executable('bar', 'bar.c', link_args: ['-l:/lib64/libpthread.so.0', '-m64'], c_args: '-m64')


Questions
  1. Notice the repetition of c_args and link_args in two of the executables. It would be nice to group them and set that once. Is this possible without having totally separate projects in separate directories? (See additional note below)
  2. I see how I can add per-target arguments at the time I define the executable. Is it possible to add them after the fact, some sort of executable.append() or something?
  3. In the original Makefile, the additional link argument adding libpthread was contingent on the gcc version. I cannot see how I would add an "if" conditional for the meson.build file. (Getting the compiler version looks easy; that's not what I'm asking.)
  4. Meson appears to do automatic include file dependency handling. But I didn't see that in the documentation. Is this true?

For #1, I tried using some python, but it was not valid meson configuration language syntax
db_args=['-m32']
db_linkargs=['-m32']
db_args.append('-Wno-error=unused-but-set-variable')


Any advice is great.

Thanks

Stephen

Jussi Pakkanen

unread,
Nov 24, 2016, 3:18:13 AM11/24/16
to Stephen Morton, The Meson Build System
On Wed, Nov 23, 2016 at 8:21 PM, Stephen Morton
<stephen....@gmail.com> wrote:


> c=compiler.get_compiler('c')
> pth = c.find_library('pthread')

There is a simpler more portable way to do this:

pth = dependency('threads')

> I see how I can add per-target arguments at the time I define the
> executable. Is it possible to add them after the fact, some sort of
> executable.append() or something?

It is not possible by design. All objects are immutable so appending
afterwards is not possible. This makes it simpler to reason about
targets, since every piece of data about it is specified in one single
location.

> Meson appears to do automatic include file dependency handling. But I didn't
> see that in the documentation. Is this true?

Yes. Just declare sources, Meson takes care of the rest.

> For #1, I tried using some python, but it was not valid meson configuration
> language syntax
> db_args=['-m32']
> db_linkargs=['-m32']
> db_args.append('-Wno-error=unused-but-set-variable')

Append is a mutating operation so it is not permitted. However we have
a += operator for this use case:

db_args += ['-Wno-error=unused-but-set-variable')]

This is not mutating the state because it creates a new object and
assigns it to db_args.

Stephen Morton

unread,
Nov 24, 2016, 11:19:34 AM11/24/16
to The Meson Build System
That is great. Thanks. 

I have some more small questions

 - Is there a reference somewhere for the meson language? (Syntax of if statements, "+=", etc.) I'm sure there is but I can't find it. I feel stupid asking, but it was not obvious.
 - Obviously the below doesn't work. I'd need some string manipulation functions to get it to work. I guess I'll find them in the document above.

c=meson.get_compiler('c')
message('compiler version' + c.version())
version = c.version()
if c.version > '3.4.0'
   dep_list += [dependency('threads')]
endif

 - I mostly build with 'ninja -C build'. Do I ever have to re-run meson? I guess I need to re-run it when creating differently built products, whether using a different cross-compiler, or perhaps different command-line build flags (?) e.g. for production versus testing builds?
   - I don't quite understand the relationship between meson and ninja. When I started I assumed that every time I changed the meson.build file, I'd need to run meson and then ninja as meson was required to "compile" the meson.build file into ninja "assembler" and then ninja would take it from there. But it seems that perhaps ninja calls back into meson. Is there a document that you can point me to that will help me grok this?

Thanks again,

Stephen

Jussi Pakkanen

unread,
Nov 24, 2016, 3:23:56 PM11/24/16
to Stephen Morton, The Meson Build System
On Thu, Nov 24, 2016 at 6:19 PM, Stephen Morton
<stephen....@gmail.com> wrote:
> That is great. Thanks.
>
> I have some more small questions
>
> - Is there a reference somewhere for the meson language? (Syntax of if
> statements, "+=", etc.) I'm sure there is but I can't find it. I feel stupid
> asking, but it was not obvious.

https://github.com/mesonbuild/meson/wiki/Syntax

> - Obviously the below doesn't work. I'd need some string manipulation
> functions to get it to work. I guess I'll find them in the document above.
>
> c=meson.get_compiler('c')
> message('compiler version' + c.version())
> version = c.version()
> if c.version > '3.4.0'
> dep_list += [dependency('threads')]
> endif

if meson.get_compiler('c').version().version_compare('>3.4.0')

https://github.com/mesonbuild/meson/wiki/Reference%20manual

> - I mostly build with 'ninja -C build'. Do I ever have to re-run meson? I
> guess I need to re-run it when creating differently built products, whether
> using a different cross-compiler, or perhaps different command-line build
> flags (?) e.g. for production versus testing builds?
>
> - I don't quite understand the relationship between meson and ninja. When
> I started I assumed that every time I changed the meson.build file, I'd need
> to run meson and then ninja as meson was required to "compile" the
> meson.build file into ninja "assembler" and then ninja would take it from
> there. But it seems that perhaps ninja calls back into meson. Is there a
> document that you can point me to that will help me grok this?

You only ever need to run the Meson binary manually when setting up a
build directory for the first time. After that you always run Ninja.
No matter what changes you do, just run Ninja, it will sort everything
out. If it doesn't, that is an error and you should file a bug.
Reply all
Reply to author
Forward
0 new messages