llvm-project
149 строк · 5.2 Кб
1#!/usr/bin/env python3
2# A tool to parse the output of `llvm-bolt --help-hidden` and update the
3# documentation in CommandLineArgumentReference.md automatically.
4# Run from the directory in which this file is located to update the docs.
5
6import subprocess7from textwrap import wrap8
9LINE_LIMIT = 8010
11
12def wrap_text(text, indent, limit=LINE_LIMIT):13wrapped_lines = wrap(text, width=limit - len(indent))14wrapped_text = ("\n" + indent).join(wrapped_lines)15return wrapped_text16
17
18def add_info(sections, section, option, description):19indent = " "20wrapped_description = "\n".join(21[22wrap_text(line, indent) if len(line) > LINE_LIMIT else line23for line in description24]25)26sections[section].append((option, indent + wrapped_description))27
28
29def parse_bolt_options(output):30section_headers = [31"Generic options:",32"Output options:",33"BOLT generic options:",34"BOLT optimization options:",35"BOLT options in relocation mode:",36"BOLT instrumentation options:",37"BOLT printing options:",38]39
40sections = {key: [] for key in section_headers}41current_section, prev_section = None, None42option, description = None, []43
44for line in output.split("\n"):45cleaned_line = line.strip()46
47if cleaned_line.casefold() in map(str.casefold, section_headers):48if prev_section != None: # Save last option from prev section49add_info(sections, current_section, option, description)50option, description = None, []51
52cleaned_line = cleaned_line.split()53# Apply lowercase to all words except the first one54cleaned_line = [cleaned_line[0]] + [55word.lower() for word in cleaned_line[1:]56]57# Join the words back together into a string58cleaned_line = " ".join(cleaned_line)59
60current_section = cleaned_line61prev_section = current_section62continue63
64if cleaned_line.startswith("-"):65if option and description:66# Join description lines, adding an extra newline for67# sub-options that start with '='68add_info(sections, current_section, option, description)69option, description = None, []70
71parts = cleaned_line.split(" ", 1)72if len(parts) > 1:73option = parts[0].strip()74descr = parts[1].strip()75descr = descr[2].upper() + descr[3:]76description = [descr]77if option.startswith("--print") or option.startswith("--time"):78current_section = "BOLT printing options:"79elif prev_section != None:80current_section = prev_section81continue82
83if cleaned_line.startswith("="):84parts = cleaned_line.split(maxsplit=1)85# Split into two parts: sub-option and description86if len(parts) == 2:87# Rejoin with a single space88cleaned_line = parts[0] + " " + parts[1].rstrip()89description.append(cleaned_line)90elif cleaned_line: # Multiline description continuation91description.append(cleaned_line)92
93add_info(sections, current_section, option, description)94return sections95
96
97def generate_markdown(sections):98markdown_lines = [99"# BOLT - a post-link optimizer developed to speed up large applications\n",100"## SYNOPSIS\n",101"`llvm-bolt <executable> [-o outputfile] <executable>.bolt "102"[-data=perf.fdata] [options]`\n",103"## OPTIONS",104]105
106for section, options in sections.items():107markdown_lines.append(f"\n### {section}")108if section == "BOLT instrumentation options:":109markdown_lines.append(110f"\n`llvm-bolt <executable> -instrument"111" [-o outputfile] <instrumented-executable>`"112)113for option, desc in options:114markdown_lines.append(f"\n- `{option}`\n")115# Split description into lines to handle sub-options116desc_lines = desc.split("\n")117for line in desc_lines:118if line.startswith("="):119# Sub-option: correct formatting with bullet120sub_option, sub_desc = line[1:].split(" ", 1)121markdown_lines.append(f" - `{sub_option}`: {sub_desc[4:]}")122else:123# Regular line of description124if line[2:].startswith("<"):125line = line.replace("<", "").replace(">", "")126markdown_lines.append(f"{line}")127
128return "\n".join(markdown_lines)129
130
131def main():132try:133help_output = subprocess.run(134["llvm-bolt", "--help-hidden"], capture_output=True, text=True, check=True135).stdout136except subprocess.CalledProcessError as e:137print("Failed to execute llvm-bolt --help:")138print(e)139return140
141sections = parse_bolt_options(help_output)142markdown = generate_markdown(sections)143
144with open("CommandLineArgumentReference.md", "w") as md_file:145md_file.write(markdown)146
147
148if __name__ == "__main__":149main()150