Debian Codegraph @

bin/dh-rust-built-using

raw
1 #!/bin/sh
2 # Generates Built-Using after a successful cargo build.
3 # Run this in the package top-level directory where debian/ is.
4 #
5 # SPDX-FileCopyrightText: 2023-2024 Jonas Smedegaard <[email protected]>
6 # SPDX-FileCopyrightText: 2018-2021 Ximin Luo <[email protected]>
7 #
8 # SPDX-License-Identifier: GPL-3-or-later and MIT
9
10 set -e
11
12 CARGO_TARGET_DIR=$1
13 BINPKG=$2
14
15 CARGO_REGISTRY="${CARGO_REGISTRY:-debian/cargo_registry}"
16 CARGO_TARGET_DIR_ABS=$(readlink -f "$CARGO_TARGET_DIR")
17 CURDIR_ABS=$(readlink -f "$PWD")
18
19 CPRIGHT_FORMAT="https://www.debian.org/doc/packaging-manuals/copyright-format/1.0"
20 SRCLEFT_LICENSES=$(echo GPL LGPL AGPL GFDL MPL CDDL CPL Artistic Perl QPL | tr ' ' '\n')
21 pkg_has_srcleft_license() {
22 pkg="$1"
23 ver="$2"
24 f="/usr/share/doc/$pkg/copyright"
25 if ! sed -nre 's!^Format: (.*)!\1!gp' "$f" | grep -qiw "$CPRIGHT_FORMAT"; then
26 echo >&2 "$0: abort: Not in machine-readable format: $f"
27 echo 2
28 elif sed -nre 's!^X-Binary-Requires-Source: (.*)!\1!gp' "$f" | grep -qiw yes; then
29 echo 1
30 elif sed -nre 's!^License: (.*)!\1!gp' "$f" | grep -qiwF "$SRCLEFT_LICENSES"; then
31 echo 1
32 else
33 echo 0
34 fi
35 }
36
37 dep_files_to_pkgs() {
38 xargs -r dpkg -S \
39 | sed -nre 's!(.*): .*!\1!gp' \
40 | sed -re 's!, !\n!g' \
41 | xargs -r dpkg-query --show \
42 | while read -r pkg ver; do echo "$pkg $ver $(pkg_has_srcleft_license "${pkg%:*}" "$ver")"; done
43 # pkg_has_srcleft_license should be accurate for all rust crates, no need to give a $containing_crate
44 # this is due to nature of crate copyright info, and the debian rust packaging policy
45 }
46
47 rust_dep_files() {
48 cat "$CARGO_TARGET_DIR/deps"/*.d \
49 | sed -nre 's!^\S*/('"$CARGO_REGISTRY"'/[^/]*)/.*!'"$CURDIR_ABS/"'\1!gp' \
50 | sort -u \
51 | xargs -r readlink -f
52 }
53
54 rust_libs() {
55 { which rustc; rust_dep_files; } | dep_files_to_pkgs
56 }
57
58 gcc_default_searchdirs() {
59 gcc -print-search-dirs \
60 | sed -nre 's!^libraries: (.*)!\1!gp' \
61 | tr ':' '\n' \
62 | sed -e 's!^=!'"$(gcc -print-sysroot)"'!g' \
63 | xargs readlink -m 2>/dev/null # suppress errors caused by early pipe closure
64 }
65
66 rust_search_lib() {
67 lib="$1"
68 {
69 cat
70 # rust does not actually search normal paths when linking static libs
71 # - see https://github.com/rust-lang/rust/issues/43118
72 # - see also `fn link_rlib` in back/link.rs which calls
73 # `pub fn find_library` in back/archive.rs which generates the error message
74 #gcc_default_searchdirs
75 } | while read -r searchdir; do
76 #echo >&2 "searching $searchdir for static lib $lib"
77 f=$(readlink -m "$searchdir/lib${lib}.a")
78 if test -f "$f"; then
79 printf "%s\n" "$f"
80 break
81 fi
82 done
83 }
84
85 native_libs() {
86 grep -Po '^cargo:rustc-link-lib=static=\K.*' -- "$CARGO_TARGET_DIR/build"/*/output 2>/dev/null \
87 | sed -e 's/:/ /' \
88 | while read -r output lib; do
89 containing_crate=$(basename "$(dirname "$output")") #"
90 test -n "$lib" || continue
91 libfile=$(sed -nre 's!^cargo:rustc-link-search=native=(.*)!\1!gp' "$output" | rust_search_lib "$lib") #'
92 srcleft=""
93 if [ "$lib" = "ffi" ]; then
94 echo >&2 "$0: static library derived from $lib freaks out dh-rust-built-using script, not adding Built-Using"
95 continue
96 fi
97 test -n "$libfile" || { echo >&2 "$0: abort: could not find static lib '$lib'; rustc should have failed already?"; exit 1; }
98 echo >&2 "$0: found static lib $lib at $libfile"
99 if [ "${libfile#"$CARGO_TARGET_DIR_ABS"/}" != "$libfile" ]; then
100 # static library source code embedded in crate
101 srcstat=$(sed -nre 's!^dh-cargo:deb-built-using='"$lib"'=([01]=.*)!\1!gp' "$output") #'
102 case "$srcstat" in
103 0=*|1=*)
104 srcleft="${srcstat%%=*}"
105 libfile="${srcstat#*=}"
106 libfile_abs=$(readlink -f "$libfile")
107 if [ "${libfile_abs##"$CURDIR_ABS"}" != "$libfile_abs" ]; then
108 # Note that this exception only applies in the case that where you are building
109 # the Debian package for $containing_crate itself. In the case where you are
110 # building a Debian package for crate X depending on $containing_crate, the
111 # latter still has to output the dh-cargo:deb-built-using in their build.rs so
112 # that the Debian package for crate X can correctly set Built-Using themselves.
113 echo >&2 "$0: static library derived from $libfile which is a crate being built, no need to add Built-Using"
114 continue
115 fi
116 ;;
117 *)
118 echo >&2 "$0: abort: could not determine source-distribution conditions of ${libfile#"$CARGO_TARGET_DIR_ABS"/}."
119 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:"
120 echo >&2 "- \$s is 1 if the license(s) of the included static libs require source distribution alongside binaries, otherwise 0"
121 exit 1
122 ;;
123 esac
124 fi
125 wpkg=$(dpkg -S "$(readlink -f "$libfile")") || true #"
126 test -n "$wpkg" || { echo >&2 "$0: abort: could not find Debian package for file $libfile"; exit 1; }
127 pkgstat=$(echo "$wpkg" | sed -nre 's!(.*): .*!\1!gp' | xargs -r dpkg-query --show) #'
128 pkg=$(echo "$pkgstat" | cut -f1)
129 ver=$(echo "$pkgstat" | cut -f2)
130 # static library source code embedded in crate (from earlier)
131 if [ -n "$srcleft" ]; then
132 echo "$pkg $ver $srcleft"
133 # static libraries from another Debian package
134 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
135 echo "$pkg $ver 0"
136 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
137 echo "$pkg $ver 1"
138 else
139 # guess the conditions based on the whole d/copyright file
140 # this loses granularity, e.g. gcc is mostly distributed as GPL-3 but the libbacktrace portion is BSD-3
141 # to retain granularity the crate package maintainer should patch build.rs as suggested
142 echo >&2 "$0: warning: guessing source-distribution conditions of $libfile, this may be inaccurate."
143 echo >&2 "$0: warning: patch build.rs to suppress the above warning"
144 srcleft=$(pkg_has_srcleft_license "${pkg%:*}" "$ver")
145 if [ "$srcleft" -gt 1 ]; then
146 echo >&2 "You must patch build.rs of ${containing_crate%-*} to output 'dh-cargo:deb-built-using=$lib=\$s~=\$PAT' where:"
147 echo >&2 "- \$s is 1 if the license(s) of the included static libs require source distribution alongside binaries, otherwise 0"
148 echo >&2 "- \$PAT is an egrep pattern matching the \"\$pkg \$ver\" combinations that satisfy \$s"
149 echo >&2 " for example '$pkg .*' matches the currently-relevant package, $pkg $ver"
150 exit 1
151 fi
152 echo "$pkg $ver $srcleft"
153 fi
154 done
155 }
156
157 output_built_using() {
158 built_using=""
159 built_using_x=""
160 while read -r src srcver srcleft; do
161 case "$srcleft" in
162 2) exit 1;;
163 1) built_using="${built_using}$src (= $srcver), ";;
164 esac
165 built_using_x="${built_using_x}$src (= $srcver), "
166 done
167 echo "cargo:Built-Using=${built_using%, }" >> "debian/$BINPKG.substvars"
168 echo "cargo:Static-Built-Using=${built_using_x%, }" >> "debian/$BINPKG.substvars"
169
170 # deprecated, only kept for binNMU/compat purposes..
171 echo "cargo:X-Cargo-Built-Using?=${built_using_x%, }" >> "debian/$BINPKG.substvars"
172 }
173
174 output() {
175 while read -r pkg ver srcleft; do
176 src=$(dpkg-query -f '${source:Package}' --show "$pkg")
177 srcver=$(dpkg-query -f '${source:Version}' --show "$pkg")
178 echo "$src $srcver $srcleft"
179 done | uniq | output_built_using
180 }
181
182 native_libs=$(native_libs) # capture output outside of pipe so set -e works
183 {
184 rust_libs
185 test -z "$native_libs" || echo "$native_libs"
186 } | LC_ALL=C.utf-8 sort -u | output