Merge pull request #26350 from jjd-uk/estuary_media_align
[xbmc.git] / cmake / modules / FindNFS.cmake
blob7a43d9b20d8e2a14d7a65a35650acd54f5b8fe24
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-")
22     endif()
24     BUILD_DEP_TARGET()
26     set(_nfs_definitions HAS_NFS_SET_TIMEOUT
27                          HAS_NFS_MOUNT_GETEXPORTS_TIMEOUT)
28   endmacro()
30   include(cmake/scripts/common/ModuleHelpers.cmake)
32   set(MODULE_LC libnfs)
34   SETUP_BUILD_VARS()
36   # Search for cmake config. Suitable for all platforms including windows
37   find_package(libnfs CONFIG QUIET
38                              HINTS ${DEPENDS_PATH}/lib/cmake
39                              ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
41   # Check for existing LIBNFS. If version >= LIBNFS-VERSION file version, dont build
42   # A corner case, but if a linux/freebsd user WANTS to build internal libnfs, build anyway
43   if((libnfs_VERSION VERSION_LESS ${${MODULE}_VER} AND ENABLE_INTERNAL_NFS) OR
44      ((CORE_SYSTEM_NAME STREQUAL linux OR CORE_SYSTEM_NAME STREQUAL freebsd) AND ENABLE_INTERNAL_NFS))
45     # Build lib
46     buildlibnfs()
47   else()
48     if(TARGET libnfs::nfs)
49       # This is for the case where a distro provides a non standard (Debug/Release) config type
50       # convert this back to either DEBUG/RELEASE or just RELEASE
51       # we only do this because we use find_package_handle_standard_args for config time output
52       # and it isnt capable of handling TARGETS, so we have to extract the info
53       get_target_property(_LIBNFS_CONFIGURATIONS libnfs::nfs IMPORTED_CONFIGURATIONS)
54       foreach(_libnfs_config IN LISTS _LIBNFS_CONFIGURATIONS)
55         # Just set to RELEASE var so select_library_configurations can continue to work its magic
56         string(TOUPPER ${_libnfs_config} _libnfs_config_UPPER)
57         if((NOT ${_libnfs_config_UPPER} STREQUAL "RELEASE") AND
58            (NOT ${_libnfs_config_UPPER} STREQUAL "DEBUG"))
59           get_target_property(LIBNFS_LIBRARY_RELEASE libnfs::nfs IMPORTED_LOCATION_${_libnfs_config_UPPER})
60         else()
61           get_target_property(LIBNFS_LIBRARY_${_libnfs_config_UPPER} libnfs::nfs IMPORTED_LOCATION_${_libnfs_config_UPPER})
62         endif()
63       endforeach()
65       # libnfs cmake config doesnt include INTERFACE_INCLUDE_DIRECTORIES
66       find_path(LIBNFS_INCLUDE_DIR NAMES nfsc/libnfs.h
67                                    HINTS ${DEPENDS_PATH}/include
68                                    ${${CORE_PLATFORM_LC}_SEARCH_CONFIG})
69     else()
70       find_package(PkgConfig QUIET)
71       # Try pkgconfig based search as last resort
72       if(PKG_CONFIG_FOUND AND NOT (WIN32 OR WINDOWS_STORE))
73         if(NFS_FIND_VERSION)
74           if(NFS_FIND_VERSION_EXACT)
75             set(NFS_FIND_SPEC "=${NFS_FIND_VERSION_COMPLETE}")
76           else()
77             set(NFS_FIND_SPEC ">=${NFS_FIND_VERSION_COMPLETE}")
78           endif()
79         endif()
81         pkg_check_modules(PC_LIBNFS libnfs${NFS_FIND_SPEC} QUIET)
82       endif()
84       find_library(LIBNFS_LIBRARY_RELEASE NAMES nfs libnfs
85                                           HINTS ${DEPENDS_PATH}/lib
86                                                 ${PC_LIBNFS_LIBDIR}
87                                           ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
88       find_path(LIBNFS_INCLUDE_DIR nfsc/libnfs.h HINTS ${PC_LIBNFS_INCLUDEDIR}
89                                                        ${DEPENDS_PATH}/include
90                                                        ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
91       set(LIBNFS_VERSION ${PC_LIBNFS_VERSION})
92     endif()
93   endif()
95   include(SelectLibraryConfigurations)
96   select_library_configurations(LIBNFS)
98   include(FindPackageHandleStandardArgs)
99   find_package_handle_standard_args(NFS
100                                     REQUIRED_VARS LIBNFS_LIBRARY LIBNFS_INCLUDE_DIR
101                                     VERSION_VAR LIBNFS_VERSION)
103   if(NFS_FOUND)
104     # Pre existing lib, so we can run checks
105     if(NOT TARGET libnfs)
106       set(CMAKE_REQUIRED_INCLUDES "${LIBNFS_INCLUDE_DIR}")
107       set(CMAKE_REQUIRED_LIBRARIES ${LIBNFS_LIBRARY})
109       # Check for nfs_set_timeout
110       check_cxx_source_compiles("
111          ${LIBNFS_CXX_INCLUDE}
112          #include <nfsc/libnfs.h>
113          int main()
114          {
115            nfs_set_timeout(NULL, 0);
116          }
117       " NFS_SET_TIMEOUT)
119       if(NFS_SET_TIMEOUT)
120         list(APPEND _nfs_definitions HAS_NFS_SET_TIMEOUT)
121       endif()
123       # Check for mount_getexports_timeout
124       check_cxx_source_compiles("
125          ${LIBNFS_CXX_INCLUDE}
126          #include <nfsc/libnfs.h>
127          int main()
128          {
129            mount_getexports_timeout(NULL, 0);
130          }
131       " NFS_MOUNT_GETEXPORTS_TIMEOUT)
133       if(NFS_MOUNT_GETEXPORTS_TIMEOUT)
134         list(APPEND _nfs_definitions HAS_NFS_MOUNT_GETEXPORTS_TIMEOUT)
135       endif()
137       unset(CMAKE_REQUIRED_INCLUDES)
138       unset(CMAKE_REQUIRED_LIBRARIES)
139     endif()
141     list(APPEND _nfs_definitions HAS_FILESYSTEM_NFS)
143     # cmake target and not building internal
144     if(TARGET libnfs::nfs AND NOT TARGET libnfs)
145       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} ALIAS libnfs::nfs)
146     else()
148       add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} UNKNOWN IMPORTED)
149       set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
150                                                                        IMPORTED_LOCATION "${LIBNFS_LIBRARY}")
151     endif()
153     # Test if target is an alias. We cant set properties on alias targets, and must find
154     # the actual target.
155     get_property(aliased_target TARGET "${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME}" PROPERTY ALIASED_TARGET)
156     if("${aliased_target}" STREQUAL "")
157       set(_nfs_target "${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME}")
158     else()
159       set(_nfs_target "${aliased_target}")
160     endif()
162     # We need to append in case the cmake config already has definitions
163     set_property(TARGET ${_nfs_target} APPEND PROPERTY
164                                               INTERFACE_COMPILE_DEFINITIONS ${_nfs_definitions})
166     # Need to manually set this, as libnfs cmake config does not provide INTERFACE_INCLUDE_DIRECTORIES
167     set_target_properties(${_nfs_target} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LIBNFS_INCLUDE_DIR})
169     if(TARGET libnfs)
170       add_dependencies(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} libnfs)
171     endif()
173     # Add internal build target when a Multi Config Generator is used
174     # We cant add a dependency based off a generator expression for targeted build types,
175     # https://gitlab.kitware.com/cmake/cmake/-/issues/19467
176     # therefore if the find heuristics only find the library, we add the internal build
177     # target to the project to allow user to manually trigger for any build type they need
178     # in case only a specific build type is actually available (eg Release found, Debug Required)
179     # This is mainly targeted for windows who required different runtime libs for different
180     # types, and they arent compatible
181     if(_multiconfig_generator)
182       if(NOT TARGET libnfs)
183         buildlibnfs()
184         set_target_properties(libnfs PROPERTIES EXCLUDE_FROM_ALL TRUE)
185       endif()
186       add_dependencies(build_internal_depends libnfs)
187     endif()
188   endif()
189 endif()