]> Git Repo - buildroot-mgba.git/blob - support/scripts/br2-external
configs: nitrogen*: bump u-boot version to 2017.07
[buildroot-mgba.git] / support / scripts / br2-external
1 #!/usr/bin/env bash
2 set -e
3
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.
7
8 # The names of the br2-external trees, once validated.
9 declare -a BR2_EXT_NAMES
10
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'
14
15 main() {
16     local OPT OPTARG
17     local br2_ext ofile ofmt
18
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))
31
32     case "${ofmt}" in
33     mk|kconfig)
34         ;;
35     *)  error "no output format specified (-m/-k)\n";;
36     esac
37     if [ -z "${ofile}" ]; then
38         error "no output file specified (-o)\n"
39     fi
40
41     exec >"${ofile}"
42
43     # Trap any unexpected error to generate a meaningful error message
44     trap "error 'unexpected error while generating ${ofile}\n'" ERR
45
46     do_validate ${@//:/ }
47
48     do_${ofmt}
49 }
50
51 # Validates the br2-external trees passed as arguments. Makes each of
52 # them canonical and store them in the global arrays BR2_EXT_NAMES
53 # and BR2_EXT_PATHS.
54 #
55 # Note: since this script is always first called from Makefile context
56 # to generate the Makefile fragment before it is called to generate the
57 # Kconfig snippet, we're sure that any error in do_validate will be
58 # interpreted in Makefile context. Going up to generating the Kconfig
59 # snippet means that there were no error.
60 #
61 do_validate() {
62     local br2_ext
63
64     if [ ${#} -eq 0 ]; then
65         # No br2-external tree is valid
66         return
67     fi
68
69     for br2_ext in "${@}"; do
70         do_validate_one "${br2_ext}"
71     done
72 }
73
74 do_validate_one() {
75     local br2_ext="${1}"
76     local br2_name br2_desc n d
77
78     if [ ! -d "${br2_ext}" ]; then
79         error "'%s': no such file or directory\n" "${br2_ext}"
80     fi
81     if [ ! -r "${br2_ext}" -o ! -x "${br2_ext}" ]; then
82         error "'%s': permission denied\n" "${br2_ext}"
83     fi
84     if [ ! -f "${br2_ext}/external.desc" ]; then
85         error "'%s': does not have a name (in 'external.desc'). See %s\n" \
86             "${br2_ext}" "${MANUAL_URL}"
87     fi
88     br2_name="$(sed -r -e '/^name: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
89     if [ -z "${br2_name}" ]; then
90         error "'%s/external.desc': does not define the name\n" "${br2_ext}"
91     fi
92     # Only ASCII chars in [A-Za-z0-9_] are permitted
93     n="$(sed -r -e 's/[A-Za-z0-9_]//g' <<<"${br2_name}" )"
94     if [ -n "${n}" ]; then
95         # Escape '$' so that it gets printed
96         error "'%s': name '%s' contains invalid chars: '%s'\n" \
97             "${br2_ext}" "${br2_name//\$/\$\$}" "${n//\$/\$\$}"
98     fi
99     eval d="\"\${BR2_EXT_PATHS_${br2_name}}\""
100     if [ -n "${d}" ]; then
101         error "'%s': name '%s' is already used in '%s'\n" \
102             "${br2_ext}" "${br2_name}" "${d}"
103     fi
104     br2_desc="$(sed -r -e '/^desc: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
105     if [ ! -f "${br2_ext}/external.mk" ]; then
106         error "'%s/external.mk': no such file or directory\n" "${br2_ext}"
107     fi
108     if [ ! -f "${br2_ext}/Config.in" ]; then
109         error "'%s/Config.in': no such file or directory\n" "${br2_ext}"
110     fi
111
112     # Register this br2-external tree, use an absolute canonical path
113     br2_ext="$( cd "${br2_ext}"; pwd )"
114     BR2_EXT_NAMES+=( "${br2_name}" )
115     eval BR2_EXT_PATHS_${br2_name}="\"\${br2_ext}\""
116     eval BR2_EXT_DESCS_${br2_name}="\"\${br2_desc:-\${br2_name}}\""
117 }
118
119 # Generate the .mk snippet that defines makefile variables
120 # for the br2-external tree
121 do_mk() {
122     local br2_name br2_ext
123
124     printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
125     printf '\n'
126
127     printf 'BR2_EXTERNAL ?='
128     for br2_name in "${BR2_EXT_NAMES[@]}"; do
129         eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
130         printf ' %s' "${br2_ext}"
131     done
132     printf '\n'
133
134     printf 'BR2_EXTERNAL_NAMES = \n'
135     printf 'BR2_EXTERNAL_DIRS = \n'
136     printf 'BR2_EXTERNAL_MKS = \n'
137
138     if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
139         printf '\n'
140         printf '# No br2-external tree defined.\n'
141         return
142     fi
143
144     for br2_name in "${BR2_EXT_NAMES[@]}"; do
145         eval br2_desc="\"\${BR2_EXT_DESCS_${br2_name}}\""
146         eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
147         printf '\n'
148         printf 'BR2_EXTERNAL_NAMES += %s\n' "${br2_name}"
149         printf 'BR2_EXTERNAL_DIRS += %s\n' "${br2_ext}"
150         printf 'BR2_EXTERNAL_MKS += %s/external.mk\n' "${br2_ext}"
151         printf 'export BR2_EXTERNAL_%s_PATH = %s\n' "${br2_name}" "${br2_ext}"
152         printf 'export BR2_EXTERNAL_%s_DESC = %s\n' "${br2_name}" "${br2_desc}"
153     done
154 }
155
156 # Generate the kconfig snippet for the br2-external tree.
157 do_kconfig() {
158     local br2_name br2_ext
159
160     printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
161     printf '\n'
162
163     if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
164         printf '# No br2-external tree defined.\n'
165         return
166     fi
167
168     printf 'menu "External options"\n'
169     printf '\n'
170
171     for br2_name in "${BR2_EXT_NAMES[@]}"; do
172         eval br2_desc="\"\${BR2_EXT_DESCS_${br2_name}}\""
173         eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
174         if [ ${#BR2_EXT_NAMES[@]} -gt 1 ]; then
175             printf 'menu "%s"\n' "${br2_desc}"
176         fi
177         printf 'comment "%s (in %s)"\n' "${br2_desc}" "${br2_ext}"
178         printf 'config BR2_EXTERNAL_%s_PATH\n' "${br2_name}"
179         printf '\tstring\n'
180         printf '\tdefault "%s"\n' "${br2_ext}"
181         printf 'source "%s/Config.in"\n' "${br2_ext}"
182         if [ ${#BR2_EXT_NAMES[@]} -gt 1 ]; then
183             printf 'endmenu # %s\n' "${br2_name}"
184         fi
185         printf '\n'
186     done
187
188     printf "endmenu # User-provided options\n"
189 }
190
191 help() {
192     cat <<-_EOF_
193         Usage:
194             ${my_name} <-m|-k> -o FILE PATH
195
196         With -m, ${my_name} generates the makefile fragment that defines
197         variables related to the br2-external trees passed as positional
198         arguments.
199
200         With -k, ${my_name} generates the kconfig snippet to include the
201         configuration options specified in the br2-external trees passed
202         as positional arguments.
203
204         Using -k and -m together is not possible. The last one wins.
205
206         Options:
207             -m  Generate the makefile fragment.
208
209             -k  Generate the kconfig snippet.
210
211             -o FILE
212                 FILE in which to generate the kconfig snippet or makefile
213                 fragment.
214
215         Returns:
216             0   If no error
217             !0  If any error
218         _EOF_
219 }
220
221 error() { local fmt="${1}"; shift; printf "BR2_EXTERNAL_ERROR = ${fmt}" "${@}"; exit 1; }
222
223 my_name="${0##*/}"
224 main "${@}"
This page took 0.041454 seconds and 4 git commands to generate.