File: generate-api-header.py

package info (click to toggle)
webkit2gtk 2.42.2-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 362,452 kB
  • sloc: cpp: 2,881,971; javascript: 282,447; ansic: 134,088; python: 43,789; ruby: 18,308; perl: 15,872; asm: 14,389; xml: 4,395; yacc: 2,350; sh: 2,074; java: 1,734; lex: 1,323; makefile: 288; pascal: 60
file content (127 lines) | stat: -rw-r--r-- 5,510 bytes parent folder | download | duplicates (10)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python3
#
# Copyright (C) 2022 Igalia S.L.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1.  Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
# 2.  Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import re
import subprocess
import sys

API_SINGLE_HEADER_CHECK = {
    "gtk4": '''#if !defined(__WEBKIT_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <webkit/webkit.h> can be included directly.\"
#endif''',
    "gtk": '''#if !defined(__WEBKIT_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <webkit2/webkit2.h> can be included directly.\"
#endif''',
    "wpe2": '''#if !defined(__WEBKIT_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <wpe/webkit.h> can be included directly.\"
#endif''',
    "wpe": '''#if !defined(__WEBKIT_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <wpe/webkit.h> can be included directly.\"
#endif'''
}

INJECTED_BUNDLE_API_SINGLE_HEADER_CHECK = {
    "gtk4": '''#if !defined(__WEBKIT_WEB_PROCESS_EXTENSION_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <webkit/webkit-web-process-extension.h> can be included directly.\"
#endif''',
    "gtk": '''#if !defined(__WEBKIT_WEB_PROCESS_EXTENSION_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <webkit2/webkit-web-extension.h> can be included directly.\"
#endif''',
    "wpe2": '''#if !defined(__WEBKIT_WEB_PROCESS_EXTENSION_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <wpe/webkit-web-process-extension.h> can be included directly.\"
#endif''',
    "wpe": '''#if !defined(__WEBKIT_WEB_PROCESS_EXTENSION_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <wpe/webkit-web-extension.h> can be included directly.\"
#endif'''
}

SHARED_API_SINGLE_HEADER_CHECK = {
    "gtk4": '''#if !defined(__WEBKIT_H_INSIDE__) && !defined(__WEBKIT_WEB_PROCESS_EXTENSION_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <webkit/webkit.h> can be included directly.\"
#endif''',
    "gtk": '''#if !defined(__WEBKIT_H_INSIDE__) && !defined(__WEBKIT_WEB_PROCESS_EXTENSION_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <webkit2/webkit2.h> can be included directly.\"
#endif''',
    "wpe2": '''#if !defined(__WEBKIT_H_INSIDE__) && !defined(__WEBKIT_WEB_PROCESS_EXTENSION_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <wpe/webkit.h> can be included directly.\"
#endif''',
    "wpe": '''#if !defined(__WEBKIT_H_INSIDE__) && !defined(__WEBKIT_WEB_PROCESS_EXTENSION_H_INSIDE__) && !defined(BUILDING_WEBKIT)
#error \"Only <wpe/webkit.h> can be included directly.\"
#endif'''
}

API_INCLUDE_PREFIX = {
    "gtk4": "webkit",
    "gtk": "webkit",
    "wpe2": "wpe",
    "wpe": "wpe"
}


def expand_ifdefs(line):
    s = re.sub(r"(ENABLE|USE)\(([A-Z0-9_]+)\)", r"(defined(\1_\2) && \1_\2)", line)
    return re.sub(r"(PLATFORM)\(([A-Z0-9_]+)\)", r"(defined(WTF_\1_\2) && WTF_\1_\2)", s)


def main(args):
    port = args[0].lower()
    input = args[1]
    output = args[2]
    unifdef = args[3]
    unifdef_args = args[4:]

    if port == "gtk" and "-DUSE_GTK4=1" in unifdef_args:
        port = "gtk4"
    if port == "wpe" and "-DENABLE_2022_GLIB_API=1" in unifdef_args:
        port = "wpe2"

    input_data = ""
    with open(input, "r", encoding="utf-8") as fd:
        for line in fd.readlines():
            if line[0] == "@":
                if line == "@API_SINGLE_HEADER_CHECK@\n":
                    input_data += API_SINGLE_HEADER_CHECK[port] + "\n"
                elif line == "@INJECTED_BUNDLE_API_SINGLE_HEADER_CHECK@\n":
                    input_data += INJECTED_BUNDLE_API_SINGLE_HEADER_CHECK[port] + "\n"
                elif line == "@SHARED_API_SINGLE_HEADER_CHECK@\n":
                    input_data += SHARED_API_SINGLE_HEADER_CHECK[port] + "\n"
            elif line.startswith("#include <@"):
                input_data += line.replace("@API_INCLUDE_PREFIX@", API_INCLUDE_PREFIX[port])
            else:
                input_data += expand_ifdefs(line)

    command = [unifdef, "-x1", "-o%s" % output] + unifdef_args + ["-"]
    p = subprocess.Popen(command, stdin=subprocess.PIPE, universal_newlines=True, encoding="utf-8")
    p.communicate(input_data)

    # unifdef returns 1 when no changes were made and output file is unmodified.
    if p.returncode != 0:
        with open(output, "w", encoding="utf-8") as fd:
            fd.write(input_data)

    return 0


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))