Build custom compiler, use as generator (flatbuffers)

571 views
Skip to first unread message

Shawn Badger

unread,
Apr 27, 2017, 12:50:32 PM4/27/17
to The Meson Build System
Hello, I recently discovered Meson and love that C++ finally has a decent build system!  Thank you Jussi (and others)!

As part of a new project I am working on, I am trying to integrate Google's flatbuffers into my build as an in-tree submodule.  This means I need to build it (CMake), then use it as a generator to convert my IDL files to source, so I can include them in my executable target.  (See below)

My current attempt is to use a custom target that builds flatc via a script.  I'm struggling to understand how I can then use that in a generator to process the IDL files.  

I could just require flatc as an installed dependency, but I have other custom use cases like this and I want to understand how it's done in Meson.  I would have loved to use wrap, but obviously I can't here without converting flatbuffer's build system to Meson (which I may submit a pull request in the future).

Any advice would be appreciated.  And if there's a better way to do this, please let me know.  Thanks!

flatc_target = custom_target('flatc',
      output : ['flatc'],
      command : [build_flatc, '@SOURCE_DIR@', '@BUILD_DIR@'])

flatc = find_program('flatc')

gen = generator(flatc,
    output : '@BASENAME@_generated.h',
    arguments : ['--cpp', '@INPUT@'])

gen_src = gen.process('test.fbs')


Jussi Pakkanen

unread,
Apr 27, 2017, 12:57:02 PM4/27/17
to Shawn Badger, The Meson Build System
On Thu, Apr 27, 2017 at 7:50 PM, Shawn Badger <shawn...@gmail.com> wrote:

> gen_src = gen.process('test.fbs')

I'm not familiar with fbs, but this seems to only generate a header
file. If that is all you need then you just need to tell Meson to
generate it before compiling sources that use it.

Add this to the target you want to use it in. So something like this:

executable('myprog', 'file_that_includes_test_generated_h.cpp', gen_src)

Meson will then ensure that the header is generated before any of the
sources is built.

If you wish to use the header in many targets, you probably want to
use a custom_target instead (generator output is regenerated for each
target it is used in).

Shawn Badger

unread,
Apr 27, 2017, 1:07:12 PM4/27/17
to The Meson Build System, shawn...@gmail.com


On Thursday, April 27, 2017 at 10:57:02 AM UTC-6, Jussi Pakkanen wrote:
On Thu, Apr 27, 2017 at 7:50 PM, Shawn Badger <shawn...@gmail.com> wrote:

> gen_src = gen.process('test.fbs')

Meson will then ensure that the header is generated before any of the
sources is built.
 
That's good to know, but the main problem I am having is that find_program('flatc') is unable to locate the compiler I built via script.  So my generator is unusable.  I am building flatc in-tree, not using it from an installed location.

Jussi Pakkanen

unread,
Apr 28, 2017, 3:54:19 PM4/28/17
to Shawn Badger, The Meson Build System
On Thu, Apr 27, 2017 at 8:07 PM, Shawn Badger <shawn...@gmail.com> wrote:

> That's good to know, but the main problem I am having is that
> find_program('flatc') is unable to locate the compiler I built via script.
> So my generator is unusable. I am building flatc in-tree, not using it from
> an installed location.

That is because it only searches in the source tree (because files in
the build tree are not there when the running the script the first
time).

find_library has a keyword argument for setting the search path. We
should probably add the same to find_program. Patches welcome. :)

mikkelfj

unread,
May 20, 2017, 7:20:16 AM5/20/17
to The Meson Build System, shawn...@gmail.com
This may not solve your problem with finding the flatc tool, but I experimented with building flatbuffers in meson a while ago.

I it is for the C based flatcc compiler, not the C++ flatc tool, but most of the principles are similar.


The above reliase on the meson branch where meson build support was added to the main flatcc project. The end result is much better dependency managememnt compared to CMake and minimal recompilation on even changed fbs schema included in a main schema. Notably, CMake has buggy custom build targets when using Ninja instead of Make as the build engine.
Reply all
Reply to author
Forward
0 new messages