zyre: disable docs
[buildroot-gz.git] / support / scripts / br2-external
blob055dc08555e1f7a472413c75db3d454aa1f61196
1 #!/bin/bash
2 set -e
4 # The names and locations of the br2-external trees, once validated.
5 declare -a BR2_EXT_NAMES
6 declare -A BR2_EXT_PATHS
7 declare -A BR2_EXT_DESCS
9 # URL to manual for help in converting old br2-external trees.
10 # Escape '#' so that make does not consider it a comment.
11 MANUAL_URL='https://buildroot.org/manual.html\#br2-external-converting'
13 main() {
14 local OPT OPTARG
15 local br2_ext ofile ofmt
17 while getopts :hkmo: OPT; do
18 case "${OPT}" in
19 h) help; exit 0;;
20 o) ofile="${OPTARG}";;
21 k) ofmt="kconfig";;
22 m) ofmt="mk";;
23 :) error "option '%s' expects a mandatory argument\n" "${OPTARG}";;
24 \?) error "unknown option '%s'\n" "${OPTARG}";;
25 esac
26 done
27 # Forget options; keep only positional args
28 shift $((OPTIND-1))
30 case "${ofmt}" in
31 mk|kconfig)
33 *) error "no output format specified (-m/-k)\n";;
34 esac
35 if [ -z "${ofile}" ]; then
36 error "no output file specified (-o)\n"
39 exec >"${ofile}"
41 do_validate ${@//:/ }
43 do_${ofmt}
46 # Validates the br2-external trees passed as arguments. Makes each of
47 # them canonical and store them in the global arrays BR2_EXT_NAMES
48 # and BR2_EXT_PATHS.
50 # Note: since this script is always first called from Makefile context
51 # to generate the Makefile fragment before it is called to generate the
52 # Kconfig snippet, we're sure that any error in do_validate will be
53 # interpreted in Makefile context. Going up to generating the Kconfig
54 # snippet means that there were no error.
56 do_validate() {
57 local br2_ext
59 if [ ${#} -eq 0 ]; then
60 # No br2-external tree is valid
61 return
64 for br2_ext in "${@}"; do
65 do_validate_one "${br2_ext}"
66 done
69 do_validate_one() {
70 local br2_ext="${1}"
71 local br2_name br2_desc n
73 if [ ! -d "${br2_ext}" ]; then
74 error "'%s': no such file or directory\n" "${br2_ext}"
76 if [ ! -r "${br2_ext}" -o ! -x "${br2_ext}" ]; then
77 error "'%s': permission denied\n" "${br2_ext}"
79 if [ ! -f "${br2_ext}/external.desc" ]; then
80 error "'%s': does not have a name (in 'external.desc'). See %s\n" \
81 "${br2_ext}" "${MANUAL_URL}"
83 br2_name="$(sed -r -e '/^name: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
84 if [ -z "${br2_name}" ]; then
85 error "'%s/external.desc': does not define the name\n" "${br2_ext}"
87 # Only ASCII chars in [A-Za-z0-9_] are permitted
88 n="$(sed -r -e 's/[A-Za-z0-9_]//g' <<<"${br2_name}" )"
89 if [ -n "${n}" ]; then
90 # Escape '$' so that it gets printed
91 error "'%s': name '%s' contains invalid chars: '%s'\n" \
92 "${br2_ext}" "${br2_name//\$/\$\$}" "${n//\$/\$\$}"
94 if [ -n "${BR2_EXT_PATHS["${br2_name}"]}" ]; then
95 error "'%s': name '%s' is already used in '%s'\n" \
96 "${br2_ext}" "${br2_name}" "${BR2_EXT_PATHS["${br2_name}"]}"
98 br2_desc="$(sed -r -e '/^desc: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
99 if [ ! -f "${br2_ext}/external.mk" ]; then
100 error "'%s/external.mk': no such file or directory\n" "${br2_ext}"
102 if [ ! -f "${br2_ext}/Config.in" ]; then
103 error "'%s/Config.in': no such file or directory\n" "${br2_ext}"
106 # Register this br2-external tree
107 BR2_EXT_NAMES+=( "${br2_name}" )
108 BR2_EXT_PATHS["${br2_name}"]="${br2_ext}"
109 BR2_EXT_DESCS["${br2_name}"]="${br2_desc:-${br2_name}}"
112 # Generate the .mk snippet that defines makefile variables
113 # for the br2-external tree
114 do_mk() {
115 local br2_name br2_ext
117 printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
118 printf '\n'
120 # We can't use ${BR2_EXT_NAMES[@]} directly: it is not guaranteed
121 # to be in the order paths were added (because it is an associative
122 # array). So we need to iterate on BR2_EXT_NAMES, which is sorted
123 # in the order names were added (because it is an indexed array).
124 printf 'BR2_EXTERNAL ?='
125 for br2_name in "${BR2_EXT_NAMES[@]}"; do
126 printf ' %s' "${BR2_EXT_PATHS["${br2_name}"]}"
127 done
128 printf '\n'
130 printf 'BR2_EXTERNAL_NAMES = \n'
131 printf 'BR2_EXTERNAL_DIRS = \n'
132 printf 'BR2_EXTERNAL_MKS = \n'
134 if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
135 printf '\n'
136 printf '# No br2-external tree defined.\n'
137 return
140 for br2_name in "${BR2_EXT_NAMES[@]}"; do
141 br2_desc="${BR2_EXT_DESCS["${br2_name}"]}"
142 br2_ext="${BR2_EXT_PATHS["${br2_name}"]}"
143 printf '\n'
144 printf 'BR2_EXTERNAL_NAMES += %s\n' "${br2_name}"
145 printf 'BR2_EXTERNAL_DIRS += %s\n' "${br2_ext}"
146 printf 'BR2_EXTERNAL_MKS += %s/external.mk\n' "${br2_ext}"
147 printf 'export BR2_EXTERNAL_%s_PATH = %s\n' "${br2_name}" "${br2_ext}"
148 printf 'export BR2_EXTERNAL_%s_DESC = %s\n' "${br2_name}" "${br2_desc}"
149 done
152 # Generate the kconfig snippet for the br2-external tree.
153 do_kconfig() {
154 local br2_name br2_ext
156 printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
157 printf '\n'
159 if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
160 printf '# No br2-external tree defined.\n'
161 return
164 printf 'menu "External options"\n'
165 printf '\n'
167 for br2_name in "${BR2_EXT_NAMES[@]}"; do
168 br2_desc="${BR2_EXT_DESCS["${br2_name}"]}"
169 br2_ext="${BR2_EXT_PATHS["${br2_name}"]}"
170 if [ ${#BR2_EXT_NAMES[@]} -gt 1 ]; then
171 printf 'menu "%s"\n' "${br2_desc}"
173 printf 'comment "%s (in %s)"\n' "${br2_desc}" "${br2_ext}"
174 printf 'config BR2_EXTERNAL_%s_PATH\n' "${br2_name}"
175 printf '\tstring\n'
176 printf '\tdefault "%s"\n' "${br2_ext}"
177 printf 'source "%s/Config.in"\n' "${br2_ext}"
178 if [ ${#BR2_EXT_NAMES[@]} -gt 1 ]; then
179 printf 'endmenu # %s\n' "${br2_name}"
181 printf '\n'
182 done
184 printf "endmenu # User-provided options\n"
187 help() {
188 cat <<-_EOF_
189 Usage:
190 ${my_name} <-m|-k> -o FILE PATH
192 With -m, ${my_name} generates the makefile fragment that defines
193 variables related to the br2-external trees passed as positional
194 arguments.
196 With -k, ${my_name} generates the kconfig snippet to include the
197 configuration options specified in the br2-external trees passed
198 as positional arguments.
200 Using -k and -m together is not possible. The last one wins.
202 Options:
203 -m Generate the makefile fragment.
205 -k Generate the kconfig snippet.
207 -o FILE
208 FILE in which to generate the kconfig snippet or makefile
209 fragment.
211 Returns:
212 0 If no error
213 !0 If any error
214 _EOF_
217 error() { local fmt="${1}"; shift; printf "BR2_EXTERNAL_ERROR = ${fmt}" "${@}"; exit 1; }
219 my_name="${0##*/}"
220 main "${@}"