#!/bin/bash

#
# Copyright 2021 Signal Messenger, LLC.
# SPDX-License-Identifier: AGPL-3.0-only
#

# To keep code size down, we try to avoid depending on multiple versions of crates.
#
# Sometimes we decide it's okay for certain dependencies.
# You can use the `cargo tree` command below to see where they come from,
# and then document them here.
#
# thiserror: minimal and highly inlinable, most of the code is synthesized at the use site
# rand_core, getrandom: waiting on snow to support rand_core 0.9
# hax-lib: highly inlinable, mostly annotated versions of simple operations
# libcrux-sha3, libcrux-intrinsics: v0.0.3 is referenced by hpke-rs, but only needed if you use X-Wing KEM
EXPECTED="
getrandom v0.2.16
getrandom v0.3.3
hax-lib v0.2.0
hax-lib v0.3.4
libcrux-intrinsics v0.0.2
libcrux-intrinsics v0.0.3
libcrux-sha3 v0.0.2
libcrux-sha3 v0.0.3
rand_core v0.6.4
rand_core v0.9.3
thiserror v1.0.69
thiserror v2.0.16"

check_cargo_tree() {
    # Only check the mobile targets, where we care most about code size.
    cargo tree \
        -p libsignal-node -p libsignal-jni -p libsignal-ffi \
        --quiet --duplicates --edges normal,no-proc-macro \
        --all-features --locked \
        --target aarch64-apple-ios \
        --target armv7-linux-androideabi \
        --target aarch64-linux-android \
        "$@"
}

if [[ "$(check_cargo_tree --depth 0 | sort -u -V)" != "${EXPECTED}" ]]; then
    check_cargo_tree
    exit 1
fi
