logo

utils-extra

Collection of extra tools for Unixes git clone https://anongit.hacktivis.me/git/utils-extra.git/

configure (3521B)


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