Path: blob/main/tools/maint/update_settings_docs.py
6165 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 subprocess20import sys2122script_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.settings import (28COMPILE_TIME_SETTINGS,29DEPRECATED_SETTINGS,30EXPERIMENTAL_SETTINGS,31LEGACY_SETTINGS,32)33from tools.utils import exit_with_error, path_from_root, read_file, safe_ensure_dirs3435header = '''\36.. _settings-reference:3738============================39Emscripten Compiler Settings40============================4142The following is a complete list of settings that can be passed to emscripten43via ``-s`` on the command line. For example ``-sASSERTIONS`` or44``-sASSERTIONS=0``. For more details see the :ref:`emcc <emcc-s-option-value>`45documentation.4647Unless otherwise noted these settings only apply when linking and have no effect48during compilation.4950.. Auto-generated by update_settings_docs.py. **DO NOT EDIT**51'''5253all_tags = {54'link': '',55'compile+link': 'Applicable during both linking and compilation',56'compile': 'Only applicable during compilation',57'experimental': 'This is an experimental setting',58'deprecated': 'This setting is deprecated',59}6061deprecated_header = '''62.. _deprecated-settings:6364===================65Deprecated Settings66===================6768The following settings have been proposed for removal from emscripten. These settings69still function but may be removed in a future version. If your project is using one of70these settings please open a bug (or reply to one of the existing bugs).7172'''7374legacy_header = '''75.. _legacy-settings:7677===============78Legacy Settings79===============8081The following settings no longer have any effect but are still accepted by emscripten82for backwards compatibility with older versions:8384'''8586output_file = path_from_root('site/source/docs/tools_reference/settings_reference.rst')878889def check_tags(setting_name, tags):90if setting_name in COMPILE_TIME_SETTINGS and 'compile' not in tags and 'compile+link' not in tags:91print(tags)92exit_with_error(f'setting {setting_name} in COMPILE_TIME_SETTINGS but missing [compile] tag')93if setting_name in DEPRECATED_SETTINGS and 'deprecated' not in tags:94exit_with_error(f'setting {setting_name} in DEPRECATED_SETTINGS but missing [deprecated] tag')95if setting_name in EXPERIMENTAL_SETTINGS and 'experimental' not in tags:96exit_with_error(f'setting {setting_name} in EXPERIMENTAL_SETTINGS but missing [experimental] tag')979899def write_setting(f, setting_name, setting_default, comment, tags):100# Convert markdown backticks to rst double backticks101f.write('\n.. _' + setting_name.lower() + ':\n')102f.write('\n' + setting_name + '\n')103f.write('=' * len(setting_name) + '\n\n')104f.write(comment + '\n')105for tag in tags:106for t in tag.split():107if all_tags[t]:108f.write('\n.. note:: ' + all_tags[t] + '\n')109# TODO: Properly handle multi-line values, like for INCOMING_MODULE_JS_API,110# which is [, newline, and then lines of content. For now print a111# placeholder.112if setting_default == '[':113setting_default = '(multi-line value, see settings.js)'114f.write('\nDefault value: ' + setting_default + '\n')115116117def write_file(f):118f.write(header)119120current_comment = []121current_tags = []122for line in read_file(path_from_root('src/settings.js')).splitlines():123if not line:124current_comment = []125current_tags = []126if line.startswith('//'):127line = line[2:]128# Strip at most one leading space129if line and line[0] == ' ':130line = line[1:]131if line.startswith('[') and line.endswith(']'):132tag = line.strip('[')133tag = tag.rstrip(']')134if tag in all_tags:135current_tags.append(tag)136continue137current_comment.append(line)138elif line.startswith('var'):139# Format:140# var NAME = DEFAULT;141# Split it and strip the final ';'.142_, setting_name, _, setting_default = line.strip(';').split(None, 3)143comment = '\n'.join(current_comment).strip()144check_tags(setting_name, current_tags)145write_setting(f, setting_name, setting_default, comment, current_tags)146current_comment = []147current_tags = []148149f.write(deprecated_header)150151for name, desc in DEPRECATED_SETTINGS.items():152f.write(f' - ``{name}``: {desc}\n')153154f.write(legacy_header)155156for name, values, *extra_fields in LEGACY_SETTINGS:157desc = f'Valid values: {values}'158if extra_fields:159desc = f'{extra_fields[0]} ({desc})'160f.write(f' - ``{name}``: {desc}\n')161162163def main(args):164if '--check' in args:165safe_ensure_dirs(path_from_root('out'))166tmp_output = path_from_root('out/settings_reference.rst')167with open(tmp_output, 'w') as f:168write_file(f)169if read_file(tmp_output) != read_file(output_file):170print(f'{output_file} is out-of-date. Please run tools/maint/update_settings_docs.py')171subprocess.call(['diff', '-u', output_file, tmp_output])172return 1173else:174with open(output_file, 'w') as f:175write_file(f)176177return 0178179180if __name__ == '__main__':181sys.exit(main(sys.argv[1:]))182183184