| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: Apache-2.0 |
| 3 |
# Copyright 2017 Niklas Claesson |
| 4 |
|
| 5 |
"""This is two implementations for how to get module names from the boost |
| 6 |
sources. One relies on json metadata files in the sources, the other relies on |
| 7 |
the folder names. |
| 8 |
|
| 9 |
Run the tool in the boost directory and append the stdout to the misc.py: |
| 10 |
|
| 11 |
boost/$ path/to/meson/tools/boost_names.py >> path/to/meson/dependencies/misc.py |
| 12 |
""" |
| 13 |
|
| 14 |
import sys |
| 15 |
import json |
| 16 |
import re |
| 17 |
import textwrap |
| 18 |
import functools |
| 19 |
import typing as T |
| 20 |
from pathlib import Path |
| 21 |
|
| 22 |
lib_dir = Path('libs') |
| 23 |
jamroot = Path('Jamroot') |
| 24 |
|
| 25 |
not_modules = ['config', 'disjoint_sets', 'headers'] |
| 26 |
|
| 27 |
export_modules = False |
| 28 |
|
| 29 |
|
| 30 |
@functools.total_ordering |
| 31 |
class BoostLibrary(): |
| 32 |
def __init__(self, name: str, shared: T.List[str], static: T.List[str], single: T.List[str], multi: T.List[str]): |
| 33 |
self.name = name |
| 34 |
self.shared = sorted(set(shared)) |
| 35 |
self.static = sorted(set(static)) |
| 36 |
self.single = sorted(set(single)) |
| 37 |
self.multi = sorted(set(multi)) |
| 38 |
|
| 39 |
def __lt__(self, other: object) -> bool: |
| 40 |
if isinstance(other, BoostLibrary): |
| 41 |
return self.name < other.name |
| 42 |
return NotImplemented |
| 43 |
|
| 44 |
def __eq__(self, other: object) -> bool: |
| 45 |
if isinstance(other, BoostLibrary): |
| 46 |
return self.name == other.name |
| 47 |
elif isinstance(other, str): |
| 48 |
return self.name == other |
| 49 |
return NotImplemented |
| 50 |
|
| 51 |
def __hash__(self) -> int: |
| 52 |
return hash(self.name) |
| 53 |
|
| 54 |
@functools.total_ordering |
| 55 |
class BoostModule(): |
| 56 |
def __init__(self, name: str, key: str, desc: str, libs: T.List[BoostLibrary]): |
| 57 |
self.name = name |
| 58 |
self.key = key |
| 59 |
self.desc = desc |
| 60 |
self.libs = libs |
| 61 |
|
| 62 |
def __lt__(self, other: object) -> bool: |
| 63 |
if isinstance(other, BoostModule): |
| 64 |
return self.key < other.key |
| 65 |
return NotImplemented |
| 66 |
|
| 67 |
|
| 68 |
def get_boost_version() -> T.Optional[str]: |
| 69 |
raw = jamroot.read_text(encoding='utf-8') |
| 70 |
m = re.search(r'BOOST_VERSION\s*:\s*([0-9\.]+)\s*;', raw) |
| 71 |
if m: |
| 72 |
return m.group(1) |
| 73 |
return None |
| 74 |
|
| 75 |
|
| 76 |
def get_libraries(jamfile: Path) -> T.List[BoostLibrary]: |
| 77 |
# Extract libraries from the boost Jamfiles. This includes: |
| 78 |
# - library name |
| 79 |
# - compiler flags |
| 80 |
|
| 81 |
libs: T.List[BoostLibrary] = [] |
| 82 |
raw = jamfile.read_text(encoding='utf-8') |
| 83 |
raw = re.sub(r'#.*\n', '\n', raw) # Remove comments |
| 84 |
raw = re.sub(r'\s+', ' ', raw) # Force single space |
| 85 |
raw = re.sub(r'}', ';', raw) # Cheat code blocks by converting } to ; |
| 86 |
|
| 87 |
cmds = raw.split(';') # Commands always terminate with a ; (I hope) |
| 88 |
cmds = [x.strip() for x in cmds] # Some cleanup |
| 89 |
|
| 90 |
project_usage_requirements: T.List[str] = [] |
| 91 |
|
| 92 |
# "Parse" the relevant sections |
| 93 |
for i in cmds: |
| 94 |
parts = i.split(' ') |
| 95 |
parts = [x for x in parts if x not in ['']] |
| 96 |
if not parts: |
| 97 |
continue |
| 98 |
|
| 99 |
# Parse project |
| 100 |
if parts[0] in ['project']: |
| 101 |
attributes: T.Dict[str, T.List[str]] = {} |
| 102 |
curr: T.Optional[str] = None |
| 103 |
|
| 104 |
for j in parts: |
| 105 |
if j == ':': |
| 106 |
curr = None |
| 107 |
elif curr is None: |
| 108 |
curr = j |
| 109 |
else: |
| 110 |
if curr not in attributes: |
| 111 |
attributes[curr] = [] |
| 112 |
attributes[curr] += [j] |
| 113 |
|
| 114 |
if 'usage-requirements' in attributes: |
| 115 |
project_usage_requirements = attributes['usage-requirements'] |
| 116 |
|
| 117 |
# Parse libraries |
| 118 |
elif parts[0] in ['lib', 'boost-lib']: |
| 119 |
assert len(parts) >= 2 |
| 120 |
|
| 121 |
# Get and check the library name |
| 122 |
lname = parts[1] |
| 123 |
if parts[0] == 'boost-lib': |
| 124 |
lname = f'boost_{lname}' |
| 125 |
if not lname.startswith('boost_'): |
| 126 |
continue |
| 127 |
|
| 128 |
# Count `:` to only select the 'usage-requirements' |
| 129 |
# See https://boostorg.github.io/build/manual/master/index.html#bbv2.main-target-rule-syntax |
| 130 |
colon_counter = 0 |
| 131 |
usage_requirements: T.List[str] = [] |
| 132 |
for j in parts: |
| 133 |
if j == ':': |
| 134 |
colon_counter += 1 |
| 135 |
elif colon_counter >= 4: |
| 136 |
usage_requirements += [j] |
| 137 |
|
| 138 |
# Get shared / static defines |
| 139 |
shared: T.List[str] = [] |
| 140 |
static: T.List[str] = [] |
| 141 |
single: T.List[str] = [] |
| 142 |
multi: T.List[str] = [] |
| 143 |
for j in usage_requirements + project_usage_requirements: |
| 144 |
m1 = re.match(r'<link>shared:<define>(.*)', j) |
| 145 |
m2 = re.match(r'<link>static:<define>(.*)', j) |
| 146 |
m3 = re.match(r'<threading>single:<define>(.*)', j) |
| 147 |
m4 = re.match(r'<threading>multi:<define>(.*)', j) |
| 148 |
|
| 149 |
if m1: |
| 150 |
shared += [f'-D{m1.group(1)}'] |
| 151 |
if m2: |
| 152 |
static += [f'-D{m2.group(1)}'] |
| 153 |
if m3: |
| 154 |
single +=[f'-D{m3.group(1)}'] |
| 155 |
if m4: |
| 156 |
multi += [f'-D{m4.group(1)}'] |
| 157 |
|
| 158 |
libs += [BoostLibrary(lname, shared, static, single, multi)] |
| 159 |
|
| 160 |
return libs |
| 161 |
|
| 162 |
|
| 163 |
def process_lib_dir(ldir: Path) -> T.List[BoostModule]: |
| 164 |
meta_file = ldir / 'meta' / 'libraries.json' |
| 165 |
bjam_file = ldir / 'build' / 'Jamfile.v2' |
| 166 |
if not meta_file.exists(): |
| 167 |
print(f'WARNING: Meta file {meta_file} does not exist') |
| 168 |
return [] |
| 169 |
|
| 170 |
# Extract libs |
| 171 |
libs: T.List[BoostLibrary] = [] |
| 172 |
if bjam_file.exists(): |
| 173 |
libs = get_libraries(bjam_file) |
| 174 |
|
| 175 |
# Extract metadata |
| 176 |
data = json.loads(meta_file.read_text(encoding='utf-8')) |
| 177 |
if not isinstance(data, list): |
| 178 |
data = [data] |
| 179 |
|
| 180 |
modules: T.List[BoostModule] = [] |
| 181 |
for i in data: |
| 182 |
modules += [BoostModule(i['name'], i['key'], i['description'], libs)] |
| 183 |
|
| 184 |
return modules |
| 185 |
|
| 186 |
|
| 187 |
def get_modules() -> T.List[BoostModule]: |
| 188 |
modules: T.List[BoostModule] = [] |
| 189 |
for i in lib_dir.iterdir(): |
| 190 |
if not i.is_dir() or i.name in not_modules: |
| 191 |
continue |
| 192 |
|
| 193 |
# numeric has sub libs |
| 194 |
subdirs = i / 'sublibs' |
| 195 |
metadir = i / 'meta' |
| 196 |
if subdirs.exists() and not metadir.exists(): |
| 197 |
for j in i.iterdir(): |
| 198 |
if not j.is_dir(): |
| 199 |
continue |
| 200 |
modules += process_lib_dir(j) |
| 201 |
else: |
| 202 |
modules += process_lib_dir(i) |
| 203 |
|
| 204 |
return modules |
| 205 |
|
| 206 |
|
| 207 |
def main() -> int: |
| 208 |
if not lib_dir.is_dir() or not jamroot.exists(): |
| 209 |
print("ERROR: script must be run in boost source directory") |
| 210 |
return 1 |
| 211 |
|
| 212 |
vers = get_boost_version() |
| 213 |
modules = get_modules() |
| 214 |
modules = sorted(modules) |
| 215 |
libraries = [x for y in modules for x in y.libs] |
| 216 |
libraries = sorted(set(libraries)) |
| 217 |
|
| 218 |
print(textwrap.dedent(f'''\ |
| 219 |
#### ---- BEGIN GENERATED ---- #### |
| 220 |
# # |
| 221 |
# Generated with tools/boost_names.py: |
| 222 |
# - boost version: {vers} |
| 223 |
# - modules found: {len(modules)} |
| 224 |
# - libraries found: {len(libraries)} |
| 225 |
# |
| 226 |
|
| 227 |
class BoostLibrary(): |
| 228 |
def __init__(self, name: str, shared: T.List[str], static: T.List[str], single: T.List[str], multi: T.List[str]): |
| 229 |
self.name = name |
| 230 |
self.shared = shared |
| 231 |
self.static = static |
| 232 |
self.single = single |
| 233 |
self.multi = multi |
| 234 |
|
| 235 |
class BoostModule(): |
| 236 |
def __init__(self, name: str, key: str, desc: str, libs: T.List[str]): |
| 237 |
self.name = name |
| 238 |
self.key = key |
| 239 |
self.desc = desc |
| 240 |
self.libs = libs |
| 241 |
|
| 242 |
|
| 243 |
# dict of all know libraries with additional compile options |
| 244 |
boost_libraries = {{\ |
| 245 |
''')) |
| 246 |
|
| 247 |
for i in libraries: |
| 248 |
print(textwrap.indent(textwrap.dedent(f"""\ |
| 249 |
'{i.name}': BoostLibrary( |
| 250 |
name='{i.name}', |
| 251 |
shared={i.shared}, |
| 252 |
static={i.static}, |
| 253 |
single={i.single}, |
| 254 |
multi={i.multi}, |
| 255 |
),\ |
| 256 |
"""), ' ')) |
| 257 |
|
| 258 |
if export_modules: |
| 259 |
print(textwrap.dedent(f'''\ |
| 260 |
}} |
| 261 |
|
| 262 |
|
| 263 |
# dict of all modules with metadata |
| 264 |
boost_modules = {{\ |
| 265 |
''')) |
| 266 |
|
| 267 |
for mod in modules: |
| 268 |
desc_escaped = re.sub(r"'", "\\'", mod.desc) |
| 269 |
print(textwrap.indent(textwrap.dedent(f"""\ |
| 270 |
'{mod.key}': BoostModule( |
| 271 |
name='{mod.name}', |
| 272 |
key='{mod.key}', |
| 273 |
desc='{desc_escaped}', |
| 274 |
libs={[x.name for x in mod.libs]}, |
| 275 |
),\ |
| 276 |
"""), ' ')) |
| 277 |
|
| 278 |
print(textwrap.dedent(f'''\ |
| 279 |
}} |
| 280 |
|
| 281 |
# # |
| 282 |
#### ---- END GENERATED ---- ####\ |
| 283 |
''')) |
| 284 |
|
| 285 |
return 0 |
| 286 |
|
| 287 |
if __name__ == '__main__': |
| 288 |
sys.exit(main()) |