configure 13 KB

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