2
1

br2-external 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/bin/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 ofile ofmt
  14. while getopts :hkmo: OPT; do
  15. case "${OPT}" in
  16. h) help; exit 0;;
  17. o) ofile="${OPTARG}";;
  18. k) ofmt="kconfig";;
  19. m) ofmt="mk";;
  20. :) error "option '%s' expects a mandatory argument\n" "${OPTARG}";;
  21. \?) error "unknown option '%s'\n" "${OPTARG}";;
  22. esac
  23. done
  24. # Forget options; keep only positional args
  25. shift $((OPTIND-1))
  26. case "${ofmt}" in
  27. mk|kconfig)
  28. ;;
  29. *) error "no output format specified (-m/-k)\n";;
  30. esac
  31. if [ -z "${ofile}" ]; then
  32. error "no output file specified (-o)\n"
  33. fi
  34. exec >"${ofile}"
  35. do_validate ${@//:/ }
  36. do_${ofmt}
  37. }
  38. # Validates the br2-external trees passed as arguments. Makes each of
  39. # them canonical and store them in the global arrays BR2_EXT_NAMES
  40. # and BR2_EXT_PATHS.
  41. #
  42. # Note: since this script is always first called from Makefile context
  43. # to generate the Makefile fragment before it is called to generate the
  44. # Kconfig snippet, we're sure that any error in do_validate will be
  45. # interpreted in Makefile context. Going up to generating the Kconfig
  46. # snippet means that there were no error.
  47. #
  48. do_validate() {
  49. local br2_ext
  50. if [ ${#} -eq 0 ]; then
  51. # No br2-external tree is valid
  52. return
  53. fi
  54. for br2_ext in "${@}"; do
  55. do_validate_one "${br2_ext}"
  56. done
  57. }
  58. do_validate_one() {
  59. local br2_ext="${1}"
  60. local br2_name br2_desc n d
  61. if [ ! -d "${br2_ext}" ]; then
  62. error "'%s': no such file or directory\n" "${br2_ext}"
  63. fi
  64. if [ ! -r "${br2_ext}" -o ! -x "${br2_ext}" ]; then
  65. error "'%s': permission denied\n" "${br2_ext}"
  66. fi
  67. if [ ! -f "${br2_ext}/external.desc" ]; then
  68. error "'%s': does not have a name (in 'external.desc'). See %s\n" \
  69. "${br2_ext}" "${MANUAL_URL}"
  70. fi
  71. br2_name="$(sed -r -e '/^name: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
  72. if [ -z "${br2_name}" ]; then
  73. error "'%s/external.desc': does not define the name\n" "${br2_ext}"
  74. fi
  75. # Only ASCII chars in [A-Za-z0-9_] are permitted
  76. n="$(sed -r -e 's/[A-Za-z0-9_]//g' <<<"${br2_name}" )"
  77. if [ -n "${n}" ]; then
  78. # Escape '$' so that it gets printed
  79. error "'%s': name '%s' contains invalid chars: '%s'\n" \
  80. "${br2_ext}" "${br2_name//\$/\$\$}" "${n//\$/\$\$}"
  81. fi
  82. eval d="\"\${BR2_EXT_PATHS_${br2_name}}\""
  83. if [ -n "${d}" ]; then
  84. error "'%s': name '%s' is already used in '%s'\n" \
  85. "${br2_ext}" "${br2_name}" "${d}"
  86. fi
  87. br2_desc="$(sed -r -e '/^desc: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
  88. if [ ! -f "${br2_ext}/external.mk" ]; then
  89. error "'%s/external.mk': no such file or directory\n" "${br2_ext}"
  90. fi
  91. if [ ! -f "${br2_ext}/Config.in" ]; then
  92. error "'%s/Config.in': no such file or directory\n" "${br2_ext}"
  93. fi
  94. # Register this br2-external tree, use an absolute canonical path
  95. br2_ext="$( cd "${br2_ext}"; pwd )"
  96. BR2_EXT_NAMES+=( "${br2_name}" )
  97. eval BR2_EXT_PATHS_${br2_name}="\"\${br2_ext}\""
  98. eval BR2_EXT_DESCS_${br2_name}="\"\${br2_desc:-\${br2_name}}\""
  99. }
  100. # Generate the .mk snippet that defines makefile variables
  101. # for the br2-external tree
  102. do_mk() {
  103. local br2_name br2_ext
  104. printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
  105. printf '\n'
  106. printf 'BR2_EXTERNAL ?='
  107. for br2_name in "${BR2_EXT_NAMES[@]}"; do
  108. eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
  109. printf ' %s' "${br2_ext}"
  110. done
  111. printf '\n'
  112. printf 'BR2_EXTERNAL_NAMES = \n'
  113. printf 'BR2_EXTERNAL_DIRS = \n'
  114. printf 'BR2_EXTERNAL_MKS = \n'
  115. if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
  116. printf '\n'
  117. printf '# No br2-external tree defined.\n'
  118. return
  119. fi
  120. for br2_name in "${BR2_EXT_NAMES[@]}"; do
  121. eval br2_desc="\"\${BR2_EXT_DESCS_${br2_name}}\""
  122. eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
  123. printf '\n'
  124. printf 'BR2_EXTERNAL_NAMES += %s\n' "${br2_name}"
  125. printf 'BR2_EXTERNAL_DIRS += %s\n' "${br2_ext}"
  126. printf 'BR2_EXTERNAL_MKS += %s/external.mk\n' "${br2_ext}"
  127. printf 'export BR2_EXTERNAL_%s_PATH = %s\n' "${br2_name}" "${br2_ext}"
  128. printf 'export BR2_EXTERNAL_%s_DESC = %s\n' "${br2_name}" "${br2_desc}"
  129. done
  130. }
  131. # Generate the kconfig snippet for the br2-external tree.
  132. do_kconfig() {
  133. local br2_name br2_ext
  134. printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
  135. printf '\n'
  136. if [ ${#BR2_EXT_NAMES[@]} -eq 0 ]; then
  137. printf '# No br2-external tree defined.\n'
  138. return
  139. fi
  140. printf 'menu "External options"\n'
  141. printf '\n'
  142. for br2_name in "${BR2_EXT_NAMES[@]}"; do
  143. eval br2_desc="\"\${BR2_EXT_DESCS_${br2_name}}\""
  144. eval br2_ext="\"\${BR2_EXT_PATHS_${br2_name}}\""
  145. if [ ${#BR2_EXT_NAMES[@]} -gt 1 ]; then
  146. printf 'menu "%s"\n' "${br2_desc}"
  147. fi
  148. printf 'comment "%s (in %s)"\n' "${br2_desc}" "${br2_ext}"
  149. printf 'config BR2_EXTERNAL_%s_PATH\n' "${br2_name}"
  150. printf '\tstring\n'
  151. printf '\tdefault "%s"\n' "${br2_ext}"
  152. printf 'source "%s/Config.in"\n' "${br2_ext}"
  153. if [ ${#BR2_EXT_NAMES[@]} -gt 1 ]; then
  154. printf 'endmenu # %s\n' "${br2_name}"
  155. fi
  156. printf '\n'
  157. done
  158. printf "endmenu # User-provided options\n"
  159. }
  160. help() {
  161. cat <<-_EOF_
  162. Usage:
  163. ${my_name} <-m|-k> -o FILE PATH
  164. With -m, ${my_name} generates the makefile fragment that defines
  165. variables related to the br2-external trees passed as positional
  166. arguments.
  167. With -k, ${my_name} generates the kconfig snippet to include the
  168. configuration options specified in the br2-external trees passed
  169. as positional arguments.
  170. Using -k and -m together is not possible. The last one wins.
  171. Options:
  172. -m Generate the makefile fragment.
  173. -k Generate the kconfig snippet.
  174. -o FILE
  175. FILE in which to generate the kconfig snippet or makefile
  176. fragment.
  177. Returns:
  178. 0 If no error
  179. !0 If any error
  180. _EOF_
  181. }
  182. error() { local fmt="${1}"; shift; printf "BR2_EXTERNAL_ERROR = ${fmt}" "${@}"; exit 1; }
  183. my_name="${0##*/}"
  184. main "${@}"