| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2022 Mark Bolhuis <[email protected]> |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
import os |
| 6 |
import typing as T |
| 7 |
|
| 8 |
from . import ExtensionModule, ModuleReturnValue, ModuleInfo |
| 9 |
from ..build import CustomTarget |
| 10 |
from ..interpreter.type_checking import NoneType, in_set_validator |
| 11 |
from ..interpreterbase import typed_pos_args, typed_kwargs, KwargInfo, FeatureNew |
| 12 |
from ..mesonlib import File, MesonException |
| 13 |
|
| 14 |
if T.TYPE_CHECKING: |
| 15 |
from typing_extensions import Literal, TypedDict |
| 16 |
|
| 17 |
from . import ModuleState |
| 18 |
from ..dependencies import Dependency |
| 19 |
from ..interpreter import Interpreter |
| 20 |
from ..programs import CommandList, Program |
| 21 |
from ..mesonlib import FileOrString |
| 22 |
|
| 23 |
class ScanXML(TypedDict): |
| 24 |
|
| 25 |
public: bool |
| 26 |
client: bool |
| 27 |
server: bool |
| 28 |
include_core_only: bool |
| 29 |
|
| 30 |
class FindProtocol(TypedDict): |
| 31 |
|
| 32 |
state: Literal['stable', 'staging', 'unstable'] |
| 33 |
version: T.Optional[int] |
| 34 |
|
| 35 |
class WaylandModule(ExtensionModule): |
| 36 |
|
| 37 |
INFO = ModuleInfo('wayland', '0.62.0', stabilized='1.8.0') |
| 38 |
|
| 39 |
def __init__(self, interpreter: Interpreter) -> None: |
| 40 |
super().__init__(interpreter) |
| 41 |
|
| 42 |
self.protocols_dep: T.Optional[Dependency] = None |
| 43 |
self.pkgdatadir: T.Optional[str] = None |
| 44 |
self.scanner_bin: T.Optional[Program] = None |
| 45 |
|
| 46 |
self.methods.update({ |
| 47 |
'scan_xml': self.scan_xml, |
| 48 |
'find_protocol': self.find_protocol, |
| 49 |
}) |
| 50 |
|
| 51 |
@typed_pos_args('wayland.scan_xml', varargs=(str, File), min_varargs=1) |
| 52 |
@typed_kwargs( |
| 53 |
'wayland.scan_xml', |
| 54 |
KwargInfo('public', bool, default=False), |
| 55 |
KwargInfo('client', bool, default=True), |
| 56 |
KwargInfo('server', bool, default=False), |
| 57 |
KwargInfo('include_core_only', bool, default=True, since='0.64.0'), |
| 58 |
) |
| 59 |
def scan_xml(self, state: ModuleState, args: T.Tuple[T.List[FileOrString]], kwargs: ScanXML) -> ModuleReturnValue: |
| 60 |
if self.scanner_bin is None: |
| 61 |
# wayland-scanner from BUILD machine must have same version as wayland |
| 62 |
# libraries from HOST machine. |
| 63 |
dep = state.dependency('wayland-client') |
| 64 |
self.scanner_bin = state.find_tool('wayland-scanner', 'wayland-scanner', 'wayland_scanner', |
| 65 |
wanted=dep.version) |
| 66 |
|
| 67 |
scope = 'public' if kwargs['public'] else 'private' |
| 68 |
# We have to cast because mypy can't deduce these are literals |
| 69 |
sides = [i for i in T.cast("T.List[Literal['client', 'server']]", ['client', 'server']) if kwargs[i]] |
| 70 |
if not sides: |
| 71 |
raise MesonException('At least one of client or server keyword argument must be set to true.') |
| 72 |
|
| 73 |
xml_files = self.interpreter.source_strings_to_files(args[0]) |
| 74 |
targets: T.List[CustomTarget] = [] |
| 75 |
for xml_file in xml_files: |
| 76 |
name = os.path.splitext(os.path.basename(xml_file.fname))[0] |
| 77 |
|
| 78 |
code = CustomTarget( |
| 79 |
f'{name}-protocol', |
| 80 |
state.subdir, |
| 81 |
state.environment, |
| 82 |
[self.scanner_bin, f'{scope}-code', '@INPUT@', '@OUTPUT@'], |
| 83 |
[xml_file], |
| 84 |
[f'{name}-protocol.c'], |
| 85 |
state.current_build_project, |
| 86 |
backend=state.backend, |
| 87 |
) |
| 88 |
targets.append(code) |
| 89 |
|
| 90 |
for side in sides: |
| 91 |
command: CommandList = [self.scanner_bin, f'{side}-header', '@INPUT@', '@OUTPUT@'] |
| 92 |
if kwargs['include_core_only']: |
| 93 |
command.append('--include-core-only') |
| 94 |
|
| 95 |
header = CustomTarget( |
| 96 |
f'{name}-{side}-protocol', |
| 97 |
state.subdir, |
| 98 |
state.environment, |
| 99 |
command, |
| 100 |
[xml_file], |
| 101 |
[f'{name}-{side}-protocol.h'], |
| 102 |
state.current_build_project, |
| 103 |
backend=state.backend, |
| 104 |
) |
| 105 |
targets.append(header) |
| 106 |
|
| 107 |
return ModuleReturnValue(targets, targets) |
| 108 |
|
| 109 |
@typed_pos_args('wayland.find_protocol', str) |
| 110 |
@typed_kwargs( |
| 111 |
'wayland.find_protocol', |
| 112 |
KwargInfo('state', str, default='stable', validator=in_set_validator({'stable', 'staging', 'unstable'})), |
| 113 |
KwargInfo('version', (int, NoneType)), |
| 114 |
) |
| 115 |
def find_protocol(self, state: ModuleState, args: T.Tuple[str], kwargs: FindProtocol) -> File: |
| 116 |
base_name = args[0] |
| 117 |
xml_state = kwargs['state'] |
| 118 |
version = kwargs['version'] |
| 119 |
|
| 120 |
if xml_state != 'stable' and version is None: |
| 121 |
raise MesonException(f'{xml_state} protocols require a version number.') |
| 122 |
|
| 123 |
if xml_state == 'stable' and version is not None: |
| 124 |
FeatureNew.single_use('Version number in stable wayland protocol', '1.5.0', state.subproject, location=state.current_node) |
| 125 |
|
| 126 |
if self.protocols_dep is None: |
| 127 |
self.protocols_dep = state.dependency('wayland-protocols') |
| 128 |
|
| 129 |
if self.pkgdatadir is None: |
| 130 |
self.pkgdatadir = self.protocols_dep.get_variable(pkgconfig='pkgdatadir', internal='pkgdatadir') |
| 131 |
|
| 132 |
xml_name = base_name |
| 133 |
if xml_state == 'unstable': |
| 134 |
xml_name += '-unstable' |
| 135 |
if version is not None: |
| 136 |
xml_name += f'-v{version}' |
| 137 |
xml_name += '.xml' |
| 138 |
|
| 139 |
path = os.path.join(self.pkgdatadir, xml_state, base_name, xml_name) |
| 140 |
|
| 141 |
if not os.path.exists(path): |
| 142 |
raise MesonException(f'The file {path} does not exist.') |
| 143 |
|
| 144 |
return File.from_absolute_file(path) |
| 145 |
|
| 146 |
|
| 147 |
def initialize(interpreter: Interpreter) -> WaylandModule: |
| 148 |
return WaylandModule(interpreter) |