Path: blob/main/tools/maint/update_settings_docs.py
4150 views
#!/usr/bin/env python31# Copyright 2021 The Emscripten Authors. All rights reserved.2# Emscripten is available under two separate licenses, the MIT license and the3# University of Illinois/NCSA Open Source License. Both these licenses can be4# found in the LICENSE file.56"""Convert src/settings.js into ReSt docs that get published as7part of the emscripten docs.89This parser for src/settings.js is somewhat fragile, and10comments need to be written in a ReSt friendly way. This11means, for example, using double backticks for keywords and12leaving a blank line before and after list blocks.1314The parser does understand several "tags" which we use15settings.js. For example [compile] and [link].16"""1718import os19import sys20import subprocess2122script_dir = os.path.dirname(os.path.abspath(__file__))23root_dir = os.path.dirname(os.path.dirname(script_dir))2425sys.path.insert(0, root_dir)2627from tools.utils import path_from_root, read_file, safe_ensure_dirs2829header = '''\30.. _settings-reference:3132============================33Emscripten Compiler Settings34============================3536The following is a complete list of settings that can be passed to emscripten37via ``-s`` on the command line. For example ``-sASSERTIONS`` or38``-sASSERTIONS=0``. For more details see the :ref:`emcc <emcc-s-option-value>`39documentation.4041Unless otherwise noted these settings only apply when linking and have no effect42during compilation.4344.. Auto-generated by update_settings_docs.py. **DO NOT EDIT**45'''4647all_tags = {48'link': '',49'compile+link': 'Applicable during both linking and compilation',50'compile': 'Only applicable during compilation',51'experimental': 'This is an experimental setting',52'deprecated': 'This setting is deprecated',53}5455output_file = path_from_root('site/source/docs/tools_reference/settings_reference.rst')565758def write_setting(f, setting_name, setting_default, comment, tags):59# Convert markdown backticks to rst double backticks60f.write('\n.. _' + setting_name.lower() + ':\n')61f.write('\n' + setting_name + '\n')62f.write('=' * len(setting_name) + '\n\n')63f.write(comment + '\n')64for tag in tags:65for t in tag.split():66if all_tags[t]:67f.write('\n.. note:: ' + all_tags[t] + '\n')68# TODO: Properly handle multi-line values, like for INCOMING_MODULE_JS_API,69# which is [, newline, and then lines of content. For now print a70# placeholder.71if setting_default == '[':72setting_default = '(multi-line value, see settings.js)'73f.write('\nDefault value: ' + setting_default + '\n')747576def write_file(f):77f.write(header)7879current_comment = []80current_tags = []81for line in read_file(path_from_root('src/settings.js')).splitlines():82if 'LEGACY_SETTINGS' in line:83break84if not line:85current_comment = []86current_tags = []87if line.startswith('//'):88line = line[2:]89# Strip at most one leading space90if line and line[0] == ' ':91line = line[1:]92if line.startswith('[') and line.endswith(']'):93tag = line.strip('[')94tag = tag.rstrip(']')95if tag in all_tags:96current_tags.append(tag)97continue98current_comment.append(line)99elif line.startswith('var'):100# Format:101# var NAME = DEFAULT;102# Split it and strip the final ';'.103_, setting_name, _, setting_default = line.strip(';').split(None, 3)104comment = '\n'.join(current_comment).strip()105write_setting(f, setting_name, setting_default, comment, current_tags)106current_comment = []107current_tags = []108109110def main(args):111if '--check' in args:112safe_ensure_dirs(path_from_root('out'))113tmp_output = path_from_root('out/settings_reference.rst')114with open(tmp_output, 'w') as f:115write_file(f)116if read_file(tmp_output) != read_file(output_file):117print(f'{output_file} is out-of-date. Please run tools/maint/update_settings_docs.py')118subprocess.call(['diff', '-u', output_file, tmp_output])119return 1120else:121with open(output_file, 'w') as f:122write_file(f)123124return 0125126127if __name__ == '__main__':128sys.exit(main(sys.argv[1:]))129130131