Don't pass 'embed-bitcode'/'lto' while building proc-macros
[ldk-c-bindings] / deterministic-build-wrappers / rustc
1 #!/bin/bash
2 # rustc calculates a unique metadata tag to mangle symbols which includes the
3 # actual path to the crate. This breaks our deterministic builds as we depend
4 # on a copy of rust-lightning via a path. We insert this shim between cargo and
5 # rustc and edit the metadata tag for rust-lightning.
6 # While we could just set RUSTFLAGS="-C metadata=42", this would break if we
7 # ever (indirectly) depend on multiple versions of the same crate.
8 args=("$@")
9 IS_LIGHTNING=false
10 SKIP_EMBED_BITCODE=false
11 for ((i=0; i<"${#args[@]}"; ++i)); do
12     case ${args[i]} in
13         --crate-name)
14                         if [ "${args[i+1]}" = "lightning" -o "${args[i+1]}" = "lightning_background_processor" -o "${args[i+1]}" = "lightning_invoice" -o "${args[i+1]}" = "lightning_persister" -o "${args[i+1]}" = "lightning_rapid_gossip_sync" -o "${args[i+1]}" = "lightning_transaction_sync" -o "${args[i+1]}" = "ldk" ]; then
15                                 IS_LIGHTNING=true
16                         fi
17                         ;;
18                 --crate-type)
19                         if [ "${args[i+1]}" = "proc-macro" ]; then
20                                 # If we're building a proc-macro, rustc incorrectly passes our RUSTFLAGS containing
21                                 # embed-bitcode=yes, which we don't want.
22                                 SKIP_EMBED_BITCODE=true
23                         fi
24                         ;;
25     esac
26 done
27 for ((i=0; i<"${#args[@]}"; ++i)); do
28     case ${args[i]} in
29         metadata*)
30                         if [ "$IS_LIGHTNING" = "true" ]; then
31                                 # Pick any random value for metadata, it doesn't matter
32                                 args[i]="metadata=42"
33                         fi
34                         ;;
35                 embed-bitcode=yes)
36                         if [ "$SKIP_EMBED_BITCODE" = "true" ]; then
37                                 args[i]="embed-bitcode=no"
38                         fi
39                         ;;
40                 lto)
41                         if [ "$SKIP_EMBED_BITCODE" = "true" ]; then
42                                 args[i]="lto=no"
43                         fi
44                         ;;
45     esac
46 done
47 $LDK_RUSTC_PATH "${args[@]}"