br2-external 3.8 KB

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