Handle top level options set in subprojects. Closes #13847.
diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py
index 98656b2..8576ace 100644
--- a/mesonbuild/coredata.py
+++ b/mesonbuild/coredata.py
@@ -658,9 +658,20 @@
             elif k.machine != MachineChoice.BUILD and not self.optstore.is_compiler_option(k):
                 unknown_options.append(k)
         if unknown_options:
-            unknown_options_str = ', '.join(sorted(str(s) for s in unknown_options))
-            sub = f'In subproject {subproject}: ' if subproject else ''
-            raise MesonException(f'{sub}Unknown options: "{unknown_options_str}"')
+            if subproject:
+                # The subproject may have top-level options that should be used
+                # when it not a subproject. Ignore those for now. With option
+                # refactor they will get per-subproject values.
+                really_unknown = []
+                for uo in unknown_options:
+                    topkey = uo.evolve(subproject='')
+                    if topkey not in self.optstore:
+                        really_unknown.append(uo)
+                unknown_options = really_unknown
+            if unknown_options:
+                unknown_options_str = ', '.join(sorted(str(s) for s in unknown_options))
+                sub = f'In subproject {subproject}: ' if subproject else ''
+                raise MesonException(f'{sub}Unknown options: "{unknown_options_str}"')
 
         if not self.is_cross_build():
             dirty |= self.copy_build_options_from_regular_ones()
diff --git a/test cases/unit/123 pkgsubproj/meson.build b/test cases/unit/123 pkgsubproj/meson.build
new file mode 100644
index 0000000..6075949
--- /dev/null
+++ b/test cases/unit/123 pkgsubproj/meson.build
@@ -0,0 +1,3 @@
+project('pkg_opt_test', 'c')
+
+subproject('sub')
diff --git a/test cases/unit/123 pkgsubproj/subprojects/sub/meson.build b/test cases/unit/123 pkgsubproj/subprojects/sub/meson.build
new file mode 100644
index 0000000..e0217df
--- /dev/null
+++ b/test cases/unit/123 pkgsubproj/subprojects/sub/meson.build
@@ -0,0 +1 @@
+project('subproject', 'c', default_options: 'pkgconfig.relocatable=true')
diff --git a/unittests/linuxliketests.py b/unittests/linuxliketests.py
index 1e9e38d..adc6033 100644
--- a/unittests/linuxliketests.py
+++ b/unittests/linuxliketests.py
@@ -1863,3 +1863,8 @@
         self.assertIn('build t9-e1: c_LINKER t9-e1.p/main.c.o | libt9-s1.a libt9-s2.a libt9-s3.a\n', content)
         self.assertIn('build t12-e1: c_LINKER t12-e1.p/main.c.o | libt12-s1.a libt12-s2.a libt12-s3.a\n', content)
         self.assertIn('build t13-e1: c_LINKER t13-e1.p/main.c.o | libt12-s1.a libt13-s3.a\n', content)
+
+    def test_sp_options(self):
+        testdir = os.path.join(self.unit_test_dir, '123 pkgsubproj')
+        self.init(testdir)
+