summaryrefslogtreecommitdiffstats
path: root/debian/rules
blob: 88719d6217f066701ae89acfe60139bd7e115b02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
#!/usr/bin/make -f
# -*- makefile -*-

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/vendor.mk
include /usr/share/dpkg/architecture.mk
SED_VERSION_SHORT := sed -re 's/([^.]+)\.([^.]+)\..*/\1.\2/'
RUST_VERSION := $(shell echo '$(DEB_VERSION_UPSTREAM)' | $(SED_VERSION_SHORT))
RUST_LONG_VERSION := $(shell echo '$(DEB_VERSION_UPSTREAM)' | sed -re 's/([^+]+).*/\1/')
LIBSTD_PKG := libstd-rust-$(RUST_VERSION)
# Sed expression that matches the "rustc" we have in our Build-Depends field
SED_RUSTC_BUILDDEP := sed -ne "/^Build-Depends:/,/^[^[:space:]\#]/{/^ *rustc:native .*,/p}" debian/control
# Version of /usr/bin/rustc
LOCAL_RUST_VERSION := $(shell rustc --version --verbose | sed -ne 's/^release: //p')

include /usr/share/dpkg/buildflags.mk
# needed for cross-compilation to avoid passing host CFLAGS to the BUILD
# compiler
export TARGET_CFLAGS = $(CFLAGS)
export TARGET_CXXFLAGS = $(CXXFLAGS)
export TARGET_CPPFLAGS = $(CPPFLAGS)
export TARGET_LDFLAGS = $(LDFLAGS)
unexport CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
export CARGO_HOME = $(CURDIR)/debian/cargo

# Defines DEB_*_RUST_TYPE triples
include debian/architecture.mk
# for dh_install substitution variable
export DEB_HOST_RUST_TYPE

# for dh_install substitution variable
export RUST_LONG_VERSION

DEB_DESTDIR := $(CURDIR)/debian/tmp

# Use system LLVM (comment out to use vendored LLVM)
LLVM_VERSION = 17
OLD_LLVM_VERSION = 16
# Cargo-specific flags
export LIBSSH2_SYS_USE_PKG_CONFIG=1
# Make it easier to test against a custom LLVM
ifneq (,$(LLVM_DESTDIR))
LLVM_LIBRARY_PATH := $(LLVM_DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH):$(LLVM_DESTDIR)/usr/lib
LD_LIBRARY_PATH := $(if $(LD_LIBRARY_PATH),$(LD_LIBRARY_PATH):$(LLVM_LIBRARY_PATH),$(LLVM_LIBRARY_PATH))
export LD_LIBRARY_PATH
endif

# Required for profiler builtin
CLANG_RT_ARCH := $(DEB_TARGET_GNU_CPU)
ifeq (i386,$(DEB_TARGET_ARCH))
CLANG_RT_ARCH = i386
endif
ifeq (armhf,$(DEB_TARGET_ARCH))
CLANG_RT_ARCH = armhf
endif

ifneq (,$(filter $(DEB_TARGET_ARCH),sparc64 mips64el hurd-i386 hurd-amd64))
# sparc64: see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1061125
# mips64el: has profiler, but buggy atm (32-bit overflow in some counter?)
CLANG_RT_ARCH =
PROFILER = "false"
else
export LLVM_PROFILER_RT_LIB=/usr/lib/llvm-$(LLVM_VERSION)/lib/clang/$(LLVM_VERSION)/lib/linux/libclang_rt.profile-$(CLANG_RT_ARCH).a
PROFILER = "true"
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NJOBS := -j $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif
RUSTBUILD = RUST_BACKTRACE=1 python3 src/bootstrap/bootstrap.py $(NJOBS)
RUSTBUILD_FLAGS = --stage 2 --config debian/config.toml --on-fail env
# rust-tidy depends on lots of modules that we strip out of the build.
# it also tries to access the network for some reason. so just disable it.
RUSTBUILD_TEST = $(RUSTBUILD) test --no-fail-fast --exclude src/tools/tidy
# To run a specific test, run something like:
#   $ debian/rules override_dh_auto_test-arch \
#     RUSTBUILD_TEST_FLAGS="src/test/run-make --test-args extern-fn-struct"
# See src/bootstrap/README.md for more options.
RUSTBUILD_TEST_FLAGS =

# https://github.com/rust-lang/rust/issues/89744
# TODO: remove when we update cargo to 1.55 / 0.56
# upstream bug still exists and is under investigation, but is hidden by newer cargo
export CARGO_PROFILE_RELEASE_BUILD_OVERRIDE_OPT_LEVEL=0

update-version:
	oldver=$(shell $(SED_RUSTC_BUILDDEP) | sed -ne 's/.*(<= \(.*\)).*/\1/gp' | $(SED_VERSION_SHORT)); \
	newver=$(RUST_VERSION); \
	debian/update-version.sh $$oldver $$newver $(RUST_LONG_VERSION) $(CARGO_NEW)

# Below we detect how we're supposed to bootstrap the stage0 compiler. See
# README.Debian for more details of the cases described below.
#
PRECONFIGURE_CHECK = :
HAVE_BINARY_TARBALL := $(shell ls -1 stage0/*/*$(DEB_HOST_RUST_TYPE)* 2>/dev/null | wc -l)
DOWNLOAD_BOOTSTRAP := false
# allow not using the binary tarball although it exists
#ifneq (,$(filter $(DEB_HOST_ARCH), amd64 arm64 armhf i386 powerpc ppc64el s390x))
#  HAVE_BINARY_TARBALL := 0
#endif
ifeq (0,$(HAVE_BINARY_TARBALL))
    # Case A (Building from source): the extracted source tree does not include
    # a bootstrapping tarball for the current architecture e.g. because the
    # distro already has a rustc for this arch, or the uploader expects that
    # this requirement be fulfilled in some other way.
    #
    # Case A-1: the builder did not select the "pkg.rustc.dlstage0" build profile.
    # In this case, we use the distro's rustc - either the previous or current version.
    ifeq (,$(findstring pkg.rustc.dlstage0,$(DEB_BUILD_PROFILES)))
        # Make it easier to test against a custom rustc
        ifneq (,$(RUST_DESTDIR))
        RUST_LIBRARY_PATH := $(RUST_DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH):$(RUST_DESTDIR)/usr/lib
        LD_LIBRARY_PATH := $(if $(LD_LIBRARY_PATH),$(LD_LIBRARY_PATH):$(RUST_LIBRARY_PATH),$(RUST_LIBRARY_PATH))
        export LD_LIBRARY_PATH
        endif
    #
    # Case A-2: the builder selected the "dlstage0" build profile.
    # In this case, the rust build scripts will download a stage0 into stage0/ and use that.
    # We don't need to do anything specific in this build file, so this case is empty.
    else
        DOWNLOAD_BOOTSTRAP := true
    endif
else
    # Case B (Bootstrapping a new distro): the extracted source tree does
    # include a bootstrapping tarball for the current architecture; see the
    # `source_orig-stage0` target below on how to build this.
    #
    # In this case, we'll bootstrap from the stage0 given in that tarball.
    # To ensure the uploader of the .dsc didn't make a mistake, we first check
    # that rustc isn't a Build-Depends for the current architecture.
    ifneq (,$(shell $(SED_RUSTC_BUILDDEP)))
    ifeq (,$(shell $(SED_RUSTC_BUILDDEP) | grep '!$(DEB_HOST_ARCH)'))
        PRECONFIGURE_CHECK = $(error found matches for stage0/*/*$(DEB_HOST_RUST_TYPE)*, \
          but rustc might be a Build-Depends for $(DEB_HOST_ARCH))
    endif
    endif
endif

BUILD_DOCS := true
ifneq (,$(findstring nodoc,$(DEB_BUILD_PROFILES)))
  BUILD_DOCS := false
endif
ifneq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
  BUILD_DOCS := false
endif

BUILD_WASM := true
ifneq (,$(findstring nowasm,$(DEB_BUILD_PROFILES)))
  BUILD_WASM := false
endif

WINDOWS_SUPPORT := amd64 i386
BUILD_WINDOWS := true
ifneq (,$(findstring nowindows,$(DEB_BUILD_PROFILES)))
  BUILD_WINDOWS := false
endif
ifeq (,$(filter $(DEB_HOST_ARCH), $(WINDOWS_SUPPORT)))
  BUILD_WINDOWS := false
else
  ifeq (,$(filter $(DEB_BUILD_ARCH), $(WINDOWS_SUPPORT)))
    ifeq (true,$(BUILD_WINDOWS))
      $(error cannot cross-compile from $(DEB_BUILD_ARCH) to $(DEB_HOST_ARCH), unless "nowindows" is in DEB_BUILD_PROFILES)
    endif
  endif
  ifeq (i386,$(DEB_HOST_ARCH))
    WINDOWS_ARCH := i686
  else
    WINDOWS_ARCH := x86_64
  endif
endif
# for dh_install substitution variable
export WINDOWS_ARCH

MAKE_OPTIMISATIONS := true
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  MAKE_OPTIMISATIONS := false
endif

VERBOSITY_SUB := $(words $(filter terse,$(DEB_BUILD_OPTIONS)))
VERBOSITY_ADD := $(words $(filter verbose,$(DEB_BUILD_OPTIONS)))
VERBOSITY := $(shell expr 2 + $(VERBOSITY_ADD) - $(VERBOSITY_SUB))

ifeq ($(shell test $(VERBOSITY) -ge 3; echo $$?),0)
  export DH_VERBOSE=1
endif

ifeq ($(shell test $(VERBOSITY) -le 0; echo $$?),0)
  export DH_QUIET=1
.SILENT:
endif

# Build products or non-source files in src/, that shouldn't go in rust-src
SRC_CLEAN = src/bootstrap/bootstrap.pyc \
	src/bootstrap/__pycache__ \
	src/etc/__pycache__/

# Try to work around #933045
ifneq (,$(filter $(DEB_BUILD_ARCH), mips mipsel))
  SYSTEM_WORKAROUNDS += export MALLOC_ARENA_MAX=1;
endif

%:
	$(SYSTEM_WORKAROUNDS) dh $@ --parallel --with bash-completion

.PHONY: .dbg-windows
.dbg-windows:
	@echo host=$(DEB_BUILD_ARCH) target=$(DEB_HOST_ARCH) BUILD_WINDOWS=$(BUILD_WINDOWS) WINDOWS_ARCH=$(WINDOWS_ARCH)

.PHONY: build
build:
	$(SYSTEM_WORKAROUNDS) dh $@ --parallel --with bash-completion

override_dh_clean:
	# Upstream contains a lot of these
	dh_clean -XCargo.toml.orig

debian/config.toml: debian/config.toml.in debian/rules
	u="$(DEB_VERSION_UPSTREAM)"; \
	if [ "$$u" != "$${u%~beta.*+dfsg*}" ]; then channel="beta"; \
	else channel="stable"; fi; \
	m4  -DRELEASE_CHANNEL="$$channel" \
		-DDEB_BUILD_RUST_TYPE="$(DEB_BUILD_RUST_TYPE)" \
		-DDEB_HOST_RUST_TYPE="$(DEB_HOST_RUST_TYPE)" \
		-DDEB_TARGET_RUST_TYPE="$(DEB_TARGET_RUST_TYPE)" \
		-DDEB_BUILD_GNU_TYPE="$(DEB_BUILD_GNU_TYPE)" \
		-DDEB_HOST_GNU_TYPE="$(DEB_HOST_GNU_TYPE)" \
		-DDEB_TARGET_GNU_TYPE="$(DEB_TARGET_GNU_TYPE)" \
		-DMAKE_OPTIMISATIONS="$(MAKE_OPTIMISATIONS)" \
		-DVERBOSITY="$(VERBOSITY)" \
		-DLLVM_DESTDIR="$(LLVM_DESTDIR)" \
		-DLLVM_VERSION="$(LLVM_VERSION)" \
		-DRUST_DESTDIR="$(RUST_DESTDIR)" \
		-DPROFILER="$(PROFILER)" \
		"$<" > "$@"
	if $(DOWNLOAD_BOOTSTRAP) || [ $(HAVE_BINARY_TARBALL) != 0 ]; \
	  then sed -i -e '/^rustc = /d' -e '/^cargo = /d' "$@"; fi
# Work around low-memory (32-bit) architectures: https://github.com/rust-lang/rust/issues/45854
# i386 and x32 fail to mmap rustc_driver when building rustdoc in >1.60
ifneq (,$(filter $(DEB_BUILD_ARCH), armhf armel i386 mips mipsel powerpc powerpcspe x32))
	sed -i -e 's/^debuginfo-level = .*/debuginfo-level = 0/g' "$@"
endif

check-no-old-llvm:
	# fail the build if we have any instances of OLD_LLVM_VERSION in debian, except for debian/changelog
	! grep --color=always -i '\(clang\|ll\(..\|d\)\)-\?$(subst .,\.,$(OLD_LLVM_VERSION))' --exclude=changelog --exclude=copyright --exclude='*.patch' --exclude-dir='.debhelper' -R debian
.PHONY: check-no-old-llvm

extra-vendor:
	if [ -d extra ]; then \
	  cd extra; \
	  for c in *; do \
	    if [ -e ../vendor/"$$c" ]; then \
	      mv -v ../vendor/"$$c" ../vendor/"$$c".backup ; \
	    fi ; \
	    echo "adding extra vendored dependency '$$c'"; \
	    cp -r ./"$$c" ../vendor/; \
	  done; \
	fi

.PHONY: extra-vendor

debian/dh_auto_configure.stamp: debian/config.toml check-no-old-llvm extra-vendor
	# fail the build if we accidentally vendored openssl, indicates we pulled in unnecessary dependencies
	test ! -e vendor/openssl-src
	# fail the build if our version contains ~exp and we are not releasing to experimental
	v="$(DEB_VERSION)"; test "$$v" = "$${v%~exp*}" -o "$(DEB_DISTRIBUTION)" = "experimental" -o "$(DEB_DISTRIBUTION)" = "UNRELEASED"
	$(PRECONFIGURE_CHECK)
	if [ -d stage0 ]; then mkdir -p build && ln -sfT ../stage0 build/cache; fi
	# work around #842634
	if test $$(grep "127.0.0.1\s*localhost" /etc/hosts | wc -l) -gt 1; then \
	  debian/ensure-patch -N debian/patches/d-test-host-duplicates.patch; fi
	# don't care about lock changes
	rm -f Cargo.lock src/bootstrap/Cargo.lock src/tools/rust-analyzer/Cargo.lock src/tools/cargo/Cargo.lock
	# We patched some crates so have to rm the checksums
	find vendor -name .cargo-checksum.json -execdir "$(CURDIR)/debian/prune-checksums" "{}" +
	# Link against system liblzma, see https://github.com/alexcrichton/xz2-rs/issues/16
	echo 'fn main() { println!("cargo:rustc-link-lib=lzma"); }' > vendor/lzma-sys/build.rs
	# We don't run ./configure because we use debian/config.toml directly
	ln -sf debian/config.toml config.toml
	touch "$@"

override_dh_auto_configure-arch: debian/dh_auto_configure.stamp
override_dh_auto_configure-indep: debian/dh_auto_configure.stamp
ifeq (true,$(BUILD_DOCS))
# Change config.toml now and not later, since that might trigger a rebuild
	sed -i -e 's/^docs = false/docs = true/' debian/config.toml
endif

override_dh_auto_clean:
	$(RM) -rf build tmp debian/cargo_home config.stamp config.mk Makefile
	$(RM) -rf $(TEST_LOG) debian/config.toml debian/*.stamp
	$(RM) -rf $(SRC_CLEAN) config.toml

debian/dh_auto_build.stamp:
	$(RUSTBUILD) build $(RUSTBUILD_FLAGS)

override_dh_auto_build-arch: debian/dh_auto_build.stamp
ifeq (true,$(BUILD_WINDOWS))
	$(RUSTBUILD) build $(RUSTBUILD_FLAGS) \
	  --host $(DEB_BUILD_RUST_TYPE) \
	  --target $(WINDOWS_ARCH)-pc-windows-gnu \
	  library/std
endif

override_dh_auto_build-indep: debian/dh_auto_build.stamp
ifeq (true,$(BUILD_WASM))
	$(RUSTBUILD) build $(RUSTBUILD_FLAGS) \
	  --host $(DEB_BUILD_RUST_TYPE) \
	  --target wasm32-unknown-unknown,wasm32-wasi \
	  library/std
endif
ifeq (true,$(BUILD_DOCS))
	$(RUSTBUILD) doc $(RUSTBUILD_FLAGS)
	$(RUSTBUILD) doc $(RUSTBUILD_FLAGS) cargo # document cargo APIs
endif

TEST_LOG = debian/rustc-tests.log
# This is advertised as "5 tests failed" in README.Debian because our counting
# method is imprecise and in practise we count some failures twice.
# temporarily bumped from 8 to 10 to account for test output changes depending
# on build path length, bump down again once 1.78 is imported
FAILURES_ALLOWED = 10
ifneq (,$(filter $(DEB_BUILD_ARCH), armhf))
# temporarily bumped from 12 to 15, see above
  FAILURES_ALLOWED = 15
endif
ifneq (,$(filter $(DEB_BUILD_ARCH), armel mips mips64el))
  FAILURES_ALLOWED = 24
endif
# workaround broken gdb 13.1 - revert to 24 once fixed
# #1031946 / #1032785
ifneq (,$(filter $(DEB_BUILD_ARCH), mipsel))
  FAILURES_ALLOWED = 25
endif
ifneq (,$(filter $(DEB_BUILD_ARCH), ppc64 s390x riscv64))
  FAILURES_ALLOWED = 40
endif
ifneq (,$(filter $(DEB_BUILD_ARCH), loong64 powerpc powerpcspe sparc64 x32 hurd-i386 hurd-amd64))
  FAILURES_ALLOWED = 180
endif
FAILED_TESTS = grep "FAILED\|^command did not execute successfully" $(TEST_LOG) | grep -v '^test result: FAILED' | grep -v 'FAILED (allowed)'
# ignore debuginfo failures on armhf due to regression in GDB 11.2
# https://sourceware.org/bugzilla/show_bug.cgi?id=29272
ifneq (,$(filter $(DEB_BUILD_ARCH), armhf))
  FAILED_TESTS += | grep -v '^test \[debuginfo-gdb\] src/test/debuginfo/'
endif
override_dh_auto_test-arch:
	# ensure that rustc_llvm is actually dynamically linked to libLLVM
	set -e; find build/*/stage2/lib/rustlib/* -name '*rustc_llvm*.so' | \
	while read x; do \
		stat -c '%s %n' "$$x"; \
		objdump -p "$$x" | grep -q "NEEDED.*LLVM"; \
		test "$$(stat -c %s "$$x")" -lt 6000000; \
	done
ifeq (, $(filter nocheck,$(DEB_BUILD_PROFILES)))
ifeq (, $(filter nocheck,$(DEB_BUILD_OPTIONS)))
	{ $(RUSTBUILD_TEST) $(RUSTBUILD_FLAGS) $(RUSTBUILD_TEST_FLAGS); echo $$?; } | tee -a $(TEST_LOG)
	# test that the log has at least 1 pass, to prevent e.g. #57709
	grep -l "^test .* \.\.\. ok$$" $(TEST_LOG)
	echo "==== Debian rustc test report ===="; \
	echo "Specific test failures:"; \
	$(FAILED_TESTS); \
	num_failures=$$($(FAILED_TESTS) | wc -l); \
	exit_code=$$(tail -n1 $(TEST_LOG)); \
	echo "Summary: exit code $$exit_code, counted $$num_failures tests failed."; \
	echo -n "$(FAILURES_ALLOWED) maximum allowed. "; \
	if test "$$num_failures" -eq 0 -a "$$exit_code" -ne 0; then \
	  echo "Aborting just in case, because we missed counting some test failures."; \
	  echo "This could happen if we failed to build the tests, or if the testsuite runner is buggy."; \
	  false; \
	elif test "$$num_failures" -le $(FAILURES_ALLOWED); then \
	  echo "Continuing..."; \
	else \
	  echo "Aborting the build."; \
	  echo "Check the logs further above for details."; \
	  false; \
	fi
# don't continue if RUSTBUILD_TEST_FLAGS is non-empty
	test -z "$(RUSTBUILD_TEST_FLAGS)"
# don't run windows tests yet
endif
endif

override_dh_auto_test-indep:
ifeq (, $(filter nocheck,$(DEB_BUILD_PROFILES)))
ifeq (, $(filter nocheck,$(DEB_BUILD_OPTIONS)))
ifeq (true,$(BUILD_WASM))
	# Ignore failures in these tests, but run them so we see what it's like
	-PATH=$(CURDIR)/debian/bin:$(PATH) $(RUSTBUILD_TEST) $(RUSTBUILD_FLAGS) $(RUSTBUILD_TEST_FLAGS) \
	  --host $(DEB_BUILD_RUST_TYPE) \
	  --target wasm32-unknown-unknown,wasm32-wasi \
	  library/std
endif
ifeq (true,$(BUILD_DOCS))
	# Run all rules that test the docs, i.e. that depend on default:doc
	$(RUSTBUILD_TEST) $(RUSTBUILD_FLAGS) src/tools/linkchecker
endif
	test -z "$(RUSTBUILD_TEST_FLAGS)"
endif
endif

run_rustbuild:
	DESTDIR=$(DEB_DESTDIR) $(RUSTBUILD) $(X_CMD) $(RUSTBUILD_FLAGS) $(X_FLAGS)

override_dh_prep:
	dh_prep
	$(RM) -f debian/dh_auto_install.stamp

debian/dh_auto_install.stamp:
	DESTDIR=$(DEB_DESTDIR) $(RUSTBUILD) install $(RUSTBUILD_FLAGS)

	mkdir -p $(DEB_DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/
	mv $(DEB_DESTDIR)/usr/lib/lib*.so $(DEB_DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/

	# Replace duplicated compile-time/run-time dylibs with symlinks
	@set -e; \
	for f in $(DEB_DESTDIR)/usr/lib/rustlib/$(DEB_HOST_RUST_TYPE)/lib/lib*.so; do \
	  name=$${f##*/}; \
	  if [ -f "$(DEB_DESTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/$$name" ]; then \
	    echo "ln -sf ../../../$(DEB_HOST_MULTIARCH)/$$name $$f"; \
	    ln -sf ../../../$(DEB_HOST_MULTIARCH)/$$name $$f; \
	  fi; \
	done

	# Remove Cargo made package cache
	rm -vf $(CURDIR)/debian/cargo/.package-cache

	touch "$@"

override_dh_auto_install-arch: debian/dh_auto_install.stamp
ifeq (true,$(BUILD_WINDOWS))
	DESTDIR=$(DEB_DESTDIR) $(RUSTBUILD) install $(RUSTBUILD_FLAGS) \
	  --host $(DEB_BUILD_RUST_TYPE) \
	  --target $(WINDOWS_ARCH)-pc-windows-gnu \
	  library/std
endif
	# Remove Cargo made package cache
	rm -vf $(CURDIR)/debian/cargo/.package-cache


override_dh_auto_install-indep: debian/dh_auto_install.stamp
ifeq (true,$(BUILD_WASM))
	DESTDIR=$(DEB_DESTDIR) $(RUSTBUILD) install $(RUSTBUILD_FLAGS) \
	  --host $(DEB_BUILD_RUST_TYPE) \
	  --target wasm32-unknown-unknown,wasm32-wasi \
	  library/std
endif
ifeq (true,$(BUILD_DOCS))
	# Install Cargo docs
	install -d $(DEB_DESTDIR)/usr/share/doc/cargo
	cp -r $(CURDIR)/build/$(DEB_BUILD_RUST_TYPE)/compiler-doc $(DEB_DESTDIR)/usr/share/doc/cargo/reference
	# Brute force to remove privacy-breach-logo lintian warning.
	# We could have updated the upstream sources but it would complexify
	# the rebase
	@set -e; \
	find $(DEB_DESTDIR)/usr/share/doc/*/html -iname '*.html' | \
	while read file; do \
	  topdir=$$(echo "$$file" | sed 's,^$(DEB_DESTDIR)/usr/share/doc/rust/html/,,; s,/[^/]*$$,/,; s,^[^/]*$$,,; s,[^/]\+/,../,g'); \
	  sed -i \
	    -e "s,https://\(doc\|www\).rust-lang.org/\(favicon.ico\|logos/rust-logo-32x32-blk.png\),$${topdir}rust-logo-32x32-blk.png," \
	    -e 's,<img src="https://github.com/rust-lang/rust-clippy/workflows/Clippy%20Test%20(bors)/badge.svg[^"]*" alt="\([^"]*\)" />,<span class="deb-privacy-replace--github.com-badge">\1</span>,g' \
	    -e 's,<img src="https://img.shields.io/[^"]*" alt="\([^"]*\)" />,<span class="deb-privacy-replace--shields-io">\1</span>,g' "$$file"; \
	done
	find $(DEB_DESTDIR) \( -iname '*.html' -empty -o -name .lock -o -name '*.inc' \) -delete;

	# mv cargo book to cargo-docs
	mv $(DEB_DESTDIR)/usr/share/doc/rust/html/cargo $(DEB_DESTDIR)/usr/share/doc/cargo/book
endif
	# Remove Cargo made package cache
	rm -vf $(CURDIR)/debian/cargo/.package-cache


override_dh_install-indep:
	dh_install
	$(RM) -rf $(SRC_CLEAN:%=debian/rust-src/usr/src/rustc-$(RUST_LONG_VERSION)/%)
	# Get rid of lintian warnings
	find debian/rust-src/usr/src/rustc-$(RUST_LONG_VERSION) \
		\( -name .gitignore \
		-o -name 'LICENSE*' \
		-o -name 'LICENCE' \
		-o -name 'license' \
		-o -name 'COPYING*' \
		-o -name '.eslintrc.js' \
		\) -delete
	# Remove files that autoload remote resources, caught by lintian
	$(RM) -rf debian/rust-src/usr/src/rustc-*/vendor/cssparser/docs/*.html
	$(RM) -rf debian/rust-src/usr/src/rustc-*/vendor/kuchiki/docs/*.html
	$(RM) -rf debian/rust-src/usr/src/rustc-*/vendor/url/docs/*.html
	$(RM) -rf debian/rust-src/usr/src/rustc-*/vendor/xz2/.gitmodules

override_dh_installchangelogs:
	dh_installchangelogs RELEASES.md

override_dh_installdocs:
	dh_installdocs -X.tex -X.aux -X.log -X.out -X.toc

override_dh_compress:
	dh_compress -X.woff

# The below override is disabled on advice from #debian-devel, because:
# - only shared libs get the "split dbgsym package" treatment by dh_strip;
#   static libs simply get their debuginfo discarded
# - strip(1) sometimes breaks wasm libs
#
#override_dh_strip:
#	# Work around #35733, #468333
#	find debian/libstd-rust-dev*/ -name '*.rlib' -execdir mv '{}' '{}.a' \;
#	# This is expected to print out lots of "File format unrecognized" warnings about
#	# rust.metadata.bin and *.deflate but the .o files inside the rlibs should be stripped
#	# Some files are still omitted because of #875780 however.
#	dh_strip -v
#	find debian/libstd-rust-dev*/ -name '*.rlib.a' -execdir sh -c 'mv "$$1" "$${1%.a}"' - '{}' \;

override_dh_dwz:
	# otherwise rustc gets an empty multifile which lintian errors on, causing
	# FTP auto-reject. this is a work-around, the lintian bug is #955752
	# double up the max entries count, else the build might fail..
	dh_dwz --no-dwz-multifile -- -L 100000000

override_dh_makeshlibs:
	dh_makeshlibs -V -N $(LIBSTD_PKG)

	# dh_makeshlibs doesn't support our "libfoo-version.so" naming
	# structure, so we have to do this ourselves.
	mkdir -p debian/$(LIBSTD_PKG)/DEBIAN
	LC_ALL=C ls debian/$(LIBSTD_PKG)/usr/lib/$(DEB_HOST_MULTIARCH)/lib*.so | \
	sed -n 's,^.*/\(lib.*\)-\(.\+\)\.so$$,\1 \2,p' | \
	while read name version; do \
	  echo "$$name $$version $(LIBSTD_PKG) (>= $(DEB_VERSION_UPSTREAM))"; \
	done > debian/$(LIBSTD_PKG)/DEBIAN/shlibs

override_dh_shlibdeps:
	dh_shlibdeps -- -x$(LIBSTD_PKG)

QUILT_SPECIAL_SNOWFLAKE_RETURN_CODE = x=$$?; if [ $$x = 2 ]; then exit 0; else exit $$x; fi
source_orig-stage0:
	QUILT_PATCHES=debian/patches quilt push -aq; $(QUILT_SPECIAL_SNOWFLAKE_RETURN_CODE)
	$(MAKE) -f debian/rules clean
	debian/make_orig-stage0_tarball.sh
	$(MAKE) -f debian/rules clean
	QUILT_PATCHES=debian/patches quilt pop -aq; $(QUILT_SPECIAL_SNOWFLAKE_RETURN_CODE)
	rm -rf .pc

get_beta_version = \
	u="$(DEB_VERSION_UPSTREAM)"; \
	if [ "$$u" != "$${u%~beta.*+dfsg*}" ]; then \
	newver=$(shell echo $(RUST_VERSION) | perl -lpe 's/(\d+)\.(\d+)/$$1 . "." . ($$2)/e'); \
	else \
	newver=$(shell echo $(RUST_VERSION) | perl -lpe 's/(\d+)\.(\d+)/$$1 . "." . ($$2+1)/e'); \
	fi

debian/watch-beta: debian/watch-beta.in debian/rules
	set -e; $(get_beta_version); \
	m4 -DOLDVER="$$oldver" -DNEWVER="$$newver.0" "$<" > "$@"

source_orig-beta: debian/watch-beta
	uscan $(USCAN_OPTS) $(if $(USCAN_DESTDIR),--destdir=$(USCAN_DESTDIR),) --verbose --watchfile "$<"
	set -e; $(get_beta_version); \
	bd="$(if $(USCAN_DESTDIR),$(USCAN_DESTDIR),..)"; \
	tar xf $$bd/rustc-$$newver.0-beta.999-src.tar.xz rustc-beta-src/version; \
	bv="$$(sed -re 's/[0-9]+.[0-9]+.[0-9]+-beta.([0-9]+) \(.*\)/\1/g' rustc-beta-src/version)"; \
	bash -c 'shopt -s nullglob; for i in '"$$bd"'/rustc*beta.999*; do mv $$i $${i/beta.999/beta.'"$$bv"'}; done'; \
	rm -f rustc-beta-src/version; \
	rmdir -p rustc-beta-src; \
	echo "prepared rustc $$newver.0~beta.$$bv in $$bd"