logo

utils

Old programs, got split in utils-std and utils-extra git clone https://anongit.hacktivis.me/git/utils.git/

configure (3711B)


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