aboutsummaryrefslogtreecommitdiff
blob: 9b66801d630a62fea944d57b7c17ce690f4ac2ca (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import sys


OS = sys.platform


def _as_tuple(items):
    if isinstance(items, str):
        return tuple(items.strip().replace(',', ' ').split())
    elif items:
        return tuple(items)
    else:
        return ()


class PreprocessorError(Exception):
    """Something preprocessor-related went wrong."""

    @classmethod
    def _msg(cls, filename, reason, **ignored):
        msg = 'failure while preprocessing'
        if reason:
            msg = f'{msg} ({reason})'
        return msg

    def __init__(self, filename, preprocessor=None, reason=None):
        if isinstance(reason, str):
            reason = reason.strip()

        self.filename = filename
        self.preprocessor = preprocessor or None
        self.reason = str(reason) if reason else None

        msg = self._msg(**vars(self))
        msg = f'({filename}) {msg}'
        if preprocessor:
            msg = f'[{preprocessor}] {msg}'
        super().__init__(msg)


class PreprocessorFailure(PreprocessorError):
    """The preprocessor command failed."""

    @classmethod
    def _msg(cls, error, **ignored):
        msg = 'preprocessor command failed'
        if error:
            msg = f'{msg} {error}'
        return msg

    def __init__(self, filename, argv, error=None, preprocessor=None):
        exitcode = -1
        if isinstance(error, tuple):
            if len(error) == 2:
                error, exitcode = error
            else:
                error = str(error)
        if isinstance(error, str):
            error = error.strip()

        self.argv = _as_tuple(argv) or None
        self.error = error if error else None
        self.exitcode = exitcode

        reason = str(self.error)
        super().__init__(filename, preprocessor, reason)


class ErrorDirectiveError(PreprocessorFailure):
    """The file hit a #error directive."""

    @classmethod
    def _msg(cls, error, **ignored):
        return f'#error directive hit ({error})'

    def __init__(self, filename, argv, error, *args, **kwargs):
        super().__init__(filename, argv, error, *args, **kwargs)


class MissingDependenciesError(PreprocessorFailure):
    """The preprocessor did not have access to all the target's dependencies."""

    @classmethod
    def _msg(cls, missing, **ignored):
        msg = 'preprocessing failed due to missing dependencies'
        if missing:
            msg = f'{msg} ({", ".join(missing)})'
        return msg

    def __init__(self, filename, missing=None, *args, **kwargs):
        self.missing = _as_tuple(missing) or None

        super().__init__(filename, *args, **kwargs)


class OSMismatchError(MissingDependenciesError):
    """The target is not compatible with the host OS."""

    @classmethod
    def _msg(cls, expected, **ignored):
        return f'OS is {OS} but expected {expected or "???"}'

    def __init__(self, filename, expected=None, *args, **kwargs):
        if isinstance(expected, str):
            expected = expected.strip()

        self.actual = OS
        self.expected = expected if expected else None

        super().__init__(filename, None, *args, **kwargs)