configure 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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_info Yes.
  51. else
  52. rm -f testfile*
  53. sub_error It seems like \""$CXX"\" is not working a working C++ compiler. Please specify compiler.
  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. --enable-initgroups Enable initialization of supplementary groups for run-as [default]
  124. --disable-initgroups Disable initialization of supplementary groups for run-as
  125. Build variables:
  126. Note: build variables can be passed in the environment, or as $0 argument (as "var=VALUE").
  127. Note: CXXFLAGS, TEST_CXXFLAGS, LDFLAGS and TEST_LDFLAGS by default will be set to options considered suitable
  128. for the platform, filtered to remove any options not supported by the compiler/linker. To disable this,
  129. specify the values explicitly (an empty string is accepted). To add options without removing the defaults,
  130. set the variable with _EXTRA appended to the name (eg CXXFLAGS_EXTRA).
  131. CXX C++ compiler
  132. CXX_FOR_BUILD C++ compiler generating code for the build system (for cross-compiles).
  133. CXXFLAGS_FOR_BUILD Flags to use when generating code for the build system.
  134. Defaults to the value of CXXFLAGS.
  135. CPPFLAGS_FOR_BUILD Preprocessor flags to use when generating code for the build system.
  136. Defaults to the value of CPPFLAGS.
  137. LDFLAGS_FOR_BUILD Link flags to use when generating code for the build system.
  138. Defaults to the value of LDFLAGS.
  139. CXXFLAGS Flags to use when compiling C++ code.
  140. CXXFLAGS_EXTRA Additional flags to use when compiling C++ code.
  141. TEST_CXXFLAGS Flags to use when compiling C++ code in tests.
  142. TEST_CXXFLAGS_EXTRA Additional flags to use when compiling C++ code in tests.
  143. CPPFLAGS Preprocessor flags to use when compiling C++ code.
  144. LDFLAGS Link flags.
  145. LDFLAGS_EXTRA Additional link flags.
  146. TEST_LDFLAGS Links flags when building test executables.
  147. TEST_LDFLAGS_EXTRA Additional link flags when building test executables.
  148. Note: paths specified via --prefix/--exec-prefix/--sbindir/--mandir, and build variable values, must be
  149. escaped for use in a makefile and in shell commands. If there are spaces in paths it is recommended to
  150. prepend a backslash (\) to them.
  151. For example: ./configure --prefix="/home/my\ home"
  152. See BUILD file for more information.
  153. _EOF
  154. exit 0
  155. }
  156. ## Don't take values from environment for these variables:
  157. for var in PREFIX \
  158. EPREFIX \
  159. SBINDIR \
  160. MANDIR \
  161. SHUTDOWN_PREFIX \
  162. BUILD_SHUTDOWN \
  163. SUPPORT_CGROUPS \
  164. USE_UTMPX \
  165. USE_INITGROUPS \
  166. SYSCONTROLSOCKET
  167. do
  168. unset $var
  169. done
  170. ## Flag parser
  171. for arg in "$@"; do
  172. case "$arg" in
  173. --help) usage ;;
  174. --quiet)
  175. info() { true; }
  176. sub_info() { true; }
  177. warning() { true; }
  178. sub_warning() { true; }
  179. ;;
  180. --clean) rm -f test* & rm -f mconfig && exit 0 ;;
  181. --platform=*) PLATFORM="${arg#*=}" && shift ;;
  182. --prefix=*) PREFIX="${arg#*=}" && shift ;;
  183. --exec-prefix=*) EPREFIX="${arg#*=}" && shift;;
  184. --sbindir=*) SBINDIR="${arg#*=}" && shift ;;
  185. --mandir=*) MANDIR="${arg#*=}" && shift ;;
  186. --syscontrolsocket=*) SYSCONTROLSOCKET="${arg#*=}" && shift ;;
  187. --shutdown-prefix=*) SHUTDOWN_PREFIX="${arg#*=}" && shift ;;
  188. --enable-shutdown|--enable-shutdown=yes) BUILD_SHUTDOWN=yes ;;
  189. --disable-shutdown|--enable-shutdown=no) BUILD_SHUTDOWN=no ;;
  190. --enable-cgroups|--enable-cgroups=yes) SUPPORT_CGROUPS=1 ;;
  191. --disable-cgroups|--enable-cgroups=no) SUPPORT_CGROUPS=0 ;;
  192. --enable-utmpx|--enable-utmpx=yes) USE_UTMPX=1 ;;
  193. --disable-utmpx|--enable-utmpx=no) USE_UTMPX=0 ;;
  194. --enable-initgroups|--enable-initgroups=yes) USE_INITGROUPS=1 ;;
  195. --disable-initgroups|--enable-initgroups=no) USE_INITGROUPS=0 ;;
  196. CXX=*|CXX_FOR_BUILD=*|CXXFLAGS_FOR_BUILD=*|CPPFLAGS_FOR_BUILD=*\
  197. |LDFLAGS_FOR_BUILD=*|CXXFLAGS=*|CXXFLAGS_EXTRA=*|TEST_CXXFLAGS=*\
  198. |TEST_CXXFLAGS_EXTRA|LDFLAGS=*|LDFLAGS_EXTRA=*|TEST_LDFLAGS=*\
  199. |TEST_LDFLAGS_EXTRA=*|CPPFLAGS=*) eval "${arg%%=*}=\${arg#*=}" ;;
  200. *=*) warning Unknown variable: "${arg%%=*}" ;;
  201. *) warning Unknown argument: "$arg" ;;
  202. esac
  203. done
  204. ## Defaults for variables not specified by user
  205. : "${PLATFORM:=$(uname)}"
  206. : "${PREFIX:="/usr"}"
  207. : "${EPREFIX:="/"}"
  208. : "${SBINDIR:="${EPREFIX%%/}/sbin"}"
  209. : "${MANDIR:="${PREFIX%%/}/share/man"}"
  210. : "${SHUTDOWN_PREFIX:=""}"
  211. : "${CXXFLAGS_EXTRA:=""}"
  212. : "${TEST_CXXFLAGS_EXTRA:=""}"
  213. : "${CPPFLAGS:=""}"
  214. : "${LDFLAGS_EXTRA:=""}"
  215. : "${TEST_LDFLAGS_EXTRA:=""}"
  216. if [ "$PLATFORM" = "Linux" ]; then
  217. : "${BUILD_SHUTDOWN:="yes"}"
  218. : "${SUPPORT_CGROUPS:="1"}"
  219. : "${SYSCONTROLSOCKET:="/run/dinitctl"}"
  220. else
  221. : "${BUILD_SHUTDOWN:="no"}"
  222. : "${SUPPORT_CGROUPS:="0"}"
  223. : "${SYSCONTROLSOCKET:="/var/run/dinitctl"}"
  224. fi
  225. ## Finalize $CXXFLAGS, $TEST_CXXFLAGS, $LDFLAGS, $TEST_LDFLAGS
  226. if [ -z "${CXXFLAGS+IS_SET}" ]; then
  227. CXXFLAGS=""
  228. AUTO_CXXFLAGS="true"
  229. else
  230. AUTO_CXXFLAGS="false"
  231. fi
  232. if [ -z "${TEST_CXXFLAGS+IS_SET}" ]; then
  233. TEST_CXXFLAGS=""
  234. AUTO_TEST_CXXFLAGS="true"
  235. else
  236. AUTO_TEST_CXXFLAGS="false"
  237. fi
  238. if [ -z "${LDFLAGS+IS_SET}" ]; then
  239. LDFLAGS=""
  240. AUTO_LDFLAGS="true"
  241. else
  242. AUTO_LDFLAGS="false"
  243. fi
  244. if [ -z "${TEST_LDFLAGS+IS_SET}" ]; then
  245. TEST_LDFLAGS=""
  246. AUTO_TEST_LDFLAGS="true"
  247. else
  248. AUTO_TEST_LDFLAGS="false"
  249. fi
  250. ## Verify PLATFORM value
  251. if [ "$PLATFORM" != "Linux" ] && [ "$PLATFORM" != "FreeBSD" ] && \
  252. [ "$PLATFORM" != "OpenBSD" ] && [ "$PLATFORM" != "Darwin" ]; then
  253. warning "$PLATFORM" platform is unknown!
  254. sub_warning Known Platforms are: Linux, FreeBSD, OpenBSD, Darwin
  255. fi
  256. ## Create testfile.cc to test c++ compiler
  257. echo "int main(int argc, char **argv) { return 0; }" > testfile.cc || error Can\'t create temporary file
  258. ## Find and test C++ compiler
  259. if [ -z "${CXX:-}" ]; then
  260. info Checking C++ compiler...
  261. for guess in g++ clang++ c++; do
  262. if type "$guess" > /dev/null 2>&1; then
  263. CXX="$guess"
  264. sub_info "$CXX"
  265. break # Found
  266. fi
  267. done
  268. if [ -z "${CXX:-}" ]; then
  269. sub_error No C++ compiler found! # Not found
  270. fi
  271. fi
  272. cxx_works "$CXX"
  273. if [ -n "${CXX_FOR_BUILD:-}" ]; then
  274. cxx_works "$CXX_FOR_BUILD"
  275. fi
  276. ## Test compiler/linker supported arguments
  277. if [ "$AUTO_CXXFLAGS" = "true" ]; then
  278. for argument in -std=c++11 \
  279. -Wall \
  280. -Os \
  281. -fno-plt
  282. do
  283. try_cxx_argument CXXFLAGS $argument
  284. done
  285. if [ "$PLATFORM" != "Darwin" ]; then
  286. try_cxx_argument CXXFLAGS -fno-rtti
  287. fi
  288. fi
  289. if [ "$AUTO_LDFLAGS" = "true" ] && [ "$AUTO_CXXFLAGS" = "true" ]; then
  290. try_both_argument CXXFLAGS LDFLAGS -flto
  291. fi
  292. if [ "$AUTO_LDFLAGS" = "true" ] && [ "$PLATFORM" = "FreeBSD" ]; then
  293. try_ld_argument LDFLAGS -lrt
  294. fi
  295. # Add $CXXFLAGS and $LDFLAGS to $TEST_{LD,CXX}FLAGS when AUTO_TEST vars are true
  296. if [ "$AUTO_TEST_CXXFLAGS" = "true" ]; then
  297. TEST_CXXFLAGS="$CXXFLAGS"
  298. fi
  299. if [ "$AUTO_TEST_LDFLAGS" = "true" ]; then
  300. TEST_LDFLAGS="$LDFLAGS"
  301. fi
  302. if [ "$AUTO_TEST_LDFLAGS" = "true" ] && [ "$AUTO_TEST_CXXFLAGS" = "true" ]; then
  303. try_both_argument TEST_CXXFLAGS TEST_LDFLAGS -fsanitize=address,undefined
  304. fi
  305. ## Create mconfig
  306. rm -f testfile*
  307. info Creating mconfig...
  308. cat << _EOF > mconfig
  309. ## Auto-generated by "$0" for "$PLATFORM"
  310. # All changes will be lost if "$0" is re-run.
  311. # See BUILD for help on all variables.
  312. # Installation path options.
  313. SBINDIR=$SBINDIR
  314. MANDIR=$MANDIR
  315. SYSCONTROLSOCKET=$SYSCONTROLSOCKET
  316. # General build options.
  317. CXX=$CXX
  318. CXXFLAGS=$CXXFLAGS
  319. CXXFLAGS_EXTRA=$CXXFLAGS_EXTRA
  320. TEST_CXXFLAGS=$TEST_CXXFLAGS
  321. TEST_CXXFLAGS_EXTRA=$TEST_CXXFLAGS_EXTRA
  322. CPPFLAGS=$CPPFLAGS
  323. LDFLAGS=$LDFLAGS
  324. LDFLAGS_EXTRA=$LDFLAGS_EXTRA
  325. TEST_LDFLAGS=$TEST_LDFLAGS
  326. TEST_LDFLAGS_EXTRA=$TEST_LDFLAGS_EXTRA
  327. BUILD_SHUTDOWN=$BUILD_SHUTDOWN
  328. # Feature settings
  329. SUPPORT_CGROUPS=$SUPPORT_CGROUPS
  330. # Optional settings
  331. SHUTDOWN_PREFIX=${SHUTDOWN_PREFIX:-}
  332. _EOF
  333. if [ -n "${USE_UTMPX:-}" ]; then
  334. echo "USE_UTMPX=$USE_UTMPX" >> mconfig
  335. fi
  336. if [ -n "${USE_INITGROUPS:-}" ]; then
  337. echo "USE_INITGROUPS=$USE_INITGROUPS" >> mconfig
  338. fi
  339. if [ -n "${CXX_FOR_BUILD:-}" ]; then
  340. {
  341. echo ""
  342. echo "# For cross-compiling"
  343. echo "CXX_FOR_BUILD=$CXX_FOR_BUILD"
  344. } >> mconfig
  345. fi
  346. if [ -n "${CXXFLAGS_FOR_BUILD:-}" ]; then
  347. echo "CXXFLAGS_FOR_BUILD=$CXXFLAGS_FOR_BUILD" >> mconfig
  348. fi
  349. if [ -n "${CPPFLAGS_FOR_BUILD:-}" ]; then
  350. echo "CPPFLAGS_FOR_BUILD=$CPPFLAGS_FOR_BUILD" >> mconfig
  351. fi
  352. if [ -n "${LDFLAGS_FOR_BUILD:-}" ]; then
  353. echo "LDFLAGS_FOR_BUILD=$LDFLAGS_FOR_BUILD" >> mconfig
  354. fi
  355. sub_info done.
  356. info Done!
  357. exit 0