|
| 1 | +// Package libpath provides utilities for finding native library paths on Linux. |
| 2 | +// |
| 3 | +// # Overview |
| 4 | +// |
| 5 | +// This package helps locate shared libraries (.so files) on Linux systems, |
| 6 | +// supporting multiple distributions and package managers. It's particularly |
| 7 | +// useful for applications that need to link against libraries like GTK, |
| 8 | +// WebKit2GTK, or other system libraries at runtime. |
| 9 | +// |
| 10 | +// # Search Strategy |
| 11 | +// |
| 12 | +// The package uses a multi-tier search strategy, trying each method in order |
| 13 | +// until a library is found: |
| 14 | +// |
| 15 | +// 1. pkg-config: Queries the pkg-config database for library paths |
| 16 | +// 2. ldconfig: Searches the dynamic linker cache |
| 17 | +// 3. Filesystem: Scans common library directories |
| 18 | +// |
| 19 | +// # Supported Distributions |
| 20 | +// |
| 21 | +// The package includes default search paths for: |
| 22 | +// |
| 23 | +// - Debian/Ubuntu (multiarch paths like /usr/lib/x86_64-linux-gnu) |
| 24 | +// - Fedora/RHEL/CentOS (/usr/lib64, /usr/lib64/gtk-*) |
| 25 | +// - Arch Linux (/usr/lib/webkit2gtk-*, /usr/lib/gtk-*) |
| 26 | +// - openSUSE (/usr/lib64/gcc/x86_64-suse-linux) |
| 27 | +// - NixOS and Nix package manager |
| 28 | +// |
| 29 | +// # Package Manager Support |
| 30 | +// |
| 31 | +// Dynamic paths are discovered from: |
| 32 | +// |
| 33 | +// - Flatpak: Scans runtime directories via `flatpak --installations` |
| 34 | +// - Snap: Globs /snap/*/current/usr/lib* directories |
| 35 | +// - Nix: Checks ~/.nix-profile/lib and /run/current-system/sw/lib |
| 36 | +// |
| 37 | +// # Caching |
| 38 | +// |
| 39 | +// Dynamic path discovery (Flatpak, Snap, Nix) is cached for performance. |
| 40 | +// The cache is populated on first access and persists for the process lifetime. |
| 41 | +// Use [InvalidateCache] to force re-discovery if packages are installed/removed |
| 42 | +// during runtime. |
| 43 | +// |
| 44 | +// # Security |
| 45 | +// |
| 46 | +// The current directory (".") is never included in search paths by default, |
| 47 | +// as this is a security risk. Use [FindLibraryPathWithOptions] with |
| 48 | +// IncludeCurrentDir if you explicitly need this behavior (not recommended |
| 49 | +// for production). |
| 50 | +// |
| 51 | +// # Performance |
| 52 | +// |
| 53 | +// Typical lookup times (cached): |
| 54 | +// |
| 55 | +// - Found via pkg-config: ~2ms (spawns external process) |
| 56 | +// - Found via ldconfig: ~1.3ms (spawns external process) |
| 57 | +// - Found via filesystem: ~0.1ms (uses cached paths) |
| 58 | +// - Not found (worst case): ~20ms (searches all paths) |
| 59 | +// |
| 60 | +// # Example Usage |
| 61 | +// |
| 62 | +// // Find a library by its pkg-config name |
| 63 | +// path, err := libpath.FindLibraryPath("webkit2gtk-4.1") |
| 64 | +// if err != nil { |
| 65 | +// log.Fatal("WebKit2GTK not found:", err) |
| 66 | +// } |
| 67 | +// fmt.Println("Found at:", path) |
| 68 | +// |
| 69 | +// // Find a specific .so file |
| 70 | +// soPath, err := libpath.FindLibraryFile("libgtk-3.so") |
| 71 | +// if err != nil { |
| 72 | +// log.Fatal("GTK3 library file not found:", err) |
| 73 | +// } |
| 74 | +// fmt.Println("Library file:", soPath) |
| 75 | +// |
| 76 | +// // Get all library search paths |
| 77 | +// for _, p := range libpath.GetAllLibPaths() { |
| 78 | +// fmt.Println(p) |
| 79 | +// } |
| 80 | +// |
| 81 | +// # Multi-Library Search |
| 82 | +// |
| 83 | +// When you don't know which version of a library is installed, use the |
| 84 | +// multi-library search functions: |
| 85 | +// |
| 86 | +// // Find any available WebKit2GTK version (first found wins) |
| 87 | +// match, err := libpath.FindFirstLibrary("webkit2gtk-4.1", "webkit2gtk-4.0", "webkit2gtk-6.0") |
| 88 | +// if err != nil { |
| 89 | +// log.Fatal("No WebKit2GTK found") |
| 90 | +// } |
| 91 | +// fmt.Printf("Found %s at %s\n", match.Name, match.Path) |
| 92 | +// |
| 93 | +// // Prefer newer versions (ordered search) |
| 94 | +// match, err := libpath.FindFirstLibraryOrdered("gtk4", "gtk+-3.0") |
| 95 | +// |
| 96 | +// // Discover all available versions |
| 97 | +// matches := libpath.FindAllLibraries("gtk+-3.0", "gtk4", "webkit2gtk-4.0", "webkit2gtk-4.1") |
| 98 | +// for _, m := range matches { |
| 99 | +// fmt.Printf("Available: %s at %s\n", m.Name, m.Path) |
| 100 | +// } |
| 101 | +// |
| 102 | +// On non-Linux platforms, stub implementations are provided that always |
| 103 | +// return [LibraryNotFoundError]. |
| 104 | +package libpath |
0 commit comments