Skip to content

Commit 08bb69f

Browse files
committed
build-sys: enable LTO unless --enable-debugging is given
Close #2885. Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 1ac739f commit 08bb69f

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ noinst_LIBRARIES += libutil.a
6262

6363
noinst_PROGRAMS = utiltest
6464

65+
AM_LDFLAGS = $(EXTRA_LDFLAGS)
66+
6567
# packcc always uses native compiler even when cross-compiling.
6668
# packcc cannot use the standard Automake rule.
6769
PACKCC_CPPFLAGS_FOR_BUILD = -fsigned-char

configure.ac

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,49 @@ PRETTY_ARG_VAR([EXTRA_CFLAGS], [extra C compiler flags],
568568
PRETTY_ARG_VAR([WARNING_CFLAGS], [C compiler warning flags],
569569
[-Wall])
570570

571+
PRETTY_ARG_VAR([EXTRA_LDFLAGS], [extra linker flags],
572+
[])
573+
574+
dnl Enable LTO unless debugging is enabled.
575+
dnl ref. https://stackoverflow.com/questions/40374061/autoconf-recipe-to-use-gcc-ar-and-gcc-ranlib
576+
use_lto=no
577+
AS_IF(test "x$cross_compiling" != "xyes"],[
578+
AX_CHECK_COMPILE_FLAG(-flto,[use_lto=yes])])
579+
AS_VAR_IF([use_lto], [yes], [
580+
AS_CASE($CC,
581+
[gcc|*/gcc], [AC_CHECK_PROGS([LTO_AR], [gcc-ar ar], [:])],
582+
[clang|*/clang], [AC_CHECK_PROGS([LTO_AR], [clang-ar ar], [:])],
583+
[use_lto=no])
584+
AC_MSG_CHECKING([the wrapper for ar working with $CC -flto])
585+
AS_VAR_IF([LTO_AR], [:], AC_MSG_ERROR([could not find AR tool working with -flto.]))
586+
AS_VAR_IF([use_lto], [yes],
587+
[AS_CASE($LTO_AR,
588+
[*-ar],[AC_MSG_RESULT([$LTO_AR])],
589+
[use_lto=no
590+
AC_MSG_RESULT([no suitable AR tool is found; truning off LTO])])],
591+
[AC_MSG_RESULT([no suitable AR tool is found; truning off LTO])])
592+
])
593+
AS_VAR_IF([use_lto], [yes], [
594+
AS_CASE($CC,
595+
[gcc|*/gcc], [AC_CHECK_PROGS([LTO_RANLIB], [gcc-ranlib ranlib], [:])],
596+
[clang|*/clang], [AC_CHECK_PROGS([LTO_RANLIB], [clang-ranlib ranlib], [:])],
597+
[use_lto=no])
598+
AC_MSG_CHECKING([the wrapper for ranlib working with $CC -flto])
599+
AS_VAR_IF([LTO_RANLIB], [:], AC_MSG_ERROR([could not find RANLIB tool working with -flto.]))
600+
AS_VAR_IF([use_lto], [yes],
601+
[AS_CASE($LTO_RANLIB,
602+
[*-ranlib],[AC_MSG_RESULT([$LTO_RANLIB])],
603+
[use_lto=no
604+
AC_MSG_RESULT([no suitable ranlib tool is found; truning off LTO])])],
605+
[AC_MSG_RESULT([no suitable ranlib is found; truning off LTO])])
606+
])
607+
AS_VAR_IF([use_lto], [yes], [
608+
AR="$LTO_AR"
609+
RANLIB="$LTO_RANLIB"
610+
EXTRA_CFLAGS="$EXTRA_CFLAGS -flto"
611+
EXTRA_LDFLAGS="$EXTRA_LDFLAGS -flto"
612+
])
613+
571614
# Checks for function availability
572615
# -----------------------------------
573616

m4/ax_check_compile_flag.m4

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# ===========================================================================
2+
# https://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html
3+
# ===========================================================================
4+
#
5+
# SYNOPSIS
6+
#
7+
# AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT])
8+
#
9+
# DESCRIPTION
10+
#
11+
# Check whether the given FLAG works with the current language's compiler
12+
# or gives an error. (Warnings, however, are ignored)
13+
#
14+
# ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on
15+
# success/failure.
16+
#
17+
# If EXTRA-FLAGS is defined, it is added to the current language's default
18+
# flags (e.g. CFLAGS) when the check is done. The check is thus made with
19+
# the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to
20+
# force the compiler to issue an error when a bad flag is given.
21+
#
22+
# INPUT gives an alternative input source to AC_COMPILE_IFELSE.
23+
#
24+
# NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this
25+
# macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG.
26+
#
27+
# LICENSE
28+
#
29+
# Copyright (c) 2008 Guido U. Draheim <[email protected]>
30+
# Copyright (c) 2011 Maarten Bosmans <[email protected]>
31+
#
32+
# Copying and distribution of this file, with or without modification, are
33+
# permitted in any medium without royalty provided the copyright notice
34+
# and this notice are preserved. This file is offered as-is, without any
35+
# warranty.
36+
37+
#serial 6
38+
39+
AC_DEFUN([AX_CHECK_COMPILE_FLAG],
40+
[AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF
41+
AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl
42+
AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [
43+
ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS
44+
_AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1"
45+
AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])],
46+
[AS_VAR_SET(CACHEVAR,[yes])],
47+
[AS_VAR_SET(CACHEVAR,[no])])
48+
_AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags])
49+
AS_VAR_IF(CACHEVAR,yes,
50+
[m4_default([$2], :)],
51+
[m4_default([$3], :)])
52+
AS_VAR_POPDEF([CACHEVAR])dnl
53+
])dnl AX_CHECK_COMPILE_FLAGS

0 commit comments

Comments
 (0)