Debian Codegraph @

debian/rules

raw
1 #!/usr/bin/make -f
2 include /usr/share/dpkg/architecture.mk
3 include /usr/share/dpkg/buildflags.mk
4 include /usr/share/rustc/architecture.mk
5 export CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
6 export DEB_HOST_RUST_TYPE DEB_HOST_GNU_TYPE
7 export CARGO_PROFILE_RELEASE_DEBUG=true
8 # stdbuf needs libstdbuf.so; upstream's GNUmakefile builds it with
9 # feat_external_libstdbuf and installs it into LIBSTDBUF_DIR.
10 export LIBSTDBUF_DIR = /usr/libexec/rust-coreutils
11
12 # who/pinky read the session list from logind rather than utmp on systemd
13 # systems, so build with the logind backend enabled.
14 export CARGOFLAGS += --features feat_systemd_logind
15
16 # libcrypto for the cksum/md5sum/sha*sum digests: much faster on CPUs without
17 # the SHA extensions. OPENSSL_NO_VENDOR links Debian's shared libcrypto instead
18 # of building the vendored openssl-src copy.
19 export CARGOFLAGS += --features openssl
20 export OPENSSL_NO_VENDOR = 1
21
22 export CARGOFLAGS += --features feat_diagnostics
23
24 # Use lld whenever it is available: it links the big multicall binary much
25 # faster than GNU ld. Fall back to the default linker on arches where lld is
26 # not installed. Always ask the linker to print its version banner (-Wl,-v)
27 # so the build log records which linker actually ran.
28 LLD := $(shell command -v ld.lld 2>/dev/null)
29 ifneq ($(LLD),)
30 export RUSTFLAGS = -C link-arg=-fuse-ld=lld -C link-arg=-Wl,-v
31 else
32 export RUSTFLAGS = -C link-arg=-Wl,-v
33 endif
34
35 # Fat LTO + 1 codegen unit + full debuginfo of the giant multicall crate is
36 # fatal on some arches: s390x hits the 150min buildd timeout, 32-bit arches OOM
37 # on the ~3GB address space ("rustc-LLVM ERROR: out of memory"). Set the cargo
38 # profile env overrides so they apply to every cargo invocation (build, test,
39 # install), unlike a Cargo.toml sed that only wraps one step.
40 ifeq ($(DEB_HOST_ARCH),s390x)
41 export CARGO_PROFILE_RELEASE_LTO = false
42 export CARGO_PROFILE_RELEASE_CODEGEN_UNITS = 16
43 export CARGO_PROFILE_RELEASE_DEBUG = 1
44 endif
45 ifeq ($(DEB_HOST_ARCH_BITS),32)
46 export CARGO_PROFILE_RELEASE_LTO = thin
47 export CARGO_PROFILE_RELEASE_CODEGEN_UNITS = 16
48 export CARGO_PROFILE_RELEASE_DEBUG = 1
49 endif
50 # PGO: instrument, train with util/build-pgo.sh, rebuild with -Cprofile-use.
51 # Doubles the build time and has to run the instrumented binary, hence 64-bit
52 # and native only. Opt out with DEB_BUILD_OPTIONS=nopgo.
53 PGO_ARCHS = amd64 arm64
54 PGO_DIR = $(CURDIR)/debian/pgo
55 PGO_PROFDATA = $(PGO_DIR)/coreutils.profdata
56
57 # llvm-profdata must match the LLVM rustc was built against: only it can read
58 # the .profraw files. Debian ships it in llvm-<N>, rustup in its sysroot.
59 LLVM_MAJOR := $(shell rustc --version --verbose | sed -n 's/^LLVM version: \([0-9]*\).*/\1/p')
60 RUST_SYSROOT := $(shell rustc --print sysroot)
61 LLVM_PROFDATA := $(firstword $(wildcard \
62 /usr/lib/llvm-$(LLVM_MAJOR)/bin/llvm-profdata \
63 $(RUST_SYSROOT)/lib/rustlib/$(DEB_HOST_RUST_TYPE)/bin/llvm-profdata))
64
65 ifeq (,$(filter nopgo,$(DEB_BUILD_OPTIONS)))
66 ifeq ($(DEB_BUILD_ARCH),$(DEB_HOST_ARCH))
67 ifneq (,$(filter $(DEB_HOST_ARCH),$(PGO_ARCHS)))
68 ifneq (,$(LLVM_PROFDATA))
69 PGO = y
70 endif
71 endif
72 endif
73 endif
74
75 ifeq ($(PGO),y)
76 # On both the test and install steps, so they share one set of artifacts.
77 PGO_RUSTFLAGS = -C profile-use=$(PGO_PROFDATA)
78 endif
79
80 VERSION := $(shell dpkg-parsechangelog -S Version|sed -e "s|.*~\(.*\)-.*|\1|g")
81 UPSTREAM_VERSION := $(shell dpkg-parsechangelog -S Version|cut -d- -f1)
82
83 # Build a multicall binary instead of multiple binaries
84 # to reduce the storage footprint
85 export MULTICALL = y
86
87 # Vendoring setup.
88 # The crates are NOT part of the upstream tarball (which is the pristine
89 # GitHub tag): they are vendored into debian/rust-vendor by the 'vendor'
90 # target below and shipped in the .debian.tar. This is what the Ubuntu
91 # package does, so the two stay mergeable.
92 export CARGO_VENDOR_DIR = debian/rust-vendor
93 export CARGO_HOME = $(CURDIR)/debian/cargo_home
94
95 # Configure dh_quilt_patch for applying vendor patches.
96 # This allows debian/rust-vendor/ to be patched without dpkg-source issues.
97 export QUILT_PATCH_DIR = debian/patches/rust-vendor
98 export QUILT_PC = debian/rust-vendor/.pc
99
100 %:
101 dh $@ --buildsystem cargo
102
103 override_dh_auto_configure:
104 # The translations live in a separate upstream repository and are shipped
105 # as a component tarball; without them the package would silently build
106 # without any localisation.
107 @if [ ! -d l10n/src ]; then \
108 echo "Error: l10n translations not found! Run 'uscan' to download the l10n tarball." >&2; \
109 exit 1; \
110 fi
111 # The tldr examples embedded in the manpages (see improve-man.diff) come
112 # from the tldr-pages release shipped in debian/, since the upstream
113 # tarball does not contain it and the build has no network access.
114 cp debian/tldr.zip docs/tldr.zip
115 dh_auto_configure
116 # Debian's dh-cargo hardcodes "prepare-debian debian/cargo_registry
117 # --link-from-system" in its configure step, while Ubuntu's honours
118 # CARGO_VENDOR_DIR. Point cargo back at our vendor directory afterwards
119 # so the build uses the same crates on both distributions.
120 DEB_CARGO_CRATE=coreutils_$(VERSION) /usr/share/cargo/bin/cargo prepare-debian $(CARGO_VENDOR_DIR)
121 # dh-cargo-vendored-sources only exists in Ubuntu's dh-cargo; it checks
122 # XS-Vendored-Sources-Rust against the vendor directory. Run it when it
123 # is there so the Ubuntu build behaves exactly as before.
124 [ ! -x /usr/share/cargo/bin/dh-cargo-vendored-sources ] || /usr/share/cargo/bin/dh-cargo-vendored-sources
125 dh_quilt_patch
126
127 .PHONY: vendor
128
129 # Maintainer-side target: regenerate debian/rust-vendor. Needs
130 # cargo-vendor-filterer, which is not packaged in Debian:
131 # cargo install cargo-vendor-filterer
132 # Run it after every new upstream import, then commit the result.
133 vendor:
134 rm -rf $(CARGO_VENDOR_DIR)
135 # Deliberately don't use the wrapper, as it expects the configure step
136 # to have occurred already.
137 # If you have to modify the path here, don't forget to change the
138 # README.source doc as well.
139 # PATH/HOME are kept so that a cargo-vendor-filterer installed with
140 # 'cargo install' is found and can reach the registry cache; everything
141 # else (CARGO_HOME, CARGO_VENDOR_DIR, ...) is deliberately cleared.
142 env -i PATH="$$PATH" HOME="$$HOME" cargo-vendor-filterer --all-features --tier 2 --platform '*-*-linux-gnu' --platform '*-*-linux-gnueabi' $(CARGO_VENDOR_DIR)
143 # Blank the checksum lists. Two reasons: it lets us patch the crates, and
144 # dpkg-source drops VCS files (.gitmodules & co) from the .debian.tar, so a
145 # crate shipping one would otherwise fail with "failed to calculate
146 # checksum". Rewritten with json, not sed: cargo-vendor-filterer has since
147 # added a "$comment" key and a line-anchored regexp silently skips those.
148 @$(CURDIR)/debian/blank-vendor-checksums.py $(CARGO_VENDOR_DIR)
149 # cargo-vendor-filterer keeps the crates' pre-publish Cargo.toml.orig;
150 # cargo never reads it and lintian flags it as debian-adds-patch-failure-file.
151 find $(CARGO_VENDOR_DIR) -name Cargo.toml.orig -delete
152 # Cleanup temp files
153 rm -rf $(CARGO_HOME)
154 # Update include-binaries
155 rm -f debian/source/include-binaries
156 dpkg-source --include-binaries -b .
157 # Update XS-Vendored-Sources-Rust field. Ubuntu generates it with
158 # dh-cargo-vendored-sources; that helper is not in Debian's dh-cargo, so
159 # read the crate names out of the vendor directory instead.
160 python3 debian/update-vendored-sources-field.py
161 # Commit changes
162 git add -f $(CARGO_VENDOR_DIR)
163 git commit -m "Update vendored rust crates" || true
164 git add debian/source/
165 git commit -m "Update debian/source/include-binaries" || true
166 git add debian/control
167 git commit debian/control -m "Update XS-Vendored-Sources-Rust field" || true
168
169 override_dh_auto_build:
170 dh_auto_build
171 ifeq ($(PGO),y)
172 # Step 1: the instrumented binary, built with *exactly* the command that
173 # builds the shipped one. A profile only applies to a matching build: with
174 # a different feature set the symbols differ, -Cprofile-use finds no data
175 # and PGO silently does nothing. Hence not the script's own step 1, which
176 # builds "--features unix" (see d/p/pgo-use-prebuilt-binary.patch).
177 CARGO_TARGET_DIR=$(PGO_DIR)/instrumented \
178 RUSTFLAGS="$(RUSTFLAGS) -C profile-generate=$(PGO_DIR)/profiles --cfg pgo_training" \
179 CARGO_HOME=$(CURDIR)/debian/cargo_home \
180 make SELINUX_ENABLED=1 PROFILE=release MULTICALL=$(MULTICALL) build-coreutils
181 # Steps 2-4: corpus, training workloads, merge. The optimized build is the
182 # package's own, via PGO_RUSTFLAGS.
183 util/build-pgo.sh --train-only \
184 --target-dir $(PGO_DIR) \
185 --instrumented-binary $(PGO_DIR)/instrumented/release/coreutils \
186 --llvm-profdata $(LLVM_PROFDATA)
187 test -f $(PGO_PROFDATA)
188 # Several GB, and nothing downstream needs it.
189 rm -rf $(PGO_DIR)/instrumented
190 else
191 @echo "PGO disabled on $(DEB_HOST_ARCH) (or by DEB_BUILD_OPTIONS=nopgo)"
192 endif
193
194 override_dh_auto_test:
195 ifeq ($(DEB_HOST_ARCH),s390x)
196 # The release test build (every util + every test) is what actually hits
197 # the buildd inactivity timeout on s390x. Tests are non-gating here (||true)
198 # anyway, so skip them and rely on the other arches for coverage.
199 @echo "Skipping the test suite on s390x to stay under the buildd timeout"
200 else
201 # for now, don't fail the build if tests are failing
202 RUSTFLAGS="$(RUSTFLAGS) $(PGO_RUSTFLAGS)" CARGO_HOME=$(CURDIR)/debian/cargo_home make PROFILE=release MULTICALL=$(MULTICALL) test||true
203 endif
204
205 override_dh_auto_install:
206 # The vendor directory is prepared once in override_dh_auto_configure.
207 # LTO/debuginfo/codegen are handled globally via CARGO_PROFILE_RELEASE_* env
208 # overrides at the top of this file, so no Cargo.toml editing is needed here.
209 RUSTFLAGS="$(RUSTFLAGS) $(PGO_RUSTFLAGS)" CARGO_HOME=$(CURDIR)/debian/cargo_home DESTDIR=$(CURDIR)/debian/tmp/ make SELINUX_ENABLED=1 PROFILE=release MULTICALL=$(MULTICALL) install
210 # Create the symlink early to be able to generate the manpages
211 cd debian/tmp/usr/share/zsh/site-functions && mkdir rust-coreutils && chmod -x _* && mv _* rust-coreutils
212 cd debian/tmp/usr/share/bash-completion/completions/ && chmod -x rust-*
213 cd debian/tmp/usr/share/fish/vendor_completions.d/ && chmod -x *.fish
214 cd debian/tmp/usr/bin && mv rust-coreutils coreutils && rm rust-*
215 # cd debian/tmp/usr/share/fish/vendor_completions.d && for f in *fish; do mv $$f rust-$$f; done
216 dh_link
217
218 override_dh_missing:
219 dh_missing --fail-missing
220
221 override_dh_fixperms:
222 dh_fixperms
223 # A shared library is not executable.
224 chmod 0644 debian/rust-coreutils/usr/libexec/rust-coreutils/libstdbuf.so
225
226 override_dh_dwz:
227 # Don't do anything. fails because of the
228 # https://github.com/rust-lang/rust/issues/66118
229
230 execute_after_dh_auto_configure:
231 # Log the toolchain and linker up front, on every arch, so the build log
232 # always records which linker was used - even if a later step times out
233 # before any link runs. The leading '-' keeps the build going on the rare
234 # arch where ld.lld is absent (we fall back to the default linker there).
235 rustc --version --verbose
236 -ld.lld --version
237 -ld --version | head -n1
238
239 override_dh_clean:
240 dh_quilt_unpatch
241 rm -rf $(PGO_DIR)
242 rm -f Cargo.toml.orig docs/tldr.zip
243 dh_clean --exclude=Cargo.toml.orig --exclude=config.guess
244
245 override_dh_update_autotools_config:
246 # don't do anything - impact vendoring otherwise