python-pathvalidate: bump version to 0.14.1
[buildroot-gz.git] / support / scripts / br2-external
blob26bcac8aa1a3c6bf1d302b0d773d5cdea7c37d1a
1 #!/bin/bash
2 set -e
4 # This script must be able to run with bash-3.1, so it can't use
5 # associative arrays. Instead, it emulates them using 'eval'. It
6 # can however use indexed arrays, supported since at least bash-3.0.
8 # The names of the br2-external trees, once validated.
9 declare -a BR2_EXT_NAMES
11 # URL to manual for help in converting old br2-external trees.
12 # Escape '#' so that make does not consider it a comment.
13 MANUAL_URL='https://buildroot.org/manual.html\#br2-external-converting'
15 main() {
16 local OPT OPTARG
17 local br2_ext ofile ofmt
19 while getopts :hkmo: OPT; do
20 case "${OPT}" in
21 h) help; exit 0;;
22 o) ofile="${OPTARG}";;
23 k) ofmt="kconfig";;
24 m) ofmt="mk";;
25 :) error "option '%s' expects a mandatory argument\n" "${OPTARG}";;
26 \?) error "unknown option '%s'\n" "${OPTARG}";;
27 esac
28 done
29 # Forget options; keep only positional args
30 shift $((OPTIND-1))
32 case "${ofmt}" in
33 mk|kconfig)
35 *) error "no output format specified (-m/-k)\n";;
36 esac
37 if [ -z "${ofile}" ]; then
38 error "no output file specified (-o)\n"
41 exec >"${ofile}"
43 do_validate ${@//:/ }
45 do_${ofmt}
48 # Validates the br2-external trees passed as arguments. Makes each of
49 # them canonical and store them in the global arrays BR2_EXT_NAMES
50 # and BR2_EXT_PATHS.
52 # Note: since this script is always first called from Makefile context
53 # to generate the Makefile fragment before it is called to generate the
54 # Kconfig snippet, we're sure that any error in do_validate will be
55 # interpreted in Makefile context. Going up to generating the Kconfig
56 # snippet means that there were no error.
58 do_validate() {
59 local br2_ext
61 if [ ${#} -eq 0 ]; then
62 # No br2-external tree is valid
63 return
66 for br2_ext in "${@}"; do
67 do_validate_one "${br2_ext}"
68 done
71 do_validate_one() {
72 local br2_ext="${1}"
73 local br2_name br2_desc n d
75 if [ ! -d "${br2_ext}" ]; then
76 error "'%s': no such file or directory\n" "${br2_ext}"
78 if [ ! -r "${br2_ext}" -o ! -x "${br2_ext}" ]; then
79 error "'%s': permission denied\n" "${br2_ext}"
81 if [ ! -f "${br2_ext}/external.desc" ]; then
82 error "'%s': does not have a name (in 'external.desc'). See %s\n" \
83 "${br2_ext}" "${MANUAL_URL}"
85 br2_name="$(sed -r -e '/^name: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
86 if [ -z "${br2_name}" ]; then
87 error "'%s/external.desc': does not define the name\n" "${br2_ext}"
89 # Only ASCII chars in [A-Za-z0-9_] are permitted
90 n="$(sed -r -e 's/[A-Za-z0-9_]//g' <<<"${br2_name}" )"
91 if [ -n "${n}" ]; then
92 # Escape '$' so that it gets printed
93 error "'%s': name '%s' contains invalid chars: '%s'\n" \
94 "${br2_ext}" "${br2_name//\$/\$\$}" "${n//\$/\$\$}"
96 eval d="\"\${BR2_EXT_PATHS_${br2_name}}\""
97 if [ -n "${d}" ]; then
98 error "'%s': name '%s' is already used in '%s'\n" \
99 "${br2_ext}" "${br2_name}" "${d}"
101 br2_desc="$(sed -r -e '/^desc: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
102 if [ ! -f "${br2_ext}/external.mk" ]; then
103 error "'%s/external.mk': no such file or directory\n" "${br2_ext}"
105 if [ ! -f "${br2_ext}/Config.in" ]; then
106 error "'%s/Config.in': no such file or directory\n" "${br2_ext}"
109 # Register this br2-external tree, use an absolute canonical path
110 br2_ext="$( cd "${br2_ext}"; pwd )"
111 BR2_EXT_NAMES+=( "${br2_name}" )
112 eval BR2_EXT_PATHS_${br2_name}="\"\${br2_ext}\""
113 eval BR2_EXT_DESCS_${br2_name}="\"\${br2_desc:-\${br2_name}}\""
116 # Generate the .mk snippet that defines makefile variables
117 # for the br2-external tree
118 do_mk() {
119 local br2_name br2_ext
121 printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
122 printf '\n'
124 printf 'BR2_EXTERNAL ?='
125 for br2_name in "${BR2_EXT_NAMES[@]}"; do
126 eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
127 printf ' %s' "${br2_ext}"
128 done
129 printf '\n'
131 printf 'BR2_EXTERNAL_NAMES = \n'
132 printf 'BR2_EXTERNAL_DIRS = \n'
133 printf 'BR2_EXTERNAL_MKS = \n'
135 if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
136 printf '\n'
137 printf '# No br2-external tree defined.\n'
138 return
141 for br2_name in "${BR2_EXT_NAMES[@]}"; do
142 eval br2_desc="\"\${BR2_EXT_DESCS_${br2_name}}\""
143 eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
144 printf '\n'
145 printf 'BR2_EXTERNAL_NAMES += %s\n' "${br2_name}"
146 printf 'BR2_EXTERNAL_DIRS += %s\n' "${br2_ext}"
147 printf 'BR2_EXTERNAL_MKS += %s/external.mk\n' "${br2_ext}"
148 printf 'export BR2_EXTERNAL_%s_PATH = %s\n' "${br2_name}" "${br2_ext}"
149 printf 'export BR2_EXTERNAL_%s_DESC = %s\n' "${br2_name}" "${br2_desc}"
150 done
153 # Generate the kconfig snippet for the br2-external tree.
154 do_kconfig() {
155 local br2_name br2_ext
157 printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
158 printf '\n'
160 if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
161 printf '# No br2-external tree defined.\n'
162 return
165 printf 'menu "External options"\n'
166 printf '\n'
168 for br2_name in "${BR2_EXT_NAMES[@]}"; do
169 eval br2_desc="\"\${BR2_EXT_DESCS_${br2_name}}\""
170 eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
171 if [ ${#BR2_EXT_NAMES[@]} -gt 1 ]; then
172 printf 'menu "%s"\n' "${br2_desc}"
174 printf 'comment "%s (in %s)"\n' "${br2_desc}" "${br2_ext}"
175 printf 'config BR2_EXTERNAL_%s_PATH\n' "${br2_name}"
176 printf '\tstring\n'
177 printf '\tdefault "%s"\n' "${br2_ext}"
178 printf 'source "%s/Config.in"\n' "${br2_ext}"
179 if [ ${#BR2_EXT_NAMES[@]} -gt 1 ]; then
180 printf 'endmenu # %s\n' "${br2_name}"
182 printf '\n'
183 done
185 printf "endmenu # User-provided options\n"
188 help() {
189 cat <<-_EOF_
190 Usage:
191 ${my_name} <-m|-k> -o FILE PATH
193 With -m, ${my_name} generates the makefile fragment that defines
194 variables related to the br2-external trees passed as positional
195 arguments.
197 With -k, ${my_name} generates the kconfig snippet to include the
198 configuration options specified in the br2-external trees passed
199 as positional arguments.
201 Using -k and -m together is not possible. The last one wins.
203 Options:
204 -m Generate the makefile fragment.
206 -k Generate the kconfig snippet.
208 -o FILE
209 FILE in which to generate the kconfig snippet or makefile
210 fragment.
212 Returns:
213 0 If no error
214 !0 If any error
215 _EOF_
218 error() { local fmt="${1}"; shift; printf "BR2_EXTERNAL_ERROR = ${fmt}" "${@}"; exit 1; }
220 my_name="${0##*/}"
221 main "${@}"