Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/maint/update_settings_docs.py
4150 views
1
#!/usr/bin/env python3
2
# Copyright 2021 The Emscripten Authors. All rights reserved.
3
# Emscripten is available under two separate licenses, the MIT license and the
4
# University of Illinois/NCSA Open Source License. Both these licenses can be
5
# found in the LICENSE file.
6
7
"""Convert src/settings.js into ReSt docs that get published as
8
part of the emscripten docs.
9
10
This parser for src/settings.js is somewhat fragile, and
11
comments need to be written in a ReSt friendly way. This
12
means, for example, using double backticks for keywords and
13
leaving a blank line before and after list blocks.
14
15
The parser does understand several "tags" which we use
16
settings.js. For example [compile] and [link].
17
"""
18
19
import os
20
import sys
21
import subprocess
22
23
script_dir = os.path.dirname(os.path.abspath(__file__))
24
root_dir = os.path.dirname(os.path.dirname(script_dir))
25
26
sys.path.insert(0, root_dir)
27
28
from tools.utils import path_from_root, read_file, safe_ensure_dirs
29
30
header = '''\
31
.. _settings-reference:
32
33
============================
34
Emscripten Compiler Settings
35
============================
36
37
The following is a complete list of settings that can be passed to emscripten
38
via ``-s`` on the command line. For example ``-sASSERTIONS`` or
39
``-sASSERTIONS=0``. For more details see the :ref:`emcc <emcc-s-option-value>`
40
documentation.
41
42
Unless otherwise noted these settings only apply when linking and have no effect
43
during compilation.
44
45
.. Auto-generated by update_settings_docs.py. **DO NOT EDIT**
46
'''
47
48
all_tags = {
49
'link': '',
50
'compile+link': 'Applicable during both linking and compilation',
51
'compile': 'Only applicable during compilation',
52
'experimental': 'This is an experimental setting',
53
'deprecated': 'This setting is deprecated',
54
}
55
56
output_file = path_from_root('site/source/docs/tools_reference/settings_reference.rst')
57
58
59
def write_setting(f, setting_name, setting_default, comment, tags):
60
# Convert markdown backticks to rst double backticks
61
f.write('\n.. _' + setting_name.lower() + ':\n')
62
f.write('\n' + setting_name + '\n')
63
f.write('=' * len(setting_name) + '\n\n')
64
f.write(comment + '\n')
65
for tag in tags:
66
for t in tag.split():
67
if all_tags[t]:
68
f.write('\n.. note:: ' + all_tags[t] + '\n')
69
# TODO: Properly handle multi-line values, like for INCOMING_MODULE_JS_API,
70
# which is [, newline, and then lines of content. For now print a
71
# placeholder.
72
if setting_default == '[':
73
setting_default = '(multi-line value, see settings.js)'
74
f.write('\nDefault value: ' + setting_default + '\n')
75
76
77
def write_file(f):
78
f.write(header)
79
80
current_comment = []
81
current_tags = []
82
for line in read_file(path_from_root('src/settings.js')).splitlines():
83
if 'LEGACY_SETTINGS' in line:
84
break
85
if not line:
86
current_comment = []
87
current_tags = []
88
if line.startswith('//'):
89
line = line[2:]
90
# Strip at most one leading space
91
if line and line[0] == ' ':
92
line = line[1:]
93
if line.startswith('[') and line.endswith(']'):
94
tag = line.strip('[')
95
tag = tag.rstrip(']')
96
if tag in all_tags:
97
current_tags.append(tag)
98
continue
99
current_comment.append(line)
100
elif line.startswith('var'):
101
# Format:
102
# var NAME = DEFAULT;
103
# Split it and strip the final ';'.
104
_, setting_name, _, setting_default = line.strip(';').split(None, 3)
105
comment = '\n'.join(current_comment).strip()
106
write_setting(f, setting_name, setting_default, comment, current_tags)
107
current_comment = []
108
current_tags = []
109
110
111
def main(args):
112
if '--check' in args:
113
safe_ensure_dirs(path_from_root('out'))
114
tmp_output = path_from_root('out/settings_reference.rst')
115
with open(tmp_output, 'w') as f:
116
write_file(f)
117
if read_file(tmp_output) != read_file(output_file):
118
print(f'{output_file} is out-of-date. Please run tools/maint/update_settings_docs.py')
119
subprocess.call(['diff', '-u', output_file, tmp_output])
120
return 1
121
else:
122
with open(output_file, 'w') as f:
123
write_file(f)
124
125
return 0
126
127
128
if __name__ == '__main__':
129
sys.exit(main(sys.argv[1:]))
130
131