Configuring Cargo to run a bash script each time after it grabs a dependency crate and before cargo builds the dependency crate

Hi Everyone,

Now that I played around with the attribute macro, I just got one more question. How can I configure my Cargo to run a script that will insert the [#macro] on top of every function after it grabs the dependency crate and before it builds the crate?

I don't think this is possible in the general case, because functions can be generated by macros: a macro invocation like some_crate::foo!() could potentially expand to a function, but your script would never be able to know that. Doing it reliably would require adding your attributes after expanding macros, but rustc doesn't currently provide hooks like that.

Also, just to for clarification: do you want to add #[macro] to the top of every function in your own crate, or to the top of every function in a user-provided crate?

Hi Kestrer

The [#macro] to clarify is an attribute macro that I added which will add some line of codes that are redundant and unlikely to affect the actual code. I want to add this on top of every function that is going to be run as cargo compiles and run the crates. Was wondering if cargo had such a feature which would allow me to run a bash script that will add this on top of every function available to me as cargo pulls in the crates in the dependencies, which I already written as follow:

pattern1="^unsafe|pub|unsafe pub fn*$"

while IFS= read -r line
  if [[ $line =~ $pattern1 ]]
    echo $match'\n'$line
  elif [[ $line = $pattern2 ]]
    echo $match'\n'$line
    printf '%s\n' "$line"
done < "$file"

Even if this was possible, that script is very fragile and would likely break. It wouldn't work on code that isn't formatted normally (though most code is, you really cannot rely on that), and it completely breaks in the presence of macros. Take for instance this code:

macro_rules! m {
   (unsafe) => {};
macro_rules! {

It would insert an attribute inside the macro invocation, breaking the code.

Right I get where you are coming from. But for the regex I am using is an exact match, meaning only if i find unsafe fn or pub fn or unsafe pub fn, then I will add this as part of the header. But you are right, there are too many uncertainties...

Hmm, looks like I might have to start my work all over because it would seem macro can't seem to give me the ability to insert codes so that I can fuzz and pick this things and use it as part of my seed selection decision.