| 1 |
#!/bin/sh |
| 2 |
# shellcheck disable=SC2154 |
| 3 |
|
| 4 |
PROGNAME="${0##*/}" |
| 5 |
|
| 6 |
tmp_output_file=/tmp/xmldoc.$$.xml |
| 7 |
|
| 8 |
cleanup() { |
| 9 |
[ -f "$tmp_output_file" ] && rm "$tmp_output_file" |
| 10 |
} |
| 11 |
|
| 12 |
trap cleanup EXIT |
| 13 |
|
| 14 |
# Fail on errors |
| 15 |
set -e |
| 16 |
|
| 17 |
usage() { |
| 18 |
cat <<-EOF |
| 19 |
Usage: ${PROGNAME} --command=(create_xml | print_dependencies) |
| 20 |
--source-tree=<asterisk_source_tree> [ --mod-subdirs=<subdir_search_list> ] |
| 21 |
[ --with-moduleinfo ] [--for-wiki ] [ --validate ] |
| 22 |
[ --output-file=<output_xml_file> ] |
| 23 |
[ --core-output-file=<core_output_xml_file> ] |
| 24 |
|
| 25 |
command: |
| 26 |
print_dependencies: Print the source files that have documentation |
| 27 |
for use by "make" as dependencies. |
| 28 |
create_xml: Create the actual XML output file. |
| 29 |
|
| 30 |
source-tree: The path to the Asterisk source tree. |
| 31 |
|
| 32 |
mod-subdirs: A quoted, space-separated list of module sub-directories |
| 33 |
to search for documentation. Defaults to |
| 34 |
"channels pbx apps codecs formats cdr cel bridges funcs tests main res addons" |
| 35 |
|
| 36 |
with-moduleinfo: Include the "MODULEINFO" block from source files. |
| 37 |
Default is to not include MODULEINFO |
| 38 |
|
| 39 |
for-wiki: Perform special post processing for wiki documentation. |
| 40 |
This creates two output files and therefore needs both |
| 41 |
<output-file> and <core-output-file>. |
| 42 |
Default is to not perform wiki post-processing. |
| 43 |
|
| 44 |
validate: Run xmllint or xmlstarlet to validate output-file. |
| 45 |
|
| 46 |
output-file: The XML file to write to if the command was |
| 47 |
"create_xml". |
| 48 |
|
| 49 |
core-output-file: The additional XML file to write to if the command was |
| 50 |
"create_xml" with "for-wiki". |
| 51 |
|
| 52 |
EOF |
| 53 |
} |
| 54 |
|
| 55 |
with_moduleinfo=0 |
| 56 |
for_wiki=0 |
| 57 |
validate=0 |
| 58 |
command="" |
| 59 |
mod_subdirs="channels pbx apps codecs formats cdr cel bridges funcs tests main res addons" |
| 60 |
source_tree="" |
| 61 |
output_file="" |
| 62 |
core_output_file="" |
| 63 |
|
| 64 |
for arg in "$@" ; do |
| 65 |
case ${arg} in |
| 66 |
--for-wiki) |
| 67 |
for_wiki=1 |
| 68 |
;; |
| 69 |
--with-moduleinfo) |
| 70 |
with_moduleinfo=1 |
| 71 |
;; |
| 72 |
--validate) |
| 73 |
validate=1 |
| 74 |
;; |
| 75 |
--command=*) |
| 76 |
command=${arg#*=} |
| 77 |
;; |
| 78 |
--source-tree=*) |
| 79 |
source_tree=${arg#*=} |
| 80 |
;; |
| 81 |
--mod-subdirs=*) |
| 82 |
mod_subdirs="${arg#*=}" |
| 83 |
;; |
| 84 |
--output-file=*) |
| 85 |
output_file=${arg#*=} |
| 86 |
;; |
| 87 |
--core-output-file=*) |
| 88 |
core_output_file=${arg#*=} |
| 89 |
;; |
| 90 |
-h|--help) |
| 91 |
usage |
| 92 |
exit 0 |
| 93 |
;; |
| 94 |
*) |
| 95 |
echo "unknown option '${arg}'." |
| 96 |
usage |
| 97 |
exit 1 |
| 98 |
;; |
| 99 |
esac |
| 100 |
done |
| 101 |
|
| 102 |
if [ "${command}" = "" ] ; then |
| 103 |
echo "No command specified" |
| 104 |
usage |
| 105 |
exit 1 |
| 106 |
fi |
| 107 |
|
| 108 |
if [ "${source_tree}" = "" ] ; then |
| 109 |
echo "No source-tree specified" |
| 110 |
usage |
| 111 |
exit 1; |
| 112 |
fi |
| 113 |
|
| 114 |
if [ ! -d "${source_tree}" ] ; then |
| 115 |
echo "Asterisk source tree '${source_tree}' doesn't exist." |
| 116 |
exit 1 |
| 117 |
fi |
| 118 |
|
| 119 |
if [ ! -f "${source_tree}/makeopts" ] ; then |
| 120 |
echo "There's no 'makeopts' in '${source_tree}'. Maybe you need to run ./configure?" |
| 121 |
exit 1 |
| 122 |
fi |
| 123 |
|
| 124 |
# This script is normally run from the top-level Makefile which |
| 125 |
# will set the tools variables to actual paths, or ':' if |
| 126 |
# the tool isn't found. If this script is run from the |
| 127 |
# command line for testing purposes however, we'll need to |
| 128 |
# set some sane defaults. |
| 129 |
if [ "${GREP}" = "" ] ; then GREP="grep" ; fi |
| 130 |
if [ "${FIND}" = "" ] ; then FIND="find" ; fi |
| 131 |
if [ "${AWK}" = "" ] ; then AWK="awk" ; fi |
| 132 |
if [ "${DIRNAME}" = "" ] ; then DIRNAME="dirname" ; fi |
| 133 |
if [ "${BASENAME}" = "" ] ; then BASENAME="basename" ; fi |
| 134 |
if [ "${SED}" = "" ] ; then SED="sed" ; fi |
| 135 |
if [ "${CAT}" = "" ] ; then CAT="cat" ; fi |
| 136 |
if [ "${XMLLINT}" = "" ] ; then XMLLINT="xmllint" ; fi |
| 137 |
if [ "${XMLSTARLET}" = "" ] ; then XMLSTARLET="xmlstarlet" ; fi |
| 138 |
|
| 139 |
if [ "${for_wiki}" -eq "1" ] || [ "${validate}" -eq "1" ]; then |
| 140 |
if [ "${XMLLINT}${XMLSTARLET}" = "::" ] ; then |
| 141 |
echo "Either xmllint or xmlstarlet is required for wiki post-processing or validation." |
| 142 |
exit 1 |
| 143 |
fi |
| 144 |
fi |
| 145 |
|
| 146 |
make_absolute() { |
| 147 |
case "$1" in |
| 148 |
/*) echo "$1" ;; |
| 149 |
*) echo "$source_tree/$1" ;; |
| 150 |
esac |
| 151 |
} |
| 152 |
|
| 153 |
if [ "${command}" = "print_dependencies" ] ; then |
| 154 |
for subdir in ${mod_subdirs} ; do |
| 155 |
subpath=$(make_absolute "$subdir") |
| 156 |
${FIND} "${subpath}" \( -name '*.c' -o -name '*.cc' -o -name '*.xml' \) \ |
| 157 |
-exec ${GREP} -l -E '(language="en_US"|appdocsxml.dtd)' '{}' \; |
| 158 |
done |
| 159 |
exit |
| 160 |
fi |
| 161 |
|
| 162 |
if [ "${command}" != "create_xml" ] ; then |
| 163 |
echo "Command '${command}' is invalid." |
| 164 |
usage |
| 165 |
exit 1 |
| 166 |
fi |
| 167 |
|
| 168 |
if [ "${output_file}" = "" ] ; then |
| 169 |
echo "output-file is required for command '${command}'." |
| 170 |
usage |
| 171 |
exit 1; |
| 172 |
fi |
| 173 |
|
| 174 |
output_dir=$(${DIRNAME} "${output_file}") |
| 175 |
if [ ! -d "${output_dir}" ] ; then |
| 176 |
echo "output destination directory '${output_dir}' doesn't exist." |
| 177 |
exit 1 |
| 178 |
fi |
| 179 |
|
| 180 |
if [ "${for_wiki}" -eq "1" ] && [ "${core_output_file}" = "" ] ; then |
| 181 |
echo "core-output-file is required for command '${command}' and 'for-wiki'." |
| 182 |
usage |
| 183 |
exit 1; |
| 184 |
fi |
| 185 |
|
| 186 |
core_output_dir=$(${DIRNAME} "${core_output_file}") |
| 187 |
if [ ! -d "${core_output_dir}" ] ; then |
| 188 |
echo "core destination directory '${core_output_dir}' doesn't exist." |
| 189 |
exit 1 |
| 190 |
fi |
| 191 |
|
| 192 |
${CAT} > "${output_file}" <<-EOF |
| 193 |
<?xml version="1.0" encoding="UTF-8"?> |
| 194 |
<!DOCTYPE docs SYSTEM "appdocsxml.dtd"> |
| 195 |
<?xml-stylesheet type="text/xsl" href="appdocsxml.xslt"?> |
| 196 |
<docs xmlns:xi="http://www.w3.org/2001/XInclude"> |
| 197 |
EOF |
| 198 |
|
| 199 |
printf "Building Documentation For: " |
| 200 |
|
| 201 |
for subdir in ${mod_subdirs} ; do |
| 202 |
printf "%s " "${subdir}" |
| 203 |
subdir_path=$(make_absolute "$subdir") |
| 204 |
for i in $(${FIND} "${subdir_path}" -name '*.c' -or -name '*.cc'); do |
| 205 |
if [ "${with_moduleinfo}" -eq "1" ] ; then |
| 206 |
MODULEINFO=$(${AWK} -f "${source_tree}/build_tools/get_moduleinfo" "${i}") |
| 207 |
if [ "${MODULEINFO}" != "" ] ; then |
| 208 |
${CAT} >> "${output_file}" <<-EOF |
| 209 |
<module language="en_US" name="$(${BASENAME} "${i}" .c)"> |
| 210 |
${MODULEINFO} |
| 211 |
</module> |
| 212 |
EOF |
| 213 |
fi |
| 214 |
fi |
| 215 |
if [ "${for_wiki}" -eq "1" ] ; then |
| 216 |
${PYTHON} build_tools/get_documentation.py < "${i}" > "${tmp_output_file}" |
| 217 |
else |
| 218 |
${AWK} -f "${source_tree}/build_tools/get_documentation" "${i}" > "${tmp_output_file}" |
| 219 |
fi |
| 220 |
if [ "${subdir}" = "main" ] ; then |
| 221 |
# Force the module to be "builtin" if the source is in the main directory. |
| 222 |
mn="builtin" |
| 223 |
else |
| 224 |
# Otherwise, let's just get the basename of the module. |
| 225 |
bn=${i##*/} |
| 226 |
mn=${bn%%.*} |
| 227 |
fi |
| 228 |
# Set the module name on specific elements |
| 229 |
${SED} -r -e "s/<(manager|managerEvent|managerEventInstance|function|application|info)\s+([^>]+)>/<\1 \2 module=\"${mn}\">/g" "${tmp_output_file}" >> "${output_file}" |
| 230 |
done |
| 231 |
for i in $(${FIND} "${subdir_path}" -name '*.xml') ; do |
| 232 |
${GREP} -q "appdocsxml.dtd" "${i}" || continue |
| 233 |
if [ "${validate}" -eq "1" ] ;then |
| 234 |
if [ "${XMLLINT}" != ":" ] ; then |
| 235 |
${XMLLINT} --dtdvalid "${source_tree}/doc/appdocsxml.dtd" --path "${source_tree}/doc" --noout "${i}" || { echo "" ; exit 1 ; } |
| 236 |
else |
| 237 |
${XMLSTARLET} val -q -d "${source_tree}/doc/appdocsxml.dtd" "${i}" || |
| 238 |
${XMLSTARLET} val -e -d "${source_tree}/doc/appdocsxml.dtd" "${i}" || { echo "" ; exit 1 ; } |
| 239 |
fi |
| 240 |
fi |
| 241 |
${SED} -r "/^\s*(<[?]xml|<.DOCTYPE|<.?docs)/d" "${i}" > "${tmp_output_file}" |
| 242 |
dirname=${i%/*} |
| 243 |
if [ "${dirname}" != "${subdir_path}" ] ; then |
| 244 |
# If we're in a subdirectory like channels/pjsip, we need to check channels/Makefile |
| 245 |
# to see which module xml files in this directory belong to. |
| 246 |
bn=${dirname##*/} |
| 247 |
mn=$(${SED} -n -r -e "s/^[$]\(call MOD_ADD_C,([^,]+),[$]\(wildcard\s+${bn}\/.*/\1/gp" "${subdir_path}/Makefile") |
| 248 |
else |
| 249 |
if [ "${subdir}" = "main" ] ; then |
| 250 |
# Force the module to be "builtin" if the XML is in the main directory. |
| 251 |
mn="builtin" |
| 252 |
else |
| 253 |
# Otherwise the xml should have be "<module>_doc.xml" suffix so |
| 254 |
# get the basename then strip the suffix. |
| 255 |
bn=${i##*/} |
| 256 |
mn=${bn%%_doc.xml} |
| 257 |
fi |
| 258 |
fi |
| 259 |
# Set the module name on specific elements |
| 260 |
${SED} -r -e "s/<(manager|managerEvent|managerEventInstance|function|application|info)\s+([^>]+)>/<\1 \2 module=\"${mn}\">/g" "${tmp_output_file}" >> "${output_file}" |
| 261 |
done |
| 262 |
done |
| 263 |
|
| 264 |
echo "</docs>" >> "${output_file}" |
| 265 |
echo "" |
| 266 |
# Some entries may already have a module attribute so remove the dup. |
| 267 |
# It's easier to do this once on the entire file rather on a source-by-source basis. |
| 268 |
cp "${output_file}" "${tmp_output_file}" |
| 269 |
${SED} -r -e 's/module="([^"]+)"\s+module="([^"]+)">/module="\1">/g' "${tmp_output_file}" > "${output_file}" |
| 270 |
|
| 271 |
|
| 272 |
if [ "${for_wiki}" -eq "1" ] ; then |
| 273 |
${PYTHON} build_tools/post_process_documentation.py -i "${output_file}" -o "${core_output_file}" |
| 274 |
fi |
| 275 |
|
| 276 |
if [ "${validate}" -eq "1" ] ;then |
| 277 |
if [ "${XMLLINT}" != ":" ] ; then |
| 278 |
${XMLLINT} --dtdvalid "${source_tree}/doc/appdocsxml.dtd" --path "${source_tree}/doc" --noout "${output_file}" || exit 1 |
| 279 |
else |
| 280 |
${XMLSTARLET} val -q -d "${source_tree}/doc/appdocsxml.dtd" "${output_file}" || |
| 281 |
${XMLSTARLET} val -e -d "${source_tree}/doc/appdocsxml.dtd" "${output_file}" || { echo "" ; exit 1 ; } |
| 282 |
fi |
| 283 |
fi |
| 284 |
|