commit 2a142eb70804f1edb7d4d777aacaf78b2239ea1c
parent a50d5c0b5381914b6ac8370e23a26292efb97d7d
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Tue, 31 Mar 2026 05:16:07 +0200
Implemented detection and use of LTO
Diffstat:
| M | configure.ac | | | 90 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ |
1 file changed, 84 insertions(+), 6 deletions(-)
diff --git a/configure.ac b/configure.ac
@@ -82,9 +82,9 @@ AS_IF([test -z "$CC" && test -z "$CPP"], [
AC_MSG_CHECKING([for build type])
AC_ARG_ENABLE([build-type],
[AS_HELP_STRING([[--enable-build-type=TYPE]],
- [enable build TYPE, a set of configuration parameters; individual settings ]
- [(asserts, sanitizers, compiler and linker flags) can be overridden by ]
- [additional configure parameters (debug, debugger, trace, neutral, release, ]
+ [enable build TYPE, a set of configuration parameters; individual settings ]dnl
+ [(asserts, sanitizers, compiler and linker flags) can be overridden by ]dnl
+ [additional configure parameters (debug, debugger, trace, neutral, release, ]dnl
[release-compact, release-hardened) [neutral]])],
[], [enable_build_type=neutral])
AS_IF([test "x${enable_build_type}" = "x"], [enable_build_type="neutral"])
@@ -95,8 +95,8 @@ AS_CASE([${enable_build_type}],
[trace], [AC_MSG_RESULT([trace. Defaults: enable asserts, debug prints, debug information, compiler optimisation for debugging])],
[debugger], [AC_MSG_RESULT([debugger. Defaults: enable asserts, debug information, no compiler optimisation, static lib])],
[neutral], [AC_MSG_RESULT([neutral. Defaults: use only user-specified compiler and linker flags])],
- [release], [AC_MSG_RESULT([release. Defaults: disable asserts, enable compiler optimisations])],
- [release-compact], [AC_MSG_RESULT([release-compact. Defaults: disable asserts, enable compiler optimisations for size, enable compact code])],
+ [release], [AC_MSG_RESULT([release. Defaults: disable asserts, enable compiler optimisations, LTO])],
+ [release-compact], [AC_MSG_RESULT([release-compact. Defaults: disable asserts, enable compiler optimisations for size, enable compact code, LTO])],
[release-hardened], [AC_MSG_RESULT([release-hardened. Defaults: disable asserts, enable compiler optimisations, enable linker and compiler hardening])],
[AC_MSG_ERROR([[Unknown build type: ${enable_build_type}]])]
)
@@ -349,7 +349,7 @@ MHD_CHECK_ADD_CC_CFLAG([-qlonglong], [CFLAGS_ac])
# Set basic optimisation flags
AS_VAR_IF([enable_build_type],["neutral"],[],
[ # Any non-neutral build types
- AC_CACHE_CHECK([whether workarounds for clang or clang-based compiler are required],
+ AC_CACHE_CHECK([whether $CC is clang or clang-based],
[mhd_cv_cc_clang_based],
[
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
@@ -685,6 +685,83 @@ MHD_CHECK_CC_CFLAG([-Wc++-compat],[CFLAGS_ac],
CFLAGS="${CFLAGS_ac} ${user_CFLAGS}"
# Additional flags are checked and added at the end of 'configure'
+AC_ARG_ENABLE([lto],[AS_HELP_STRING([[--enable-lto[=ARG]]],[add Link Time Optimisation compiler flags (yes, no, auto) [auto]])],
+ [
+ AS_CASE([${enableval}],
+ [yes|no|auto],[],
+ [AC_MSG_ERROR([invalid parameter --enable-lto=${enableval}])]
+ )
+ ],[enable_lto=auto]
+)
+
+AS_VAR_IF([enable_lto],["auto"],
+ [AS_CASE([${enable_build_type}],[release|release-compact],[],[enable_lto=no])]
+)
+
+AS_VAR_IF([enable_lto],["no"],[use_lto="no"],
+ [
+ CFLAGS="${CFLAGS_ac} ${user_CFLAGS}"
+ LDFLAGS="${LDFLAGS_ac} ${user_LDFLAGS}"
+ lto_CFLAGS=""
+ lto_LDFLAGS=""
+ use_lto=""
+ AC_CACHE_CHECK([whether $CC is clang or clang-based],
+ [mhd_cv_cc_clang_based],
+ [
+ AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
+#if ! defined(__clang__) && ! defined(__llvm__)
+#error Compiler is not clang-based
+fail test here %%%@<:@-1@:>@
+#endif
+ ]]
+ )
+ ],
+ [mhd_cv_cc_clang_based="yes"],[mhd_cv_cc_clang_based="no"]
+ )
+ ]
+ )
+ AS_VAR_IF([mhd_cv_cc_clang_based],["yes"],
+ [MHD_CHECK_ADD_CC_LDFLAG([-fuse-ld=lld],[lto_LDFLAGS])]
+ )
+ LDFLAGS="${lto_LDFLAGS} ${LDFLAGS_ac} ${user_LDFLAGS}"
+ MHD_FIND_ADD_CC_CFLAG_IFELSE(
+ [
+ use_lto="yes"
+ MHD_PREPEND_FLAG_TO_VAR([lto_LDFLAGS],[$mhd_cc_found_flag])
+ ],[use_lto="no"],[lto_CFLAGS],
+ [-flto=auto],[-flto=full],[-flto]
+ )
+ LDFLAGS="${lto_LDFLAGS} ${LDFLAGS_ac} ${user_LDFLAGS}"
+ AS_VAR_IF([use_lto],["yes"],
+ [
+ MHD_CHECK_ADD_CC_CFLAG([-ffat-lto-objects],[lto_CFLAGS],
+ [],
+ [
+ test "X${enable_static}" = "Xyes" && test "X${enable_lto}" = "Xauto" && use_lto="no"
+ ]
+ )
+ ]
+ )
+ AS_VAR_IF([use_lto],["yes"],
+ [MHD_FIND_ADD_CC_CFLAG([lto_CFLAGS],[-flto-partition=one],[-flto-partition=none])]
+ )
+ AS_VAR_IF([use_lto],["yes"],
+ [
+ MHD_PREPEND_FLAG_TO_VAR([CFLAGS_ac],[${lto_CFLAGS}])
+ MHD_PREPEND_FLAG_TO_VAR([LDFLAGS_ac],[${lto_LDFLAGS}])
+ ],
+ [
+ AS_VAR_IF([enable_lto],["yes"],
+ [AC_MSG_ERROR([LTO cannot be enabled, but requested by --enable-lto])]
+ )
+ ]
+ )
+ LDFLAGS="${LDFLAGS_ac} ${user_LDFLAGS}"
+ CFLAGS="${CFLAGS_ac} ${user_CFLAGS}"
+ ]
+)
+
+
# Check for headers that are ALWAYS required
AC_CHECK_HEADERS_ONCE([stdio.h string.h stdint.h stdarg.h errno.h limits.h fcntl.h], [],
[AC_MSG_ERROR([Compiling libmicrohttpd requires standard C and POSIX headers files])], [AC_INCLUDES_DEFAULT])
@@ -9503,6 +9580,7 @@ AC_MSG_NOTICE([[${PACKAGE_NAME} ${PACKAGE_VERSION} Configuration Summary:
[ Code build options ]
Compact code : ${enable_compact_code} (${compact_code_MSG})
+ LTO flags : ${use_lto}
Use thread names : ${enable_thread_names}
Use debug asserts: ${use_asserts_MSG=no}
Use sanitizers : ${enabled_sanitizers:=no}