logo

utils-extra

Collection of extra tools for Unixes

configure (3550B)


  1. #!/bin/sh
  2. # SPDX-FileCopyrightText: 2017-2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. DEPS=""
  5. usage() {
  6. cat <<END
  7. Usage: [variables] configure [variables]
  8. Variables:
  9. PREFIX=DIR
  10. BINDIR=DIR
  11. MANDIR=DIR
  12. PKGCONFIG=BIN
  13. MSGFMT=BIN
  14. CC=BIN
  15. MAKE=BIN
  16. MANDOC=BIN
  17. SHELLCHECK=BIN
  18. FLAWFINDER=BIN
  19. GCOV=BIN
  20. REUSE=BIN
  21. CFLAGS=OPTIONS
  22. EXTRA_CFLAGS=OPTIONS
  23. LDFLAGS=OPTIONS
  24. LDSTATIC=OPTIONS
  25. Variables are set in the following order: Default, Environment, Arguments
  26. Dependencies: See the README.md file
  27. END
  28. }
  29. is_ok() {
  30. status="$?"
  31. if test $status -eq 0; then
  32. printf " OK\n"
  33. else
  34. printf " FAIL\n"
  35. fi
  36. return $status
  37. }
  38. or_die() {
  39. is_ok || exit 1
  40. }
  41. pkg_config_check() {
  42. printf 'Checking: %s %s ...' "${PKGCONFIG}" "$*"
  43. "${PKGCONFIG}" "$@"
  44. is_ok
  45. }
  46. gen_targets() {
  47. printf 'EXE = '
  48. printf '%s\n ' cmd/*.c sh/* | sed -e 's;.c$;;' | tr -d '\n'
  49. echo
  50. printf 'MAN1 = '
  51. printf '%s\n ' cmd/*.1 sh-man/*.1 | tr -d '\n'
  52. echo
  53. }
  54. check_cmd() {
  55. var="$1"
  56. full_cmd="$2"
  57. # shellcheck disable=SC2086
  58. set -- $full_cmd
  59. cmd="$1"
  60. printf 'Checking $%s = %s command existence ...' "$var" "$full_cmd"
  61. command -v "$cmd" >/dev/null ; is_ok
  62. }
  63. ## User configuration
  64. # defaults
  65. PREFIX="${PREFIX:-/usr/local}"
  66. PKGCONFIG="${PKGCONFIG:-pkg-config}"
  67. MSGFMT="${MSGFMT:-msgfmt}"
  68. CC="${CC:-cc}"
  69. MAKE="${MAKE:-make}"
  70. GCOV="${GCOV:-gcov}"
  71. # -DDEBUG: Otherwise assert() does nothing, fine to be removed in production
  72. CFLAGS="${CFLAGS:--g -O2 -DDEBUG}"
  73. MANDOC="${MANDOC:-mandoc}"
  74. SHELLCHECK="${SHELLCHECK:-shellcheck}"
  75. FLAWFINDER="${FLAWFINDER:-flawfinder}"
  76. REUSE="${REUSE:-reuse}"
  77. # Also allow variables through arguments
  78. for i; do
  79. case "$i" in
  80. -h|--help)
  81. usage
  82. exit 1
  83. ;;
  84. -*)
  85. printf "Unknown argument ‘%s’\n" "${i}"
  86. usage
  87. exit 1
  88. ;;
  89. *=*)
  90. # shellcheck disable=SC2163
  91. export "$i"
  92. shift
  93. ;;
  94. *)
  95. printf "Unknown argument ‘%s’\n" "${i}"
  96. usage
  97. exit 1
  98. ;;
  99. esac
  100. done
  101. # Fallback definitions for dirs, based on $PREFIX
  102. BINDIR="${BINDIR:-${PREFIX}/bin}"
  103. MANDIR="${MANDIR:-${PREFIX}/share/man}"
  104. # Add some extra CFLAGS
  105. CFLAGS="${CFLAGS} ${EXTRA_CFLAGS}"
  106. printf 'Pruning old configurations ...'
  107. rm -f config.mk ; or_die
  108. ## System checks
  109. # commands
  110. check_cmd PKGCONFIG "$PKGCONFIG" || exit 1
  111. check_cmd CC "$CC" || exit 1
  112. check_cmd MAKE "$MAKE" || exit 1
  113. if check_cmd GCOV "$GCOV"
  114. then
  115. :
  116. else
  117. echo 'Notice: "coverage" target will fail'
  118. GCOV="false"
  119. fi
  120. if check_cmd MANDOC "$MANDOC"
  121. then
  122. :
  123. else
  124. echo 'Notice: Linting depending on mandoc disabled'
  125. MANDOC="true"
  126. fi
  127. if check_cmd SHELLCHECK "$SHELLCHECK"
  128. then
  129. :
  130. else
  131. echo 'Notice: Linting depending on shellcheck disabled'
  132. SHELLCHECK="true"
  133. fi
  134. if check_cmd FLAWFINDER "$FLAWFINDER"
  135. then
  136. :
  137. else
  138. echo 'Notice: Linting depending on flawfinder disabled'
  139. FLAWFINDER="true"
  140. fi
  141. if check_cmd REUSE "$REUSE"
  142. then
  143. :
  144. else
  145. echo 'Notice: Copyright linting depending on reuse disabled'
  146. REUSE="true"
  147. fi
  148. echo
  149. # pkg-config
  150. for dep in ${DEPS}
  151. do
  152. pkg_config_check --exists "$dep" || exit 1
  153. done
  154. echo
  155. ## Configuration write
  156. printf 'Writing to config.mk ...'
  157. cat >config.mk <<EOF
  158. # Autogenerated by ./configure
  159. PREFIX = ${PREFIX}
  160. BINDIR = ${BINDIR}
  161. MANDIR = ${MANDIR}
  162. PKGCONFIG = ${PKGCONFIG}
  163. CC = ${CC}
  164. MAKE = ${MAKE}
  165. MANDOC = ${MANDOC}
  166. SHELLCHECK = ${SHELLCHECK}
  167. FLAWFINDER = ${FLAWFINDER}
  168. MSGFMT = ${MSGFMT}
  169. DBG = ${DBG}
  170. GCOV = ${GCOV}
  171. REUSE = ${REUSE}
  172. CFLAGS = ${CFLAGS}
  173. LDFLAGS = ${LDFLAGS}
  174. LDSTATIC = ${LDSTATIC}
  175. EOF
  176. is_ok
  177. gen_targets >> config.mk ; or_die
  178. echo
  179. echo 'Done, you can now run make'