cmake: detect fts also in external lib

Not all libc implementations support fts. fts support may be added via
an external library.

One such example is musl libc. It doesn't support fts. But support for
fts can be added to the target device via musl-fts, which provides
libfts.

When libzip encounters such a setup (fts.h available but symbols in
libfts.so) the build fails at the linking stage:

zipcmp.c:(.text.startup+0x130): undefined reference to `fts_open'
/home/sk/tmp/openwrt/staging_dir/toolchain-mips_24kc_gcc-8.3.0_musl/lib/gcc/mips-openwrt-linux-musl/8.3.0/../../../../mips-openwrt-linux-musl/bin/ld: zipcmp.c:(.text.startup+0x172): undefined reference to `fts_read'

This commit sets up cmake so that it checks for fts symbols first in
libc and - if none encountered there - in an external libfts. So in case
libc provides fts nothing changes, libzip will use libc's fts symbols
(if fts.h is present). If on the other hand libc doesn't provide fts but
libfts does, it'll link it in where required (zipcmp).

Signed-off-by: Sebastian Kemper <sebastian_ml@gmx.net>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b050a18..fbd65f6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,6 +23,7 @@
 
 INCLUDE(CheckFunctionExists)
 INCLUDE(CheckIncludeFiles)
+INCLUDE(CheckLibraryExists)
 INCLUDE(CheckSymbolExists)
 INCLUDE(CheckTypeSize)
 INCLUDE(CheckCSourceRuns)
@@ -161,6 +162,20 @@
 CHECK_FUNCTION_EXISTS(strtoull HAVE_STRTOULL)
 
 CHECK_INCLUDE_FILES("sys/types.h;sys/stat.h;fts.h" HAVE_FTS_H)
+# fts functions may be in external library
+IF(HAVE_FTS_H)
+  CHECK_FUNCTION_EXISTS(fts_open HAVE_FTS_OPEN)
+  IF(NOT HAVE_FTS_OPEN)
+    CHECK_LIBRARY_EXISTS(fts fts_open "" HAVE_LIB_FTS)
+  ENDIF(NOT HAVE_FTS_OPEN)
+ENDIF(HAVE_FTS_H)
+
+IF(HAVE_LIB_FTS)
+  SET(FTS_LIB fts)
+ELSE()
+  SET(FTS_LIB "")
+ENDIF()
+
 CHECK_INCLUDE_FILES(stdbool.h HAVE_STDBOOL_H)
 CHECK_INCLUDE_FILES(strings.h HAVE_STRINGS_H)
 CHECK_INCLUDE_FILES(unistd.h HAVE_UNISTD_H)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8675f9c..59db62a 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -6,7 +6,7 @@
 ENDIF(NOT HAVE_GETOPT)
 
 ADD_EXECUTABLE(zipcmp zipcmp.c ${SRC_EXTRA_FILES})
-TARGET_LINK_LIBRARIES(zipcmp zip ${ZLIB_LIBRARIES})
+TARGET_LINK_LIBRARIES(zipcmp zip ${FTS_LIB} ${ZLIB_LIBRARIES})
 INSTALL(TARGETS zipcmp RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
 
 ADD_EXECUTABLE(zipmerge zipmerge.c ${SRC_EXTRA_FILES})