mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.54k stars 1.61k forks source link

rust: Proper support for cbindgen #13092

Open gfxstrand opened 5 months ago

gfxstrand commented 5 months ago

We currently have a very handy built-in wrapper for bindgen:

_nil_bindings_rs = rust.bindgen(
  input: ['nil_bindings.h', _nil_format_table],
  output: 'nil_bindings.rs',
  c_args: [
    pre_args,
  ],
  include_directories : [inc_include, inc_src, include_directories('.')],
  args: [
    '--raw-line', '#![allow(non_camel_case_types)]',
    '--raw-line', '#![allow(non_snake_case)]',
    '--raw-line', '#![allow(non_upper_case_globals)]',
    '--allowlist-function', 'util_format_description',
    '--allowlist-type', 'nv_device_info',
    '--allowlist-type', 'nv_device_type',
    '--no-prepend-enum-name',
  ],
  dependencies: _libnil_deps,
)

We don't have anything similar for cbindgen. Currently in Mesa, we're invoking it directly via custom_target() and that works okay but it's a bit clunky:

prog_cbindgen = find_program('cbindgen', required : false, native : true)

# Thanks to cbindgen, this does need to include everything
_libnil_files = files(
  'lib.rs', # lib.rs has to come first
  'extent.rs',
  'format.rs',
  'image.rs',
  'tic.rs',
  'tiling.rs',
)

_nil_h = custom_target(
  'nil_h',
  input : [files('cbindgen.toml'), _libnil_files],
  output : ['nil.h'],
  command : [
    prog_cbindgen, '-q', '--config', '@INPUT0@', '--lang', 'c',
    '--output', '@OUTPUT0@', '--', '@INPUT1@'
  ],
)

It seems to be very much designed to be run from cargo but it is possible to invoke from Meson with some work. Probably the biggiest annoyance is that we have to list every Rust file in the meson instead of just invoking it on lib.rs and letting it go. That could probably be wrapped by Meson for us.

I don't have a particular proposal for what I want rust.cbindgen() to look like but I think we can do better than us having to list a bunch of rust files.

dcbaker commented 5 months ago

@dwlsalmeida and I had been talking about this a while back, and I have some work on this. I pushed the work I have, which is not completed. Let me have a look at what's in Mesa and we'll go from there

dcbaker commented 5 months ago

Probably the biggiest annoyance is that we have to list every Rust file in the meson instead of just invoking it on lib.rs and letting it go. That could probably be wrapped by Meson for us.

I asked them to add support for Make compatible depfiles, and it was added, so with more recent versions of cbindgen this isn't a problem, cbindgen will tell Ninja about any files it consumes, and Ninja will do the right thing.

dcbaker commented 5 months ago

Okay, I opened a Draft PR with what I have cleaned up a little bit. I've also checked this with Mesa, and it seems to work for the one case there so far.