Skip to content

Commit ca2a4e7

Browse files
authored
[libc] Generate configure.rst from the JSON config information. (#65791)
1 parent 37b5388 commit ca2a4e7

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

libc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ foreach(opt IN LISTS global_config)
143143
message(STATUS "${opt_name}: ${opt_value}")
144144
set(${opt_name} ${opt_value})
145145
endforeach()
146+
generate_config_doc(${main_config_file} ${LIBC_SOURCE_DIR}/docs/configure.rst)
146147
load_libc_config(${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/config.json ${cmd_line_conf})
147148
load_libc_config(${LIBC_SOURCE_DIR}/config/${LIBC_TARGET_OS}/${LIBC_TARGET_ARCHITECTURE}/config.json ${cmd_line_conf})
148149

libc/cmake/modules/LibcConfig.cmake

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ function(read_libc_config config_file opt_list)
4747
# to load. If there are no config options, it is better to remove that
4848
# config.json file instead of including an empty file.
4949
message(FATAL_ERROR "${config_file}: Does not contain any config option groups")
50-
return()
5150
endif()
5251
math(EXPR group_count_1 "${group_count} - 1")
5352

@@ -135,3 +134,84 @@ function(load_libc_config config_file)
135134
set(${opt_name} ${opt_value} PARENT_SCOPE)
136135
endforeach()
137136
endfunction()
137+
138+
function(generate_config_doc config_file doc_file)
139+
if(NOT EXISTS ${config_file})
140+
message(FATAL_ERROR "${config_file} does not exist")
141+
endif()
142+
file(READ ${config_file} json_config)
143+
string(JSON group_count ERROR_VARIABLE json_error LENGTH ${json_config})
144+
if(json_error)
145+
message(FATAL_ERROR "${config_file}: ${json_error}")
146+
endif()
147+
if(${group_count} EQUAL 0)
148+
message(FATAL_ERROR "${config_file}: Does not contain any config option groups")
149+
endif()
150+
math(EXPR group_count_1 "${group_count} - 1")
151+
152+
set(doc_string ".. _configure:\n"
153+
"..\n"
154+
" Do not edit this file directly. CMake will auto generate it.\n"
155+
" If the changes are intended, add this file to your commit.\n"
156+
"\n"
157+
"==========================\n"
158+
"Configure Options\n"
159+
"==========================\n"
160+
"\n"
161+
"Below is the full set of options one can use to configure the libc build.\n"
162+
"An option can be given an explicit value on the CMake command line using\n"
163+
"the following syntax:\n"
164+
"\n"
165+
".. code-block:: sh\n"
166+
"\n"
167+
" $> cmake <other build options> -D<libc config option name>=<option value> <more options>\n"
168+
"\n"
169+
"For example:\n"
170+
"\n"
171+
".. code-block:: sh\n"
172+
"\n"
173+
" $> cmake <other build options> -DLIBC_CONF_PRINTF_DISABLE_FLOAT=ON <more options>\n"
174+
"\n"
175+
"See the main ``config/config.json``, and the platform and architecture specific\n"
176+
"overrides in ``config/<platform>/config.json`` and ``config/<platform>/<arch>/config.json,``\n"
177+
"to learn about the defaults for your platform and target.\n"
178+
"\n")
179+
180+
foreach(group_num RANGE ${group_count_1})
181+
string(JSON group_name ERROR_VARIABLE json_error MEMBER ${json_config} ${group_num})
182+
if(json_error)
183+
message(FATAL_ERROR "${config_file}: ${json_error}")
184+
endif()
185+
string(APPEND doc_string "* **\"${group_name}\" options**\n")
186+
string(JSON option_map ERROR_VARIABLE json_error GET ${json_config} ${group_name})
187+
if(json_error)
188+
message(FATAL_ERROR ${json_error})
189+
endif()
190+
string(JSON option_count ERROR_VARIABLE jsor_error LENGTH ${option_map})
191+
if(json_error)
192+
message(FATAL_ERROR ${json_error})
193+
endif()
194+
if(${option_count} EQUAL 0)
195+
message(FATAL_ERROR "${config_file}: No options listed against the config option group '${group_name}'")
196+
endif()
197+
198+
math(EXPR option_count_1 "${option_count} - 1")
199+
foreach(opt_num RANGE ${option_count_1})
200+
string(JSON option_name ERROR_VARIABLE json_error MEMBER ${option_map} ${opt_num})
201+
if(json_error)
202+
message(FATAL_ERROR ${json_error})
203+
endif()
204+
string(JSON opt_object ERROR_VARIABLE json_error GET ${option_map} ${option_name})
205+
if(json_error)
206+
message(FATAL_ERROR "Error generating ${doc_file}: ${json_error}\n${opt_object}")
207+
endif()
208+
string(JSON opt_doc ERROR_VARIABLE json_error GET ${opt_object} "doc")
209+
if(json_error)
210+
message(FATAL_ERROR "Error generating ${doc_file}: ${json_error}")
211+
endif()
212+
string(APPEND doc_string " - ``${option_name}``: ${opt_doc}\n")
213+
endforeach()
214+
endforeach()
215+
message(STATUS "Writing config doc to ${doc_file}")
216+
file(WRITE ${doc_file} ${doc_string})
217+
endfunction()

libc/docs/configure.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.. _configure:
2+
..
3+
Do not edit this file directly. CMake will auto generate it.
4+
If the changes are intended, add this file to your commit.
5+
6+
==========================
7+
Configure Options
8+
==========================
9+
10+
Below is the full set of options one can use to configure the libc build.
11+
An option can be given an explicit value on the CMake command line using
12+
the following syntax:
13+
14+
.. code-block:: sh
15+
16+
$> cmake <other build options> -D<libc config option name>=<option value> <more options>
17+
18+
For example:
19+
20+
.. code-block:: sh
21+
22+
$> cmake <other build options> -DLIBC_CONF_PRINTF_DISABLE_FLOAT=ON <more options>
23+
24+
See the main ``config/config.json``, and the platform and architecture specific
25+
overrides in ``config/<platform>/config.json`` and ``config/<platform>/<arch>/config.json,``
26+
to learn about the defaults for your platform and target.
27+
28+
* **"printf" options**
29+
- ``LIBC_CONF_PRINTF_DISABLE_FLOAT``: Disable printing floating point values in printf and friends.
30+
- ``LIBC_CONF_PRINTF_DISABLE_INDEX_MODE``: Disable index mode in the printf format string.
31+
- ``LIBC_CONF_PRINTF_DISABLE_WRITE_INT``: Disable handling of %n in printf format string.

libc/docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ stages there is no ABI stability in any form.
5252
usage_modes
5353
overlay_mode
5454
fullbuild_mode
55+
configure
5556
gpu/index.rst
5657

5758
.. toctree::

0 commit comments

Comments
 (0)