Skip to content

build-sys: enable LTO #3922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ noinst_LIBRARIES += libutil.a

noinst_PROGRAMS = utiltest

AM_LDFLAGS = $(EXTRA_LDFLAGS)

# packcc always uses native compiler even when cross-compiling.
# packcc cannot use the standard Automake rule.
PACKCC_CPPFLAGS_FOR_BUILD = -fsigned-char
Expand All @@ -84,7 +86,7 @@ readtags_CPPFLAGS = -I. -I$(srcdir) -I$(srcdir)/main -I$(srcdir)/libreadtags
if ENABLE_DEBUGGING
readtags_CPPFLAGS+= $(DEBUG_CPPFLAGS)
endif
readtags_CFLAGS = $(COVERAGE_CFLAGS) $(WARNING_CFLAGS)
readtags_CFLAGS = $(EXTRA_CFLAGS) $(WARNING_CFLAGS) $(COVERAGE_CFLAGS)
dist_readtags_SOURCES = $(READTAGS_SRCS) $(READTAGS_HEADS)
readtags_CPPFLAGS += $(GNULIB_CPPFLAGS)
readtags_CPPFLAGS += -DREADTAGS_DSL -I$(srcdir)/dsl
Expand Down
63 changes: 53 additions & 10 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ AC_ARG_ENABLE(readcmd,
[do not include readtags command during install])],
[], [enable_readcmd=yes])

# AC_ARG_WITH(perl-regex,
# [AS_HELP_STRING([--with-perl-regex],
# [use Perl pcre interface, if available])])

AC_ARG_ENABLE(etags,
[AS_HELP_STRING([--enable-etags],
[enable the installation of links for etags])])
Expand Down Expand Up @@ -224,6 +220,10 @@ AC_ARG_ENABLE([static],
[AS_HELP_STRING([--enable-static],
[enable static linking (mainly for MinGW)])])

AC_ARG_ENABLE([lto],
[AS_HELP_STRING([--enable-lto],
[enable link time optimization])])

AC_ARG_PROGRAM

# Process configuration options
Expand Down Expand Up @@ -264,7 +264,9 @@ AC_PROG_CC_C99
# Typically, this is immediately after AC_PROG_CC, ...
gl_EARLY

AC_ARG_VAR(RANLIB,[ranlib command or path])
AC_PROG_RANLIB
PRETTY_ARG_VAR([AR], [ar command or path], [ar])
AC_C_BIGENDIAN

if test "$cross_compiling" = "yes"; then
Expand Down Expand Up @@ -324,15 +326,9 @@ AC_SUBST([LDFLAGS_FOR_BUILD])
AC_SUBST([BUILD_OBJEXT])
AC_SUBST([BUILD_EXEEXT])

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

if test "x${AR}" = "x" ; then
AR=ar
fi

if test "x${WINDRES}" = "x" ; then
WINDRES=windres
fi
Expand Down Expand Up @@ -576,6 +572,53 @@ PRETTY_ARG_VAR([EXTRA_CFLAGS], [extra C compiler flags],
PRETTY_ARG_VAR([WARNING_CFLAGS], [C compiler warning flags],
[-Wall])

PRETTY_ARG_VAR([EXTRA_LDFLAGS], [extra linker flags],
[])

dnl ref. https://stackoverflow.com/questions/40374061/autoconf-recipe-to-use-gcc-ar-and-gcc-ranlib
use_lto=no
enable_debugging
AS_IF([test "x$cross_compiling" != "xyes" -a "x$enable_lto" != "xno"],[
AX_CHECK_COMPILE_FLAG(-flto,[use_lto=yes])])
AS_VAR_IF([use_lto], [yes], [
AS_CASE($CC,
[gcc|*/gcc], [AC_CHECK_PROGS([LTO_AR], [gcc-ar ar], [:])],
[clang|*/clang], [AC_CHECK_PROGS([LTO_AR], [clang-ar ar], [:])],
[use_lto=no])
AC_MSG_CHECKING([the wrapper for ar working with $CC -flto])
AS_VAR_IF([LTO_AR], [:], AC_MSG_ERROR([could not find AR tool working with -flto.]))
AS_VAR_IF([use_lto], [yes],
[AS_CASE($LTO_AR,
[*-ar],[AC_MSG_RESULT([$LTO_AR])],
[use_lto=no
AC_MSG_RESULT([no suitable AR tool is found; truning off LTO])])],
[AC_MSG_RESULT([no suitable AR tool is found; truning off LTO])])
])
AS_VAR_IF([use_lto], [yes], [
AS_CASE($CC,
[gcc|*/gcc], [AC_CHECK_PROGS([LTO_RANLIB], [gcc-ranlib ranlib], [:])],
[clang|*/clang], [AC_CHECK_PROGS([LTO_RANLIB], [clang-ranlib ranlib], [:])],
[use_lto=no])
AC_MSG_CHECKING([the wrapper for ranlib working with $CC -flto])
AS_VAR_IF([LTO_RANLIB], [:], AC_MSG_ERROR([could not find RANLIB tool working with -flto.]))
AS_VAR_IF([use_lto], [yes],
[AS_CASE($LTO_RANLIB,
[*-ranlib],[AC_MSG_RESULT([$LTO_RANLIB])],
[use_lto=no
AC_MSG_RESULT([no suitable ranlib tool is found; truning off LTO])])],
[AC_MSG_RESULT([no suitable ranlib is found; truning off LTO])])
])
AS_VAR_IF([use_lto], [yes], [
AR="$LTO_AR"
RANLIB="$LTO_RANLIB"
EXTRA_CFLAGS="$EXTRA_CFLAGS -flto"
EXTRA_LDFLAGS="$EXTRA_LDFLAGS -flto"
],[
AS_VAR_IF([enable_lto], [yes], [
AC_MSG_ERROR([though --enable-lto is specified, the fto feature is not available nor usable.])
])
])

# Checks for function availability
# -----------------------------------

Expand Down
53 changes: 53 additions & 0 deletions m4/ax_check_compile_flag.m4
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# ===========================================================================
# https://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT])
#
# DESCRIPTION
#
# Check whether the given FLAG works with the current language's compiler
# or gives an error. (Warnings, however, are ignored)
#
# ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on
# success/failure.
#
# If EXTRA-FLAGS is defined, it is added to the current language's default
# flags (e.g. CFLAGS) when the check is done. The check is thus made with
# the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to
# force the compiler to issue an error when a bad flag is given.
#
# INPUT gives an alternative input source to AC_COMPILE_IFELSE.
#
# NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this
# macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG.
#
# LICENSE
#
# Copyright (c) 2008 Guido U. Draheim <[email protected]>
# Copyright (c) 2011 Maarten Bosmans <[email protected]>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.

#serial 6

AC_DEFUN([AX_CHECK_COMPILE_FLAG],
[AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF
AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl
AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [
ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS
_AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1"
AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])],
[AS_VAR_SET(CACHEVAR,[yes])],
[AS_VAR_SET(CACHEVAR,[no])])
_AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags])
AS_VAR_IF(CACHEVAR,yes,
[m4_default([$2], :)],
[m4_default([$3], :)])
AS_VAR_POPDEF([CACHEVAR])dnl
])dnl AX_CHECK_COMPILE_FLAGS