Add kernel and userland properties to machine objects.
diff --git a/docs/markdown/Cross-compilation.md b/docs/markdown/Cross-compilation.md
index fb22222..159d0ab 100644
--- a/docs/markdown/Cross-compilation.md
+++ b/docs/markdown/Cross-compilation.md
@@ -212,6 +212,8 @@
 ```ini
 [host_machine]
 system = 'windows'
+userland = 'windows'
+kernel = 'windows'
 cpu_family = 'x86'
 cpu = 'i686'
 endian = 'little'
@@ -221,9 +223,13 @@
 purposes. The corresponding target definition would look the same but
 have `target_machine` in the header. These values are available in
 your Meson scripts. There are three predefined variables called,
-surprisingly, [[@build_machine]], [[@host_machine]] and [[@target_machine]].
-Determining the operating system of your host machine is simply a
-matter of calling `host_machine.system()`.
+surprisingly, [[@build_machine]], [[@host_machine]] and
+[[@target_machine]]. Determining the operating system of your host
+machine is simply a matter of calling `host_machine.system()`.
+Starting from version 1.2.0 you can get more fine grained information
+using the `.userland()` and `.kernel()` methods. The return values of
+these functions are documented in [the reference table
+page](Reference-tables.md).
 
 There are two different values for the CPU. The first one is
 `cpu_family`. It is a general type of the CPU. This should have a
diff --git a/docs/markdown/Reference-tables.md b/docs/markdown/Reference-tables.md
index fb7deda..dbbb92d 100644
--- a/docs/markdown/Reference-tables.md
+++ b/docs/markdown/Reference-tables.md
@@ -161,6 +161,42 @@
 Any string not listed above is not guaranteed to remain stable in
 future releases.
 
+## Kernel names (since 1.2.0)
+
+Native names as returned by the `.kernel()` method.
+
+| Value               | Comment                         |
+| -----               | -------                         |
+| linux   | |
+| freebsd | |
+| openbsd | |
+| windows | |
+| xnu                 | Kernel of various Apple OSes    |
+
+
+## Userland names (since 1.2.0)
+
+Native names as returned by the `.userland()` method.
+
+| Value               | Comment                          |
+| -----               | -------                          |
+| gnu                 | Linux or Hurd                    |
+| musl                | Linux using Musl libc            |
+| macos               | The OS formerly known as Mac OSX |
+| freebsd | |
+| openbsd | |
+| windows | |
+
+Recommended cross build names for systems that can not run Meson
+natively.
+
+| Value               | Comment                         |
+| -----               | -------                         |
+| ios                 | Apple iOS                       |
+| tvos                | Apple tvOS                      |
+| watchos             | Apple watchOS                   |
+| ipados              | Apple iPadOS                    |
+
 ## Language arguments parameter names
 
 These are the parameter names for passing language specific arguments to your build target.
diff --git a/docs/markdown/snippets/moremachinedata.md b/docs/markdown/snippets/moremachinedata.md
new file mode 100644
index 0000000..27d9a7e
--- /dev/null
+++ b/docs/markdown/snippets/moremachinedata.md
@@ -0,0 +1,12 @@
+## Machine objects get `kernel` and `userland` properties
+
+Meson has traditionally provided a `system` property to detect the
+system being run on. However this is not enough to reliably
+differentiate between e.g. an iOS platform from a watchOS one. Two new
+properties, namely `kernel` and `userland` have been added to make
+this configuration doable.
+
+These new properties are not necessary in cross files for now, but if
+they are not defined and a build file tries to access them, Meson will
+exit with a hard error. It is expected that at some point in the
+future defining the new properties will become mandatory.
diff --git a/mesonbuild/envconfig.py b/mesonbuild/envconfig.py
index 50c974b..964e352 100644
--- a/mesonbuild/envconfig.py
+++ b/mesonbuild/envconfig.py
@@ -260,6 +260,8 @@
     cpu_family: str
     cpu: str
     endian: str
+    kernel: T.Optional[str]
+    userland: T.Optional[str]
 
     def __post_init__(self) -> None:
         self.is_64_bit: bool = self.cpu_family in CPU_FAMILIES_64_BIT
@@ -283,7 +285,11 @@
         if endian not in ('little', 'big'):
             mlog.warning(f'Unknown endian {endian}')
 
-        return cls(literal['system'], cpu_family, literal['cpu'], endian)
+        system = literal['system']
+        kernel = literal.get('kernel', None)
+        userland = literal.get('userland', None)
+
+        return cls(system, cpu_family, literal['cpu'], endian, kernel, userland)
 
     def is_windows(self) -> bool:
         """
diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py
index 36aa94e..9b67137 100644
--- a/mesonbuild/environment.py
+++ b/mesonbuild/environment.py
@@ -395,6 +395,29 @@
     # detect_cpu_family() above.
     return trial
 
+kernel_mappings = {'freebsd': 'freebsd',
+                   'openbsd': 'openbsd',
+                   'windows': 'windows',
+                   'android': 'linux',
+                   'cygwin': 'windows',
+                   'darwin': 'xnu',
+                   }
+
+userland_mappings = {'freebsd': 'freebsd',
+                     'openbsd': 'openbsd',
+                     'windows': 'windows',
+                     'darwin': 'macos',
+                     'gnu': 'gnu',
+                     }
+
+def detect_kernel(system: str) -> T.Optional[str]:
+    return kernel_mappings.get(system, None)
+
+def detect_userland(system: str) -> T.Optional[str]:
+    if system == 'linux':
+        return 'gnu' # Fixme, check whether we are on a glibc system.
+    return userland_mappings.get(system, None)
+
 def detect_system() -> str:
     if sys.platform == 'cygwin':
         return 'cygwin'
@@ -411,11 +434,14 @@
     underlying ''detect_*'' method can be called to explicitly use the
     partial information.
     """
+    system = detect_system()
     return MachineInfo(
-        detect_system(),
+        system,
         detect_cpu_family(compilers) if compilers is not None else None,
         detect_cpu(compilers) if compilers is not None else None,
-        sys.byteorder)
+        sys.byteorder,
+        detect_kernel(system),
+        detect_userland(system))
 
 # TODO make this compare two `MachineInfo`s purely. How important is the
 # `detect_cpu_family({})` distinction? It is the one impediment to that.
diff --git a/mesonbuild/interpreter/interpreterobjects.py b/mesonbuild/interpreter/interpreterobjects.py
index fa91714..6807321 100644
--- a/mesonbuild/interpreter/interpreterobjects.py
+++ b/mesonbuild/interpreter/interpreterobjects.py
@@ -639,6 +639,8 @@
                              'cpu': self.cpu_method,
                              'cpu_family': self.cpu_family_method,
                              'endian': self.endian_method,
+                             'kernel': self.kernel_method,
+                             'userland': self.userland_method,
                              })
 
     @noPosargs
@@ -661,6 +663,21 @@
     def endian_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> str:
         return self.held_object.endian
 
+    @noPosargs
+    @noKwargs
+    def kernel_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> str:
+        if self.held_object.kernel is not None:
+            return self.held_object.kernel
+        raise InterpreterException('Kernel not defined or could not be autodetected.')
+
+    @noPosargs
+    @noKwargs
+    def userland_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> str:
+        if self.held_object.userland is not None:
+            return self.held_object.userland
+        raise InterpreterException('Userland not defined or could not be autodetected.')
+
+
 class IncludeDirsHolder(ObjectHolder[build.IncludeDirs]):
     pass