--- /dev/null
+Backported from:
+
+https://gitlab.com/gpsd/gpsd/-/commit/28fb46ac70059d3b0eb64041c35ef83027bd8506
+https://gitlab.com/gpsd/gpsd/-/commit/c5ed9736d859fed0682e60e899e9617ac67da11b
+
+From c2b4d3fb9a9e011bdc2fb891b78c6ce13f0c7101 Mon Sep 17 00:00:00 2001
+From: "Sergey V. Lobanov" <sergey@lobanov.in>
+Date: Mon, 24 Jan 2022 16:01:54 -0800
+Subject: [PATCH] SConscript: Add target_platform config option.
+
+This patch adds an ability to redefine target platform using config
+optiont target_platform=<platform> to support cross-platform compilation.
+
+This allows cross cimpileg for openWRT (target_platform=linux) on
+osX (sys.platform() == darwin)
+
+Signed-off-by: Gary E. Miller <gem@rellim.com>
+---
+ SConscript | 41 +++++++++++++++++++++++++++--------------
+ 1 file changed, 27 insertions(+), 14 deletions(-)
+
+--- a/SConscript
++++ b/SConscript
+@@ -52,6 +52,7 @@ EnsurePythonVersion(2, 6)
+ # e.g. "scons-3" on CentOS 8.
+ scons_executable_name = os.path.basename(sys.argv[0]) or 'scons'
+
++
+ # Have scons rebuild an existing target when the source(s) MD5 changes
+ # Do not use time to prevent rebuilding when sources, like gpsd_config.h,
+ # are rebuilt, but with no changes.
+@@ -351,6 +352,7 @@ boolopts = (
+ ("gpsdclients", True, "gspd client programs"),
+ ("gpsd", True, "gpsd itself"),
+ ("implicit_link", imloads, "implicit linkage is supported in shared libs"),
++ # FIXME: should check for Pi, not for "linux"
+ ("magic_hat", sys.platform.startswith('linux'),
+ "special Linux PPS hack for Raspberry Pi et al"),
+ ("minimal", False, "turn off every option not set on the command line"),
+@@ -410,6 +412,10 @@ nonboolopts = (
+ "Prefix to the binary tools to use (gcc, ld, etc.)\n"
+ "For cross-compiling, or building with multiple local toolchains.\n"
+ ),
++ # If build and target platform are different, then redefining target
++ # platform might be necessary to use better build flags
++ ("target_platform", sys.platform,
++ "target platform for cross-compiling (linux, darwin, etc.)"),
+ ("target_python", def_target_python, "target Python version as command"),
+ )
+
+@@ -878,13 +884,16 @@ have_valgrind = False
+ # per SCons 4.0.1 doc: Section 23.9. Not Configuring When Cleaning Targets
+ if not cleaning and not helping:
+ # OS X aliases gcc to clang
++ if (sys.platform != config.env['target_platform']):
++ announce("Target system is: %s" % config.env['target_platform'])
++
+ announce("cc is %s, version %s" % (env['CC'], env['CCVERSION']))
+ # clang accepts -pthread, then warns it is unused.
+ if not config.CheckCC():
+ announce("ERROR: CC doesn't work")
+
+ if ((config.CheckCompilerOption("-pthread") and
+- not sys.platform.startswith('darwin'))):
++ not config.env['target_platform'].startswith('darwin'))):
+ config.env.MergeFlags("-pthread")
+
+ confdefs = ["/* gpsd_config.h generated by scons, do not hand-hack. */\n"]
+@@ -935,7 +944,7 @@ if not cleaning and not helping:
+ # confdefs.append('#endif\n')
+ # Reinstated for FreeBSD (below) 16-Aug-2019
+
+- if sys.platform.startswith('linux'):
++ if config.env['target_platform'].startswith('linux'):
+ # for cfmakeraw(), strsep(), etc. on CentOS 7
+ # glibc 2.19 and before
+ # sets __USE_MISC
+@@ -947,7 +956,7 @@ if not cleaning and not helping:
+ confdefs.append('#if !defined(_GNU_SOURCE)')
+ confdefs.append('#define _GNU_SOURCE 1')
+ confdefs.append('#endif\n')
+- elif sys.platform.startswith('darwin'):
++ elif config.env['target_platform'].startswith('darwin'):
+ # strlcpy() and SIGWINCH need _DARWIN_C_SOURCE
+ confdefs.append('#if !defined(_DARWIN_C_SOURCE)')
+ confdefs.append('#define _DARWIN_C_SOURCE 1\n')
+@@ -962,7 +971,7 @@ if not cleaning and not helping:
+ "-Wl,-compatibility_version,%s" % libgps_version,
+ "-Wl,-install_name,%s/$TARGET.srcpath" %
+ installdir('libdir', add_destdir=False)]
+- elif sys.platform.startswith('freebsd'):
++ elif config.env['target_platform'].startswith('freebsd'):
+ # for isascii(), putenv(), nice(), strptime()
+ confdefs.append('#if !defined(_XOPEN_SOURCE)')
+ confdefs.append('#define _XOPEN_SOURCE 700')
+@@ -975,7 +984,7 @@ if not cleaning and not helping:
+ confdefs.append('#if !defined(__BSD_VISIBLE)')
+ confdefs.append("#define __BSD_VISIBLE 1\n")
+ confdefs.append('#endif\n')
+- elif sys.platform.startswith('openbsd'):
++ elif config.env['target_platform'].startswith('openbsd'):
+ # required to define u_int in sys/time.h
+ confdefs.append('#if !defined(_BSD_SOURCE)')
+ confdefs.append("#define _BSD_SOURCE 1\n")
+@@ -984,12 +993,12 @@ if not cleaning and not helping:
+ confdefs.append('#if !defined(__BSD_VISIBLE)')
+ confdefs.append("#define __BSD_VISIBLE 1\n")
+ confdefs.append('#endif\n')
+- elif sys.platform.startswith('netbsd'):
++ elif config.env['target_platform'].startswith('netbsd'):
+ # required to get strlcpy(), and more, from string.h
+ confdefs.append('#if !defined(_NETBSD_SOURCE)')
+ confdefs.append("#define _NETBSD_SOURCE 1\n")
+ confdefs.append('#endif\n')
+- elif sys.platform.startswith('sunos5'):
++ elif config.env['target_platform'].startswith('sunos5'):
+ # tested with gcc-5.5 on slowlaris 10
+ # required to get isascii(), and more, from ctype.h
+ confdefs.append('#if !defined(__XPG4_CHAR_CLASS__)')
+@@ -1044,11 +1053,11 @@ if not cleaning and not helping:
+ ncurseslibs = ['!ncurses5-config --libs --cflags']
+ elif WhereIs('ncursesw5-config'):
+ ncurseslibs = ['!ncursesw5-config --libs --cflags']
+- elif sys.platform.startswith('freebsd'):
++ elif config.env['target_platform'].startswith('freebsd'):
+ ncurseslibs = ['-lncurses']
+- elif (sys.platform.startswith('darwin') or
+- sys.platform.startswith('openbsd') or
+- sys.platform.startswith('sunos5')):
++ elif (config.env['target_platform'].startswith('darwin') or
++ config.env['target_platform'].startswith('openbsd') or
++ config.env['target_platform'].startswith('sunos5')):
+ ncurseslibs = ['-lcurses']
+ else:
+ announce('Turning off ncurses support, library not found.')
+@@ -1064,7 +1073,8 @@ if not cleaning and not helping:
+ announce("pkg_config is confused about the state "
+ "of libusb-1.0.")
+ usbflags = []
+- elif sys.platform.startswith("freebsd"):
++ elif config.env['target_platform'].startswith('freebsd'):
++ # FIXME: shold directly test for libusb existence.
+ confdefs.append("#define HAVE_LIBUSB 1\n")
+ usbflags = ["-lusb"]
+ else:
+@@ -2498,8 +2508,11 @@ if qt_env:
+ binaryinstall.append(GPSLibraryInstall(qt_env, installdir('libdir'),
+ compiled_qgpsmmlib, libgps_version))
+
+-if ((not env['debug'] and not env['debug_opt'] and not env['profiling'] and
+- not env['nostrip'] and not sys.platform.startswith('darwin'))):
++if ((not env['debug'] and
++ not env['debug_opt'] and
++ not env['profiling'] and
++ not env['nostrip'] and
++ not env['target_platform'].startswith('darwin'))):
+ env.AddPostAction(binaryinstall, '$STRIP $TARGET')
+
+ binaryinstall.append(env.Install(installdir('bindir'), bin_scripts))