gen-kconfig-doc.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # gen-kconfig-doc.py — generate Sphinx .rst file from Kconfig files
  5. #
  6. # This script iterates over Kconfig and Kconfig.projbuild files in
  7. # ESP-IDF component directories, and outputs documentation for these options
  8. # as ReST markup.
  9. # For each option in Kconfig file (e.g. 'FOO'), CONFIG_FOO link target is
  10. # generated, allowing options to be referenced in other documents
  11. # (using :envvar:`CONFIG_FOO`)
  12. #
  13. # This script uses kconfiglib library to do all the work of parsing Kconfig
  14. # files: https://github.com/ulfalizer/Kconfiglib
  15. #
  16. # Copyright 2017 Espressif Systems (Shanghai) PTE LTD
  17. #
  18. # Licensed under the Apache License, Version 2.0 (the "License");
  19. # you may not use this file except in compliance with the License.
  20. # You may obtain a copy of the License at
  21. #
  22. # http:#www.apache.org/licenses/LICENSE-2.0
  23. #
  24. # Unless required by applicable law or agreed to in writing, software
  25. # distributed under the License is distributed on an "AS IS" BASIS,
  26. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. # See the License for the specific language governing permissions and
  28. # limitations under the License.
  29. import os
  30. import kconfiglib
  31. # Indentation to be used in the generated file
  32. INDENT = ' '
  33. # Characters used when underlining section heading
  34. HEADING_SYMBOLS = '#*=-^"+'
  35. # Keep the heading level in sync with api-reference/kconfig.rst
  36. INITIAL_HEADING_LEVEL = 2
  37. MAX_HEADING_LEVEL = 5
  38. OPTION_HEADING_LEVEL = 6
  39. def print_menu_contents(title, items, heading_level, breadcrumbs, genindex):
  40. if not genindex:
  41. if title:
  42. print_section_heading(title, heading_level)
  43. if heading_level == INITIAL_HEADING_LEVEL+1:
  44. print_menu_contents(title, items, heading_level, breadcrumbs, True)
  45. for entry in items:
  46. if entry.is_menu():
  47. if len(breadcrumbs) > 0:
  48. new_breadcrumbs = breadcrumbs + ' > ' + entry.get_title()
  49. else:
  50. new_breadcrumbs = entry.get_title()
  51. print_menu_contents(entry.get_title(), entry.get_items(),
  52. min(heading_level + 1, MAX_HEADING_LEVEL),
  53. new_breadcrumbs, genindex)
  54. elif genindex:
  55. print_item(entry, breadcrumbs)
  56. elif entry.is_choice():
  57. print_choice(entry, breadcrumbs)
  58. else:
  59. if len(entry.get_prompts()) == 0:
  60. # Skip entries which can never be visible
  61. continue
  62. # Currently this does not handle 'menuconfig' entires in any special way,
  63. # as Kconfglib offers no way of recognizing them automatically.
  64. print_option(entry, breadcrumbs)
  65. # Trailing newline after every option
  66. print
  67. def print_choice(choice, breadcrumbs):
  68. print_option(choice, breadcrumbs)
  69. print
  70. print '%sAvailable options' % INDENT
  71. for opt in choice.get_symbols():
  72. # Format available options as a list
  73. print '%s- %s' % (INDENT * 2, opt.name)
  74. def print_section_heading(title, heading_level):
  75. print title
  76. print HEADING_SYMBOLS[heading_level] * len(title)
  77. print
  78. def print_option(opt, breadcrumbs):
  79. # add link target so we can use :envvar:`CONFIG_FOO`
  80. print '.. envvar:: CONFIG_%s' % opt.name
  81. print
  82. if len(opt.prompts) > 0:
  83. print '%s%s' % (INDENT, opt.prompts[0][0])
  84. print
  85. if opt.get_help() is not None:
  86. # Help text normally contains newlines, but spaces at the beginning of
  87. # each line are stripped by kconfiglib. We need to re-indent the text
  88. # to produce valid ReST.
  89. print '%s%s' % (INDENT, opt.get_help().replace('\n', '\n%s' % INDENT))
  90. print '%sFound in\n%s' % (INDENT, INDENT * 2 + breadcrumbs)
  91. print
  92. def print_item(opt, breadcrumbs):
  93. print '- :envvar:`CONFIG_%s`' % opt.name
  94. print
  95. def process_kconfig_file(kconfig_file, heading_level, breadcrumbs):
  96. if os.path.exists(kconfig_file):
  97. cfg = kconfiglib.Config(kconfig_file, print_warnings=True)
  98. print_menu_contents(None, cfg.get_top_level_items(), heading_level, breadcrumbs, False)
  99. def print_all_components():
  100. heading_level = INITIAL_HEADING_LEVEL
  101. # Currently this works only for IDF components.
  102. # TODO: figure out if this can be re-used for documenting applications?
  103. components_path = os.path.join(os.path.curdir, '../..', 'components')
  104. for component_name in os.listdir(components_path):
  105. if component_name.startswith('.'):
  106. continue # skip system thumbnail folders
  107. kconfig_file_path = os.path.join(components_path, component_name, 'Kconfig')
  108. process_kconfig_file(kconfig_file_path, heading_level, 'Component config')
  109. process_kconfig_file(kconfig_file_path + '.projbuild', heading_level, '')
  110. kconfig_file_path = os.path.join(os.path.curdir, '../..', 'Kconfig.compiler')
  111. process_kconfig_file(kconfig_file_path, heading_level, '')
  112. if __name__ == '__main__':
  113. print_all_components()