Include git commit hash as string into Rust program

后端 未结 6 2046
小蘑菇
小蘑菇 2021-02-12 23:33

I host a Rust project in git repository and I want to make it print the version on some command. How can I include the version into the program? I thought that the build script

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-12 23:44

    I can only think about writing data to some file, but I think this is overkill for this case.

    That's unfortunate, because that is the only way of doing it. Environment variables can't work because changes to the environment can't "leak" into other, non-child processes.

    For simpler things, you can instruct Cargo to define conditional compilation flags, but those aren't powerful enough to communicate a string [1].

    The details of generating code from a build script is detailed in the code generation section of the Cargo documentation.


    [1]: I mean, unless you feel like breaking the hash into 160 config flags and then re-assembling them in the source being compiled, but that's even more overkill.

提交回复
热议问题