configure 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #!/bin/sh
  2. ### Automatic build configuration script for Dinit.
  3. ### This script generates an "mconfig" file suitable for building on the current system.
  4. ## Initial preparation
  5. set -eu
  6. cd "$(dirname "$0")"
  7. ## Helper functions
  8. # According to POSIX, echo has some unspecified behavior in some cases, for example
  9. # when its first argument is "-n" or backslash ("\"). We prefer to rely on
  10. # POSIX documented things.
  11. # So we replace shell built-in echo with a printf based function.
  12. # For more info see http://www.etalabs.net/sh_tricks.html
  13. echo()
  14. {
  15. IFS=" " printf %s\\n "$*"
  16. }
  17. info()
  18. {
  19. echo "$*"
  20. }
  21. sub_info()
  22. {
  23. echo " ... $*"
  24. }
  25. error()
  26. {
  27. >&2 echo "Error: $*"
  28. exit 1
  29. }
  30. sub_error()
  31. {
  32. >&2 echo " ... Error: $*"
  33. exit 1
  34. }
  35. warning()
  36. {
  37. echo
  38. echo "Warning: $*"
  39. }
  40. sub_warning()
  41. {
  42. echo " ... $*"
  43. echo
  44. }
  45. cxx_works()
  46. {
  47. info Checking whether \""$1"\" is a working C++ compiler...
  48. if ! $CXX -o testfile.o testfile.cc; then
  49. rm -f testfile*
  50. sub_error It seems like \""$CXX"\" is not working a working C++ compiler. Please specify compiler.
  51. else
  52. rm -f testfile
  53. sub_info Yes.
  54. fi
  55. }
  56. try_cxx_argument()
  57. {
  58. info Checking whether the compiler accepts "$2"...
  59. if $CXX $CXXFLAGS $2 $CXXFLAGS_EXTRA $LDFLAGS $LDFLAGS_EXTRA testfile.cc -o testfile > /dev/null 2>&1; then
  60. rm testfile
  61. sub_info Yes.
  62. eval "$1=\"\${$1} \$2\""
  63. eval "$1=\${$1# }"
  64. else
  65. if [ "$2" = "-std=c++11" ]; then
  66. sub_error "It seems like the C++ compiler don't support the (required) \"$2\" option!"
  67. else
  68. sub_info No.
  69. fi
  70. fi
  71. }
  72. try_ld_argument()
  73. {
  74. info Checking whether "$2" is accepted as a link-time option...
  75. if $CXX $CXXFLAGS $CXXFLAGS_EXTRA $LDFLAGS $LDFLAGS_EXTRA $2 testfile.cc -o testfile > /dev/null 2>&1; then
  76. sub_info Yes.
  77. eval "$1=\"\${$1} \$2\""
  78. eval "$1=\${$1# }"
  79. else
  80. sub_info No.
  81. fi
  82. }
  83. try_both_argument()
  84. {
  85. info Checking whether the compiler/linker accept "$3"...
  86. if $CXX $CXXFLAGS $CXXFLAGS_EXTRA $LDFLAGS $LDFLAGS_EXTRA $3 testfile.cc -o testfile > /dev/null 2>&1; then
  87. sub_info Yes.
  88. eval "$1=\"\${$1} \$3\""
  89. eval "$1=\${$1# }"
  90. eval "$2=\"\${$2} \$3\""
  91. eval "$2=\${$2# }"
  92. else
  93. sub_info No.
  94. fi
  95. }
  96. usage()
  97. {
  98. cat << _EOF
  99. Usage: $0 [OPTION]...
  100. Generates build configuration for Dinit.
  101. Defaults for the options are specified in brackets.
  102. --help This help message.
  103. --quiet Don't print normal messages, just errors.
  104. --clean Clear mconfig and configure's temporary files.
  105. Target options:
  106. --platform=PLATFORM Set the platform manually (Just for cross-platform cross-compile!) [autodetected]
  107. Note: for all cross-compiles please specify correct CXX and CXX_FOR_BUILD!
  108. Installation directories:
  109. --prefix=PREFIX Main installation prefix [/usr]
  110. --exec-prefix=EPREFIX Main executables prefix [/]
  111. --sbindir=SBINDIR Dinit executables [EPREFIX/sbin]
  112. --mandir=MANDIR Dinit man-pages location [PREFIX/share/man]
  113. --syscontrolsocket=SOCKETPATH Dinitctl socket location [/run/dinitctl] on Linux systems
  114. [/var/run/dinitctl] on other systems
  115. Optional options:
  116. --shutdown-prefix=PREFIX Name prefix for shutdown, poweroff, reboot, halt programs []
  117. --enable-shutdown Build shutdown, poweroff, reboot, halt programs [Enabled only on Linux systems]
  118. --disable-shutdown Don't build shutdown, poweroff, reboot, halt programs
  119. --enable-cgroups Enable Cgroups support [Enabled only on Linux based systems]
  120. --disable-cgroups Disable Cgroups support
  121. --enable-utmpx Enable manipulating the utmp/utmpx database via the related POSIX functions [auto]
  122. --disable-utmpx Disable manipulating the utmp/utmpx database via the related POSIX functions
  123. Build variables:
  124. Note: build variables can be passed in the environment, or as $0 argument (as "var=VALUE").
  125. Note: CXXFLAGS, TEST_CXXFLAGS, LDFLAGS and TEST_LDFLAGS by default will be set to options considered suitable
  126. for the platform, filtered to remove any options not supported by the compiler/linker. To disable this,
  127. specify the values explicitly (an empty string is accepted). To add options without removing the defaults,
  128. set the variable with _EXTRA appended to the name (eg CXXFLAGS_EXTRA).
  129. CXX C++ compiler
  130. CXX_FOR_BUILD C++ compiler generating code for the build system (for cross-compiles).
  131. CXXFLAGS_FOR_BUILD Flags to use when generating code for the build system.
  132. Defaults to the value of CXXFLAGS.
  133. CPPFLAGS_FOR_BUILD Preprocessor flags to use when generating code for the build system.
  134. Defaults to the value of CPPFLAGS.
  135. LDFLAGS_FOR_BUILD Link flags to use when generating code for the build system.
  136. Defaults to the value of LDFLAGS.
  137. CXXFLAGS Flags to use when compiling C++ code.
  138. CXXFLAGS_EXTRA Additional flags to use when compiling C++ code.
  139. TEST_CXXFLAGS Flags to use when compiling C++ code in tests.
  140. TEST_CXXFLAGS_EXTRA Additional flags to use when compiling C++ code in tests.
  141. CPPFLAGS Preprocessor flags to use when compiling C++ code.
  142. LDFLAGS Link flags.
  143. LDFLAGS_EXTRA Additional link flags.
  144. TEST_LDFLAGS Links flags when building test executables.
  145. TEST_LDFLAGS_EXTRA Additional link flags when building test executables.
  146. Note: paths specified via --prefix/--exec-prefix/--sbindir/--mandir, and build variable values, must be
  147. escaped for use in a makefile and in shell commands. If there are spaces in paths it is recommended to
  148. prepend a backslash (\) to them.
  149. For example: ./configure --prefix="/home/my\ home"
  150. See BUILD file for more information.
  151. _EOF
  152. exit 0
  153. }
  154. ## Don't take values from environment for these variables:
  155. for var in PREFIX \
  156. EPREFIX \
  157. SBINDIR \
  158. MANDIR \
  159. NO_SANITIZE \
  160. SHUTDOWN_PREFIX \
  161. BUILD_SHUTDOWN \
  162. SUPPORT_CGROUPS \
  163. SYSCONTROLSOCKET
  164. do
  165. unset $var
  166. done
  167. ## Flag parser
  168. for arg in "$@"; do
  169. case "$arg" in
  170. --help) usage ;;
  171. --quiet)
  172. info() { true; }
  173. sub_info() { true; }
  174. warning() { true; }
  175. sub_warning() { true; }
  176. ;;
  177. --clean) rm -f test* & rm -f mconfig && exit 0 ;;
  178. --platform=*) PLATFORM="${arg#*=}" && shift ;;
  179. --prefix=*) PREFIX="${arg#*=}" && shift ;;
  180. --exec-prefix=*) EPREFIX="${arg#*=}" && shift;;
  181. --sbindir=*) SBINDIR="${arg#*=}" && shift ;;
  182. --mandir=*) MANDIR="${arg#*=}" && shift ;;
  183. --syscontrolsocket=*) SYSCONTROLSOCKET="${arg#*=}" && shift ;;
  184. --shutdown-prefix=*) SHUTDOWN_PREFIX="${arg#*=}" && shift ;;
  185. --enable-shutdown|--enable-shutdown=yes) BUILD_SHUTDOWN=yes ;;
  186. --disable-shutdown|--enable-shutdown=no) BUILD_SHUTDOWN=no ;;
  187. --enable-cgroups|--enable-cgroups=yes) SUPPORT_CGROUPS=1 ;;
  188. --disable-cgroups|--enable-cgroups=no) SUPPORT_CGROUPS=0 ;;
  189. --enable-utmpx|--enable-utmpx=yes) USE_UTMPX=1 ;;
  190. --disable-utmpx|--enable-utmpx=no) USE_UTMPX=0 ;;
  191. CXX=*|CXX_FOR_BUILD=*|CXXFLAGS_FOR_BUILD=*|CPPFLAGS_FOR_BUILD=*\
  192. |LDFLAGS_FOR_BUILD=*|CXXFLAGS=*|CXXFLAGS_EXTRA=*|TEST_CXXFLAGS=*\
  193. |TEST_CXXFLAGS_EXTRA|LDFLAGS=*|LDFLAGS_EXTRA=*|TEST_LDFLAGS=*\
  194. |TEST_LDFLAGS_EXTRA=*|CPPFLAGS=*) eval "${arg%%=*}=\${arg#*=}" ;;
  195. *=*) warning Unknown variable: "${arg%%=*}" ;;
  196. *) warning Unknown argument: "$arg" ;;
  197. esac
  198. done
  199. ## Defaults for variables not specified by user
  200. : "${PLATFORM:=$(uname)}"
  201. : "${PREFIX:="/usr"}"
  202. : "${EPREFIX:="/"}"
  203. : "${SBINDIR:="${EPREFIX%%/}/sbin"}"
  204. : "${MANDIR:="${PREFIX%%/}/share/man"}"
  205. : "${NO_SANITIZE:="auto"}"
  206. : "${SHUTDOWN_PREFIX:=""}"
  207. : "${CXXFLAGS_EXTRA:=""}"
  208. : "${TEST_CXXFLAGS_EXTRA:=""}"
  209. : "${CPPFLAGS:=""}"
  210. : "${LDFLAGS_EXTRA:=""}"
  211. : "${TEST_LDFLAGS_EXTRA:=""}"
  212. if [ "$PLATFORM" = "Linux" ]; then
  213. : "${BUILD_SHUTDOWN:="yes"}"
  214. : "${SUPPORT_CGROUPS:="1"}"
  215. : "${SYSCONTROLSOCKET:="/run/dinitctl"}"
  216. else
  217. : "${BUILD_SHUTDOWN:="no"}"
  218. : "${SUPPORT_CGROUPS:="0"}"
  219. : "${SYSCONTROLSOCKET:="/var/run/dinitctl"}"
  220. fi
  221. ## Finalize $CXXFLAGS, $TEST_CXXFLAGS, $LDFLAGS, $TEST_LDFLAGS
  222. if [ -z "${CXXFLAGS+IS_SET}" ]; then
  223. CXXFLAGS=""
  224. AUTO_CXXFLAGS="true"
  225. else
  226. AUTO_CXXFLAGS="false"
  227. fi
  228. if [ -z "${TEST_CXXFLAGS+IS_SET}" ]; then
  229. TEST_CXXFLAGS=""
  230. AUTO_TEST_CXXFLAGS="true"
  231. else
  232. AUTO_TEST_CXXFLAGS="false"
  233. fi
  234. if [ -z "${LDFLAGS+IS_SET}" ]; then
  235. LDFLAGS=""
  236. AUTO_LDFLAGS="true"
  237. else
  238. AUTO_LDFLAGS="false"
  239. fi
  240. if [ -z "${TEST_LDFLAGS+IS_SET}" ]; then
  241. TEST_LDFLAGS=""
  242. AUTO_TEST_LDFLAGS="true"
  243. else
  244. AUTO_TEST_LDFLAGS="false"
  245. fi
  246. ## Verify PLATFORM value
  247. if [ "$PLATFORM" != "Linux" ] && [ "$PLATFORM" != "FreeBSD" ] && \
  248. [ "$PLATFORM" != "OpenBSD" ] && [ "$PLATFORM" != "Darwin" ]; then
  249. warning "$PLATFORM" platform is unknown!
  250. sub_warning Known Platforms are: Linux, FreeBSD, OpenBSD, Darwin
  251. fi
  252. ## Create testfile.cc to test c++ compiler
  253. echo "int main(int argc, char **argv) { return 0; }" > testfile.cc || error Can\'t create temporary file
  254. ## Find and test C++ compiler
  255. if [ -z "${CXX:-}" ]; then
  256. info Checking C++ compiler...
  257. for guess in g++ clang++ c++; do
  258. if type "$guess" > /dev/null 2>&1; then
  259. CXX="$guess"
  260. sub_info "$CXX"
  261. break # Found
  262. fi
  263. done
  264. if [ -z "${CXX:-}" ]; then
  265. sub_error No C++ compiler found! # Not found
  266. fi
  267. fi
  268. cxx_works "$CXX"
  269. if [ -n "${CXX_FOR_BUILD:-}" ]; then
  270. cxx_works "$CXX_FOR_BUILD"
  271. fi
  272. ## Test compiler/linker supported arguments
  273. if [ "$AUTO_CXXFLAGS" = "true" ]; then
  274. for argument in -std=c++11 \
  275. -Wall \
  276. -Os \
  277. -fno-plt
  278. do
  279. try_cxx_argument CXXFLAGS $argument
  280. done
  281. if [ "$PLATFORM" != "Darwin" ]; then
  282. try_cxx_argument CXXFLAGS -fno-rtti
  283. fi
  284. fi
  285. if [ "$AUTO_LDFLAGS" = "true" ] && [ "$AUTO_CXXFLAGS" = "true" ]; then
  286. try_both_argument CXXFLAGS LDFLAGS -flto
  287. fi
  288. if [ "$AUTO_LDFLAGS" = "true" ] && [ "$PLATFORM" = "FreeBSD" ]; then
  289. try_ld_argument LDFLAGS -lrt
  290. fi
  291. # Add $CXXFLAGS and $LDFLAGS to $TEST_{LD,CXX}FLAGS when AUTO_TEST vars are true
  292. if [ "$AUTO_TEST_CXXFLAGS" = "true" ]; then
  293. TEST_CXXFLAGS="$CXXFLAGS"
  294. fi
  295. if [ "$AUTO_TEST_LDFLAGS" = "true" ]; then
  296. TEST_LDFLAGS="$LDFLAGS"
  297. fi
  298. if [ "$AUTO_TEST_LDFLAGS" = "true" ] && [ "$AUTO_TEST_CXXFLAGS" = "true" ]; then
  299. try_both_argument TEST_CXXFLAGS TEST_LDFLAGS -fsanitize=address,undefined
  300. fi
  301. ## Create mconfig
  302. rm -f testfile*
  303. info Creating mconfig...
  304. cat << _EOF > mconfig
  305. ## Auto-generated by "$0" for "$PLATFORM"
  306. # All changes will be lost if "$0" is re-run.
  307. # See BUILD for help on all variables.
  308. # Installation path options.
  309. SBINDIR=$SBINDIR
  310. MANDIR=$MANDIR
  311. SYSCONTROLSOCKET=$SYSCONTROLSOCKET
  312. # General build options.
  313. CXX=$CXX
  314. CXXFLAGS=$CXXFLAGS
  315. CXXFLAGS_EXTRA=$CXXFLAGS_EXTRA
  316. TEST_CXXFLAGS=$TEST_CXXFLAGS
  317. TEST_CXXFLAGS_EXTRA=$TEST_CXXFLAGS_EXTRA
  318. CPPFLAGS=$CPPFLAGS
  319. LDFLAGS=$LDFLAGS
  320. LDFLAGS_EXTRA=$LDFLAGS_EXTRA
  321. TEST_LDFLAGS=$TEST_LDFLAGS
  322. TEST_LDFLAGS_EXTRA=$TEST_LDFLAGS_EXTRA
  323. BUILD_SHUTDOWN=$BUILD_SHUTDOWN
  324. # Feature settings
  325. SUPPORT_CGROUPS=$SUPPORT_CGROUPS
  326. # Optional settings
  327. SHUTDOWN_PREFIX=${SHUTDOWN_PREFIX:-}
  328. _EOF
  329. if [ -n "${USE_UTMPX:-}" ]; then
  330. echo "USE_UTMPX=$USE_UTMPX" >> mconfig
  331. fi
  332. if [ -n "${CXX_FOR_BUILD:-}" ]; then
  333. {
  334. echo ""
  335. echo "# For cross-compiling"
  336. echo "CXX_FOR_BUILD=$CXX_FOR_BUILD"
  337. } >> mconfig
  338. fi
  339. if [ -n "${CXXFLAGS_FOR_BUILD:-}" ]; then
  340. echo "CXXFLAGS_FOR_BUILD=$CXXFLAGS_FOR_BUILD" >> mconfig
  341. fi
  342. if [ -n "${CPPFLAGS_FOR_BUILD:-}" ]; then
  343. echo "CPPFLAGS_FOR_BUILD=$CPPFLAGS_FOR_BUILD" >> mconfig
  344. fi
  345. if [ -n "${LDFLAGS_FOR_BUILD:-}" ]; then
  346. echo "LDFLAGS_FOR_BUILD=$LDFLAGS_FOR_BUILD" >> mconfig
  347. fi
  348. sub_info done.
  349. info Done!
  350. exit 0