#!/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 = 15
OLD_LLVM_VERSION = 14
# 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

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

.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

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)" \
		"$<" > "$@"
	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

debian/dh_auto_configure.stamp: debian/config.toml check-no-old-llvm
	# fail the build if we accidentally vendored openssl, indicates we pulled in unnecessary dependencies
	test ! -e vendor/openssl
	# 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
	# 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)
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.
FAILURES_ALLOWED = 8
ifneq (,$(filter $(DEB_BUILD_ARCH), armhf))
  FAILURES_ALLOWED = 12
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))
  FAILURES_ALLOWED = 40
endif
ifneq (,$(filter $(DEB_BUILD_ARCH), powerpc powerpcspe riscv64 sparc64 x32))
  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

	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

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))
	# 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;
endif

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
	dh_dwz --no-dwz-multifile

override_dh_makeshlibs:
	dh_makeshlibs -V

	# 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
	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"