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})
13 set(CMAKE_ARGS -DBUILD_SHARED_LIBS=OFF
15 -DENABLE_DOCUMENTATION=OFF
17 -DENABLE_EXAMPLES=OFF)
19 if(WIN32 OR WINDOWS_STORE)
20 set(${MODULE}_C_FLAGS "/sdl-")
21 set(${MODULE}_CXX_FLAGS "/sdl-")
26 set(_nfs_definitions HAS_NFS_SET_TIMEOUT
27 HAS_NFS_MOUNT_GETEXPORTS_TIMEOUT)
30 include(cmake/scripts/common/ModuleHelpers.cmake)
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))
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})
61 get_target_property(LIBNFS_LIBRARY_${_libnfs_config_UPPER} libnfs::nfs IMPORTED_LOCATION_${_libnfs_config_UPPER})
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})
70 find_package(PkgConfig QUIET)
71 # Try pkgconfig based search as last resort
72 if(PKG_CONFIG_FOUND AND NOT (WIN32 OR WINDOWS_STORE))
74 if(NFS_FIND_VERSION_EXACT)
75 set(NFS_FIND_SPEC "=${NFS_FIND_VERSION_COMPLETE}")
77 set(NFS_FIND_SPEC ">=${NFS_FIND_VERSION_COMPLETE}")
81 pkg_check_modules(PC_LIBNFS libnfs${NFS_FIND_SPEC} QUIET)
84 find_library(LIBNFS_LIBRARY_RELEASE NAMES nfs libnfs
85 HINTS ${DEPENDS_PATH}/lib
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})
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)
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>
115 nfs_set_timeout(NULL, 0);
120 list(APPEND _nfs_definitions HAS_NFS_SET_TIMEOUT)
123 # Check for mount_getexports_timeout
124 check_cxx_source_compiles("
125 ${LIBNFS_CXX_INCLUDE}
126 #include <nfsc/libnfs.h>
129 mount_getexports_timeout(NULL, 0);
131 " NFS_MOUNT_GETEXPORTS_TIMEOUT)
133 if(NFS_MOUNT_GETEXPORTS_TIMEOUT)
134 list(APPEND _nfs_definitions HAS_NFS_MOUNT_GETEXPORTS_TIMEOUT)
137 unset(CMAKE_REQUIRED_INCLUDES)
138 unset(CMAKE_REQUIRED_LIBRARIES)
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)
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}")
153 # Test if target is an alias. We cant set properties on alias targets, and must find
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}")
159 set(_nfs_target "${aliased_target}")
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})
170 add_dependencies(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} libnfs)
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)
184 set_target_properties(libnfs PROPERTIES EXCLUDE_FROM_ALL TRUE)
186 add_dependencies(build_internal_depends libnfs)