Skip to content

Commit 54c9112

Browse files
Explorer09fpletz
andcommitted
Allow custom search path for libnl; try pkg-config when needed
This is helpful when libnl packages are installed in a location outside the default search paths of the libraries. Gentoo and NixOS need this feature to build. Co-authored-by: Franz Pletz <[email protected]> Signed-off-by: Kang-Che Sung <[email protected]>
1 parent b7053a4 commit 54c9112

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

configure.ac

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,29 @@ case "$enable_capabilities" in
10331033
esac
10341034

10351035

1036+
# $1: libnl-3 search path
1037+
htop_try_link_libnl3 () {
1038+
htop_save_LDFLAGS=$LDFLAGS
1039+
htop_save_LIBS=$LIBS
1040+
1041+
LIBS="-lnl-3 $LIBS"
1042+
if test "x$1" != x; then
1043+
# New library path searched after what user has specified
1044+
LDFLAGS="$LDFLAGS -L$1"
1045+
fi
1046+
AC_LINK_IFELSE([AC_LANG_PROGRAM([[
1047+
/* struct nl_sock* nl_socket_alloc(void); */
1048+
void* nl_socket_alloc(void);
1049+
]], [[
1050+
void* sock = nl_socket_alloc();
1051+
]])],
1052+
[htop_libnl3_link_succeed=yes],
1053+
[htop_libnl3_link_succeed=no])
1054+
1055+
LDFLAGS=$htop_save_LDFLAGS
1056+
LIBS=$htop_save_LIBS
1057+
} # htop_try_link_libnl3
1058+
10361059
AC_ARG_ENABLE([delayacct],
10371060
[AS_HELP_STRING([--enable-delayacct],
10381061
[enable Linux delay accounting support; requires libnl-3 and libnl-genl-3 @<:@default=check@:>@])],
@@ -1061,19 +1084,41 @@ case "$enable_delayacct" in
10611084
LIBNL3_CFLAGS="-I/usr/include/libnl3"
10621085
])
10631086

1064-
old_CFLAGS="$CFLAGS"
1087+
htop_save_CFLAGS=$CFLAGS
10651088
# New include path searched after what user has specified
10661089
CFLAGS="$CFLAGS $LIBNL3_CFLAGS"
10671090
AC_CHECK_HEADERS([netlink/attr.h netlink/handlers.h netlink/msg.h],
10681091
[],
10691092
[if test "$enable_delayacct" = yes; then
10701093
AC_MSG_ERROR([can not find required header files netlink/attr.h, netlink/handlers.h, netlink/msg.h])
10711094
fi
1072-
enable_delayacct=no
1073-
break])
1074-
CFLAGS="$old_CFLAGS"
1095+
enable_delayacct=no])
1096+
CFLAGS=$htop_save_CFLAGS
10751097

10761098
if test "$enable_delayacct" != no; then
1099+
AC_MSG_CHECKING([the search path of libnl-3])
1100+
1101+
htop_libnl3_link_succeed=no
1102+
htop_try_link_libnl3 "$LIBNL3_LIBDIR"
1103+
if test "$htop_libnl3_link_succeed$LIBNL3_LIBDIR" = no && test "x$PKG_CONFIG" != x; then
1104+
LIBNL3_LIBDIR=`$PKG_CONFIG --variable=libdir libnl-3.0 2>/dev/null`
1105+
if test "x$LIBNL3_LIBDIR" != x; then
1106+
htop_try_link_libnl3 "$LIBNL3_LIBDIR"
1107+
fi
1108+
fi
1109+
1110+
if test "x$LIBNL3_LIBDIR" = x; then
1111+
AC_MSG_RESULT([(default)])
1112+
else
1113+
# The path must end with a slash
1114+
LIBNL3_LIBDIR=`echo "x$LIBNL3_LIBDIR" | sed 's/^x//; s|/*$|/|'`
1115+
AC_MSG_RESULT([$LIBNL3_LIBDIR])
1116+
fi
1117+
AC_DEFINE_UNQUOTED([LIBNL3_LIBDIR], ["$LIBNL3_LIBDIR"], [libnl-3 search path; use the default search paths if empty])
1118+
if test "$htop_libnl3_link_succeed" = no; then
1119+
AC_MSG_WARN([libnl-3 binary currently not present; will be needed in htop runtime for delay accounting support])
1120+
fi
1121+
10771122
enable_delayacct=yes
10781123
fi
10791124
;;

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)