Search code examples
autoconf

best practice for building with/out included libraries


a project ships with a copy of library foo, in a filesystem layout like:

myproject/
myproject/src/ # sources of my project
myproject/libfoo/ # import of "foo" library

the standard (autotools-based) build-system builds libfoo, then builds myproject which dynamically links against libfoo. libfoo is basically unmodified (with some minor amendments to properly fit into the build-system). libfoo uses autotools itself, so i'm usually calling configure recursively using AC_CONFIG_SUBDIRS.

however, libfoo is already packaged for various distributions, so i would like to avoid building against the imported library on these systems and rather use system-wide installation - this way i get the benefits of a better maintained version of libfoo (less bugs, security issues,...). otoh, i want keep libfoo in my source-tree, so that i have a fallback for building on systems that do not ship that library (without the user requiring to separately fetch the sources and build the lib themselves).

i can think of a number of configure-flags i could instroduce, so the user can select whether they want to build the project with the system-installed, the local or without the library. (it's an optional dependency). disabling the "local foo", should completely disable building of libfoo (and probably also configuring foo)

e.g. something like:

./configure --enable-foo=no    # aka "--disable-foo": build without foo
./configure --enable-foo       # use system-wide foo
./configure --enable-foo=local # use local copy of foo

alternatively:

./configure --disable-foo
./configure --enable-foo  --disable-local-foo
./configure --enable-foo  --enable-local-foo

but i'd like to do this in a standard-conformant way.

what's the best practice for selecting via autoconf, whether to use a local copy or a library, a system-wide copy or to not use the library at all?

pointers to projects that use such a mechanism are most welcome.


Solution

  • I have a similar in my project where I use the included version of the BuDDy library when (1) the library isn't already installed, or (2) it is installed but does not have to interface I expect, or (3) configure was run with --with-included-buddy.

    You can see the configure macro here. After that I just use $(BUDDY_CPPFLAGS) and $(BUDDY_LDFLAGS) in the Makefile.ams, and the top-level Makefile.am only include the buddy directory conditionally in SUBDIRS.