Skip to content

Commit 08a8910

Browse files
Explorer09fpletz
andcommitted
Allow custom search path for libnl; try pkg-config when needed
Introduce LIBNL3_LIBDIR configure variable and preprocessor token to specify a custom path for loading libnl-3 and libnl-genl-3 using dlopen(3). 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. User may specify LIBNL3_LIBDIR manually during configure. If it's not specified, configure will try the following in order: (1) find whether libnl exists within the default library search paths by try linking it (LIBNL3_LIBDIR being empty), (2) locate the 'libdir' value of libnl-3.0 package through pkg-config and try linking with the directory included, and (3) if both fail, go back to the default search paths (LIBNL3_LIBDIR being empty) and assume the library doesn't exist during the build but will exist at htop runtime. Co-authored-by: Franz Pletz <fpletz@fnordicwalking.de> Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
1 parent f58a219 commit 08a8910

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

configure.ac

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,29 @@ 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],
10201043
[enable Linux delay accounting support; requires libnl-3 and libnl-genl-3 @<:@default=check@:>@])],
@@ -1045,7 +1068,7 @@ case "$enable_delayacct" in
10451068
])
10461069
fi
10471070

1048-
old_CFLAGS="$CFLAGS"
1071+
htop_save_CFLAGS=$CFLAGS
10491072
# New include path searched after what user has specified
10501073
CFLAGS="$CFLAGS $LIBNL3_CFLAGS"
10511074
AC_CHECK_HEADERS([netlink/attr.h netlink/handlers.h netlink/msg.h],
@@ -1054,9 +1077,32 @@ case "$enable_delayacct" in
10541077
AC_MSG_ERROR([can not find required header files netlink/attr.h, netlink/handlers.h, netlink/msg.h])
10551078
fi
10561079
enable_delayacct=no])
1057-
CFLAGS="$old_CFLAGS"
1080+
CFLAGS=$htop_save_CFLAGS
10581081

10591082
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+
10601106
enable_delayacct=yes
10611107
fi
10621108
;;

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)