br2-external 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. BR2_EXT="$(cd "${br2_ext}"; pwd -P )"
  59. }
  60. # Generate the .mk snippet that defines makefile variables
  61. # for the br2-external tree
  62. do_mk() {
  63. local BR2_EXT="${1}"
  64. printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
  65. printf '\n'
  66. printf 'BR2_EXTERNAL ?= %s\n' "${BR2_EXT}"
  67. printf 'BR2_EXTERNAL_MK =\n'
  68. printf '\n'
  69. if [ -z "${BR2_EXT}" ]; then
  70. printf '# No br2-external tree defined.\n'
  71. return
  72. fi
  73. printf 'BR2_EXTERNAL_MK = %s/external.mk\n' "${BR2_EXT}"
  74. }
  75. # Generate the kconfig snippet for the br2-external tree.
  76. do_kconfig() {
  77. printf '#\n# Automatically generated file; DO NOT EDIT.\n#\n'
  78. printf '\n'
  79. if [ -z "${BR2_EXT}" ]; then
  80. printf '# No br2-external tree defined.\n'
  81. return
  82. fi
  83. printf 'config BR2_EXTERNAL\n'
  84. printf '\tstring\n'
  85. printf '\tdefault "%s"\n' "${BR2_EXT}"
  86. printf '\n'
  87. printf 'menu "User-provided options"\n'
  88. printf '\n'
  89. printf 'source "%s/Config.in"\n' "${BR2_EXT}"
  90. printf '\n'
  91. printf "endmenu # User-provided options\n"
  92. }
  93. help() {
  94. cat <<-_EOF_
  95. Usage:
  96. ${my_name} <-m|-k> -o FILE PATH
  97. With -m, ${my_name} generates the makefile fragment that defines
  98. variables related to the br2-external tree passed as positional
  99. argument.
  100. With -k, ${my_name} generates the kconfig snippet to include the
  101. configuration options specified in the br2-external tree passed
  102. as positional argument.
  103. Using -k and -m together is not possible. The last one wins.
  104. Options:
  105. -m Generate the makefile fragment.
  106. -k Generate the kconfig snippet.
  107. -o FILE
  108. FILE in which to generate the kconfig snippet or makefile
  109. fragment.
  110. Returns:
  111. 0 If no error
  112. !0 If any error
  113. _EOF_
  114. }
  115. error() { local fmt="${1}"; shift; printf "BR2_EXTERNAL_ERROR = ${fmt}" "${@}"; exit 1; }
  116. my_name="${0##*/}"
  117. main "${@}"