br2-external 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #!/usr/bin/env bash
  2. set -e
  3. # This script must be able to run with bash-3.1, so it can't use
  4. # associative arrays. Instead, it emulates them using 'eval'. It
  5. # can however use indexed arrays, supported since at least bash-3.0.
  6. # The names of the br2-external trees, once validated.
  7. declare -a BR2_EXT_NAMES
  8. # URL to manual for help in converting old br2-external trees.
  9. # Escape '#' so that make does not consider it a comment.
  10. MANUAL_URL='https://buildroot.org/manual.html\#br2-external-converting'
  11. main() {
  12. local OPT OPTARG
  13. local br2_ext outputdir
  14. while getopts :d: OPT; do
  15. case "${OPT}" in
  16. d) outputdir="${OPTARG}";;
  17. :) error "option '%s' expects a mandatory argument\n" "${OPTARG}";;
  18. \?) error "unknown option '%s'\n" "${OPTARG}";;
  19. esac
  20. done
  21. # Forget options; keep only positional args
  22. shift $((OPTIND-1))
  23. if [ -z "${outputdir}" ]; then
  24. error "no output directory specified (-d)\n"
  25. fi
  26. # Trap any unexpected error to generate a meaningful error message
  27. trap "error 'unexpected error while generating ${ofile}\n'" ERR
  28. mkdir -p "${outputdir}"
  29. do_validate "${outputdir}" ${@//:/ }
  30. do_mk "${outputdir}"
  31. do_kconfig "${outputdir}"
  32. }
  33. # Validates the br2-external trees passed as arguments. Makes each of
  34. # them canonical and store them in the global arrays BR2_EXT_NAMES
  35. # and BR2_EXT_PATHS.
  36. #
  37. # Note: since this script is always first called from Makefile context
  38. # to generate the Makefile fragment before it is called to generate the
  39. # Kconfig snippet, we're sure that any error in do_validate will be
  40. # interpreted in Makefile context. Going up to generating the Kconfig
  41. # snippet means that there were no error.
  42. #
  43. do_validate() {
  44. local outputdir="${1}"
  45. local br2_ext
  46. shift
  47. if [ ${#} -eq 0 ]; then
  48. # No br2-external tree is valid
  49. return
  50. fi
  51. for br2_ext in "${@}"; do
  52. do_validate_one "${br2_ext}"
  53. done >"${outputdir}/.br2-external.mk"
  54. }
  55. do_validate_one() {
  56. local br2_ext="${1}"
  57. local br2_name br2_desc n d
  58. if [ ! -d "${br2_ext}" ]; then
  59. error "'%s': no such file or directory\n" "${br2_ext}"
  60. fi
  61. if [ ! -r "${br2_ext}" -o ! -x "${br2_ext}" ]; then
  62. error "'%s': permission denied\n" "${br2_ext}"
  63. fi
  64. if [ ! -f "${br2_ext}/external.desc" ]; then
  65. error "'%s': does not have an 'external.desc'. See %s\n" \
  66. "${br2_ext}" "${MANUAL_URL}"
  67. fi
  68. br2_name="$(sed -r -e '/^name: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
  69. if [ -z "${br2_name}" ]; then
  70. error "'%s/external.desc': does not define the name\n" "${br2_ext}"
  71. fi
  72. # Only ASCII chars in [A-Za-z0-9_] are permitted
  73. n="$(sed -r -e 's/[A-Za-z0-9_]//g' <<<"${br2_name}" )"
  74. if [ -n "${n}" ]; then
  75. # Escape '$' so that it gets printed
  76. error "'%s': name '%s' contains invalid chars: '%s'\n" \
  77. "${br2_ext}" "${br2_name//\$/\$\$}" "${n//\$/\$\$}"
  78. fi
  79. eval d="\"\${BR2_EXT_PATHS_${br2_name}}\""
  80. if [ -n "${d}" ]; then
  81. error "'%s': name '%s' is already used in '%s'\n" \
  82. "${br2_ext}" "${br2_name}" "${d}"
  83. fi
  84. br2_desc="$(sed -r -e '/^desc: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
  85. if [ ! -f "${br2_ext}/external.mk" ]; then
  86. error "'%s/external.mk': no such file or directory\n" "${br2_ext}"
  87. fi
  88. if [ ! -f "${br2_ext}/Config.in" ]; then
  89. error "'%s/Config.in': no such file or directory\n" "${br2_ext}"
  90. fi
  91. # Register this br2-external tree, use an absolute canonical path
  92. br2_ext="$( cd "${br2_ext}"; pwd )"
  93. BR2_EXT_NAMES+=( "${br2_name}" )
  94. eval BR2_EXT_PATHS_${br2_name}="\"\${br2_ext}\""
  95. eval BR2_EXT_DESCS_${br2_name}="\"\${br2_desc:-\${br2_name}}\""
  96. }
  97. # Generate the .mk snippet that defines makefile variables
  98. # for the br2-external tree
  99. do_mk() {
  100. local outputdir="${1}"
  101. local br2_name br2_desc br2_ext
  102. {
  103. printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
  104. printf '\n'
  105. printf 'BR2_EXTERNAL ?='
  106. for br2_name in "${BR2_EXT_NAMES[@]}"; do
  107. eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
  108. printf ' %s' "${br2_ext}"
  109. done
  110. printf '\n'
  111. printf 'BR2_EXTERNAL_NAMES = \n'
  112. printf 'BR2_EXTERNAL_DIRS = \n'
  113. printf 'BR2_EXTERNAL_MKS = \n'
  114. if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
  115. printf '\n'
  116. printf '# No br2-external tree defined.\n'
  117. return
  118. fi
  119. for br2_name in "${BR2_EXT_NAMES[@]}"; do
  120. eval br2_desc="\"\${BR2_EXT_DESCS_${br2_name}}\""
  121. eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
  122. printf '\n'
  123. printf 'BR2_EXTERNAL_NAMES += %s\n' "${br2_name}"
  124. printf 'BR2_EXTERNAL_DIRS += %s\n' "${br2_ext}"
  125. printf 'BR2_EXTERNAL_MKS += %s/external.mk\n' "${br2_ext}"
  126. printf 'export BR2_EXTERNAL_%s_PATH = %s\n' "${br2_name}" "${br2_ext}"
  127. printf 'export BR2_EXTERNAL_%s_DESC = %s\n' "${br2_name}" "${br2_desc}"
  128. done
  129. } >"${outputdir}/.br2-external.mk"
  130. }
  131. # Generate the kconfig snippets for the br2-external tree.
  132. do_kconfig() {
  133. local outputdir="${1}"
  134. local br2_name br2_desc br2_ext br2
  135. local -a items
  136. items=(
  137. paths
  138. menus
  139. toolchains
  140. jpeg
  141. openssl
  142. skeleton
  143. init
  144. )
  145. for br2 in "${items[@]}"; do
  146. {
  147. printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
  148. printf '\n'
  149. if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
  150. printf '# No br2-external tree defined.\n'
  151. fi
  152. } >"${outputdir}/.br2-external.in.${br2}"
  153. done
  154. if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
  155. return
  156. fi
  157. printf 'menu "External options"\n\n' >>"${outputdir}/.br2-external.in.menus"
  158. for br2_name in "${BR2_EXT_NAMES[@]}"; do
  159. eval br2_desc="\"\${BR2_EXT_DESCS_${br2_name}}\""
  160. eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
  161. {
  162. printf 'config BR2_EXTERNAL_%s_PATH\n' "${br2_name}"
  163. printf '\tstring\n'
  164. printf '\tdefault "%s"\n' "${br2_ext}"
  165. printf '\n'
  166. } >>"${outputdir}/.br2-external.in.paths"
  167. {
  168. if [ ${#BR2_EXT_NAMES[@]} -gt 1 ]; then
  169. printf 'menu "%s"\n' "${br2_desc}"
  170. fi
  171. printf 'comment "%s (in %s)"\n' "${br2_desc}" "${br2_ext}"
  172. printf 'source "%s/Config.in"\n' "${br2_ext}"
  173. if [ ${#BR2_EXT_NAMES[@]} -gt 1 ]; then
  174. printf 'endmenu # %s\n' "${br2_name}"
  175. fi
  176. printf '\n'
  177. } >>"${outputdir}/.br2-external.in.menus"
  178. if [ -f "${br2_ext}/provides/toolchains.in" ]; then
  179. printf 'comment "Toolchains from: %s"\n' "${br2_desc}"
  180. printf 'source "%s/provides/toolchains.in"\n' "${br2_ext}"
  181. printf '\n'
  182. else
  183. printf '# No toolchain from: %s\n\n' "${br2_desc}"
  184. fi >>"${outputdir}/.br2-external.in.toolchains"
  185. if [ -f "${br2_ext}/provides/jpeg.in" ]; then
  186. printf 'comment "jpeg from: %s"\n' "${br2_desc}"
  187. printf 'source "%s/provides/jpeg.in"\n' "${br2_ext}"
  188. printf '\n'
  189. else
  190. printf '# No jpeg from: %s\n\n' "${br2_desc}"
  191. fi >>"${outputdir}/.br2-external.in.jpeg"
  192. if [ -f "${br2_ext}/provides/openssl.in" ]; then
  193. printf 'comment "openssl from: %s"\n' "${br2_desc}"
  194. printf 'source "%s/provides/openssl.in"\n' "${br2_ext}"
  195. printf '\n'
  196. else
  197. printf '# No openssl from: %s\n\n' "${br2_desc}"
  198. fi >>"${outputdir}/.br2-external.in.openssl"
  199. if [ -f "${br2_ext}/provides/skeleton.in" ]; then
  200. printf 'comment "skeleton from: %s"\n' "${br2_desc}"
  201. printf 'source "%s/provides/skeleton.in"\n' "${br2_ext}"
  202. printf '\n'
  203. else
  204. printf '# No skeleton from: %s\n\n' "${br2_desc}"
  205. fi >>"${outputdir}/.br2-external.in.skeleton"
  206. if [ -f "${br2_ext}/provides/init.in" ]; then
  207. printf 'comment "init from: %s"\n' "${br2_desc}"
  208. printf 'source "%s/provides/init.in"\n' "${br2_ext}"
  209. printf '\n'
  210. else
  211. printf '# No init from: %s\n\n' "${br2_desc}"
  212. fi >>"${outputdir}/.br2-external.in.init"
  213. done
  214. printf 'endmenu\n' >>"${outputdir}/.br2-external.in.menus"
  215. }
  216. error() { local fmt="${1}"; shift; printf "BR2_EXTERNAL_ERROR = ${fmt}" "${@}"; exit 1; }
  217. my_name="${0##*/}"
  218. main "${@}"