br2-external 4.8 KB

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