#!/bin/sh # Generates Built-Using after a successful cargo build. # Run this in the package top-level directory where debian/ is. # # SPDX-FileCopyrightText: 2023-2024 Jonas Smedegaard # SPDX-FileCopyrightText: 2018-2021 Ximin Luo # # SPDX-License-Identifier: GPL-3-or-later and MIT set -e CARGO_TARGET_DIR=$1 BINPKG=$2 CARGO_REGISTRY="${CARGO_REGISTRY:-debian/cargo_registry}" CARGO_TARGET_DIR_ABS=$(readlink -f "$CARGO_TARGET_DIR") CURDIR_ABS=$(readlink -f "$PWD") CPRIGHT_FORMAT="https://www.debian.org/doc/packaging-manuals/copyright-format/1.0" SRCLEFT_LICENSES=$(echo GPL LGPL AGPL GFDL MPL CDDL CPL Artistic Perl QPL | tr ' ' '\n') pkg_has_srcleft_license() { pkg="$1" ver="$2" f="/usr/share/doc/$pkg/copyright" if ! sed -nre 's!^Format: (.*)!\1!gp' "$f" | grep -qiw "$CPRIGHT_FORMAT"; then echo >&2 "$0: abort: Not in machine-readable format: $f" echo 2 elif sed -nre 's!^X-Binary-Requires-Source: (.*)!\1!gp' "$f" | grep -qiw yes; then echo 1 elif sed -nre 's!^License: (.*)!\1!gp' "$f" | grep -qiwF "$SRCLEFT_LICENSES"; then echo 1 else echo 0 fi } dep_files_to_pkgs() { xargs -r dpkg -S \ | sed -nre 's!(.*): .*!\1!gp' \ | sed -re 's!, !\n!g' \ | xargs -r dpkg-query --show \ | while read -r pkg ver; do echo "$pkg $ver $(pkg_has_srcleft_license "${pkg%:*}" "$ver")"; done # pkg_has_srcleft_license should be accurate for all rust crates, no need to give a $containing_crate # this is due to nature of crate copyright info, and the debian rust packaging policy } rust_dep_files() { cat "$CARGO_TARGET_DIR/deps"/*.d \ | sed -nre 's!^\S*/('"$CARGO_REGISTRY"'/[^/]*)/.*!'"$CURDIR_ABS/"'\1!gp' \ | sort -u \ | xargs -r readlink -f } rust_libs() { { which rustc; rust_dep_files; } | dep_files_to_pkgs } gcc_default_searchdirs() { gcc -print-search-dirs \ | sed -nre 's!^libraries: (.*)!\1!gp' \ | tr ':' '\n' \ | sed -e 's!^=!'"$(gcc -print-sysroot)"'!g' \ | xargs readlink -m 2>/dev/null # suppress errors caused by early pipe closure } rust_search_lib() { lib="$1" { cat # rust does not actually search normal paths when linking static libs # - see https://github.com/rust-lang/rust/issues/43118 # - see also `fn link_rlib` in back/link.rs which calls # `pub fn find_library` in back/archive.rs which generates the error message #gcc_default_searchdirs } | while read -r searchdir; do #echo >&2 "searching $searchdir for static lib $lib" f=$(readlink -m "$searchdir/lib${lib}.a") if test -f "$f"; then printf "%s\n" "$f" break fi done } native_libs() { grep -Po '^cargo:rustc-link-lib=static=\K.*' -- "$CARGO_TARGET_DIR/build"/*/output 2>/dev/null \ | sed -e 's/:/ /' \ | while read -r output lib; do containing_crate=$(basename "$(dirname "$output")") #" test -n "$lib" || continue libfile=$(sed -nre 's!^cargo:rustc-link-search=native=(.*)!\1!gp' "$output" | rust_search_lib "$lib") #' srcleft="" if [ "$lib" = "ffi" ]; then echo >&2 "$0: static library derived from $lib freaks out dh-rust-built-using script, not adding Built-Using" continue fi test -n "$libfile" || { echo >&2 "$0: abort: could not find static lib '$lib'; rustc should have failed already?"; exit 1; } echo >&2 "$0: found static lib $lib at $libfile" if [ "${libfile#"$CARGO_TARGET_DIR_ABS"/}" != "$libfile" ]; then # static library source code embedded in crate srcstat=$(sed -nre 's!^dh-cargo:deb-built-using='"$lib"'=([01]=.*)!\1!gp' "$output") #' case "$srcstat" in 0=*|1=*) srcleft="${srcstat%%=*}" libfile="${srcstat#*=}" libfile_abs=$(readlink -f "$libfile") if [ "${libfile_abs##"$CURDIR_ABS"}" != "$libfile_abs" ]; then # Note that this exception only applies in the case that where you are building # the Debian package for $containing_crate itself. In the case where you are # building a Debian package for crate X depending on $containing_crate, the # latter still has to output the dh-cargo:deb-built-using in their build.rs so # that the Debian package for crate X can correctly set Built-Using themselves. echo >&2 "$0: static library derived from $libfile which is a crate being built, no need to add Built-Using" continue fi ;; *) echo >&2 "$0: abort: could not determine source-distribution conditions of ${libfile#"$CARGO_TARGET_DIR_ABS"/}." echo >&2 "You must patch build.rs of ${containing_crate%-*} to output 'println!(\"dh-cargo:deb-built-using=$lib=\$s={}\", env::var(\"CARGO_MANIFEST_DIR\").unwrap());' where:" echo >&2 "- \$s is 1 if the license(s) of the included static libs require source distribution alongside binaries, otherwise 0" exit 1 ;; esac fi wpkg=$(dpkg -S "$(readlink -f "$libfile")") || true #" test -n "$wpkg" || { echo >&2 "$0: abort: could not find Debian package for file $libfile"; exit 1; } pkgstat=$(echo "$wpkg" | sed -nre 's!(.*): .*!\1!gp' | xargs -r dpkg-query --show) #' pkg=$(echo "$pkgstat" | cut -f1) ver=$(echo "$pkgstat" | cut -f2) # static library source code embedded in crate (from earlier) if [ -n "$srcleft" ]; then echo "$pkg $ver $srcleft" # static libraries from another Debian package elif sed -nre 's!^dh-cargo:deb-built-using='"$lib"'=0~=(.*)!\1!gp' "$output" | { echo "${pkg%:*} $ver" | grep -qExf /dev/fd/3; } 3<&0; then echo "$pkg $ver 0" elif sed -nre 's!^dh-cargo:deb-built-using='"$lib"'=1~=(.*)!\1!gp' "$output" | { echo "${pkg%:*} $ver" | grep -qExf /dev/fd/3; } 3<&0; then echo "$pkg $ver 1" else # guess the conditions based on the whole d/copyright file # this loses granularity, e.g. gcc is mostly distributed as GPL-3 but the libbacktrace portion is BSD-3 # to retain granularity the crate package maintainer should patch build.rs as suggested echo >&2 "$0: warning: guessing source-distribution conditions of $libfile, this may be inaccurate." echo >&2 "$0: warning: patch build.rs to suppress the above warning" srcleft=$(pkg_has_srcleft_license "${pkg%:*}" "$ver") if [ "$srcleft" -gt 1 ]; then echo >&2 "You must patch build.rs of ${containing_crate%-*} to output 'dh-cargo:deb-built-using=$lib=\$s~=\$PAT' where:" echo >&2 "- \$s is 1 if the license(s) of the included static libs require source distribution alongside binaries, otherwise 0" echo >&2 "- \$PAT is an egrep pattern matching the \"\$pkg \$ver\" combinations that satisfy \$s" echo >&2 " for example '$pkg .*' matches the currently-relevant package, $pkg $ver" exit 1 fi echo "$pkg $ver $srcleft" fi done } output_built_using() { built_using="" built_using_x="" while read -r src srcver srcleft; do case "$srcleft" in 2) exit 1;; 1) built_using="${built_using}$src (= $srcver), ";; esac built_using_x="${built_using_x}$src (= $srcver), " done echo "cargo:Built-Using=${built_using%, }" >> "debian/$BINPKG.substvars" echo "cargo:Static-Built-Using=${built_using_x%, }" >> "debian/$BINPKG.substvars" # deprecated, only kept for binNMU/compat purposes.. echo "cargo:X-Cargo-Built-Using?=${built_using_x%, }" >> "debian/$BINPKG.substvars" } output() { while read -r pkg ver srcleft; do src=$(dpkg-query -f '${source:Package}' --show "$pkg") srcver=$(dpkg-query -f '${source:Version}' --show "$pkg") echo "$src $srcver $srcleft" done | uniq | output_built_using } native_libs=$(native_libs) # capture output outside of pipe so set -e works { rust_libs test -z "$native_libs" || echo "$native_libs" } | LC_ALL=C.utf-8 sort -u | output