Skip to content

meson build files generated from crates cannot be sufficiently customized #13922

Closed
@inklesspen

Description

@inklesspen

I'm trying to get a barebones Rust-based Python extension module to build with meson. (I can of course build it with cargo, and therefore with maturin or setuptools-rust, but I prefer using meson-python when possible.)

Currently I'm stuck on configuring the build for a transitive dependency, target-lexicon, which is used by pyo3. This is one of the many crates which uses a build.rs file; in this crate, the build.rs file generates a source file, and optionally outputs an extra feature flag to enable. The logic to generate this source file is sufficiently complicated that it seemed to me that the simplest thing to do here would be to actually compile and run the build.rs file.

I'm running the unreleased dev version of meson (current commit is 33376ef).

The autogenerated meson.build file for this dependency looks like this:

project(
  'target-lexicon',
  'rust',
  version : '0.12.16',
  meson_version : '>= 1.7.0',
  default_options : ['rust_std=2018'],
  license : 'Apache-2.0 WITH LLVM-exception'
)
rust = import('rust')
message(
  'Enabled features:',
  ['default']
)
system_deps_args = []
extra_args = []
extra_deps = []
fs = import('fs')
if fs.is_dir('meson')
  subdir('meson')
endif
features_args = ['--cfg', 'feature="default"']
lib = static_library(
  'target_lexicon',
  'src/lib.rs',
  dependencies : [extra_deps],
  rust_dependency_map : {},
  rust_args : [features_args, extra_args, system_deps_args]
)
dep = declare_dependency(
  link_with : lib,
  variables : {'features' : 'default'}
)
meson.override_dependency('target-lexicon-0.12-rs', dep)

So I added a subprojects/target-lexicon-0.12.16/meson/meson.build file, with these contents:

rustc = find_program('rustc')
rustc_info = run_command(rustc, '-vV', check: true).stdout().splitlines()
target_triple = ''
foreach info_line : rustc_info
    if info_line.startswith('host')
        target_triple = info_line.split(':')[1].strip()
        break
    endif
endforeach
build_rs = executable(
    'build_script_build',
    [
        '../build.rs',
        '../src/data_model.rs',
        '../src/triple.rs',
        '../src/targets.rs',
    ],
)
build_output = custom_target(
    'host.rs',
    command: [build_rs],
    output: 'host.rs',
    env: {
        'OUT_DIR': meson.current_build_dir(),
        'TARGET': target_triple,
        'RUSTC': rustc.full_path(),
    },
)

This does build a host.rs file with the same contents that cargo would. However, I can't figure out any means to add the custom_tgt as a source file in the lib target. Additionally, since features_args is defined after the subdir('meson') call, I cannot augment it with the 'feature="rust_1_40"' which the build script will normally define.

Perhaps in a scenario like this, the best thing to do would be to actually copy the generated meson.build file into the subprojects directory, create a cargo wrap, and make the meson.build do the right thing there. However, I have been unable to find any command to do this, or any guidance on doing it manually.

Advice more than welcome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions