br2-external 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/bin/bash
  2. set -e
  3. # The name and location of the br2-external tree, once validated.
  4. declare BR2_NAME
  5. declare BR2_EXT
  6. # URL to manual for help in converting old br2-external trees.
  7. # Escape '#' so that make does not consider it a comment.
  8. MANUAL_URL='https://buildroot.org/manual/manual.html\#br2-external-converting'
  9. main() {
  10. local OPT OPTARG
  11. local br2_ext ofile ofmt
  12. while getopts :hkmo: OPT; do
  13. case "${OPT}" in
  14. h) help; exit 0;;
  15. o) ofile="${OPTARG}";;
  16. k) ofmt="kconfig";;
  17. m) ofmt="mk";;
  18. :) error "option '%s' expects a mandatory argument\n" "${OPTARG}";;
  19. \?) error "unknown option '%s'\n" "${OPTARG}";;
  20. esac
  21. done
  22. # Forget options; keep only positional args
  23. shift $((OPTIND-1))
  24. # Accept 0 or 1 br2-external tree.
  25. if [ ${#} -gt 1 ]; then
  26. error "only zero or one br2-external tree allowed.\n"
  27. fi
  28. br2_ext="${1}"
  29. case "${ofmt}" in
  30. mk|kconfig)
  31. ;;
  32. *) error "no output format specified (-m/-k)\n";;
  33. esac
  34. if [ -z "${ofile}" ]; then
  35. error "no output file specified (-o)\n"
  36. fi
  37. exec >"${ofile}"
  38. do_validate "${br2_ext}"
  39. do_${ofmt}
  40. }
  41. # Validates the br2-external tree passed as argument. Makes it cannonical
  42. # and store it in global variable BR2_EXT.
  43. #
  44. # Note: since this script is always first called from Makefile context
  45. # to generate the Makefile fragment before it is called to generate the
  46. # Kconfig snippet, we're sure that any error in do_validate will be
  47. # interpreted in Makefile context. Going up to generating the Kconfig
  48. # snippet means that there were no error.
  49. #
  50. do_validate() {
  51. local br2_ext="${1}"
  52. local br2_name n
  53. # No br2-external tree is valid
  54. if [ -z "${br2_ext}" ]; then
  55. return
  56. fi
  57. if [ ! -d "${br2_ext}" ]; then
  58. error "'%s': no such file or directory\n" "${br2_ext}"
  59. fi
  60. if [ ! -r "${br2_ext}" -o ! -x "${br2_ext}" ]; then
  61. error "'%s': permission denied\n" "${br2_ext}"
  62. fi
  63. if [ ! -f "${br2_ext}/external.desc" ]; then
  64. error "'%s': does not have a name (in 'external.desc'). See %s\n" \
  65. "${br2_ext}" "${MANUAL_URL}"
  66. fi
  67. br2_name="$(sed -r -e '/^name: +(.*)$/!d; s//\1/' "${br2_ext}/external.desc")"
  68. if [ -z "${br2_name}" ]; then
  69. error "'%s/external.desc': does not define the name\n" "${br2_ext}"
  70. fi
  71. # Only ASCII chars in [A-Za-z0-9_] are permitted
  72. n="$(sed -r -e 's/[A-Za-z0-9_]//g' <<<"${br2_name}" )"
  73. if [ -n "${n}" ]; then
  74. # Escape '$' so that it gets printed
  75. error "'%s': name '%s' contains invalid chars: '%s'\n" \
  76. "${br2_ext}" "${br2_name//\$/\$\$}" "${n//\$/\$\$}"
  77. fi
  78. if [ ! -f "${br2_ext}/external.mk" ]; then
  79. error "'%s/external.mk': no such file or directory\n" "${br2_ext}"
  80. fi
  81. if [ ! -f "${br2_ext}/Config.in" ]; then
  82. error "'%s/Config.in': no such file or directory\n" "${br2_ext}"
  83. fi
  84. BR2_NAME="${br2_name}"
  85. BR2_EXT="$(cd "${br2_ext}"; pwd -P )"
  86. }
  87. # Generate the .mk snippet that defines makefile variables
  88. # for the br2-external tree
  89. do_mk() {
  90. printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
  91. printf '\n'
  92. printf 'BR2_EXTERNAL ?= %s\n' "${BR2_EXT}"
  93. printf 'BR2_EXTERNAL_NAME = \n'
  94. printf 'BR2_EXTERNAL_MK =\n'
  95. printf '\n'
  96. if [ -z "${BR2_NAME}" ]; then
  97. printf '# No br2-external tree defined.\n'
  98. return
  99. fi
  100. printf 'BR2_EXTERNAL_NAME = %s\n' "${BR2_NAME}"
  101. printf 'BR2_EXTERNAL_MK = %s/external.mk\n' "${BR2_EXT}"
  102. printf 'BR2_EXTERNAL_%s_PATH = %s\n' "${BR2_NAME}" "${BR2_EXT}"
  103. }
  104. # Generate the kconfig snippet for the br2-external tree.
  105. do_kconfig() {
  106. printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
  107. printf '\n'
  108. if [ -z "${BR2_NAME}" ]; then
  109. printf '# No br2-external tree defined.\n'
  110. return
  111. fi
  112. printf 'menu "User-provided options"\n'
  113. printf '\n'
  114. printf 'comment "%s (in %s)"\n' "${BR2_NAME}" "${BR2_EXT}"
  115. printf '\n'
  116. printf 'config BR2_EXTERNAL_%s_PATH\n' "${BR2_NAME}"
  117. printf '\tstring\n'
  118. printf '\tdefault "%s"\n' "${BR2_EXT}"
  119. printf '\n'
  120. printf 'source "$BR2_EXTERNAL_%s_PATH/Config.in"\n' "${BR2_NAME}"
  121. printf '\n'
  122. printf "endmenu # User-provided options\n"
  123. }
  124. help() {
  125. cat <<-_EOF_
  126. Usage:
  127. ${my_name} <-m|-k> -o FILE PATH
  128. With -m, ${my_name} generates the makefile fragment that defines
  129. variables related to the br2-external tree passed as positional
  130. argument.
  131. With -k, ${my_name} generates the kconfig snippet to include the
  132. configuration options specified in the br2-external tree passed
  133. as positional argument.
  134. Using -k and -m together is not possible. The last one wins.
  135. Options:
  136. -m Generate the makefile fragment.
  137. -k Generate the kconfig snippet.
  138. -o FILE
  139. FILE in which to generate the kconfig snippet or makefile
  140. fragment.
  141. Returns:
  142. 0 If no error
  143. !0 If any error
  144. _EOF_
  145. }
  146. error() { local fmt="${1}"; shift; printf "BR2_EXTERNAL_ERROR = ${fmt}" "${@}"; exit 1; }
  147. my_name="${0##*/}"
  148. main "${@}"