summaryrefslogtreecommitdiffstats
path: root/build/plugins/macros_with_error.py
blob: 53365f9b51ccd24e148a0a32603627951882482a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import sys

import _common

import ymake


def onmacros_with_error(unit, *args):
    sys.stderr.write('This macros will fail\n')
    raise Exception('Expected fail in MACROS_WITH_ERROR')


def onrestrict_path(unit, *args):
    if args:
        if 'MSG' in args:
            pos = args.index('MSG')
            paths, msg = args[:pos], args[pos + 1 :]
            msg = ' '.join(msg)
        else:
            paths, msg = args, 'forbidden'
        if not _common.strip_roots(unit.path()).startswith(paths):
            error_msg = "Path '[[imp]]{}[[rst]]' is restricted - [[bad]]{}[[rst]]. Valid path prefixes are: [[unimp]]{}[[rst]]".format(
                unit.path(), msg, ', '.join(paths)
            )
            ymake.report_configure_error(error_msg)


def onassert(unit, *args):
    val = unit.get(args[0])
    if val and val.lower() == "no":
        msg = ' '.join(args[1:])
        ymake.report_configure_error(msg)


def onvalidate_in_dirs(unit, *args):
    files_var = args[0]
    pattern = args[1]
    no_srcdir = args[2] == '--'
    srcdir = '' if no_srcdir else args[2]
    dirs = args[3:] if no_srcdir else args[4:]
    pfx = '[[imp]]DECLARE_IN_DIRS[[rst]]: '

    if '**' in pattern:
        ymake.report_configure_error(f"{pfx}'**' in files mask is prohibited. Seen [[unimp]]{pattern}[[rst]]")
        unit.set([files_var, ""])

    for pat in ('*', '?'):
        if pat in srcdir:
            ymake.report_configure_error(
                f"{pfx}'{pat}' in [[imp]]SRCDIR[[rst]] argument is prohibited. Seen [[unimp]]{srcdir}[[rst]]"
            )
            unit.set([files_var, ""])

    for dir in dirs:
        for pat in ('*', '?', '$', '..'):
            if pat in dir:
                ymake.report_configure_error(
                    f"{pfx}'{pat}' in [[imp]]DIRS[[rst]] argument is prohibited. Seen [[unimp]]{dir}[[rst]]"
                )
                unit.set([files_var, ""])


def on_assert_no_yamake(unit, *args):
    """
    @usage: _ASSERT_NO_YAMAKE(<files> [MSG <message>])

    Check that no ya.make files are found in <files> list.
    Macro is primarily targeted to recursive globs validation and so made internal as `_GLOB` macro is.

    - Optional MSG allows overriding the reason
    - Macro is incompatible with `_LATE_GLOB`

    @example:
    ```
    macro NO_MODULES_INSIDE() {
        _GLOB(YAMAKES **/ya.make EXCLUDE ya.make)
        _ASSERT_NO_YAMAKE($YAMAKES MSG no modules expected inside the current one)
    }
    ```
    """

    if args:
        if 'MSG' in args:
            pos = args.index('MSG')
            files, msg = args[:pos], args[pos + 1 :]
            msg = ' '.join(msg)
        else:
            files, msg = args, 'prohibited from recursive glob match'

        found = next((f for f in files if f.endswith('ya.make')), None)
        if found:
            error_msg = "unexpected ya.make: '[[imp]]{}[[rst]]' - {}".format(found, msg)
            ymake.report_configure_error(error_msg)