Skip to content

Commit 36ea1ef

Browse files
authored
Merge pull request #1698 from Explorer09/pkgconfig-libnl
Use pkg-config to detect libnl header and library paths
2 parents 0290c58 + 08a8910 commit 36ea1ef

File tree

2 files changed

+80
-18
lines changed

2 files changed

+80
-18
lines changed

configure.ac

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,39 +1015,101 @@ case "$enable_capabilities" in
10151015
esac
10161016

10171017

1018+
# $1: libnl-3 search path
1019+
htop_try_link_libnl3 () {
1020+
htop_save_LDFLAGS=$LDFLAGS
1021+
htop_save_LIBS=$LIBS
1022+
1023+
LIBS="-lnl-3 $LIBS"
1024+
if test "x$1" != x; then
1025+
# New library path searched after what user has specified
1026+
LDFLAGS="$LDFLAGS -L$1"
1027+
fi
1028+
AC_LINK_IFELSE([AC_LANG_PROGRAM([[
1029+
/* struct nl_sock* nl_socket_alloc(void); */
1030+
void* nl_socket_alloc(void);
1031+
]], [[
1032+
void* sock = nl_socket_alloc();
1033+
]])],
1034+
[htop_libnl3_link_succeed=yes],
1035+
[htop_libnl3_link_succeed=no])
1036+
1037+
LDFLAGS=$htop_save_LDFLAGS
1038+
LIBS=$htop_save_LIBS
1039+
} # htop_try_link_libnl3
1040+
10181041
AC_ARG_ENABLE([delayacct],
10191042
[AS_HELP_STRING([--enable-delayacct],
1020-
[enable Linux delay accounting support; requires pkg-config, libnl-3 and libnl-genl-3 @<:@default=check@:>@])],
1043+
[enable Linux delay accounting support; requires libnl-3 and libnl-genl-3 @<:@default=check@:>@])],
10211044
[],
10221045
[enable_delayacct=check])
10231046
case "$enable_delayacct" in
1024-
no)
1047+
no|yes)
10251048
;;
10261049
check)
10271050
if test "$my_htop_platform" != linux; then
10281051
enable_delayacct=no
10291052
elif test "$enable_static" = yes; then
10301053
enable_delayacct=no
1031-
else
1032-
old_CFLAGS="$CFLAGS"
1033-
CFLAGS="$CFLAGS -I/usr/include/libnl3"
1034-
AC_CHECK_HEADERS([netlink/attr.h netlink/handlers.h netlink/msg.h], [enable_delayacct=yes], [enable_delayacct=no])
1035-
CFLAGS="$old_CFLAGS"
10361054
fi
10371055
;;
1038-
yes)
1039-
old_CFLAGS="$CFLAGS"
1040-
CFLAGS="$CFLAGS -I/usr/include/libnl3"
1041-
AC_CHECK_HEADERS([netlink/attr.h netlink/handlers.h netlink/msg.h], [], [AC_MSG_ERROR([can not find required header files netlink/attr.h, netlink/handlers.h, netlink/msg.h])])
1042-
CFLAGS="$old_CFLAGS"
1043-
;;
10441056
*)
10451057
AC_MSG_ERROR([bad value '$enable_delayacct' for --enable-delayacct])
10461058
;;
10471059
esac
1060+
1061+
case "$enable_delayacct" in
1062+
check|yes)
1063+
if test "x${LIBNL3_CFLAGS+y}" = x; then
1064+
m4_ifdef([PKG_PROG_PKG_CONFIG], [
1065+
PKG_CHECK_MODULES(LIBNL3, libnl-3.0, [], [LIBNL3_CFLAGS="-I/usr/include/libnl3"])
1066+
], [
1067+
LIBNL3_CFLAGS="-I/usr/include/libnl3"
1068+
])
1069+
fi
1070+
1071+
htop_save_CFLAGS=$CFLAGS
1072+
# New include path searched after what user has specified
1073+
CFLAGS="$CFLAGS $LIBNL3_CFLAGS"
1074+
AC_CHECK_HEADERS([netlink/attr.h netlink/handlers.h netlink/msg.h],
1075+
[],
1076+
[if test "$enable_delayacct" = yes; then
1077+
AC_MSG_ERROR([can not find required header files netlink/attr.h, netlink/handlers.h, netlink/msg.h])
1078+
fi
1079+
enable_delayacct=no])
1080+
CFLAGS=$htop_save_CFLAGS
1081+
1082+
if test "$enable_delayacct" != no; then
1083+
AC_MSG_CHECKING([the search path of libnl-3])
1084+
1085+
htop_libnl3_link_succeed=no
1086+
htop_try_link_libnl3 "$LIBNL3_LIBDIR"
1087+
if test "$htop_libnl3_link_succeed${LIBNL3_LIBDIR+y}" = no && test "x$PKG_CONFIG" != x; then
1088+
LIBNL3_LIBDIR=`$PKG_CONFIG --variable=libdir libnl-3.0 2>/dev/null`
1089+
if test "x$LIBNL3_LIBDIR" != x; then
1090+
htop_try_link_libnl3 "$LIBNL3_LIBDIR"
1091+
fi
1092+
fi
1093+
1094+
if test "x$LIBNL3_LIBDIR" = x; then
1095+
AC_MSG_RESULT([(default)])
1096+
else
1097+
# The path must end with a slash
1098+
LIBNL3_LIBDIR=`echo "x$LIBNL3_LIBDIR" | sed 's/^x//; s|/*$|/|'`
1099+
AC_MSG_RESULT([$LIBNL3_LIBDIR])
1100+
fi
1101+
AC_DEFINE_UNQUOTED([LIBNL3_LIBDIR], ["$LIBNL3_LIBDIR"], [libnl-3 search path; use the default search paths if empty])
1102+
if test "$htop_libnl3_link_succeed" = no; then
1103+
AC_MSG_WARN([libnl-3 binary currently not present; will be needed in htop runtime for delay accounting support])
1104+
fi
1105+
1106+
enable_delayacct=yes
1107+
fi
1108+
;;
1109+
esac
10481110
if test "$enable_delayacct" = yes; then
10491111
AC_DEFINE([HAVE_DELAYACCT], [1], [Define if delay accounting support should be enabled.])
1050-
AM_CFLAGS="$AM_CFLAGS -I/usr/include/libnl3"
1112+
AM_CFLAGS="$AM_CFLAGS $LIBNL3_CFLAGS"
10511113
fi
10521114
AM_CONDITIONAL([HAVE_DELAYACCT], [test "$enable_delayacct" = yes])
10531115

linux/LibNl.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,17 @@ static int load_libnl(void) {
7878
if (libnlHandle && libnlGenlHandle)
7979
return 0;
8080

81-
libnlHandle = dlopen("libnl-3.so", RTLD_LAZY);
81+
libnlHandle = dlopen(LIBNL3_LIBDIR "libnl-3.so", RTLD_LAZY);
8282
if (!libnlHandle) {
83-
libnlHandle = dlopen("libnl-3.so.200", RTLD_LAZY);
83+
libnlHandle = dlopen(LIBNL3_LIBDIR "libnl-3.so.200", RTLD_LAZY);
8484
if (!libnlHandle) {
8585
goto dlfailure;
8686
}
8787
}
8888

89-
libnlGenlHandle = dlopen("libnl-genl-3.so", RTLD_LAZY);
89+
libnlGenlHandle = dlopen(LIBNL3_LIBDIR "libnl-genl-3.so", RTLD_LAZY);
9090
if (!libnlGenlHandle) {
91-
libnlGenlHandle = dlopen("libnl-genl-3.so.200", RTLD_LAZY);
91+
libnlGenlHandle = dlopen(LIBNL3_LIBDIR "libnl-genl-3.so.200", RTLD_LAZY);
9292
if (!libnlGenlHandle) {
9393
goto dlfailure;
9494
}

0 commit comments

Comments
 (0)