logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git

configure (5057B)


  1. #!/bin/sh
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. target_filter='-e cmd/expr.tab'
  5. usage() {
  6. cat <<END
  7. Usage: [variables] configure [variables]
  8. Variables:
  9. PREFIX=DIR
  10. BINDIR=DIR
  11. SHELLDIR=DIR
  12. PERLDIR=DIR
  13. MANDIR=DIR
  14. CROSS_COMPILE=TRIPLE
  15. MSGFMT=BIN
  16. CC=BIN
  17. YACC=BIN
  18. MAKE=BIN
  19. M4=BIN
  20. MANDOC=BIN
  21. GCOV=BIN
  22. CRAM=BIN
  23. REUSE=BIN
  24. CFLAGS=OPTIONS
  25. LDFLAGS=OPTIONS
  26. LDSTATIC=OPTIONS
  27. EXTRA_CFLAGS=OPTIONS
  28. Variables are set in the following order: Default, Environment, Arguments
  29. Dependencies: See the README.md file
  30. END
  31. }
  32. is_ok() {
  33. status="$?"
  34. if test $status -eq 0; then
  35. printf " OK\n"
  36. else
  37. printf " FAIL\n"
  38. fi
  39. return $status
  40. }
  41. or_die() {
  42. is_ok || exit 1
  43. }
  44. gen_targets() {
  45. printf 'EXE = '
  46. printf '%s\n ' cmd/*.c cmd/*.y | grep -v -F ${target_filter} | sed -e 's;\.c$;;' -e 's;\.y$;;' | tr -d '\n'
  47. echo
  48. printf 'MAN1SO = '
  49. printf '%s\n' cmd/*.1.in \
  50. | grep -v -F ${target_filter} \
  51. | sed -e 's;\.in$;;' -e 's;^cmd/;build/cmd/;' \
  52. | tr '\n' ' '
  53. echo
  54. printf 'MAN1 = ${MAN1SO} '
  55. printf '%s\n ' cmd/*.1 | grep -v -F ${target_filter} | tr -d '\n'
  56. echo
  57. }
  58. check_cmd() {
  59. var="$1"
  60. full_cmd="$2"
  61. # shellcheck disable=SC2086
  62. set -- $full_cmd
  63. cmd="$1"
  64. printf 'Checking $%s = %s command existence ...' "$var" "$full_cmd"
  65. command -v "$cmd" >/dev/null ; is_ok
  66. }
  67. check_header() {
  68. header="$1"
  69. set -- $CC -E -x c -
  70. printf 'Checking <%s> header (printf "#include <%s>" | %s) ...' "$header" "$header" "$*"
  71. printf '#include <%s>\n' "$header" | $@ >/dev/null
  72. is_ok
  73. }
  74. check_conftest() {
  75. conftest="$1"
  76. printf 'Checking if conftest %s compiles ...' "$conftest"
  77. ${CC} ${CFLAGS} -Werror=implicit-function-declaration -c -o "${conftest}.bin" "${conftest}" 2>&1
  78. is_ok
  79. rm "${conftest}.bin"
  80. }
  81. check_cflag() {
  82. printf "checking whether compiler accepts %s... " "$@"
  83. ${CC} ${CFLAGS} "$@" -c -o "configure.d/cflag_test.bin" "configure.d/cflag_test.c" 2>&1
  84. is_ok
  85. rm "configure.d/cflag_test.bin"
  86. }
  87. set -o pipefail
  88. ## User configuration
  89. # defaults
  90. PREFIX="${PREFIX:-/usr/local}"
  91. MSGFMT="${MSGFMT:-msgfmt}"
  92. CC="${CC:-${CROSS_COMPILE}cc}"
  93. YACC="${YACC:-yacc}"
  94. MAKE="${MAKE:-make}"
  95. GCOV="${GCOV:-${CROSS_COMPILE}gcov}"
  96. CFLAGS="${CFLAGS:--g -O2}"
  97. M4="${M4:-m4}"
  98. MANDOC="${MANDOC:-mandoc}"
  99. CRAM="${CRAM:-cram}"
  100. REUSE="${REUSE:-reuse}"
  101. # Also allow variables through arguments
  102. for i; do
  103. case "$i" in
  104. -h|--help)
  105. usage
  106. exit 1
  107. ;;
  108. -*)
  109. printf "Unknown argument ‘%s’\n" "${i}"
  110. usage
  111. exit 1
  112. ;;
  113. *=*)
  114. # shellcheck disable=SC2163
  115. export "$i"
  116. shift
  117. ;;
  118. *)
  119. printf "Unknown argument ‘%s’\n" "${i}"
  120. usage
  121. exit 1
  122. ;;
  123. esac
  124. done
  125. # Fallback definitions for dirs, based on $PREFIX
  126. BINDIR="${BINDIR:-${PREFIX}/bin}"
  127. SHELLDIR="${SHELLDIR:-${BINDIR}}"
  128. PERLDIR="${PERLDIR:-${BINDIR}}"
  129. MANDIR="${MANDIR:-${PREFIX}/share/man}"
  130. # Add some extra CFLAGS
  131. CFLAGS="${CFLAGS} ${EXTRA_CFLAGS}"
  132. # Otherwise assert() can end up doing nothing, don't remove
  133. CFLAGS="${CFLAGS} -DDEBUG -UNDEBUG"
  134. ## System checks
  135. # commands
  136. check_cmd CC "$CC" || exit 1
  137. check_cmd YACC "$YACC" || exit 1
  138. check_cmd MAKE "$MAKE" || exit 1
  139. check_cmd M4 "$M4" || exit 1
  140. if ! check_cmd GCOV "$GCOV"
  141. then
  142. echo 'Notice: "coverage" target will fail'
  143. GCOV="false"
  144. fi
  145. if ! check_cmd MANDOC "$MANDOC"
  146. then
  147. echo 'Notice: Linting depending on mandoc disabled'
  148. MANDOC="true"
  149. fi
  150. if ! check_cmd CRAM "$CRAM"
  151. then
  152. echo "Notice: cram not found, trying prysk"
  153. CRAM="prysk"
  154. if check_cmd CRAM "$CRAM"
  155. then
  156. :
  157. else
  158. CRAM="false"
  159. echo 'Notice: Testsuite depending on cram/psyk disabled'
  160. fi
  161. fi
  162. if ! check_cmd REUSE "$REUSE"
  163. then
  164. echo 'Notice: Copyright linting depending on reuse disabled'
  165. REUSE="true"
  166. fi
  167. echo
  168. check_cflag -Wwrite-strings && CFLAGS="${CFLAGS} -Wwrite-strings"
  169. if ! check_header mntent.h; then
  170. echo 'Disabling cmd/df'
  171. target_filter="${target_filter} -e cmd/df"
  172. fi
  173. if ! check_header sys/sysmacros.h; then
  174. echo 'Disabling cmd/mknod'
  175. target_filter="${target_filter} -e cmd/mknod"
  176. fi
  177. check_conftest configure.d/sendfile_linux.c && CFLAGS="${CFLAGS} -DHAS_SENDFILE"
  178. check_conftest configure.d/copy_file_range.c && CFLAGS="${CFLAGS} -DHAS_COPY_FILE_RANGE"
  179. if ! check_conftest configure.d/reallocarray.c; then
  180. echo 'Disabling cmd/tr cmd/cut'
  181. target_filter="${target_filter} -e cmd/tr. -e cmd/cut."
  182. fi
  183. check_conftest configure.d/getopt_long.c && CFLAGS="${CFLAGS} -DHAS_GETOPT_LONG"
  184. check_conftest configure.d/syncfs.c && CFLAGS="${CFLAGS} -DHAS_SYNCFS"
  185. echo
  186. ## Configuration write
  187. printf 'Writing to config.mk ...'
  188. cat >config.mk <<EOF
  189. # Autogenerated by ./configure
  190. PREFIX = ${PREFIX}
  191. BINDIR = ${BINDIR}
  192. SHELLDIR = ${SHELLDIR}
  193. PERLDIR = ${PERLDIR}
  194. MANDIR = ${MANDIR}
  195. CC = ${CC}
  196. YACC = ${YACC}
  197. MAKE = ${MAKE}
  198. M4 = ${M4}
  199. MANDOC = ${MANDOC}
  200. MSGFMT = ${MSGFMT}
  201. DBG = ${DBG}
  202. GCOV = ${GCOV}
  203. CRAM = ${CRAM}
  204. REUSE = ${REUSE}
  205. CFLAGS = ${CFLAGS}
  206. LDFLAGS = ${LDFLAGS}
  207. LDSTATIC = ${LDSTATIC}
  208. EOF
  209. is_ok
  210. printf 'Adding targets to config.mk ...'
  211. gen_targets >> config.mk ; or_die
  212. echo
  213. echo 'Done, you can now run make'