|
1 | | -# FindLibBSD.cmake |
2 | | -# |
3 | | -# Find libbsd, a compatibility library providing BSD functions on other platforms |
| 1 | +# Finds the libbsd library, a compatibility library providing BSD functions. |
4 | 2 | # |
5 | 3 | # Defines: |
6 | | -# LibBSD_FOUND |
7 | | -# LibBSD_INCLUDE_DIRS |
8 | | -# LibBSD_LIBRARIES |
| 4 | +# LibBSD_FOUND - True if libbsd was found. |
| 5 | +# LibBSD_INCLUDE_DIRS - The include directories for libbsd. |
| 6 | +# LibBSD_LIBRARIES - The libraries to link against for libbsd. |
9 | 7 | # |
10 | | -# Provides imported target: |
11 | | -# LibBSD::LibBSD |
| 8 | +# Provides Imported Target: |
| 9 | +# LibBSD::LibBSD - The imported target for libbsd. |
| 10 | + |
| 11 | +cmake_policy(SET CMP0074 NEW) |
12 | 12 |
|
13 | | -cmake_policy(SET CMP0074 NEW) # Use CMAKE_PREFIX_PATH in find_package() |
| 13 | +find_package(PkgConfig QUIET) |
| 14 | +if (PkgConfig_FOUND) |
| 15 | + pkg_check_modules(PC_LibBSD QUIET libbsd) |
| 16 | +endif () |
14 | 17 |
|
15 | | -unset(LibBSD_FOUND CACHE) |
16 | | -unset(LibBSD_INCLUDE_DIRS CACHE) |
17 | | -unset(LibBSD_LIBRARIES CACHE) |
18 | | -unset(LibBSD_IMPORTED_TARGET_CREATED CACHE) |
| 18 | +find_path(LibBSD_INCLUDE_DIR |
| 19 | + NAMES bsd/string.h libbsd.h |
| 20 | + HINTS ${PC_LibBSD_INCLUDE_HINTS} ${PC_LibBSD_INCLUDEDIR} |
| 21 | +) |
19 | 22 |
|
20 | | -# Search hint prefixes — add your custom install prefix by setting LIBBSD_ROOT environment variable |
21 | | -set(_LIBBSD_HINTS |
22 | | - "$ENV{LIBBSD_ROOT}" |
23 | | - "/usr/local" |
24 | | - "/usr/lib" |
25 | | - "/opt/local" |
26 | | - "/opt" |
27 | | - "/usr" |
28 | | - "/usr/local/include" # Homebrew macOS common prefix |
29 | | - "/usr/include" |
| 23 | +find_library(LibBSD_LIBRARY |
| 24 | + NAMES bsd |
| 25 | + HINTS ${PC_LibBSD_LIBRARY_HINTS} ${PC_LibBSD_LIBDIR} |
30 | 26 | ) |
31 | 27 |
|
32 | | -# Remove empty and duplicate entries |
33 | | -list(FILTER _LIBBSD_HINTS EXCLUDE REGEX "^$") |
34 | | -list(REMOVE_DUPLICATES _LIBBSD_HINTS) |
| 28 | +include(FindPackageHandleStandardArgs) |
| 29 | +find_package_handle_standard_args(LibBSD |
| 30 | + FOUND_VAR LibBSD_FOUND |
| 31 | + REQUIRED_VARS LibBSD_LIBRARY LibBSD_INCLUDE_DIR |
| 32 | +) |
35 | 33 |
|
36 | | -# Remove project source directory to avoid matching your own bsd folder in source tree |
37 | | -if (DEFINED CMAKE_SOURCE_DIR) |
38 | | - list(FILTER _LIBBSD_HINTS EXCLUDE REGEX "^${CMAKE_SOURCE_DIR}$") |
39 | | -endif () |
| 34 | +if (LibBSD_FOUND) |
| 35 | + set(LibBSD_LIBRARIES ${LibBSD_LIBRARY}) |
| 36 | + set(LibBSD_INCLUDE_DIRS ${LibBSD_INCLUDE_DIR}) |
40 | 37 |
|
41 | | -function(_libbsd_create_imported_target) |
42 | 38 | if (NOT TARGET LibBSD::LibBSD) |
43 | 39 | add_library(LibBSD::LibBSD UNKNOWN IMPORTED) |
44 | 40 | set_target_properties(LibBSD::LibBSD PROPERTIES |
45 | | - INTERFACE_INCLUDE_DIRECTORIES "${LibBSD_INCLUDE_DIRS}" |
46 | | - IMPORTED_LOCATION "${LibBSD_LIBRARIES}" |
47 | | - ) |
48 | | - endif () |
49 | | - set(LibBSD_IMPORTED_TARGET_CREATED TRUE PARENT_SCOPE) |
50 | | -endfunction() |
51 | | - |
52 | | -# Function: resolve linker names like "bsd" into absolute library paths to avoid Ninja errors |
53 | | -function(_libbsd_resolve_libraries input_libs output_var) |
54 | | - set(_resolved_libs) |
55 | | - separate_arguments(_libs UNIX_COMMAND "${input_libs}") |
56 | | - |
57 | | - foreach (_lib IN LISTS _libs) |
58 | | - string(REGEX REPLACE "^-l" "" _libname "${_lib}") |
59 | | - |
60 | | - set(_found_path "") |
61 | | - |
62 | | - # First attempt: search in pkgconfig paths without NO_DEFAULT_PATH to allow /usr/local/lib lookup |
63 | | - if (DEFINED LIBBSD_LIBRARY_DIRS AND LIBBSD_LIBRARY_DIRS) |
64 | | - find_library(_found_path NAMES ${_libname} |
65 | | - PATHS ${LIBBSD_LIBRARY_DIRS} |
66 | | - NO_CMAKE_ENVIRONMENT_PATH NO_CMAKE_PATH NO_CMAKE_SYSTEM_PATH |
67 | | - ) |
68 | | - endif () |
69 | | - |
70 | | - # If not found, try default system search paths without restrictions |
71 | | - if (NOT _found_path) |
72 | | - find_library(_found_path NAMES ${_libname}) |
73 | | - endif () |
74 | | - |
75 | | - # If still not found, try to sanitize common library extensions manually |
76 | | - if (NOT _found_path) |
77 | | - if (NOT DEFINED TARGET_ARCH) |
78 | | - include("${CMAKE_SOURCE_DIR}/cmake/get_arch.cmake") |
79 | | - get_arch() |
80 | | - endif (NOT DEFINED TARGET_ARCH) |
81 | | - string(TOLOWER TARGET_ARCH arch) |
82 | | - |
83 | | - # try known install path hardcoded — add your known lib path: |
84 | | - foreach (p |
85 | | - "/usr/local/lib/lib${_libname}.dylib" |
86 | | - "/usr/local/lib/lib${_libname}.a" |
87 | | - "/usr/lib/${arch}-linux-gnu/libbsd.so" |
88 | | - "/usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/libbsd.so" |
89 | | - "/usr/lib/x86_64-linux-gnu/libbsd.so" |
90 | | - "/usr/lib/aarch64-linux-gnu/libbsd.so" |
91 | | - "/usr/lib/arm-linux-gnueabihf/libbsd.so" |
92 | | - "/usr/lib/i386-linux-gnu/libbsd.so" |
93 | | - "/usr/lib/libbsd.so") |
94 | | - if (EXISTS "${p}" AND NOT IS_DIRECTORY "${p}") |
95 | | - set(_found_path "${p}") |
96 | | - endif (EXISTS "${p}" AND NOT IS_DIRECTORY "${p}") |
97 | | - endforeach (p |
98 | | - "/usr/local/lib/lib${_libname}.dylib" |
99 | | - "/usr/local/lib/lib${_libname}.a" |
100 | | - "/usr/lib/${arch}-linux-gnu/libbsd.so" |
101 | | - "/usr/lib/${CMAKE_HOST_SYSTEM_PROCESSOR}-linux-gnu/libbsd.so" |
102 | | - "/usr/lib/x86_64-linux-gnu/libbsd.so" |
103 | | - "/usr/lib/aarch64-linux-gnu/libbsd.so" |
104 | | - "/usr/lib/arm-linux-gnueabihf/libbsd.so" |
105 | | - "/usr/lib/i386-linux-gnu/libbsd.so" |
106 | | - "/usr/lib/libbsd.so") |
107 | | - endif () |
108 | | - |
109 | | - if (_found_path) |
110 | | - list(APPEND _resolved_libs "${_found_path}") |
111 | | - else () |
112 | | - list(APPEND _resolved_libs "${_lib}") |
113 | | - message(WARNING "FindLibBSD: Failed to resolve absolute path for library: ${_lib}") |
114 | | - endif () |
115 | | - endforeach () |
116 | | - set(${output_var} "${_resolved_libs}" PARENT_SCOPE) |
117 | | -endfunction() |
118 | | - |
119 | | -# --- Attempt pkg-config detection first --- |
120 | | - |
121 | | -find_package(PkgConfig QUIET) |
122 | | - |
123 | | -if (PkgConfig_FOUND) |
124 | | - # Prepare / prepend PKG_CONFIG_PATH with hints pkgconfig dirs |
125 | | - set(_pkgconfig_paths) |
126 | | - foreach (_hint IN LISTS _LIBBSD_HINTS) |
127 | | - foreach (_subdir IN ITEMS "lib/pkgconfig" "lib64/pkgconfig" "share/pkgconfig") |
128 | | - if (_hint AND IS_DIRECTORY "${_hint}/${_subdir}") |
129 | | - list(APPEND _pkgconfig_paths "${_hint}/${_subdir}") |
130 | | - endif () |
131 | | - endforeach () |
132 | | - endforeach () |
133 | | - list(REMOVE_DUPLICATES _pkgconfig_paths) |
134 | | - |
135 | | - if (WIN32) |
136 | | - set(_path_sep ";") |
137 | | - else () |
138 | | - set(_path_sep " :") |
139 | | - endif () |
140 | | - |
141 | | - if (_pkgconfig_paths) |
142 | | - list(JOIN _pkgconfig_paths "${_path_sep}" _joined_pcp) |
143 | | - if (DEFINED ENV{PKG_CONFIG_PATH} AND NOT "$ENV{PKG_CONFIG_PATH}" STREQUAL "") |
144 | | - set(ENV{PKG_CONFIG_PATH} "${_joined_pcp}${_path_sep}$ENV{PKG_CONFIG_PATH}") |
145 | | - else () |
146 | | - set(ENV{PKG_CONFIG_PATH} "${_joined_pcp}") |
147 | | - endif () |
148 | | - endif () |
149 | | - |
150 | | - pkg_check_modules(LIBBSD libbsd) |
151 | | - |
152 | | - if (LIBBSD_FOUND) |
153 | | - # Resolve libraries to absolute paths |
154 | | - _libbsd_resolve_libraries("${LIBBSD_LIBRARIES}" LibBSD_LIBRARIES) |
155 | | - |
156 | | - set(LibBSD_FOUND TRUE CACHE BOOL "LibBSD found via pkg-config") |
157 | | - set(LibBSD_INCLUDE_DIRS ${LIBBSD_INCLUDE_DIRS} CACHE STRING "LibBSD include dirs") |
158 | | - set(LibBSD_LIBRARIES "${LibBSD_LIBRARIES}" CACHE STRING "LibBSD libraries") |
159 | | - |
160 | | - # Normalize include dirs |
161 | | - set(_abs_inc_dirs) |
162 | | - foreach (dir IN LISTS LibBSD_INCLUDE_DIRS) |
163 | | - get_filename_component(abs_dir "${dir}" ABSOLUTE) |
164 | | - list(APPEND _abs_inc_dirs "${abs_dir}") |
165 | | - endforeach () |
166 | | - list(REMOVE_DUPLICATES _abs_inc_dirs) |
167 | | - set(LibBSD_INCLUDE_DIRS "${_abs_inc_dirs}" CACHE INTERNAL "") |
168 | | - |
169 | | - _libbsd_create_imported_target() |
170 | | - |
171 | | - message(STATUS "FindLibBSD: Found via pkg-config") |
172 | | - message(STATUS " Include dirs: ${LibBSD_INCLUDE_DIRS}") |
173 | | - message(STATUS " Libraries: ${LibBSD_LIBRARIES}") |
174 | | - endif () |
175 | | -endif () |
176 | | - |
177 | | -# --- Manual fallback if pkg-config fails --- |
178 | | - |
179 | | -if (NOT LibBSD_FOUND) |
180 | | - # Try to find unique header: prefer libbsd.h |
181 | | - find_path(LibBSD_INCLUDE_DIRS |
182 | | - NAMES libbsd.h |
183 | | - PATHS ${_LIBBSD_HINTS} |
184 | | - PATH_SUFFIXES include |
185 | | - NO_DEFAULT_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CMAKE_PATH NO_CMAKE_SYSTEM_PATH |
186 | | - ) |
187 | | - |
188 | | - # Optional fallback: less reliable and may pick local bsd/ |
189 | | - if (NOT LibBSD_INCLUDE_DIRS) |
190 | | - find_path(LibBSD_INCLUDE_DIRS |
191 | | - NAMES bsd/string.h |
192 | | - PATHS ${_LIBBSD_HINTS} |
193 | | - PATH_SUFFIXES include |
194 | | - NO_DEFAULT_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CMAKE_PATH NO_CMAKE_SYSTEM_PATH |
| 41 | + IMPORTED_LOCATION "${LibBSD_LIBRARY}" |
| 42 | + INTERFACE_INCLUDE_DIRECTORIES "${LibBSD_INCLUDE_DIR}" |
195 | 43 | ) |
196 | | - endif () |
197 | | - |
198 | | - find_library(LibBSD_LIBRARIES |
199 | | - NAMES bsd |
200 | | - PATHS ${_LIBBSD_HINTS} |
201 | | - PATH_SUFFIXES lib lib64 |
202 | | - NO_DEFAULT_PATH NO_CMAKE_ENVIRONMENT_PATH NO_CMAKE_PATH NO_CMAKE_SYSTEM_PATH |
203 | | - ) |
204 | | - |
205 | | - if (LibBSD_INCLUDE_DIRS AND LibBSD_LIBRARIES) |
206 | | - get_filename_component(LibBSD_INCLUDE_DIRS_ABS "${LibBSD_INCLUDE_DIRS}" ABSOLUTE) |
207 | | - set(LibBSD_INCLUDE_DIRS "${LibBSD_INCLUDE_DIRS_ABS}" CACHE INTERNAL "") |
208 | | - get_filename_component(LibBSD_LIBRARIES_ABS "${LibBSD_LIBRARIES}" ABSOLUTE) |
209 | | - set(LibBSD_LIBRARIES "${LibBSD_LIBRARIES_ABS}" CACHE INTERNAL "") |
210 | | - |
211 | | - set(LibBSD_FOUND TRUE CACHE BOOL "LibBSD found via fallback") |
212 | | - |
213 | | - _libbsd_create_imported_target() |
214 | | - |
215 | | - message(STATUS "FindLibBSD: Found via manual fallback") |
216 | | - message(STATUS " Include dirs: ${LibBSD_INCLUDE_DIRS}") |
217 | | - message(STATUS " Libraries: ${LibBSD_LIBRARIES}") |
218 | | - else () |
219 | | - set(LibBSD_FOUND FALSE CACHE BOOL "LibBSD found") |
220 | | - message(STATUS "FindLibBSD: LibBSD not found") |
221 | | - endif () |
222 | | -endif () |
| 44 | + endif (NOT TARGET LibBSD::LibBSD) |
| 45 | +endif (LibBSD_FOUND) |
223 | 46 |
|
224 | | -mark_as_advanced(LibBSD_FOUND LibBSD_INCLUDE_DIRS LibBSD_LIBRARIES) |
| 47 | +mark_as_advanced(LibBSD_INCLUDE_DIR LibBSD_LIBRARY) |
0 commit comments