virtme-ng icon indicating copy to clipboard operation
virtme-ng copied to clipboard

virtme-ng-init: add some useful info when PID>1

Open jimc opened this issue 1 year ago • 5 comments

report vng-init version info, as written to src/version.rs, by ../setup.py, using version.py's notion of VERSION.

this yields:

[jimc@gandalf virtme-ng]$ ./virtme/guest/bin/virtme-ng-init virtme-ng-init: must be run as PID 1 virtme-ng-init: Build Information: virtme-ng-init: Package Name: virtme-ng-init virtme-ng-init: Package Version: 1.33+33.g5893a4b.dirty virtme-ng-init: Manifest Directory: /home/jimc/projects/virtme-ng/virtme_ng_init

or: _ _ __ ()_ | | _ __ ___ ___ _ __ __ _ \ \ / / | | | _ _ \ / _ ____| _ \ / _ | \ V /| | | | || | | | | | /| | | | (| | _/ ||| _|| || |_|_| || ||__ | |___/ kernel version: 6.14.0-rc7-dd-00059-gb4d7289f3f54 x86_64 virtme-ng-init version: 1.33+34.g723e552.dirty (CTRL+d to exit)

So far, its only useful for blaming, but theres room for more details, if/when any useful ones become apparent.

jimc avatar Mar 19 '25 04:03 jimc

ok. this is probably no go. setyp.py writes version.rs into the subdir. so its a build artifact. but then its not in the repo for clean builds everwhere.

jimc avatar Mar 20 '25 22:03 jimc

So it appears that setup.py is not run 1st as part of CI

it writes the target/version.rs file, which must be there for the compile to succeed.

Can someone conjure a fix ?

jimc avatar Apr 07 '25 15:04 jimc

can we depend upon package variables ?

use std::env;
use std::fs;
use std::path::Path;

fn main() {
    // This is a simplified example - you'd need a robust way
    // to get the VERSION from your Python setup.py

    // For demonstration, let's hardcode a version or get it from an env var
    let version = env::var("CARGO_PKG_VERSION").unwrap_or("0.1.0");
    let out_dir = env::var("OUT_DIR").unwrap();
    let dest_path = Path::new(&out_dir).join("version.rs");

    fs::write(
        &dest_path,
        format!("pub const VERSION: &str = \"{}\";\n", version),
    )
    .unwrap();

    println!("cargo:rustc-env=BUILD_VERSION={}", version);
    println!("cargo:rustc-cfg=build_version=\"{}\"", version);
}

jimc avatar Apr 07 '25 18:04 jimc

On Mon, Apr 7, 2025 at 10:42 AM Matthieu Baerts @.***> wrote:

@.**** requested changes on this pull request.

In setup.py https://github.com/arighi/virtme-ng/pull/256#discussion_r2031626913:

@@ -50,6 +51,11 @@ "RUSTFLAGS", "" )

+# Generate Rust version file +version_rs_path = pathlib.Path("virtme_ng_init/src/version.rs") +version_rs_path.parent.mkdir(parents=True, exist_ok=True) +version_rs_path.write_text(f'pub const VERSION: &str = "{VERSION}";\n')

I don't know Rust well, (maybe @arighi https://github.com/arighi can help here? :) ) but I think the idea is to add a build.rs file in virtme_ng_init/ that will execute python3 virtme_ng/version.py and set a build env var:

// build.rsuse std::process::Command;fn main() { // note: add error checking yourself. let output = Command::new("python3").args(&["virtme_ng/version.py"]).output().unwrap(); let version = String::from_utf8(output.stdout).unwrap(); println!("cargo:rustc-env=VNG_VERSION={}", version);}

Then in main.rs, you can use env!("VNG_VERSION")

Source: https://stackoverflow.com/a/44407625

This is pretty slick, and thanks for the link. it has git derived env VERSION, one might expect it to be a quite reliable setting on github :-)

— Reply to this email directly, view it on GitHub https://github.com/arighi/virtme-ng/pull/256#pullrequestreview-2747466289, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAUBQ7BZVDVBYOMATFDWF32YKTITAVCNFSM6AAAAABZJZUI5OVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDONBXGQ3DMMRYHE . You are receiving this because you authored the thread.Message ID: @.***>

jimc avatar Apr 07 '25 18:04 jimc

can we depend upon package variables ?

That's different, that will be the version of the init program, no?

Maybe we could also set an env var containing VERSION at build time in setup.py and use it in build.rs?

matttbe avatar Apr 07 '25 19:04 matttbe