Skip to content

Provide MAGIC_VERSION metadata #205

@robo9k

Description

@robo9k

It might be helpful for consumers to know the libmagic version they link against, e.g. MAGIC_VERSION from magic.h
See robo9k/rust-magic#454 (comment)

Build scripts can be overridden, see https://doc.rust-lang.org/cargo/reference/build-scripts.html#overriding-build-scripts
This is expected and supported for magic-sys if someone wants to use neither pkg-config nor vcpkg

Thus the libmagic version would have to be communicated with build script metadata (and not generated & included code files)
When the magic-sys build script runs
https://doc.rust-lang.org/cargo/reference/build-scripts.html#the-links-manifest-key

cargo:version={libmagic_version}

When the script is overridden by a user

[target.x86_64-unknown-linux-gnu.magic]
metadata_version = "5xy"

Metadata might also have to be set to a fake but valid value for docs.rs, possibly approximated by using the greatest activated crate version feature "v5-xy"

pkg-config already provides Library::version
vcpkg however does not, see https://redirect.github.com/mcgoo/vcpkg-rs/pull/56

Our setup-libmagic action does something similar with external tools, i.e. also for vcpkg: pkgconf.exe --modversion libmagic

Without pkg-config {libmagic_version} could be determined using a dependency on cc

#include <magic.h>
RUST_LIBMAGIC_VERSION=MAGIC_VERSION
let mut cc = cc::Build::new();
cc.includes(include_dirs);
let expanded = cc.file("build/expando.c").try_expand()?;
// TODO: parse version, println! metadata

Then finally a dependent could use this in their own build script, e.g. in magic

let libmagic_version = env::var("DEP_MAGIC_VERSION");

This could be used to e.g. codegen a file with const MAGIC_VERSION: c_int = 547; and include! it
Note that libmagic.pc has Version as 5.47, but it might be more intuitive to match the number format from runtime magic_version


Downsides to the above solution are; a new dependency on cc in magic-sys
and a requirement for a build script in dependents, e.g. in magic
This might still be more acceptable than runtime bindgen. Maybe regex instead of full cc?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions