diff --git a/cross/iphone.txt b/cross/iphone.txt index 943b4c8aa..b7149c164 100644 --- a/cross/iphone.txt +++ b/cross/iphone.txt @@ -25,6 +25,8 @@ has_function_hfkerhisadf = false [host_machine] system = 'darwin' +subsystem = 'ios' +kernel = 'xnu' cpu_family = 'aarch64' cpu = 'aarch64' endian = 'little' diff --git a/cross/tvos.txt b/cross/tvos.txt index 016a58986..6f6bfb5d1 100644 --- a/cross/tvos.txt +++ b/cross/tvos.txt @@ -22,6 +22,8 @@ has_function_hfkerhisadf = false [host_machine] system = 'darwin' +subsystem = 'tvos' +kernel = 'xnu' cpu_family = 'arm' cpu = 'arm64' endian = 'little' diff --git a/docs/markdown/Cross-compilation.md b/docs/markdown/Cross-compilation.md index fb222220e..1d0e46332 100644 --- a/docs/markdown/Cross-compilation.md +++ b/docs/markdown/Cross-compilation.md @@ -212,6 +212,8 @@ target machines look the same. Here is a sample for host machine. ```ini [host_machine] system = 'windows' +subsystem = 'windows' +kernel = 'nt' cpu_family = 'x86' cpu = 'i686' endian = 'little' @@ -221,9 +223,13 @@ These values define the machines sufficiently for cross compilation 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 `.subsystem()` 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 7354cd4b5..b553832eb 100644 --- a/docs/markdown/Reference-tables.md +++ b/docs/markdown/Reference-tables.md @@ -163,9 +163,44 @@ These are provided by the `.system()` method call. 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 | | +| netbsd | | +| nt | | +| xnu | Kernel of various Apple OSes | +| sunos | | +| dragonfly | | +| haiku| | +| none | For e.g. bare metal embedded | + + +## Subsystem names (since 1.2.0) + +A more specific description of the system in question. Most values are +meant to be used in cross files only, as those platforms can not run +Meson natively. + +| Value | Comment | +| ----- | ------- | +| macos | Apple macOS (formerly OSX) | +| ios | Apple iOS | +| ios-simulator | | +| tvos | Apple tvOS | +| tvos-simulator | | +| watchos | Apple watchOS | +| watchos-simulator | | + ## Language arguments parameter names -These are the parameter names for passing language specific arguments to your build target. +These are the parameter names for passing language specific arguments +to your build target. | Language | compiler name | linker name | | ------------- | ------------- | ----------------- | diff --git a/docs/markdown/snippets/moremachinedata.md b/docs/markdown/snippets/moremachinedata.md new file mode 100644 index 000000000..978cb9c01 --- /dev/null +++ b/docs/markdown/snippets/moremachinedata.md @@ -0,0 +1,12 @@ +## Machine objects get `kernel` and `subsystem` 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 `subsystem` have been added so these +setups can be reliably detected. + +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 50c974bd1..1bf6ab9f7 100644 --- a/mesonbuild/envconfig.py +++ b/mesonbuild/envconfig.py @@ -260,6 +260,8 @@ class MachineInfo(HoldableObject): cpu_family: str cpu: str endian: str + kernel: T.Optional[str] + subsystem: 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 @@ class MachineInfo(HoldableObject): 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) + subsystem = literal.get('subsystem', None) + + return cls(system, cpu_family, literal['cpu'], endian, kernel, subsystem) def is_windows(self) -> bool: """ diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py index 74bae3209..688070f4a 100644 --- a/mesonbuild/environment.py +++ b/mesonbuild/environment.py @@ -395,6 +395,27 @@ def detect_cpu(compilers: CompilersDict) -> str: # detect_cpu_family() above. return trial +KERNEL_MAPPINGS: T.Mapping[str, str] = {'freebsd': 'freebsd', + 'openbsd': 'openbsd', + 'netbsd': 'netbsd', + 'windows': 'nt', + 'android': 'linux', + 'linux': 'linux', + 'cygwin': 'nt', + 'darwin': 'xnu', + 'sunos': 'sunos', + 'dragonfly': 'dragonfly', + 'haiku': 'haiku', + } + +def detect_kernel(system: str) -> T.Optional[str]: + return KERNEL_MAPPINGS.get(system, None) + +def detect_subsystem(system: str) -> T.Optional[str]: + if system == 'darwin': + return 'macos' + return system + def detect_system() -> str: if sys.platform == 'cygwin': return 'cygwin' @@ -411,11 +432,14 @@ def detect_machine_info(compilers: T.Optional[CompilersDict] = None) -> MachineI 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_subsystem(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 4ca7f5298..fac3b0e55 100644 --- a/mesonbuild/interpreter/interpreterobjects.py +++ b/mesonbuild/interpreter/interpreterobjects.py @@ -639,6 +639,8 @@ class MachineHolder(ObjectHolder['MachineInfo']): 'cpu': self.cpu_method, 'cpu_family': self.cpu_family_method, 'endian': self.endian_method, + 'kernel': self.kernel_method, + 'subsystem': self.subsystem_method, }) @noPosargs @@ -661,6 +663,21 @@ class MachineHolder(ObjectHolder['MachineInfo']): 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 subsystem_method(self, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> str: + if self.held_object.subsystem is not None: + return self.held_object.subsystem + raise InterpreterException('Subsystem not defined or could not be autodetected.') + + class IncludeDirsHolder(ObjectHolder[build.IncludeDirs]): pass diff --git a/mesonbuild/scripts/env2mfile.py b/mesonbuild/scripts/env2mfile.py index 6eda79ad5..27a7dd969 100755 --- a/mesonbuild/scripts/env2mfile.py +++ b/mesonbuild/scripts/env2mfile.py @@ -44,6 +44,10 @@ def add_arguments(parser: 'argparse.ArgumentParser') -> None: help='Generate a native compilation file.') parser.add_argument('--system', default=None, help='Define system for cross compilation.') + parser.add_argument('--subsystem', default=None, + help='Define subsystem for cross compilation.') + parser.add_argument('--kernel', default=None, + help='Define kernel for cross compilation.') parser.add_argument('--cpu', default=None, help='Define cpu for cross compilation.') parser.add_argument('--cpu-family', default=None, @@ -61,6 +65,8 @@ class MachineInfo: self.cmake: T.Dict[str, T.Union[str, T.List[str]]] = {} self.system: T.Optional[str] = None + self.subsystem: T.Optional[str] = None + self.kernel: T.Optional[str] = None self.cpu: T.Optional[str] = None self.cpu_family: T.Optional[str] = None self.endian: T.Optional[str] = None @@ -181,6 +187,8 @@ def detect_cross_debianlike(options: T.Any) -> MachineInfo: data[k] = v host_arch = data['DEB_HOST_GNU_TYPE'] host_os = data['DEB_HOST_ARCH_OS'] + host_subsystem = host_os + host_kernel = 'linux' host_cpu_family = deb_cpu_family_map.get(data['DEB_HOST_GNU_CPU'], data['DEB_HOST_GNU_CPU']) host_cpu = deb_cpu_map.get(data['DEB_HOST_ARCH'], @@ -213,6 +221,8 @@ def detect_cross_debianlike(options: T.Any) -> MachineInfo: except ValueError: pass infos.system = host_os + infos.subsystem = host_subsystem + infos.kernel = host_kernel infos.cpu_family = host_cpu_family infos.cpu = host_cpu infos.endian = host_endian @@ -260,6 +270,11 @@ def write_machine_file(infos: MachineInfo, ofilename: str, write_system_info: bo ofile.write(f"cpu_family = '{infos.cpu_family}'\n") ofile.write(f"endian = '{infos.endian}'\n") ofile.write(f"system = '{infos.system}'\n") + if infos.subsystem: + ofile.write(f"subsystem = '{infos.subsystem}'\n") + if infos.kernel: + ofile.write(f"kernel = '{infos.kernel}'\n") + os.replace(tmpfilename, ofilename) def detect_language_args_from_envvars(langname: str, envvar_suffix: str = '') -> T.Tuple[T.List[str], T.List[str]]: @@ -306,7 +321,7 @@ def detect_properties_from_envvars(infos: MachineInfo, envvar_suffix: str = '') infos.properties['sys_root'] = var def detect_cross_system(infos: MachineInfo, options: T.Any) -> None: - for optname in ('system', 'cpu', 'cpu_family', 'endian'): + for optname in ('system', 'subsystem', 'kernel', 'cpu', 'cpu_family', 'endian'): v = getattr(options, optname) if not v: mlog.error(f'Cross property "{optname}" missing, set it with --{optname.replace("_", "-")}.')