build: check for udev and kmod using pkg-config
authorThierry Reding <treding@nvidia.com>
Tue, 7 Mar 2017 13:40:40 +0000 (14:40 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Wed, 8 Mar 2017 13:43:52 +0000 (14:43 +0100)
Using AC_CHECK_LIB is dangerous because it modifies the global LIBS
variable that will be passed to each linker invocation. If care is not
taken, each binary will end up depending on libraries listed in this
variable.

A typical way to avoid this is to save and restore the LIBS variable
before and after the AC_CHECK_LIB function, respectively. That's quite
tedious, though, and often a better solution is to detect the presence
as well as compile and linker flags with pkg-config. This can be done
using the PKG_CHECK_MODULES autoconf macro.

Both udev and kmod ship pkg-config files, so use PKG_CHECK_MODULES for
them.

Signed-off-by: Thierry Reding <treding@nvidia.com>
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
configure.ac
tests/unit/Makefile.am

index 946059b486a70786a4ef433684499af6a7e53830..8117e45f95c55d485c8e8f6a1bf0341157c3b00c 100644 (file)
@@ -96,21 +96,15 @@ AC_ARG_ENABLE([tests],
        [with_tests=false])
 AM_CONDITIONAL([WITH_TESTS], [test "x$with_tests" = xtrue])
 
-AC_DEFUN([HEADER_NOT_FOUND_TESTS],
-       [ERR_NOT_FOUND([$1 header], [tests])])
-
 AC_DEFUN([FUNC_NOT_FOUND_TESTS],
        [ERR_NOT_FOUND([$1()], [tests])])
 
 if test "x$with_tests" = xtrue
 then
-       AC_CHECK_LIB([kmod], [kmod_module_probe_insert_module], [],
-               [AC_MSG_ERROR([libkmod not found (needed to build tests])])
-       AC_CHECK_LIB([udev], [udev_monitor_new_from_netlink], [],
-               [AC_MSG_ERROR([libudev not found (needed to build tests])])
-       AC_CHECK_HEADERS([libkmod.h], [], [HEADER_NOT_FOUND_TESTS([libkmod.h])])
-       AC_CHECK_HEADERS([libudev.h], [], [HEADER_NOT_FOUND_TESTS([libudev.h])])
        AC_CHECK_FUNC([qsort], [], [FUNC_NOT_FOUND_TESTS([qsort])])
+
+       PKG_CHECK_MODULES(KMOD, libkmod)
+       PKG_CHECK_MODULES(UDEV, libudev)
 fi
 
 AC_CHECK_PROG([has_doxygen], [doxygen], [true], [false])
index c42286e542d63ab80fe7084dd445d438ae0faa9a..031be75b5bb47ed32c119b90d3efd7352b6c3c0e 100644 (file)
@@ -7,8 +7,8 @@
 #
 
 AM_CFLAGS = -I$(top_srcdir)/include/ -include $(top_builddir)/config.h
-AM_CFLAGS += -Wall -Wextra -g
-LDADD = ../../src/lib/libgpiod.la -lkmod -ludev
+AM_CFLAGS += -Wall -Wextra -g $(KMOD_CFLAGS) $(UDEV_CFLAGS)
+LDADD = ../../src/lib/libgpiod.la $(KMOD_LIBS) $(UDEV_LIBS)
 
 check_PROGRAMS = gpiod-unit