logo

utils-std

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

configure (5688B)


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