Skip to content

Commit b44fba3

Browse files
authored
Merge pull request #3922 from masatake/enable-lto
build-sys: enable LTO
2 parents a14ae7a + 28d7248 commit b44fba3

File tree

3 files changed

+109
-11
lines changed

3 files changed

+109
-11
lines changed

Makefile.am

Lines changed: 3 additions & 1 deletion
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
@@ -84,7 +86,7 @@ readtags_CPPFLAGS = -I. -I$(srcdir) -I$(srcdir)/main -I$(srcdir)/libreadtags
8486
if ENABLE_DEBUGGING
8587
readtags_CPPFLAGS+= $(DEBUG_CPPFLAGS)
8688
endif
87-
readtags_CFLAGS = $(COVERAGE_CFLAGS) $(WARNING_CFLAGS)
89+
readtags_CFLAGS = $(EXTRA_CFLAGS) $(WARNING_CFLAGS) $(COVERAGE_CFLAGS)
8890
dist_readtags_SOURCES = $(READTAGS_SRCS) $(READTAGS_HEADS)
8991
readtags_CPPFLAGS += $(GNULIB_CPPFLAGS)
9092
readtags_CPPFLAGS += -DREADTAGS_DSL -I$(srcdir)/dsl

configure.ac

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,6 @@ AC_ARG_ENABLE(readcmd,
182182
[do not include readtags command during install])],
183183
[], [enable_readcmd=yes])
184184

185-
# AC_ARG_WITH(perl-regex,
186-
# [AS_HELP_STRING([--with-perl-regex],
187-
# [use Perl pcre interface, if available])])
188-
189185
AC_ARG_ENABLE(etags,
190186
[AS_HELP_STRING([--enable-etags],
191187
[enable the installation of links for etags])])
@@ -224,6 +220,10 @@ AC_ARG_ENABLE([static],
224220
[AS_HELP_STRING([--enable-static],
225221
[enable static linking (mainly for MinGW)])])
226222

223+
AC_ARG_ENABLE([lto],
224+
[AS_HELP_STRING([--enable-lto],
225+
[enable link time optimization])])
226+
227227
AC_ARG_PROGRAM
228228

229229
# Process configuration options
@@ -264,7 +264,9 @@ AC_PROG_CC_C99
264264
# Typically, this is immediately after AC_PROG_CC, ...
265265
gl_EARLY
266266

267+
AC_ARG_VAR(RANLIB,[ranlib command or path])
267268
AC_PROG_RANLIB
269+
PRETTY_ARG_VAR([AR], [ar command or path], [ar])
268270
AC_C_BIGENDIAN
269271

270272
if test "$cross_compiling" = "yes"; then
@@ -324,15 +326,9 @@ AC_SUBST([LDFLAGS_FOR_BUILD])
324326
AC_SUBST([BUILD_OBJEXT])
325327
AC_SUBST([BUILD_EXEEXT])
326328

327-
AC_ARG_VAR(AR,[ar command or path])
328-
AC_ARG_VAR(RANLIB,[ranlib command or path])
329329
AC_ARG_VAR(WINDRES,[windres command or path, used with mingw-w64])
330330
AC_SUBST([WINDRES])
331331

332-
if test "x${AR}" = "x" ; then
333-
AR=ar
334-
fi
335-
336332
if test "x${WINDRES}" = "x" ; then
337333
WINDRES=windres
338334
fi
@@ -576,6 +572,53 @@ PRETTY_ARG_VAR([EXTRA_CFLAGS], [extra C compiler flags],
576572
PRETTY_ARG_VAR([WARNING_CFLAGS], [C compiler warning flags],
577573
[-Wall])
578574

575+
PRETTY_ARG_VAR([EXTRA_LDFLAGS], [extra linker flags],
576+
[])
577+
578+
dnl ref. https://stackoverflow.com/questions/40374061/autoconf-recipe-to-use-gcc-ar-and-gcc-ranlib
579+
use_lto=no
580+
enable_debugging
581+
AS_IF([test "x$cross_compiling" != "xyes" -a "x$enable_lto" != "xno"],[
582+
AX_CHECK_COMPILE_FLAG(-flto,[use_lto=yes])])
583+
AS_VAR_IF([use_lto], [yes], [
584+
AS_CASE($CC,
585+
[gcc|*/gcc], [AC_CHECK_PROGS([LTO_AR], [gcc-ar ar], [:])],
586+
[clang|*/clang], [AC_CHECK_PROGS([LTO_AR], [clang-ar ar], [:])],
587+
[use_lto=no])
588+
AC_MSG_CHECKING([the wrapper for ar working with $CC -flto])
589+
AS_VAR_IF([LTO_AR], [:], AC_MSG_ERROR([could not find AR tool working with -flto.]))
590+
AS_VAR_IF([use_lto], [yes],
591+
[AS_CASE($LTO_AR,
592+
[*-ar],[AC_MSG_RESULT([$LTO_AR])],
593+
[use_lto=no
594+
AC_MSG_RESULT([no suitable AR tool is found; truning off LTO])])],
595+
[AC_MSG_RESULT([no suitable AR tool is found; truning off LTO])])
596+
])
597+
AS_VAR_IF([use_lto], [yes], [
598+
AS_CASE($CC,
599+
[gcc|*/gcc], [AC_CHECK_PROGS([LTO_RANLIB], [gcc-ranlib ranlib], [:])],
600+
[clang|*/clang], [AC_CHECK_PROGS([LTO_RANLIB], [clang-ranlib ranlib], [:])],
601+
[use_lto=no])
602+
AC_MSG_CHECKING([the wrapper for ranlib working with $CC -flto])
603+
AS_VAR_IF([LTO_RANLIB], [:], AC_MSG_ERROR([could not find RANLIB tool working with -flto.]))
604+
AS_VAR_IF([use_lto], [yes],
605+
[AS_CASE($LTO_RANLIB,
606+
[*-ranlib],[AC_MSG_RESULT([$LTO_RANLIB])],
607+
[use_lto=no
608+
AC_MSG_RESULT([no suitable ranlib tool is found; truning off LTO])])],
609+
[AC_MSG_RESULT([no suitable ranlib is found; truning off LTO])])
610+
])
611+
AS_VAR_IF([use_lto], [yes], [
612+
AR="$LTO_AR"
613+
RANLIB="$LTO_RANLIB"
614+
EXTRA_CFLAGS="$EXTRA_CFLAGS -flto"
615+
EXTRA_LDFLAGS="$EXTRA_LDFLAGS -flto"
616+
],[
617+
AS_VAR_IF([enable_lto], [yes], [
618+
AC_MSG_ERROR([though --enable-lto is specified, the fto feature is not available nor usable.])
619+
])
620+
])
621+
579622
# Checks for function availability
580623
# -----------------------------------
581624

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)