Discussion:
LIBS
Harlan Stenn
2013-07-20 02:07:12 UTC
Permalink
So for reasons that may or may not still be valid, I've had folks
successfully lobby me to separate out libraries that are needed for all
programs from libraries that are needed for certain programs.

I believe the reasons for this have to do with the size of the
executables - folks Noticed that linking with unnecessary libraries
caused the resulting executables to be much bigger and sucked up memory,
especially in embedded systems.

But if I don't put all of the detected libraries in LIBS then things
like AC_CHECK_FUNCS won't find them.

Suggestions on good ways to proceed?
--
Harlan Stenn <***@ntp.org>
http://networktimefoundation.org - be a member!
Zack Weinberg
2013-07-20 02:35:18 UTC
Permalink
LDFLAGS=-Wl,--as-needed

and you're done. This really should be the default -- it corresponds
to the way static linkage works -- but there are probably too many
Makefiles out there expecting the opposite behavior to change it now.
Post by Harlan Stenn
So for reasons that may or may not still be valid, I've had folks
successfully lobby me to separate out libraries that are needed for all
programs from libraries that are needed for certain programs.
I believe the reasons for this have to do with the size of the
executables - folks Noticed that linking with unnecessary libraries
caused the resulting executables to be much bigger and sucked up memory,
especially in embedded systems.
But if I don't put all of the detected libraries in LIBS then things
like AC_CHECK_FUNCS won't find them.
Suggestions on good ways to proceed?
--
http://networktimefoundation.org - be a member!
_______________________________________________
Autoconf mailing list
https://lists.gnu.org/mailman/listinfo/autoconf
Harlan Stenn
2013-07-20 03:00:41 UTC
Permalink
Post by Zack Weinberg
LDFLAGS=-Wl,--as-needed
and you're done. This really should be the default -- it corresponds
to the way static linkage works -- but there are probably too many
Makefiles out there expecting the opposite behavior to change it now.
But that solution is not always available, right?

I'm wondering if one solution might be to have AC_CHECK_FUNC and friends
have a way to provide "additional libraries to search".

H
Russ Allbery
2013-07-20 03:34:26 UTC
Permalink
Post by Harlan Stenn
So for reasons that may or may not still be valid, I've had folks
successfully lobby me to separate out libraries that are needed for all
programs from libraries that are needed for certain programs.
I believe the reasons for this have to do with the size of the
executables - folks Noticed that linking with unnecessary libraries
caused the resulting executables to be much bigger and sucked up memory,
especially in embedded systems.
But if I don't put all of the detected libraries in LIBS then things
like AC_CHECK_FUNCS won't find them.
Suggestions on good ways to proceed?
I do lots and lots and lots of this sort of thing:

dnl Add the library flags to the default compiler flags and then remove them.
dnl
dnl To use these macros, pass the prefix string used for the variables as the
dnl only argument. For example, to use these for a library with KRB5 as a
dnl prefix, one would use:
dnl
dnl AC_DEFUN([RRA_LIB_KRB5_SWITCH], [RRA_LIB_HELPER_SWITCH([KRB5])])
dnl AC_DEFUN([RRA_LIB_KRB5_RESTORE], [RRA_LIB_HELPER_RESTORE([KRB5])])
dnl
dnl Then, wrap checks for library features with RRA_LIB_KRB5_SWITCH and
dnl RRA_LIB_KRB5_RESTORE.
AC_DEFUN([RRA_LIB_HELPER_SWITCH],
[rra_$1[]_save_CPPFLAGS="$CPPFLAGS"
rra_$1[]_save_LDFLAGS="$LDFLAGS"
rra_$1[]_save_LIBS="$LIBS"
CPPFLAGS="$$1[]_CPPFLAGS $CPPFLAGS"
LDFLAGS="$$1[]_LDFLAGS $LDFLAGS"
LIBS="$$1[]_LIBS $LIBS"])

AC_DEFUN([RRA_LIB_HELPER_RESTORE],
[CPPFLAGS="$rra_$1[]_save_CPPFLAGS"
LDFLAGS="$rra_$1[]_save_LDFLAGS"
LIBS="$rra_$1[]_save_LIBS"])

combined with adding libraries explicitly to XXX_LIBS in the probe macros,
and then linking with XXX_LDFLAGS and XXX_LIBS when that library is
needed. It gets quite tedious, but it does work, including on hosts where
--as-needed isn't supported.
--
Russ Allbery (***@stanford.edu) <http://www.eyrie.org/~eagle/>
Harlan Stenn
2013-07-20 05:21:29 UTC
Permalink
Thanks Russ!

H
Bastien ROUCARIES
2013-07-22 06:24:52 UTC
Permalink
Post by Russ Allbery
Post by Harlan Stenn
So for reasons that may or may not still be valid, I've had folks
successfully lobby me to separate out libraries that are needed for all
programs from libraries that are needed for certain programs.
I believe the reasons for this have to do with the size of the
executables - folks Noticed that linking with unnecessary libraries
caused the resulting executables to be much bigger and sucked up memory,
especially in embedded systems.
But if I don't put all of the detected libraries in LIBS then things
like AC_CHECK_FUNCS won't find them.
Suggestions on good ways to proceed?
Russ could you add some proper licence text to this ? Preferally all
permissive license ?

I will add your stuff to autoconf-archive as an optionnal flags to:
AX_SAVE_FLAGS/AX_RESTORE_FLAGS
(see http://www.gnu.org/software/autoconf-archive/ax_save_flags.html#ax_save_flags)
and I will add the AX_SWITCH_FLAGS macro
Post by Russ Allbery
dnl Add the library flags to the default compiler flags and then remove them.
dnl
dnl To use these macros, pass the prefix string used for the variables as the
dnl only argument. For example, to use these for a library with KRB5 as a
dnl
dnl AC_DEFUN([RRA_LIB_KRB5_SWITCH], [RRA_LIB_HELPER_SWITCH([KRB5])])
dnl AC_DEFUN([RRA_LIB_KRB5_RESTORE], [RRA_LIB_HELPER_RESTORE([KRB5])])
dnl
dnl Then, wrap checks for library features with RRA_LIB_KRB5_SWITCH and
dnl RRA_LIB_KRB5_RESTORE.
AC_DEFUN([RRA_LIB_HELPER_SWITCH],
[rra_$1[]_save_CPPFLAGS="$CPPFLAGS"
rra_$1[]_save_LDFLAGS="$LDFLAGS"
rra_$1[]_save_LIBS="$LIBS"
CPPFLAGS="$$1[]_CPPFLAGS $CPPFLAGS"
LDFLAGS="$$1[]_LDFLAGS $LDFLAGS"
LIBS="$$1[]_LIBS $LIBS"])
AC_DEFUN([RRA_LIB_HELPER_RESTORE],
[CPPFLAGS="$rra_$1[]_save_CPPFLAGS"
LDFLAGS="$rra_$1[]_save_LDFLAGS"
LIBS="$rra_$1[]_save_LIBS"])
combined with adding libraries explicitly to XXX_LIBS in the probe macros,
and then linking with XXX_LDFLAGS and XXX_LIBS when that library is
needed. It gets quite tedious, but it does work, including on hosts where
--as-needed isn't supported.
--
_______________________________________________
Autoconf mailing list
https://lists.gnu.org/mailman/listinfo/autoconf
Russ Allbery
2013-07-22 18:21:33 UTC
Permalink
Post by Bastien ROUCARIES
Russ could you add some proper licence text to this ? Preferally all
permissive license ?
Sorry about that. Here's the complete file (from rra-c-util), which
includes all that stuff as well as some other macros. It's the core
framework that I use to write library probes.

dnl Helper functions to manage compiler variables.
dnl
dnl These are a wide variety of helper macros to make it easier to construct
dnl standard macros to probe for a library and to set library-specific
dnl CPPFLAGS, LDFLAGS, and LIBS shell substitution variables. Most of them
dnl take as one of the arguments the prefix string to use for variables, which
dnl is usually something like "KRB5" or "GSSAPI".
dnl
dnl Depends on RRA_ENABLE_REDUCED_DEPENDS and RRA_SET_LDFLAGS.
dnl
dnl The canonical version of this file is maintained in the rra-c-util
dnl package, available at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
dnl
dnl Written by Russ Allbery <***@stanford.edu>
dnl Copyright 2011
dnl The Board of Trustees of the Leland Stanford Junior University
dnl
dnl This file is free software; the authors give unlimited permission to copy
dnl and/or distribute it, with or without modifications, as long as this
dnl notice is preserved.

dnl Add the library flags to the default compiler flags and then remove them.
dnl
dnl To use these macros, pass the prefix string used for the variables as the
dnl only argument. For example, to use these for a library with KRB5 as a
dnl prefix, one would use:
dnl
dnl AC_DEFUN([RRA_LIB_KRB5_SWITCH], [RRA_LIB_HELPER_SWITCH([KRB5])])
dnl AC_DEFUN([RRA_LIB_KRB5_RESTORE], [RRA_LIB_HELPER_RESTORE([KRB5])])
dnl
dnl Then, wrap checks for library features with RRA_LIB_KRB5_SWITCH and
dnl RRA_LIB_KRB5_RESTORE.
AC_DEFUN([RRA_LIB_HELPER_SWITCH],
[rra_$1[]_save_CPPFLAGS="$CPPFLAGS"
rra_$1[]_save_LDFLAGS="$LDFLAGS"
rra_$1[]_save_LIBS="$LIBS"
CPPFLAGS="$$1[]_CPPFLAGS $CPPFLAGS"
LDFLAGS="$$1[]_LDFLAGS $LDFLAGS"
LIBS="$$1[]_LIBS $LIBS"])

AC_DEFUN([RRA_LIB_HELPER_RESTORE],
[CPPFLAGS="$rra_$1[]_save_CPPFLAGS"
LDFLAGS="$rra_$1[]_save_LDFLAGS"
LIBS="$rra_$1[]_save_LIBS"])

dnl Given _root, _libdir, and _includedir variables set for a library (set by
dnl RRA_LIB_HELPER_WITH*), set the LDFLAGS and CPPFLAGS variables for that
dnl library accordingly. Takes the variable prefix as the only argument.
AC_DEFUN([RRA_LIB_HELPER_PATHS],
[AS_IF([test x"$rra_$1[]_libdir" != x],
[$1[]_LDFLAGS="-L$rra_$1[]_libdir"],
[AS_IF([test x"$rra_$1[]_root" != x],
[RRA_SET_LDFLAGS([$1][_LDFLAGS], [${rra_$1[]_root}])])])
AS_IF([test x"$rra_$1[]_includedir" != x],
[$1[]_CPPFLAGS="-I$rra_$1[]_includedir"],
[AS_IF([test x"$rra_$1[]_root" != x],
[AS_IF([test x"$rra_$1[]_root" != x/usr],
[$1[]_CPPFLAGS="-I${rra_$1[]_root}/include"])])])])

dnl Check whether a library works. This is used as a sanity check on the
dnl results of *-config shell scripts. Takes four arguments; the first, if
dnl "true", says that a working library is mandatory and errors out if it
dnl doesn't. The second is the variable prefix. The third is a function to
dnl look for that should be in the libraries. The fourth is the
dnl human-readable name of the library for error messages.
AC_DEFUN([RRA_LIB_HELPER_CHECK],
[RRA_LIB_HELPER_SWITCH([$2])
AC_CHECK_FUNC([$3], [],
[AS_IF([test x"$1" = xtrue],
[AC_MSG_FAILURE([unable to link with $4 library])])
$2[]_CPPFLAGS=
$2[]_LDFLAGS=
$2[]_LIBS=])
RRA_LIB_HELPER_RESTORE([$2])])

dnl Initialize the variables used by a library probe and set the appropriate
dnl ones as substitution variables. Takes the library variable prefix as its
dnl only argument.
AC_DEFUN([RRA_LIB_HELPER_VAR_INIT],
[rra_$1[]_root=
rra_$1[]_libdir=
rra_$1[]_includedir=
rra_use_$1=
$1[]_CPPFLAGS=
$1[]_LDFLAGS=
$1[]_LIBS=
AC_SUBST([$1][_CPPFLAGS])
AC_SUBST([$1][_LDFLAGS])
AC_SUBST([$1][_LIBS])])

dnl Handles --with options for a non-optional library. First argument is the
dnl base for the switch names. Second argument is the short description.
dnl Third argument is the variable prefix. The variables set are used by
dnl RRA_LIB_HELPER_PATHS.
AC_DEFUN([RRA_LIB_HELPER_WITH],
[AC_ARG_WITH([$1],
[AS_HELP_STRING([--with-][$1][=DIR],
[Location of $2 headers and libraries])],
[AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
[rra_$3[]_root="$withval"])])
AC_ARG_WITH([$1][-include],
[AS_HELP_STRING([--with-][$1][-include=DIR],
[Location of $2 headers])],
[AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
[rra_$3[]_includedir="$withval"])])
AC_ARG_WITH([$1][-lib],
[AS_HELP_STRING([--with-][$1][-lib=DIR],
[Location of $2 libraries])],
[AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
[rra_$3[]_libdir="$withval"])])])

dnl Handles --with options for an optional library, so --with-<library> can
dnl cause the checks to be skipped entirely or become mandatory. Sets an
dnl rra_use_PREFIX variable to true or false if the library is explicitly
dnl enabled or disabled.
dnl
dnl First argument is the base for the switch names. Second argument is the
dnl short description. Third argument is the variable prefix.
dnl
dnl The variables set are used by RRA_LIB_HELPER_PATHS.
AC_DEFUN([RRA_LIB_HELPER_WITH_OPTIONAL],
[AC_ARG_WITH([$1],
[AS_HELP_STRING([--with-][$1][@<:@=DIR@:>@],
[Location of $2 headers and libraries])],
[AS_IF([test x"$withval" = xno],
[rra_use_$3=false],
[AS_IF([test x"$withval" != yes], [rra_$3[]_root="$withval"])
rra_use_$3=true])])
AC_ARG_WITH([$1][-include],
[AS_HELP_STRING([--with-][$1][-include=DIR],
[Location of $2 headers])],
[AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
[rra_$3[]_includedir="$withval"])])
AC_ARG_WITH([$1][-lib],
[AS_HELP_STRING([--with-][$1][-lib=DIR],
[Location of $2 libraries])],
[AS_IF([test x"$withval" != xyes && test x"$withval" != xno],
[rra_$3[]_libdir="$withval"])])])
--
Russ Allbery (***@stanford.edu) <http://www.eyrie.org/~eagle/>
Loading...