Is there a way to run a shell command after my cdylib has been successfully built?

I'm creating a Neovim plugin with the nvim_oxi crate.
It builds a C dynamic library that can be loaded and used by Neovim's Lua API.

cargo generates a cdylib artifact named libexample.so for example, but I want to remove the lib prefix because when I call require("example") in Lua, it looks for example.so, not libexample.so.

  1. If I want to do something like this, is it common practice to write a shell script that wraps cargo build? Or does Cargo support something like a post-build hook, or a way to set the artifact name for a cdylib?
  2. Is there a way to get the file path of the artifact generated by cargo build, so that I can rename it? (Ideally, the method should work for both debug and release builds.)

alternatively, you can tell lua to search for libexample.so by modifying package.cpath, for example, append './lib?.so' to the default value:

package.cpath = package.cpath .. ';./lib?.so'
local example = require 'example'

using scripts to automate project specific tasks is very common. for example, you can create a release.sh script in the project root directory like this:

#!/bin/sh
cargo build --release
cp target/release/libexample.so ./lua_modules/example.so

in addition to shell script, I also suggest you check `just:

no and no, to my knowledge.

cargo can generate machine readable output in json format, you can extract the artifact path from the json data. see:

1 Like

Thank you so much.

I confirmed that lib?.so works fine and successfully got the artifact path with jq.

$ cargo build --release --message-format json | jq --raw-output 'select(.reason == "compiler-artifact" and .target.name == "example") | .filenames[]'
/home/rust/example/target/release/libexample.so

Thanks to lib?.so, there's no need to rename the artifact anymore, but --message-format json will still be useful when I want to move the artifact.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.