From 9a1f1fd88da1651dd89e6800b7f862dfb0d27fa6 Mon Sep 17 00:00:00 2001
From: Nicolas Boulenguez <nicolas@debian.org>
Date: Thu, 31 Dec 2020 01:42:53 +0100
Subject: [PATCH 09/21] Replace calls to dpkg-parsechangelog with
 /usr/share/dpkg/pkg-info.mk

The snippet improves consistency with other packages and is more
tested (for example, it will return the correct result if upstream
version ever contains a dash).
---
 debian/rules | 25 +++++++++++--------------
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/debian/rules b/debian/rules
index 87417ad..ad12f88 100755
--- a/debian/rules
+++ b/debian/rules
@@ -13,6 +13,8 @@
 
 ###############################################################################
 
+include /usr/share/dpkg/pkg-info.mk
+
 vafilt = $(subst $(2)=,,$(filter $(2)=%,$(1)))
 DPKG_VARS := $(shell dpkg-architecture)
 DEB_BUILD_GNU_TYPE	?= $(call vafilt,$(DPKG_VARS),DEB_BUILD_GNU_TYPE)
@@ -127,8 +129,7 @@ ifneq ($(distribution)-$(CROSS_ARCHS),Ubuntu-)
   NATIVE_ARCHS += mipsn32r6 mipsn32r6el mipsr6 mipsr6el mips64r6 mips64r6el
 endif
 
-src_name := $(shell dpkg-parsechangelog -S Source)
-ifeq ($(src_name),binutils)
+ifeq ($(DEB_SOURCE),binutils)
   with_native	= yes
   with_cross	= yes
   same_source	= yes
@@ -161,7 +162,7 @@ ifeq ($(src_name),binutils)
     # c-t-b passes this one ...
     CROSS_ARCHS := $(filter-out $(DEB_HOST_ARCH),$(CROSS_ARCHS))
   endif
-else ifeq ($(src_name),binutils-mipsen)
+else ifeq ($(DEB_SOURCE),binutils-mipsen)
   with_native	=
   with_cross	= yes
   same_source	=
@@ -338,12 +339,8 @@ endif
 
 SPACE = $(EMPTY) $(EMPTY)
 COMMA = ,
-CHANGELOG_VARS := $(shell dpkg-parsechangelog | \
-        sed -n 's/ /_/g;/^[^_]/s/^\([^:]*\):_\(.*\)/\1=\2/p')
 
-DEB_VERSION	:= $(call vafilt,$(CHANGELOG_VARS),Version)
 DEB_SVERSION	:= $(shell echo $(DEB_VERSION) | sed 's/+b[0-9][0-9]*$$//')
-DEB_UPSTREAM	:= $(firstword $(subst -,$(SPACE),$(DEB_VERSION)))
 
 VERSION		:= $(shell sed -n 's/^ *VERSION=\(.*\)/\1/p' bfd/configure | head -1 | tr -d \')
 #DATE		:= $(shell sed -n 's/.* \([0-9]*\)$$/\1/p' bfd/version.h)
@@ -356,8 +353,8 @@ BUILD_DAY  := $(shell date -Idate -u -d "$(BUILD_DATE)")
 is_rc = yes
 is_rc =
 
-ifneq ($(DEB_UPSTREAM),$(VERSION)$(DATE_EXT))
-  $(error upstream ($(VERSION)$(DATE_EXT)) and debian ($(DEB_UPSTREAM)) version mismatch)
+ifneq ($(DEB_VERSION_UPSTREAM),$(VERSION)$(DATE_EXT))
+  $(error upstream ($(VERSION)$(DATE_EXT)) and debian ($(DEB_VERSION_UPSTREAM)) version mismatch)
 endif
 
 SINGLE_VERSION= -system
@@ -1495,9 +1492,9 @@ endif
 
 	$(install_dir) $(D_CROSS)/DEBIAN $(D_CROSS)-dbg/DEBIAN
 	if [ "$(is_rc)" = yes ]; then \
-	  nver=$$(echo $(DEB_UPSTREAM) | awk -F. '{ OFS="."; NF--; $$NF=$$NF+1; print }'); \
+	  nver=$$(echo $(DEB_VERSION_UPSTREAM) | awk -F. '{ OFS="."; NF--; $$NF=$$NF+1; print }'); \
 	else \
-	  nver=$$(echo $(DEB_UPSTREAM) | awk -F. '{ OFS="."; if (NF > 2) $$NF=$$NF+1; else $$++NF=1; print }'); \
+	  nver=$$(echo $(DEB_VERSION_UPSTREAM) | awk -F. '{ OFS="."; if (NF > 2) $$NF=$$NF+1; else $$++NF=1; print }'); \
 	fi; \
 	date_ext=$(DATE_EXT); \
 	[ "$(is_rc)" = yes ] && date_ext=; \
@@ -1570,9 +1567,9 @@ ifneq (,$(TARGET))
 else
 	: # generate some control & helper files
 	if [ "$(is_rc)" = yes ]; then \
-	  nver=$$(echo $(DEB_UPSTREAM) | awk -F. '{ OFS="."; NF--; $$NF=$$NF+1; print }'); \
+	  nver=$$(echo $(DEB_VERSION_UPSTREAM) | awk -F. '{ OFS="."; NF--; $$NF=$$NF+1; print }'); \
 	else \
-	  nver=$$(echo $(DEB_UPSTREAM) | awk -F. '{ OFS="."; if (NF > 2) $$NF=$$NF+1; else $$++NF=1; print }'); \
+	  nver=$$(echo $(DEB_VERSION_UPSTREAM) | awk -F. '{ OFS="."; if (NF > 2) $$NF=$$NF+1; else $$++NF=1; print }'); \
 	fi; \
 	for i in debian/*.in; do \
 	    case "$$i" in debian/control.in|debian/control.cross.in) continue; esac; \
@@ -1581,7 +1578,7 @@ else
 	    sed -e 's/@VER@/$(VERSION)/g' \
 		-e 's/@DEB_VER@/$(DEB_VERSION)/g' \
 		-e 's/@DEB_SVER@/$(DEB_SVERSION)/g' \
-		-e 's/@DEB_UVER@/$(DEB_UPSTREAM)/g' \
+		-e 's/@DEB_UVER@/$(DEB_VERSION_UPSTREAM)/g' \
 		-e "s/@DEB_NVER@/$$nver/g" \
 		-e "s/@DATE_EXT@/$$date_ext/g" \
 		-e 's/@DEB_ARCH@/$(DEB_HOST_ARCH)/g' \
-- 
2.30.1

