File: generate-version-header.py

package info (click to toggle)
apache-arrow 23.0.1-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 76,220 kB
  • sloc: cpp: 654,608; python: 70,522; ruby: 45,964; ansic: 18,742; sh: 7,365; makefile: 669; javascript: 125; xml: 41
file content (178 lines) | stat: -rwxr-xr-x 5,266 bytes parent folder | download | duplicates (3)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.


import argparse
from io import TextIOBase
from pathlib import Path
import re


def main():
    parser = argparse.ArgumentParser(
        description="Generate C header with version macros")
    parser.add_argument(
        "--library",
        required=True,
        help="The library name to use in macro prefixes")
    parser.add_argument(
        "--version",
        required=True,
        help="The library version number")
    parser.add_argument(
        "--input",
        type=Path,
        required=True,
        help="Path to the input template file")
    parser.add_argument(
        "--output",
        type=Path,
        required=True,
        help="Path to the output file to generate")

    args = parser.parse_args()

    with open(args.input, "r", encoding="utf-8") as input_file, \
            open(args.output, "w", encoding="utf-8") as output_file:
        write_header(
            input_file, output_file, args.library, args.version)


def write_header(
        input_file: TextIOBase,
        output_file: TextIOBase,
        library_name: str,
        version: str):
    if "-" in version:
        version, version_tag = version.split("-")
    else:
        version_tag = ""
    version_major, version_minor, version_micro = [int(v) for v in version.split(".")]

    encoded_versions = generate_encoded_versions(library_name)
    visibility_macros = generate_visibility_macros(library_name)
    availability_macros = generate_availability_macros(library_name)

    replacements = {
        "VERSION_MAJOR": str(version_major),
        "VERSION_MINOR": str(version_minor),
        "VERSION_MICRO": str(version_micro),
        "VERSION_TAG": version_tag,
        "ENCODED_VERSIONS": encoded_versions,
        "VISIBILITY_MACROS": visibility_macros,
        "AVAILABILITY_MACROS": availability_macros,
    }

    output_file.write(re.sub(
        r"@([A-Z_]+)@", lambda match: replacements[match[1]], input_file.read()))


def generate_visibility_macros(library: str) -> str:
    return f"""#if (defined(_WIN32) || defined(__CYGWIN__)) && defined(_MSC_VER) && \
  !defined({library}_STATIC_COMPILATION)
#  define {library}_EXPORT __declspec(dllexport)
#  define {library}_IMPORT __declspec(dllimport)
#else
#  define {library}_EXPORT
#  define {library}_IMPORT
#endif

#ifdef {library}_COMPILATION
#  define {library}_API {library}_EXPORT
#else
#  define {library}_API {library}_IMPORT
#endif

#define {library}_EXTERN {library}_API extern"""


def generate_encoded_versions(library: str) -> str:
    macros = []

    for major_version, minor_version in ALL_VERSIONS:
        macros.append(f"""/**
 * {library}_VERSION_{major_version}_{minor_version}:
 *
 * You can use this macro value for compile time API version check.
 *
 * Since: {major_version}.{minor_version}.0
 */
#define {library}_VERSION_{major_version}_{minor_version} G_ENCODE_VERSION({major_version}, {minor_version})""")  # noqa: E501

    return "\n\n".join(macros)


def generate_availability_macros(library: str) -> str:
    macros = [f"""#define {library}_AVAILABLE_IN_ALL {library}_EXTERN"""]

    for major_version, minor_version in ALL_VERSIONS:
        macros.append(f"""#if {library}_VERSION_MIN_REQUIRED >= {library}_VERSION_{major_version}_{minor_version}
#  define {library}_DEPRECATED_IN_{major_version}_{minor_version}               {library}_DEPRECATED
#  define {library}_DEPRECATED_IN_{major_version}_{minor_version}_FOR(function) {library}_DEPRECATED_FOR(function)
#else
#  define {library}_DEPRECATED_IN_{major_version}_{minor_version}
#  define {library}_DEPRECATED_IN_{major_version}_{minor_version}_FOR(function)
#endif

#if {library}_VERSION_MAX_ALLOWED < {library}_VERSION_{major_version}_{minor_version}
#  define {library}_AVAILABLE_IN_{major_version}_{minor_version} {library}_EXTERN {library}_UNAVAILABLE({major_version}, {minor_version})
#else
#  define {library}_AVAILABLE_IN_{major_version}_{minor_version} {library}_EXTERN
#endif""")  # noqa: E501

    return "\n\n".join(macros)


ALL_VERSIONS = [
    (23, 0),
    (22, 0),
    (21, 0),
    (20, 0),
    (19, 0),
    (18, 0),
    (17, 0),
    (16, 0),
    (15, 0),
    (14, 0),
    (13, 0),
    (12, 0),
    (11, 0),
    (10, 0),
    (9, 0),
    (8, 0),
    (7, 0),
    (6, 0),
    (5, 0),
    (4, 0),
    (3, 0),
    (2, 0),
    (1, 0),
    (0, 17),
    (0, 16),
    (0, 15),
    (0, 14),
    (0, 13),
    (0, 12),
    (0, 11),
    (0, 10),
]


if __name__ == '__main__':
    main()