| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# xdt-gen-visibility -- generates GNU visibility aliases and attributes |
| 4 |
# |
| 5 |
# Copyright (C) 2024 Brian Tarricone <[email protected]> |
| 6 |
# |
| 7 |
# This program is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
from argparse import ArgumentParser |
| 21 |
import sys |
| 22 |
from typing import Any, Dict, List, Optional |
| 23 |
|
| 24 |
|
| 25 |
def read_symbols_file(input_filename: Optional[str]) -> Dict[str, Any]: |
| 26 |
if input_filename is not None: |
| 27 |
inp = open(input_filename, 'r') |
| 28 |
else: |
| 29 |
inp = sys.stdin |
| 30 |
|
| 31 |
files = {} |
| 32 |
cur_symbols = None |
| 33 |
for line in inp: |
| 34 |
line = line.strip() |
| 35 |
|
| 36 |
if line.startswith('# file:'): |
| 37 |
cur_symbols = [] |
| 38 |
files[line[7:]] = cur_symbols |
| 39 |
continue |
| 40 |
elif line == '' or line.startswith('#'): |
| 41 |
continue |
| 42 |
|
| 43 |
symbol = {} |
| 44 |
|
| 45 |
if line.startswith('var:'): |
| 46 |
symbol['type'] = 'var' |
| 47 |
line = line[4:] |
| 48 |
else: |
| 49 |
symbol['type'] = 'func' |
| 50 |
|
| 51 |
parts = line.split() |
| 52 |
symbol['name'] = parts[0] |
| 53 |
|
| 54 |
symbol['attrs'] = [] |
| 55 |
for extra in parts[1:]: |
| 56 |
if extra.startswith('attr:'): |
| 57 |
symbol['attrs'].append(extra[5:]) |
| 58 |
|
| 59 |
cur_symbols.append(symbol) |
| 60 |
|
| 61 |
if input_filename is not None: |
| 62 |
inp.close() |
| 63 |
|
| 64 |
return files |
| 65 |
|
| 66 |
|
| 67 |
def build_attrs_str(attrs: List[str]) -> str: |
| 68 |
attrs_str = ' '.join(symbol['attrs']) |
| 69 |
if attrs_str != '': |
| 70 |
attrs_str = ' ' + attrs_str |
| 71 |
return attrs_str |
| 72 |
|
| 73 |
|
| 74 |
def header_decls_for_symbol(symbol: Dict[str, Any]) -> str: |
| 75 |
name = symbol['name'] |
| 76 |
attrs_str = build_attrs_str(symbol['attrs']) |
| 77 |
|
| 78 |
s = f'extern __typeof({name}) IA__{name} ' \ |
| 79 |
+ f'__attribute__((visibility("hidden"))){attrs_str};\n' |
| 80 |
s += f'#define {name} IA__{name}\n' |
| 81 |
s += '\n' |
| 82 |
|
| 83 |
return s |
| 84 |
|
| 85 |
|
| 86 |
def source_decls_for_symbol(symbol: Dict[str, Any]) -> str: |
| 87 |
name = symbol['name'] |
| 88 |
attrs_str = build_attrs_str(symbol['attrs']) |
| 89 |
|
| 90 |
need_disable_warnings = symbol['type'] == 'var' |
| 91 |
|
| 92 |
s = f'#undef {name}\n' |
| 93 |
if need_disable_warnings: |
| 94 |
s += '#pragma GCC diagnostic push\n' |
| 95 |
s += '#pragma GCC diagnostic ignored "-Wredundant-decls"\n' |
| 96 |
s += f'extern __typeof({name}) {name} ' \ |
| 97 |
+ f'__attribute__((alias("IA__{name}"), ' \ |
| 98 |
+ f'visibility("default"))){attrs_str};\n' |
| 99 |
if need_disable_warnings: |
| 100 |
s += '#pragma GCC diagnostic pop\n' |
| 101 |
s += '\n' |
| 102 |
return s |
| 103 |
|
| 104 |
|
| 105 |
parser = ArgumentParser( |
| 106 |
prog='xdt-gen-visibility', |
| 107 |
description='Generates GNU visibility headers and source' |
| 108 |
) |
| 109 |
|
| 110 |
parser.add_argument( |
| 111 |
'--kind', |
| 112 |
action='store', |
| 113 |
choices=['header', 'source'], |
| 114 |
required=True, |
| 115 |
) |
| 116 |
parser.add_argument( |
| 117 |
'--ifdef-guard-format', |
| 118 |
action='store', |
| 119 |
metavar='FMT', |
| 120 |
default='__{file_stem_upper_snake}_{file_type_upper}__', |
| 121 |
help='String format for #ifndef file guards. Possible replacements are ' |
| 122 |
+ '{file_stem_upper_snake}, ' |
| 123 |
+ '{file_stem_lower_snake}, ' |
| 124 |
+ '{file_type_upper}, ' |
| 125 |
+ '{file_type_lower}' |
| 126 |
) |
| 127 |
parser.add_argument('input', nargs='?', metavar='INPUT') |
| 128 |
parser.add_argument('output', nargs='?', metavar='OUTPUT') |
| 129 |
|
| 130 |
args = parser.parse_args() |
| 131 |
|
| 132 |
if args.kind == 'source': |
| 133 |
deftype = 'c' |
| 134 |
elif args.kind == 'header': |
| 135 |
deftype = 'h' |
| 136 |
else: |
| 137 |
raise f'Invalid --kind "{args.kind}"' |
| 138 |
|
| 139 |
files = read_symbols_file(args.input) |
| 140 |
|
| 141 |
output_s = """/* |
| 142 |
* This file was generated with xdt-gen-visibility. Do not edit it directly. |
| 143 |
*/ |
| 144 |
|
| 145 |
#include <glib.h> |
| 146 |
|
| 147 |
#if defined(ENABLE_SYMBOL_VISIBILITY) \ |
| 148 |
&& defined(__GNUC__) \ |
| 149 |
&& defined(G_HAVE_GNUC_VISIBILITY) |
| 150 |
|
| 151 |
G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 152 |
|
| 153 |
""" |
| 154 |
|
| 155 |
for filename, symbols in files.items(): |
| 156 |
guard_format_params = { |
| 157 |
'file_stem_upper_snake': filename.replace('-', '_').upper(), |
| 158 |
'file_stem_lower_snake': filename.replace('-', '_'), |
| 159 |
'file_type_upper': deftype.upper(), |
| 160 |
'file_type_lower': deftype, |
| 161 |
} |
| 162 |
ifdef_guard = args.ifdef_guard_format.format(**guard_format_params) |
| 163 |
|
| 164 |
output_s += f'#ifdef {ifdef_guard}\n' |
| 165 |
output_s += '\n' |
| 166 |
|
| 167 |
for symbol in symbols: |
| 168 |
if args.kind == 'header': |
| 169 |
output_s += header_decls_for_symbol(symbol) |
| 170 |
elif args.kind == 'source': |
| 171 |
output_s += source_decls_for_symbol(symbol) |
| 172 |
|
| 173 |
output_s += f'#endif /* {ifdef_guard} */\n' |
| 174 |
output_s += '\n' |
| 175 |
output_s += '\n' |
| 176 |
|
| 177 |
output_s += """G_GNUC_END_IGNORE_DEPRECATIONS |
| 178 |
|
| 179 |
#endif /* ENABLE_SYMBOL_VISIBILITY && __GNUC__ && G_HAVE_GNUC_VISIBILITY */ |
| 180 |
""" |
| 181 |
|
| 182 |
if args.output is not None: |
| 183 |
with open(args.output, 'w') as outp: |
| 184 |
outp.write(output_s) |
| 185 |
else: |
| 186 |
print(output_s, end='') |