Merge pull request #25883 from CrystalP/fix-slowscan
[xbmc.git] / cmake / modules / FindNFS.cmake
blob76a1090652c8a4ad4b1a6d20c6afb65fa500342b
1 #.rst:
2 # FindNFS
3 # -------
4 # Finds the libnfs library
6 # This will define the following target:
8 #   ${APP_NAME_LC}::NFS   - The libnfs library
10 if(NOT TARGET ${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME})
12   macro(buildlibnfs)
13     set(CMAKE_ARGS -DBUILD_SHARED_LIBS=OFF
14                    -DENABLE_TESTS=OFF
15                    -DENABLE_DOCUMENTATION=OFF
16                    -DENABLE_UTILS=OFF
17                    -DENABLE_EXAMPLES=OFF)
19     if(WIN32 OR WINDOWS_STORE)
20       set(${MODULE}_C_FLAGS "/sdl-")
21       set(${MODULE}_CXX_FLAGS "/sdl-")
23       set(patches "${CORE_SOURCE_DIR}/tools/depends/target/${MODULE_LC}/01-MSUWP-compat.patch")
24       generate_patchcommand("${patches}")
25     endif()
27     BUILD_DEP_TARGET()
29     set(_nfs_definitions HAS_NFS_SET_TIMEOUT
30                          HAS_NFS_MOUNT_GETEXPORTS_TIMEOUT)
31   endmacro()
33   include(cmake/scripts/common/ModuleHelpers.cmake)
35   set(MODULE_LC libnfs)
37   SETUP_BUILD_VARS()
39   # Search for cmake config. Suitable for all platforms including windows
40   find_package(libnfs CONFIG QUIET
41                              HINTS ${DEPENDS_PATH}/lib/cmake
42                              ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
44   # Check for existing LIBNFS. If version >= LIBNFS-VERSION file version, dont build
45   # A corner case, but if a linux/freebsd user WANTS to build internal libnfs, build anyway
46   if((libnfs_VERSION VERSION_LESS ${${MODULE}_VER} AND ENABLE_INTERNAL_NFS) OR
47      ((CORE_SYSTEM_NAME STREQUAL linux OR CORE_SYSTEM_NAME STREQUAL freebsd) AND ENABLE_INTERNAL_NFS))
48     # Build lib
49     buildlibnfs()
50   else()
51     if(TARGET libnfs::nfs)
52       # This is for the case where a distro provides a non standard (Debug/Release) config type
53       # convert this back to either DEBUG/RELEASE or just RELEASE
54       # we only do this because we use find_package_handle_standard_args for config time output
55       # and it isnt capable of handling TARGETS, so we have to extract the info
56       get_target_property(_LIBNFS_CONFIGURATIONS libnfs::nfs IMPORTED_CONFIGURATIONS)
57       foreach(_libnfs_config IN LISTS _LIBNFS_CONFIGURATIONS)
58         # Just set to RELEASE var so select_library_configurations can continue to work its magic
59         string(TOUPPER ${_libnfs_config} _libnfs_config_UPPER)
60         if((NOT ${_libnfs_config_UPPER} STREQUAL "RELEASE") AND
61            (NOT ${_libnfs_config_UPPER} STREQUAL "DEBUG"))
62           get_target_property(LIBNFS_LIBRARY_RELEASE libnfs::nfs IMPORTED_LOCATION_${_libnfs_config_UPPER})
63         else()
64           get_target_property(LIBNFS_LIBRARY_${_libnfs_config_UPPER} libnfs::nfs IMPORTED_LOCATION_${_libnfs_config_UPPER})
65         endif()
66       endforeach()
68       # libnfs cmake config doesnt include INTERFACE_INCLUDE_DIRECTORIES
69       find_path(LIBNFS_INCLUDE_DIR NAMES nfsc/libnfs.h
70                                    HINTS ${DEPENDS_PATH}/include
71                                    ${${CORE_PLATFORM_LC}_SEARCH_CONFIG})
72     else()
73       find_package(PkgConfig)
74       # Try pkgconfig based search as last resort
75       if(PKG_CONFIG_FOUND AND NOT (WIN32 OR WINDOWS_STORE))
76         if(NFS_FIND_VERSION)
77           if(NFS_FIND_VERSION_EXACT)
78             set(NFS_FIND_SPEC "=${NFS_FIND_VERSION_COMPLETE}")
79           else()
80             set(NFS_FIND_SPEC ">=${NFS_FIND_VERSION_COMPLETE}")
81           endif()
82         endif()
84         pkg_check_modules(PC_LIBNFS libnfs${NFS_FIND_SPEC} QUIET)
85       endif()
87       find_library(LIBNFS_LIBRARY_RELEASE NAMES nfs libnfs
88                                           HINTS ${DEPENDS_PATH}/lib
89                                                 ${PC_LIBNFS_LIBDIR}
90                                           ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
91       find_path(LIBNFS_INCLUDE_DIR nfsc/libnfs.h HINTS ${PC_LIBNFS_INCLUDEDIR}
92                                                        ${DEPENDS_PATH}/include
93                                                        ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
94       set(LIBNFS_VERSION ${PC_LIBNFS_VERSION})
95     endif()
96   endif()
98   include(SelectLibraryConfigurations)
99   select_library_configurations(LIBNFS)
101   include(FindPackageHandleStandardArgs)
102   find_package_handle_standard_args(NFS
103                                     REQUIRED_VARS LIBNFS_LIBRARY LIBNFS_INCLUDE_DIR
104                                     VERSION_VAR LIBNFS_VERSION)
106   if(NFS_FOUND)
107     # Pre existing lib, so we can run checks
108     if(NOT TARGET libnfs)
109       set(CMAKE_REQUIRED_INCLUDES "${LIBNFS_INCLUDE_DIR}")
110       set(CMAKE_REQUIRED_LIBRARIES ${LIBNFS_LIBRARY})
112       # Check for nfs_set_timeout
113       check_cxx_source_compiles("
114          ${LIBNFS_CXX_INCLUDE}
115          #include <nfsc/libnfs.h>
116          int main()
117          {
118            nfs_set_timeout(NULL, 0);
119          }
120       " NFS_SET_TIMEOUT)
122       if(NFS_SET_TIMEOUT)
123         list(APPEND _nfs_definitions HAS_NFS_SET_TIMEOUT)
124       endif()
126       # Check for mount_getexports_timeout
127       check_cxx_source_compiles("
128          ${LIBNFS_CXX_INCLUDE}
129          #include <nfsc/libnfs.h>
130          int main()
131          {
132            mount_getexports_timeout(NULL, 0);
133          }
134       " NFS_MOUNT_GETEXPORTS_TIMEOUT)
136       if(NFS_MOUNT_GETEXPORTS_TIMEOUT)
137         list(APPEND _nfs_definitions HAS_NFS_MOUNT_GETEXPORTS_TIMEOUT)
138       endif()
140       unset(CMAKE_REQUIRED_INCLUDES)
141       unset(CMAKE_REQUIRED_LIBRARIES)
142     endif()
144     list(APPEND _nfs_definitions HAS_FILESYSTEM_NFS)
146     # cmake target and not building internal
147     if(TARGET libnfs::nfs AND NOT TARGET libnfs)
148       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} ALIAS libnfs::nfs)
149     else()
151       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} UNKNOWN IMPORTED)
152       set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
153                                                                        IMPORTED_LOCATION "${LIBNFS_LIBRARY}")
154     endif()
156     # Test if target is an alias. We cant set properties on alias targets, and must find
157     # the actual target.
158     get_property(aliased_target TARGET "${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME}" PROPERTY ALIASED_TARGET)
159     if("${aliased_target}" STREQUAL "")
160       set(_nfs_target "${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME}")
161     else()
162       set(_nfs_target "${aliased_target}")
163     endif()
165     # We need to append in case the cmake config already has definitions
166     set_property(TARGET ${_nfs_target} APPEND PROPERTY
167                                               INTERFACE_COMPILE_DEFINITIONS ${_nfs_definitions})
169     # Need to manually set this, as libnfs cmake config does not provide INTERFACE_INCLUDE_DIRECTORIES
170     set_target_properties(${_nfs_target} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LIBNFS_INCLUDE_DIR})
172     if(TARGET libnfs)
173       add_dependencies(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} libnfs)
174     endif()
176     # Add internal build target when a Multi Config Generator is used
177     # We cant add a dependency based off a generator expression for targeted build types,
178     # https://gitlab.kitware.com/cmake/cmake/-/issues/19467
179     # therefore if the find heuristics only find the library, we add the internal build
180     # target to the project to allow user to manually trigger for any build type they need
181     # in case only a specific build type is actually available (eg Release found, Debug Required)
182     # This is mainly targeted for windows who required different runtime libs for different
183     # types, and they arent compatible
184     if(_multiconfig_generator)
185       if(NOT TARGET libnfs)
186         buildlibnfs()
187         set_target_properties(libnfs PROPERTIES EXCLUDE_FROM_ALL TRUE)
188       endif()
189       add_dependencies(build_internal_depends libnfs)
190     endif()
191   endif()
192 endif()