diff -pruN 4.0.2-1/debian/changelog 4.0.2-1ubuntu1/debian/changelog
--- 4.0.2-1/debian/changelog	2020-11-14 22:10:15.000000000 +0000
+++ 4.0.2-1ubuntu1/debian/changelog	2021-06-11 00:18:00.000000000 +0000
@@ -1,3 +1,10 @@
+swig (4.0.2-1ubuntu1) impish; urgency=medium
+
+  * d/p/php8-compatibility.patch: Backport upstream patch to add
+    preliminary PHP 8.0 support, required for building mapserver.
+
+ -- Bryce Harrington <bryce@canonical.com>  Fri, 11 Jun 2021 00:18:00 +0000
+
 swig (4.0.2-1) unstable; urgency=medium
 
   * New upstream release 4.0.2.
diff -pruN 4.0.2-1/debian/control 4.0.2-1ubuntu1/debian/control
--- 4.0.2-1/debian/control	2020-11-14 22:09:34.000000000 +0000
+++ 4.0.2-1ubuntu1/debian/control	2021-06-11 00:09:35.000000000 +0000
@@ -6,7 +6,6 @@ Standards-Version: 4.5.0
 Vcs-Git: https://salsa.debian.org/debian/swig.git
 Vcs-Browser: https://salsa.debian.org/debian/swig
 Homepage: http://www.swig.org/
-Testsuite: autopkgtest
 Build-Depends: dpkg-dev (>= 1.16.1~),
                bison,
                debhelper (>= 10~),
diff -pruN 4.0.2-1/debian/patches/fix_flatnested.patch 4.0.2-1ubuntu1/debian/patches/fix_flatnested.patch
--- 4.0.2-1/debian/patches/fix_flatnested.patch	2020-01-16 22:30:31.000000000 +0000
+++ 4.0.2-1ubuntu1/debian/patches/fix_flatnested.patch	1970-01-01 00:00:00.000000000 +0000
@@ -1,69 +0,0 @@
-Subject: fix crash using "flatnested" feature for out-of-scope defined nested classes
-Origin: upstream, https://github.com/swig/swig/commit/8bc38dc0070740d1a4b1ec522f80c7e292a74850
-Author: Vladimir Kalinin <vkalinin@opendesign.com>
-Bug-Debian: https://bugs.debian.org/768280
-
-commit 8bc38dc0070740d1a4b1ec522f80c7e292a74850
-Date:   Tue Nov 4 19:30:53 2014 +0300
-
-    fixes "flatnested"  feature for out-of-scope defined nested classes
-
-diff --git a/Examples/test-suite/nested_scope.i b/Examples/test-suite/nested_scope.i
-index 7894783..059b735 100644
---- a/Examples/test-suite/nested_scope.i
-+++ b/Examples/test-suite/nested_scope.i
-@@ -26,4 +26,15 @@ namespace ns {
- #endif
- 	};
- }
-+#ifndef __clang__
-+		class Outer1 {
-+			struct Nested1;
-+		public:
-+			struct Nested2;
-+		};
-+		struct Outer1::Nested2 {
-+			int data;
-+		};
-+#endif
-+
- %}
-diff --git a/Source/CParse/parser.y b/Source/CParse/parser.y
-index 61b0462..c4061b1 100644
---- a/Source/CParse/parser.y
-+++ b/Source/CParse/parser.y
-@@ -1018,7 +1018,7 @@ static Node *nested_forward_declaration(const char *storage, const char *kind, S
-     }
-   }
- 
--  if (!GetFlag(currentOuterClass, "nested")) {
-+  if (!currentOuterClass || !GetFlag(currentOuterClass, "nested")) {
-     if (nn && Equal(nodeType(nn), "classforward")) {
-       Node *n = nn;
-       SWIG_WARN_NODE_BEGIN(n);
-@@ -3451,8 +3451,14 @@ cpp_class_decl  : storage_class cpptype idcolon inherit LBRACE {
- 		   nscope = Getattr($<node>$, "nested:nscope");
- 		   Delattr($<node>$, "nested:innerscope");
- 		   Delattr($<node>$, "nested:nscope");
--		   if (nscope_inner && Strcmp(nodeType(nscope_inner), "class") == 0) /* actual parent class for this class */
--		     Setattr($$, "nested:outer", nscope_inner);
-+		   if (nscope_inner && Strcmp(nodeType(nscope_inner), "class") == 0) { /* actual parent class for this class */
-+		     Node* forward_declaration = Swig_symbol_clookup_no_inherit(Getattr($<node>$,"name"), Getattr(nscope_inner, "symtab"));
-+		     if (forward_declaration) {
-+		       Setattr($<node>$, "access", Getattr(forward_declaration, "access"));
-+		     }
-+		     Setattr($<node>$, "nested:outer", nscope_inner);
-+		     SetFlag($<node>$, "nested");
-+                   }
- 		   if (!currentOuterClass)
- 		     inclass = 0;
- 		   cscope = Getattr($$, "prev_symtab");
-@@ -3531,6 +3537,8 @@ cpp_class_decl  : storage_class cpptype idcolon inherit LBRACE {
- 		   } else if (nscope_inner) {
- 		     /* this is tricky */
- 		     /* we add the declaration in the original namespace */
-+		     if (Strcmp(nodeType(nscope_inner), "class") == 0 && cparse_cplusplus && ignore_nested_classes && !GetFlag($$, "feature:flatnested"))
-+		       $$ = nested_forward_declaration($1, $2, $3, Copy($3), $9);
- 		     appendChild(nscope_inner, $$);
- 		     Swig_symbol_setscope(Getattr(nscope_inner, "symtab"));
- 		     Delete(Namespaceprefix);
diff -pruN 4.0.2-1/debian/patches/php8-compatibility.patch 4.0.2-1ubuntu1/debian/patches/php8-compatibility.patch
--- 4.0.2-1/debian/patches/php8-compatibility.patch	1970-01-01 00:00:00.000000000 +0000
+++ 4.0.2-1ubuntu1/debian/patches/php8-compatibility.patch	2021-06-11 00:09:35.000000000 +0000
@@ -0,0 +1,326 @@
+From 843f9658a8fe5de517260dbea49fdbb9d498fb90 Mon Sep 17 00:00:00 2001
+From: Bryce Harrington <bryce@canonical.com>
+Date: Fri, 11 Jun 2021 00:02:20 +0000
+Subject: [PATCH] From 3584c7d49cb598ce79d5e285d6c17b2dedfe3ecb Mon Sep 17
+ 00:00:00 2001 From: Olly Betts <olly@survex.com> Date: Wed, 17 Mar 2021
+ 12:45:17 +1300 Subject: [PATCH] Add initial support for PHP8
+
+Testcase director_overload2 is failing, but the rest of the testsuite
+passes.
+---
+
+Backported by bryce@canonical.com for 4.0.2-1 from:
+https://github.com/swig/swig/commit/3584c7d49cb598ce79d5e285d6c17b2dedfe3ecb.patch
+---
+ .travis.yml                                   |  4 ++
+ Doc/Manual/Php.html                           |  6 +-
+ Doc/Manual/Preprocessor.html                  |  2 +-
+ Doc/Manual/SWIG.html                          |  2 +-
+ Examples/Makefile.in                          |  6 +-
+ .../php/evil_diamond_prop_runme.php           |  2 +-
+ Lib/cdata.i                                   |  2 +-
+ Lib/exception.i                               |  2 +-
+ Lib/php/phprun.swg                            |  8 ++-
+ Source/Modules/php.cxx                        | 55 +++++++++++++++----
+ configure.ac                                  | 10 ++--
+ 11 files changed, 72 insertions(+), 27 deletions(-)
+
+diff --git a/.travis.yml b/.travis.yml
+index b806c85e0..a7965c60a 100644
+--- a/.travis.yml
++++ b/.travis.yml
+@@ -157,6 +157,10 @@ matrix:
+       env: SWIGLANG=perl5
+       sudo: required
+       dist: xenial
++    - compiler: gcc
++      os: linux
++      env: SWIGLANG=php VER=8.0
++      dist: xenial
+     - compiler: gcc
+       os: linux
+       env: SWIGLANG=php VER=7.0
+diff --git a/Doc/Manual/Php.html b/Doc/Manual/Php.html
+index d0ec0df7f..20d2ac3bc 100644
+--- a/Doc/Manual/Php.html
++++ b/Doc/Manual/Php.html
+@@ -51,12 +51,12 @@
+ 
+ <p>
+ In this chapter, we discuss SWIG's support of PHP.  SWIG currently supports
+-generating wrappers for PHP7.  Support for PHP5 was removed in SWIG 4.0.0
+-and support for PHP4 was removed in SWIG 1.3.37.
++generating wrappers for PHP7 and PHP8.  Support for PHP5 was removed in SWIG
++4.0.0 and support for PHP4 was removed in SWIG 1.3.37.
+ </p>
+ 
+ <p>
+-Currently any PHP7 release should work.
++Currently any PHP7 or PHP8 release should work.
+ </p>
+ 
+ <p>
+diff --git a/Doc/Manual/Preprocessor.html b/Doc/Manual/Preprocessor.html
+index 66061a597..4e783c801 100644
+--- a/Doc/Manual/Preprocessor.html
++++ b/Doc/Manual/Preprocessor.html
+@@ -123,7 +123,7 @@ SWIGOCAML                       Defined when using OCaml
+ SWIGOCTAVE                      Defined when using Octave
+ SWIGPERL                        Defined when using Perl
+ SWIGPHP                         Defined when using PHP (any version)
+-SWIGPHP7                        Defined when using PHP7
++SWIGPHP7                        Defined when using PHP 7 or later
+ SWIGPYTHON                      Defined when using Python
+ SWIGR                           Defined when using R
+ SWIGRUBY                        Defined when using Ruby
+diff --git a/Doc/Manual/SWIG.html b/Doc/Manual/SWIG.html
+index c54d117e0..f48842565 100644
+--- a/Doc/Manual/SWIG.html
++++ b/Doc/Manual/SWIG.html
+@@ -127,7 +127,7 @@ Supported Target Language Options
+      -lua            - Generate Lua wrappers
+      -octave         - Generate Octave wrappers
+      -perl5          - Generate Perl 5 wrappers
+-     -php7           - Generate PHP 7 wrappers
++     -php7           - Generate PHP 7 or later wrappers
+      -python         - Generate Python wrappers
+      -r              - Generate R (aka GNU S) wrappers
+      -ruby           - Generate Ruby wrappers
+diff --git a/Examples/Makefile.in b/Examples/Makefile.in
+index 87386f7eb..40aab30e0 100644
+--- a/Examples/Makefile.in
++++ b/Examples/Makefile.in
+@@ -1056,7 +1056,7 @@ ruby_clean:
+ 	rm -f *.@OBJEXT@ *$(RUBY_SO)
+ 
+ ##################################################################
+-#####                       PHP7                            ######
++#####                       PHP                             ######
+ ##################################################################
+ 
+ PHP         = @PHP@
+@@ -1069,7 +1069,7 @@ PHP_SCRIPT  = $(SRCDIR)$(RUNME).php
+ # -------------------------------------------------------------------
+ 
+ php: $(SRCDIR_SRCS)
+-	$(SWIG) -php7 $(SWIGOPT) -o $(ISRCS) $(INTERFACEPATH)
++	$(SWIG) -php $(SWIGOPT) -o $(ISRCS) $(INTERFACEPATH)
+ 	$(CC) -c $(CCSHARED) $(CPPFLAGS) $(CFLAGS) $(SRCDIR_SRCS) $(ISRCS) $(INCLUDES) $(PHP_INCLUDE)
+ 	$(LDSHARED) $(CFLAGS) $(LDFLAGS) $(OBJS) $(IOBJS) $(LIBS) -o $(LIBPREFIX)$(TARGET)$(PHP_SO)
+ 
+@@ -1078,7 +1078,7 @@ php: $(SRCDIR_SRCS)
+ # --------------------------------------------------------------------
+ 
+ php_cpp: $(SRCDIR_SRCS)
+-	$(SWIG) -php7 -c++ $(SWIGOPT) -o $(ICXXSRCS) $(INTERFACEPATH)
++	$(SWIG) -php -c++ $(SWIGOPT) -o $(ICXXSRCS) $(INTERFACEPATH)
+ 	$(CXX) -c $(CCSHARED) $(CPPFLAGS) $(CXXFLAGS) $(SRCDIR_SRCS) $(SRCDIR_CXXSRCS) $(ICXXSRCS) $(INCLUDES) $(PHP_INCLUDE)
+ 	$(CXXSHARED) $(CXXFLAGS) $(LDFLAGS) $(OBJS) $(IOBJS) $(LIBS) $(CPP_DLLIBS) -o $(LIBPREFIX)$(TARGET)$(PHP_SO)
+ 
+diff --git a/Examples/test-suite/php/evil_diamond_prop_runme.php b/Examples/test-suite/php/evil_diamond_prop_runme.php
+index 9bdb7435f..645328aff 100644
+--- a/Examples/test-suite/php/evil_diamond_prop_runme.php
++++ b/Examples/test-suite/php/evil_diamond_prop_runme.php
+@@ -31,7 +31,7 @@ check::is_a($spam,"spam");
+ check::equal(1,$spam->_foo,"1==spam->_foo");
+ check::equal(2,$spam->_bar,"2==spam->_bar");
+ // multiple inheritance not supported in PHP
+-set_error_handler(NULL, 0); // Don't complain that _baz is unknown.
++set_error_handler(function () {return true;}, E_NOTICE|E_WARNING); // Don't complain that _baz is unknown.
+ check::equal(null,$spam->_baz,"null==spam->_baz");
+ restore_error_handler();
+ check::equal(4,$spam->_spam,"4==spam->_spam");
+diff --git a/Lib/cdata.i b/Lib/cdata.i
+index f18ed4af5..cd1526643 100644
+--- a/Lib/cdata.i
++++ b/Lib/cdata.i
+@@ -21,7 +21,7 @@ typedef struct SWIGCDATA {
+ }
+ %typemap(in) (const void *indata, int inlen) = (char *STRING, int LENGTH);
+ 
+-#elif SWIGPHP7
++#elif SWIGPHP
+ 
+ %typemap(out) SWIGCDATA {
+   ZVAL_STRINGL($result, $1.data, $1.len);
+diff --git a/Lib/exception.i b/Lib/exception.i
+index ee9ce9bc6..3d6eeccdf 100644
+--- a/Lib/exception.i
++++ b/Lib/exception.i
+@@ -12,7 +12,7 @@
+ %insert("runtime") "swigerrors.swg"
+ 
+ 
+-#ifdef SWIGPHP7
++#ifdef SWIGPHP
+ %{
+ #include "zend_exceptions.h"
+ #define SWIG_exception(code, msg) do { zend_throw_exception(NULL, (char*)msg, code); goto thrown; } while (0)
+diff --git a/Lib/php/phprun.swg b/Lib/php/phprun.swg
+index a07a1b9f8..aae71136e 100644
+--- a/Lib/php/phprun.swg
++++ b/Lib/php/phprun.swg
+@@ -12,8 +12,8 @@ extern "C" {
+ #include "zend_exceptions.h"
+ #include "php.h"
+ 
+-#if PHP_MAJOR_VERSION != 7
+-# error These bindings need PHP7 - to generate PHP5 bindings use: SWIG < 4.0.0 and swig -php5
++#if PHP_MAJOR_VERSION < 7
++# error These bindings need PHP 7 or later - to generate PHP5 bindings use: SWIG < 4.0.0 and swig -php5
+ #endif
+ 
+ #include "ext/standard/php_string.h"
+@@ -204,7 +204,11 @@ SWIG_ConvertPtr(zval *z, void **ptr, swig_type_info *ty, int flags) {
+ 
+   switch (Z_TYPE_P(z)) {
+     case IS_OBJECT: {
++#if PHP_MAJOR_VERSION < 8
+       HashTable * ht = Z_OBJ_HT_P(z)->get_properties(z);
++#else
++      HashTable * ht = Z_OBJ_HT_P(z)->get_properties(Z_OBJ_P(z));
++#endif
+       if (ht) {
+         zval * _cPtr = zend_hash_str_find(ht, "_cPtr", sizeof("_cPtr") - 1);
+         if (_cPtr) {
+diff --git a/Source/Modules/php.cxx b/Source/Modules/php.cxx
+index 1edbd874c..34c2db804 100644
+--- a/Source/Modules/php.cxx
++++ b/Source/Modules/php.cxx
+@@ -473,6 +473,20 @@ public:
+     s_arginfo = NewString("/* arginfo subsection */\n");
+     arginfo_used = NewHash();
+ 
++    // Add arginfo we'll definitely need for *_alter_newobject and *_get_newobject.
++    SetFlag(arginfo_used, "1");
++    Append(s_arginfo,
++	   "ZEND_BEGIN_ARG_INFO_EX(swig_arginfo_1, 0, 0, 0)\n"
++	   " ZEND_ARG_INFO(0,arg1)\n"
++	   "ZEND_END_ARG_INFO()\n");
++
++    SetFlag(arginfo_used, "2");
++    Append(s_arginfo,
++	   "ZEND_BEGIN_ARG_INFO_EX(swig_arginfo_2, 0, 0, 0)\n"
++	   " ZEND_ARG_INFO(0,arg1)\n"
++	   " ZEND_ARG_INFO(0,arg2)\n"
++	   "ZEND_END_ARG_INFO()\n");
++
+     /* start the function entry section */
+     s_entry = NewString("/* entry subsection */\n");
+ 
+@@ -653,8 +667,8 @@ public:
+     }
+     Printv(f_begin, s_vdecl, s_wrappers, NIL);
+     Printv(f_begin, all_cs_entry, "\n\n", s_arginfo, "\n\n", s_entry,
+-	" SWIG_ZEND_NAMED_FE(swig_", module, "_alter_newobject,_wrap_swig_", module, "_alter_newobject,NULL)\n"
+-	" SWIG_ZEND_NAMED_FE(swig_", module, "_get_newobject,_wrap_swig_", module, "_get_newobject,NULL)\n"
++	" SWIG_ZEND_NAMED_FE(swig_", module, "_alter_newobject,_wrap_swig_", module, "_alter_newobject,swig_arginfo_2)\n"
++	" SWIG_ZEND_NAMED_FE(swig_", module, "_get_newobject,_wrap_swig_", module, "_get_newobject,swig_arginfo_1)\n"
+ 	" ZEND_FE_END\n};\n\n", NIL);
+     Printv(f_begin, s_init, NIL);
+     Delete(s_header);
+@@ -689,25 +703,46 @@ public:
+ 
+     // We want to only emit each different arginfo once, as that reduces the
+     // size of both the generated source code and the compiled extension
+-    // module.  To do this, we name the arginfo to encode the number of
+-    // parameters and which (if any) are passed by reference by using a
+-    // sequence of 0s (for non-reference) and 1s (for by references).
++    // module.  The parameters at this level are just named arg1, arg2, etc
++    // so we generate an arginfo name with the number of parameters and a
++    // bitmap value saying which (if any) are passed by reference.
+     ParmList *l = Getattr(n, "parms");
+-    String * arginfo_code = NewStringEmpty();
++    unsigned long bitmap = 0, bit = 1;
++    int n_params = 0;
++    bool overflowed = false;
+     for (Parm *p = l; p; p = Getattr(p, "tmap:in:next")) {
+       /* Ignored parameters */
+       if (checkAttribute(p, "tmap:in:numinputs", "0")) {
+ 	continue;
+       }
+-      Append(arginfo_code, GetFlag(p, "tmap:in:byref") ? "1" : "0");
++      ++n_params;
++      if (GetFlag(p, "tmap:in:byref")) {
++	  bitmap |= bit;
++	  if (bit == 0) overflowed = true;
++      }
++      bit <<= 1;
++    }
++    String * arginfo_code;
++    if (overflowed) {
++      // We overflowed the bitmap so just generate a unique name - this only
++      // happens for a function with more parameters than bits in a long
++      // where a high numbered parameter is passed by reference, so should be
++      // rare in practice.
++      static int overflowed_counter = 0;
++      arginfo_code = NewStringf("z%d", ++overflowed_counter);
++    } else if (bitmap == 0) {
++      // No parameters passed by reference.
++      arginfo_code = NewStringf("%d", n_params);
++    } else {
++      arginfo_code = NewStringf("%d_%lx", n_params, bitmap);
+     }
+ 
+     if (!GetFlag(arginfo_used, arginfo_code)) {
+-      // Not had this one before, so emit it.
++      // Not had this one before so emit it.
+       SetFlag(arginfo_used, arginfo_code);
+       Printf(s_arginfo, "ZEND_BEGIN_ARG_INFO_EX(swig_arginfo_%s, 0, 0, 0)\n", arginfo_code);
+-      for (const char * p = Char(arginfo_code); *p; ++p) {
+-	Printf(s_arginfo, " ZEND_ARG_PASS_INFO(%c)\n", *p);
++      for (Parm *p = l; p; p = Getattr(p, "tmap:in:next")) {
++	Printf(s_arginfo, " ZEND_ARG_INFO(%d,%s)\n", GetFlag(p, "tmap:in:byref"), Getattr(p, "lname"));
+       }
+       Printf(s_arginfo, "ZEND_END_ARG_INFO()\n");
+     }
+diff --git a/configure.ac b/configure.ac
+index 22e3672c3..c50c973d2 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -2035,7 +2035,7 @@ AC_SUBST(RUBYSO)
+ AC_SUBST(RUBYDYNAMICLINKING)
+ 
+ #-------------------------------------------------------------------------
+-# Look for PHP7
++# Look for PHP
+ #-------------------------------------------------------------------------
+ 
+ PHPBIN=
+@@ -2049,7 +2049,7 @@ if test x"${PHPBIN}" = xno; then
+     PHP=
+ else
+     if test "x$PHPBIN" = xyes; then
+-      AC_CHECK_PROGS(PHP, [php7.3 php7.2 php7.1 php7.0 php])
++      AC_CHECK_PROGS(PHP, [php8.0 php7.4 php7.3 php7.2 php7.1 php7.0 php])
+     else
+       PHP=$PHPBIN
+     fi
+@@ -2060,12 +2060,14 @@ else
+       case $PHP in
+         *7.*)
+           PHPCONFIG=`echo "$PHP"|sed 's/7\...*$/-config&/'` ;;
++        *8.*)
++          PHPCONFIG=`echo "$PHP"|sed 's/8\...*$/-config&/'` ;;
+         *)
+           PHPCONFIG=$PHP-config ;;
+       esac
+       php_version=`$PHPCONFIG --version 2>/dev/null`
+       case $php_version in
+-      7.*)
++      7.*|8.*)
+         PHPINC=`$PHPCONFIG --includes 2>/dev/null`
+         if test -n "$PHPINC"; then
+           AC_MSG_RESULT($PHPINC)
+@@ -2076,7 +2078,7 @@ else
+       "")
+         AC_MSG_RESULT([could not find $PHPCONFIG or obtain PHP version from it]) ;;
+       *)
+-        AC_MSG_RESULT([found PHP $php_version - not PHP 7]) ;;
++        AC_MSG_RESULT([found PHP $php_version - not PHP 7 or 8]) ;;
+       esac
+     fi
+ fi
+-- 
+2.31.1
+
diff -pruN 4.0.2-1/debian/patches/series 4.0.2-1ubuntu1/debian/patches/series
--- 4.0.2-1/debian/patches/series	2020-11-14 21:39:03.000000000 +0000
+++ 4.0.2-1ubuntu1/debian/patches/series	2021-06-11 00:09:35.000000000 +0000
@@ -1,3 +1,4 @@
 ccache_program_transform.patch
 ccache-rename.diff
 ccache-use-cppflags
+php8-compatibility.patch
diff -pruN 4.0.2-1/debian/rules 4.0.2-1ubuntu1/debian/rules
--- 4.0.2-1/debian/rules	2020-02-19 20:29:34.000000000 +0000
+++ 4.0.2-1ubuntu1/debian/rules	2021-06-11 00:09:35.000000000 +0000
@@ -12,6 +12,7 @@ export DH_ALWAYS_EXCLUDE=.svn
 
 DPKG_EXPORT_BUILDFLAGS = 1
 include /usr/share/dpkg/buildflags.mk
+include /usr/share/dpkg/architecture.mk
 
 # XXX: patch to pike detection submitted upstream, need to adapt
 # configure args
@@ -21,7 +22,6 @@ CONFIGURE_ARGS := \
 	--with-swiglibdir=/usr/share/swig4.0		\
     --program-suffix=4.0
 
-DEB_TARGET_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
 NO_PACKAGES :=
 
 configure-stamp: $(QUILT_STAMPFN)
