fix(deps): repin puregotk lazy registration fix
- commit
- d685b6e820d6baba0d0dea254169664012ed203d
- author
- brice · · 2026-07-14 18:24 UTC
- parent
- ec6db2e
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
907 files · +27874 -25294
M go.mod +3 -1
| @@ -11,7 +11,7 @@ require ( | |||
| 11 | 11 | github.com/bnema/purego-pipewire v0.1.5-0.20260714124450-6504b9cdf02a | |
| 12 | 12 | github.com/bnema/purego-sqlite v0.1.4-0.20260714131321-3614fe683b9c | |
| 13 | 13 | github.com/bnema/purego-webp v0.2.1-0.20260714152238-7bc9c402fdf6 | |
| 14 | - | github.com/bnema/puregotk v0.7.0 | |
| 14 | + | github.com/bnema/puregotk v0.7.1-0.20260714181430-679a9ed72959 | |
| 15 | 15 | github.com/charmbracelet/bubbles v1.0.0 | |
| 16 | 16 | github.com/charmbracelet/bubbletea v1.3.10 | |
| 17 | 17 | github.com/charmbracelet/lipgloss v1.1.0 | |
| @@ -47,6 +47,7 @@ require ( | |||
| 47 | 47 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect | |
| 48 | 48 | github.com/fatih/color v1.19.0 // indirect | |
| 49 | 49 | github.com/go-viper/mapstructure/v2 v2.5.0 // indirect | |
| 50 | + | github.com/google/go-cmp v0.7.0 // indirect | |
| 50 | 51 | github.com/inconshreveable/mousetrap v1.1.0 // indirect | |
| 51 | 52 | github.com/lucasb-eyer/go-colorful v1.4.0 // indirect | |
| 52 | 53 | github.com/mattn/go-colorable v0.1.14 // indirect | |
| @@ -80,6 +81,7 @@ require ( | |||
| 80 | 81 | golang.org/x/text v0.40.0 // indirect | |
| 81 | 82 | golang.org/x/tools v0.47.0 // indirect | |
| 82 | 83 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | |
| 84 | + | mvdan.cc/gofumpt v0.9.2 // indirect | |
| 83 | 85 | ) | |
| 84 | 86 | ||
| 85 | 87 | tool github.com/a-h/templ/cmd/templ | |
| @@ -11,7 +11,7 @@ require ( | |||||
| 11 | github.com/bnema/purego-pipewire v0.1.5-0.20260714124450-6504b9cdf02a | 11 | github.com/bnema/purego-pipewire v0.1.5-0.20260714124450-6504b9cdf02a | ||
| 12 | github.com/bnema/purego-sqlite v0.1.4-0.20260714131321-3614fe683b9c | 12 | github.com/bnema/purego-sqlite v0.1.4-0.20260714131321-3614fe683b9c | ||
| 13 | github.com/bnema/purego-webp v0.2.1-0.20260714152238-7bc9c402fdf6 | 13 | github.com/bnema/purego-webp v0.2.1-0.20260714152238-7bc9c402fdf6 | ||
| 14 | - | github.com/bnema/puregotk v0.7.0 | 14 | + | github.com/bnema/puregotk v0.7.1-0.20260714181430-679a9ed72959 |
| 15 | github.com/charmbracelet/bubbles v1.0.0 | 15 | github.com/charmbracelet/bubbles v1.0.0 | ||
| 16 | github.com/charmbracelet/bubbletea v1.3.10 | 16 | github.com/charmbracelet/bubbletea v1.3.10 | ||
| 17 | github.com/charmbracelet/lipgloss v1.1.0 | 17 | github.com/charmbracelet/lipgloss v1.1.0 | ||
| @@ -47,6 +47,7 @@ require ( | |||||
| 47 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect | 47 | github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect | ||
| 48 | github.com/fatih/color v1.19.0 // indirect | 48 | github.com/fatih/color v1.19.0 // indirect | ||
| 49 | github.com/go-viper/mapstructure/v2 v2.5.0 // indirect | 49 | github.com/go-viper/mapstructure/v2 v2.5.0 // indirect | ||
| 50 | + | github.com/google/go-cmp v0.7.0 // indirect | |||
| 50 | github.com/inconshreveable/mousetrap v1.1.0 // indirect | 51 | github.com/inconshreveable/mousetrap v1.1.0 // indirect | ||
| 51 | github.com/lucasb-eyer/go-colorful v1.4.0 // indirect | 52 | github.com/lucasb-eyer/go-colorful v1.4.0 // indirect | ||
| 52 | github.com/mattn/go-colorable v0.1.14 // indirect | 53 | github.com/mattn/go-colorable v0.1.14 // indirect | ||
| @@ -80,6 +81,7 @@ require ( | |||||
| 80 | golang.org/x/text v0.40.0 // indirect | 81 | golang.org/x/text v0.40.0 // indirect | ||
| 81 | golang.org/x/tools v0.47.0 // indirect | 82 | golang.org/x/tools v0.47.0 // indirect | ||
| 82 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | 83 | gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect | ||
| 84 | + | mvdan.cc/gofumpt v0.9.2 // indirect | |||
| 83 | ) | 85 | ) | ||
| 84 | 86 | ||||
| 85 | tool github.com/a-h/templ/cmd/templ | 87 | tool github.com/a-h/templ/cmd/templ | ||
M go.sum +4 -16 generated file: collapsed
M vendor/github.com/bnema/puregotk/internal/core/core.go +197 -4
| @@ -8,6 +8,7 @@ import ( | |||
| 8 | 8 | "os" | |
| 9 | 9 | "os/exec" | |
| 10 | 10 | "path/filepath" | |
| 11 | + | "reflect" | |
| 11 | 12 | "runtime" | |
| 12 | 13 | "strings" | |
| 13 | 14 | "sync" | |
| @@ -16,6 +17,184 @@ import ( | |||
| 16 | 17 | "github.com/bnema/purego" | |
| 17 | 18 | ) | |
| 18 | 19 | ||
| 20 | + | // LibraryOpener opens a shared library path and returns its handle. | |
| 21 | + | type LibraryOpener func(path string) (uintptr, error) | |
| 22 | + | ||
| 23 | + | // LibraryPathResolver returns the candidate paths for a named library. | |
| 24 | + | type LibraryPathResolver func(library string) ([]string, error) | |
| 25 | + | ||
| 26 | + | // SymbolResolver resolves and publishes a symbol into target using library handles. | |
| 27 | + | type SymbolResolver func(target any, libraries []uintptr, symbol string) error | |
| 28 | + | ||
| 29 | + | // LazyResolver safely caches library opens and symbol publication. It is safe for | |
| 30 | + | // concurrent use. Its injected dependencies make resolution deterministic in tests. | |
| 31 | + | type LazyResolver struct { | |
| 32 | + | paths LibraryPathResolver | |
| 33 | + | opener LibraryOpener | |
| 34 | + | resolve SymbolResolver | |
| 35 | + | ||
| 36 | + | mu sync.Mutex | |
| 37 | + | libraries map[string]*lazyLibrary | |
| 38 | + | symbols map[lazySymbolKey]*lazySymbol | |
| 39 | + | } | |
| 40 | + | ||
| 41 | + | type lazyLibrary struct { | |
| 42 | + | once sync.Once | |
| 43 | + | handles []uintptr | |
| 44 | + | err error | |
| 45 | + | } | |
| 46 | + | ||
| 47 | + | type lazySymbolKey struct { | |
| 48 | + | library string | |
| 49 | + | symbol string | |
| 50 | + | target uintptr | |
| 51 | + | } | |
| 52 | + | ||
| 53 | + | type lazySymbol struct { | |
| 54 | + | once sync.Once | |
| 55 | + | err error | |
| 56 | + | } | |
| 57 | + | ||
| 58 | + | // NewLazyResolver constructs an isolated lazy resolver. All dependencies must | |
| 59 | + | // be non-nil. | |
| 60 | + | func NewLazyResolver(paths LibraryPathResolver, opener LibraryOpener, resolve SymbolResolver) *LazyResolver { | |
| 61 | + | if paths == nil || opener == nil || resolve == nil { | |
| 62 | + | panic("core: lazy resolver dependencies must not be nil") | |
| 63 | + | } | |
| 64 | + | return &LazyResolver{ | |
| 65 | + | paths: paths, | |
| 66 | + | opener: opener, | |
| 67 | + | resolve: resolve, | |
| 68 | + | libraries: make(map[string]*lazyLibrary), | |
| 69 | + | symbols: make(map[lazySymbolKey]*lazySymbol), | |
| 70 | + | } | |
| 71 | + | } | |
| 72 | + | ||
| 73 | + | // Register resolves and publishes symbol into target once. Library and symbol | |
| 74 | + | // failures are cached as well as successful resolutions. | |
| 75 | + | func (r *LazyResolver) Register(target any, library, symbol string) error { | |
| 76 | + | if r == nil { | |
| 77 | + | return fmt.Errorf("core: nil lazy resolver") | |
| 78 | + | } | |
| 79 | + | value := reflect.ValueOf(target) | |
| 80 | + | if !value.IsValid() || value.Kind() != reflect.Ptr || value.IsNil() { | |
| 81 | + | return fmt.Errorf("core: lazy symbol target must be a non-nil pointer") | |
| 82 | + | } | |
| 83 | + | key := lazySymbolKey{library: library, symbol: symbol, target: value.Pointer()} | |
| 84 | + | ||
| 85 | + | r.mu.Lock() | |
| 86 | + | state := r.symbols[key] | |
| 87 | + | if state == nil { | |
| 88 | + | // Generated binding tests and embedders may provide a function before | |
| 89 | + | // the first call. Do not replace that explicit implementation with a | |
| 90 | + | // symbol. Once a resolution has started, use its sync.Once rather than | |
| 91 | + | // inspecting a function that the resolver may be publishing. | |
| 92 | + | if value.Elem().Kind() == reflect.Func && !value.Elem().IsNil() { | |
| 93 | + | r.mu.Unlock() | |
| 94 | + | return nil | |
| 95 | + | } | |
| 96 | + | state = &lazySymbol{} | |
| 97 | + | r.symbols[key] = state | |
| 98 | + | } | |
| 99 | + | r.mu.Unlock() | |
| 100 | + | ||
| 101 | + | state.once.Do(func() { | |
| 102 | + | libraries, err := r.open(library) | |
| 103 | + | if err != nil { | |
| 104 | + | state.err = err | |
| 105 | + | return | |
| 106 | + | } | |
| 107 | + | state.err = r.resolve(target, libraries, symbol) | |
| 108 | + | }) | |
| 109 | + | return state.err | |
| 110 | + | } | |
| 111 | + | ||
| 112 | + | // RegisterOptional is Register for optional libraries. It reports whether the | |
| 113 | + | // symbol was published and caches an unavailable-library failure. | |
| 114 | + | func (r *LazyResolver) RegisterOptional(target any, library, symbol string) bool { | |
| 115 | + | return r.Register(target, library, symbol) == nil | |
| 116 | + | } | |
| 117 | + | ||
| 118 | + | func (r *LazyResolver) open(name string) ([]uintptr, error) { | |
| 119 | + | r.mu.Lock() | |
| 120 | + | state := r.libraries[name] | |
| 121 | + | if state == nil { | |
| 122 | + | state = &lazyLibrary{} | |
| 123 | + | r.libraries[name] = state | |
| 124 | + | } | |
| 125 | + | r.mu.Unlock() | |
| 126 | + | ||
| 127 | + | state.once.Do(func() { | |
| 128 | + | paths, err := r.paths(name) | |
| 129 | + | if err != nil { | |
| 130 | + | state.err = err | |
| 131 | + | return | |
| 132 | + | } | |
| 133 | + | if len(paths) == 0 { | |
| 134 | + | state.err = fmt.Errorf("core: no paths found for library %s", name) | |
| 135 | + | return | |
| 136 | + | } | |
| 137 | + | var lastErr error | |
| 138 | + | for _, path := range paths { | |
| 139 | + | handle, err := r.opener(path) | |
| 140 | + | if err != nil { | |
| 141 | + | lastErr = err | |
| 142 | + | continue | |
| 143 | + | } | |
| 144 | + | state.handles = append(state.handles, handle) | |
| 145 | + | } | |
| 146 | + | if len(state.handles) == 0 { | |
| 147 | + | state.err = fmt.Errorf("core: open library %s: %w", name, lastErr) | |
| 148 | + | } | |
| 149 | + | }) | |
| 150 | + | return state.handles, state.err | |
| 151 | + | } | |
| 152 | + | ||
| 153 | + | var defaultLazyResolver = NewLazyResolver( | |
| 154 | + | func(name string) ([]string, error) { | |
| 155 | + | paths := tryFindPaths(name) | |
| 156 | + | if len(paths) == 0 { | |
| 157 | + | return nil, fmt.Errorf("core: no paths found for library %s", name) | |
| 158 | + | } | |
| 159 | + | return paths, nil | |
| 160 | + | }, | |
| 161 | + | func(path string) (uintptr, error) { | |
| 162 | + | return purego.Dlopen(path, purego.RTLD_NOW|purego.RTLD_GLOBAL) | |
| 163 | + | }, | |
| 164 | + | func(target any, libraries []uintptr, symbol string) error { | |
| 165 | + | var lastErr error | |
| 166 | + | for _, library := range libraries { | |
| 167 | + | address, err := purego.Dlsym(library, symbol) | |
| 168 | + | if err != nil { | |
| 169 | + | lastErr = err | |
| 170 | + | continue | |
| 171 | + | } | |
| 172 | + | registerFuncSafe(target, address) | |
| 173 | + | return nil | |
| 174 | + | } | |
| 175 | + | return fmt.Errorf("core: resolve symbol %s: %w", symbol, lastErr) | |
| 176 | + | }, | |
| 177 | + | ) | |
| 178 | + | ||
| 179 | + | // LazyRegister publishes a generated function's native symbol on its first | |
| 180 | + | // call. Required-library failures panic; optional-library failures return false. | |
| 181 | + | func LazyRegister(target any, library, symbol string, optional bool) bool { | |
| 182 | + | if err := defaultLazyResolver.Register(target, library, symbol); err != nil { | |
| 183 | + | if optional { | |
| 184 | + | return false | |
| 185 | + | } | |
| 186 | + | panic(err) | |
| 187 | + | } | |
| 188 | + | return true | |
| 189 | + | } | |
| 190 | + | ||
| 191 | + | // LibraryAvailable reports whether an optional library can be opened. The open | |
| 192 | + | // result, including failure, is shared with LazyRegister. | |
| 193 | + | func LibraryAvailable(library string) bool { | |
| 194 | + | _, err := defaultLazyResolver.open(library) | |
| 195 | + | return err == nil | |
| 196 | + | } | |
| 197 | + | ||
| 19 | 198 | func PuregoSafeRegister(fptr interface{}, libs []uintptr, name string) { | |
| 20 | 199 | for _, lib := range libs { | |
| 21 | 200 | sym, err := purego.Dlsym(lib, name) | |
| @@ -53,7 +232,10 @@ var paths = map[string][]string{ | |||
| 53 | 232 | ||
| 54 | 233 | // names is a lookup from library names to shared object filenames | |
| 55 | 234 | // This is populated dynamically via SetSharedLibrary | |
| 56 | - | var names = map[string][]string{} | |
| 235 | + | var ( | |
| 236 | + | libraryConfigMu sync.RWMutex | |
| 237 | + | names = map[string][]string{} | |
| 238 | + | ) | |
| 57 | 239 | ||
| 58 | 240 | // pkgConfNames is a lookup from library names to pkg-config library names | |
| 59 | 241 | // This is populated dynamically via SetPackageName | |
| @@ -63,6 +245,8 @@ var pkgConfNames = map[string]string{} | |||
| 63 | 245 | // This is used by the code generator to set package names from GIR files. | |
| 64 | 246 | // It won't override existing entries to preserve defaults. | |
| 65 | 247 | func SetPackageName(libName, pkgName string) { | |
| 248 | + | libraryConfigMu.Lock() | |
| 249 | + | defer libraryConfigMu.Unlock() | |
| 66 | 250 | if _, exists := pkgConfNames[libName]; !exists && pkgName != "" { | |
| 67 | 251 | pkgConfNames[libName] = pkgName | |
| 68 | 252 | } | |
| @@ -72,16 +256,22 @@ func SetPackageName(libName, pkgName string) { | |||
| 72 | 256 | // This is used by the code generator to set library names from GIR files. | |
| 73 | 257 | // It won't override existing entries to preserve defaults. | |
| 74 | 258 | func SetSharedLibraries(libName string, sharedLibs []string) { | |
| 259 | + | libraryConfigMu.Lock() | |
| 260 | + | defer libraryConfigMu.Unlock() | |
| 75 | 261 | if _, exists := names[libName]; !exists && len(sharedLibs) > 0 { | |
| 76 | - | names[libName] = sharedLibs | |
| 262 | + | names[libName] = append([]string(nil), sharedLibs...) | |
| 77 | 263 | } | |
| 78 | 264 | } | |
| 79 | 265 | ||
| 80 | 266 | // findSos tries to find all shared objects from a path and a library name | |
| 81 | 267 | // It does this by mapping the library name to all suitable shared object filenames and then trying some suffixes | |
| 82 | 268 | func findSos(path string, name string) []string { | |
| 269 | + | libraryConfigMu.RLock() | |
| 270 | + | libraryNames := append([]string(nil), names[name]...) | |
| 271 | + | libraryConfigMu.RUnlock() | |
| 272 | + | ||
| 83 | 273 | sos := []string{} | |
| 84 | - | for _, n := range names[name] { | |
| 274 | + | for _, n := range libraryNames { | |
| 85 | 275 | suffixes := []string{"", ".0", ".1", ".2"} | |
| 86 | 276 | fn := filepath.Join(path, n) | |
| 87 | 277 | for _, s := range suffixes { | |
| @@ -97,7 +287,10 @@ func findSos(path string, name string) []string { | |||
| 97 | 287 | // it does this by running pkg-config --libs-only-L libname | |
| 98 | 288 | // and then it loops over the directories returned and finds all suitable ones | |
| 99 | 289 | func findPkgConf(name string) []string { | |
| 100 | - | cmd := exec.Command("pkg-config", "--libs-only-L", pkgConfNames[name]) | |
| 290 | + | libraryConfigMu.RLock() | |
| 291 | + | pkgName := pkgConfNames[name] | |
| 292 | + | libraryConfigMu.RUnlock() | |
| 293 | + | cmd := exec.Command("pkg-config", "--libs-only-L", pkgName) | |
| 101 | 294 | var out, outerr bytes.Buffer | |
| 102 | 295 | cmd.Stdout = &out | |
| 103 | 296 | cmd.Stderr = &outerr | |
| @@ -8,6 +8,7 @@ import ( | |||||
| 8 | "os" | 8 | "os" | ||
| 9 | "os/exec" | 9 | "os/exec" | ||
| 10 | "path/filepath" | 10 | "path/filepath" | ||
| 11 | + | "reflect" | |||
| 11 | "runtime" | 12 | "runtime" | ||
| 12 | "strings" | 13 | "strings" | ||
| 13 | "sync" | 14 | "sync" | ||
| @@ -16,6 +17,184 @@ import ( | |||||
| 16 | "github.com/bnema/purego" | 17 | "github.com/bnema/purego" | ||
| 17 | ) | 18 | ) | ||
| 18 | 19 | ||||
| 20 | + | // LibraryOpener opens a shared library path and returns its handle. | |||
| 21 | + | type LibraryOpener func(path string) (uintptr, error) | |||
| 22 | + | ||||
| 23 | + | // LibraryPathResolver returns the candidate paths for a named library. | |||
| 24 | + | type LibraryPathResolver func(library string) ([]string, error) | |||
| 25 | + | ||||
| 26 | + | // SymbolResolver resolves and publishes a symbol into target using library handles. | |||
| 27 | + | type SymbolResolver func(target any, libraries []uintptr, symbol string) error | |||
| 28 | + | ||||
| 29 | + | // LazyResolver safely caches library opens and symbol publication. It is safe for | |||
| 30 | + | // concurrent use. Its injected dependencies make resolution deterministic in tests. | |||
| 31 | + | type LazyResolver struct { | |||
| 32 | + | paths LibraryPathResolver | |||
| 33 | + | opener LibraryOpener | |||
| 34 | + | resolve SymbolResolver | |||
| 35 | + | ||||
| 36 | + | mu sync.Mutex | |||
| 37 | + | libraries map[string]*lazyLibrary | |||
| 38 | + | symbols map[lazySymbolKey]*lazySymbol | |||
| 39 | + | } | |||
| 40 | + | ||||
| 41 | + | type lazyLibrary struct { | |||
| 42 | + | once sync.Once | |||
| 43 | + | handles []uintptr | |||
| 44 | + | err error | |||
| 45 | + | } | |||
| 46 | + | ||||
| 47 | + | type lazySymbolKey struct { | |||
| 48 | + | library string | |||
| 49 | + | symbol string | |||
| 50 | + | target uintptr | |||
| 51 | + | } | |||
| 52 | + | ||||
| 53 | + | type lazySymbol struct { | |||
| 54 | + | once sync.Once | |||
| 55 | + | err error | |||
| 56 | + | } | |||
| 57 | + | ||||
| 58 | + | // NewLazyResolver constructs an isolated lazy resolver. All dependencies must | |||
| 59 | + | // be non-nil. | |||
| 60 | + | func NewLazyResolver(paths LibraryPathResolver, opener LibraryOpener, resolve SymbolResolver) *LazyResolver { | |||
| 61 | + | if paths == nil || opener == nil || resolve == nil { | |||
| 62 | + | panic("core: lazy resolver dependencies must not be nil") | |||
| 63 | + | } | |||
| 64 | + | return &LazyResolver{ | |||
| 65 | + | paths: paths, | |||
| 66 | + | opener: opener, | |||
| 67 | + | resolve: resolve, | |||
| 68 | + | libraries: make(map[string]*lazyLibrary), | |||
| 69 | + | symbols: make(map[lazySymbolKey]*lazySymbol), | |||
| 70 | + | } | |||
| 71 | + | } | |||
| 72 | + | ||||
| 73 | + | // Register resolves and publishes symbol into target once. Library and symbol | |||
| 74 | + | // failures are cached as well as successful resolutions. | |||
| 75 | + | func (r *LazyResolver) Register(target any, library, symbol string) error { | |||
| 76 | + | if r == nil { | |||
| 77 | + | return fmt.Errorf("core: nil lazy resolver") | |||
| 78 | + | } | |||
| 79 | + | value := reflect.ValueOf(target) | |||
| 80 | + | if !value.IsValid() || value.Kind() != reflect.Ptr || value.IsNil() { | |||
| 81 | + | return fmt.Errorf("core: lazy symbol target must be a non-nil pointer") | |||
| 82 | + | } | |||
| 83 | + | key := lazySymbolKey{library: library, symbol: symbol, target: value.Pointer()} | |||
| 84 | + | ||||
| 85 | + | r.mu.Lock() | |||
| 86 | + | state := r.symbols[key] | |||
| 87 | + | if state == nil { | |||
| 88 | + | // Generated binding tests and embedders may provide a function before | |||
| 89 | + | // the first call. Do not replace that explicit implementation with a | |||
| 90 | + | // symbol. Once a resolution has started, use its sync.Once rather than | |||
| 91 | + | // inspecting a function that the resolver may be publishing. | |||
| 92 | + | if value.Elem().Kind() == reflect.Func && !value.Elem().IsNil() { | |||
| 93 | + | r.mu.Unlock() | |||
| 94 | + | return nil | |||
| 95 | + | } | |||
| 96 | + | state = &lazySymbol{} | |||
| 97 | + | r.symbols[key] = state | |||
| 98 | + | } | |||
| 99 | + | r.mu.Unlock() | |||
| 100 | + | ||||
| 101 | + | state.once.Do(func() { | |||
| 102 | + | libraries, err := r.open(library) | |||
| 103 | + | if err != nil { | |||
| 104 | + | state.err = err | |||
| 105 | + | return | |||
| 106 | + | } | |||
| 107 | + | state.err = r.resolve(target, libraries, symbol) | |||
| 108 | + | }) | |||
| 109 | + | return state.err | |||
| 110 | + | } | |||
| 111 | + | ||||
| 112 | + | // RegisterOptional is Register for optional libraries. It reports whether the | |||
| 113 | + | // symbol was published and caches an unavailable-library failure. | |||
| 114 | + | func (r *LazyResolver) RegisterOptional(target any, library, symbol string) bool { | |||
| 115 | + | return r.Register(target, library, symbol) == nil | |||
| 116 | + | } | |||
| 117 | + | ||||
| 118 | + | func (r *LazyResolver) open(name string) ([]uintptr, error) { | |||
| 119 | + | r.mu.Lock() | |||
| 120 | + | state := r.libraries[name] | |||
| 121 | + | if state == nil { | |||
| 122 | + | state = &lazyLibrary{} | |||
| 123 | + | r.libraries[name] = state | |||
| 124 | + | } | |||
| 125 | + | r.mu.Unlock() | |||
| 126 | + | ||||
| 127 | + | state.once.Do(func() { | |||
| 128 | + | paths, err := r.paths(name) | |||
| 129 | + | if err != nil { | |||
| 130 | + | state.err = err | |||
| 131 | + | return | |||
| 132 | + | } | |||
| 133 | + | if len(paths) == 0 { | |||
| 134 | + | state.err = fmt.Errorf("core: no paths found for library %s", name) | |||
| 135 | + | return | |||
| 136 | + | } | |||
| 137 | + | var lastErr error | |||
| 138 | + | for _, path := range paths { | |||
| 139 | + | handle, err := r.opener(path) | |||
| 140 | + | if err != nil { | |||
| 141 | + | lastErr = err | |||
| 142 | + | continue | |||
| 143 | + | } | |||
| 144 | + | state.handles = append(state.handles, handle) | |||
| 145 | + | } | |||
| 146 | + | if len(state.handles) == 0 { | |||
| 147 | + | state.err = fmt.Errorf("core: open library %s: %w", name, lastErr) | |||
| 148 | + | } | |||
| 149 | + | }) | |||
| 150 | + | return state.handles, state.err | |||
| 151 | + | } | |||
| 152 | + | ||||
| 153 | + | var defaultLazyResolver = NewLazyResolver( | |||
| 154 | + | func(name string) ([]string, error) { | |||
| 155 | + | paths := tryFindPaths(name) | |||
| 156 | + | if len(paths) == 0 { | |||
| 157 | + | return nil, fmt.Errorf("core: no paths found for library %s", name) | |||
| 158 | + | } | |||
| 159 | + | return paths, nil | |||
| 160 | + | }, | |||
| 161 | + | func(path string) (uintptr, error) { | |||
| 162 | + | return purego.Dlopen(path, purego.RTLD_NOW|purego.RTLD_GLOBAL) | |||
| 163 | + | }, | |||
| 164 | + | func(target any, libraries []uintptr, symbol string) error { | |||
| 165 | + | var lastErr error | |||
| 166 | + | for _, library := range libraries { | |||
| 167 | + | address, err := purego.Dlsym(library, symbol) | |||
| 168 | + | if err != nil { | |||
| 169 | + | lastErr = err | |||
| 170 | + | continue | |||
| 171 | + | } | |||
| 172 | + | registerFuncSafe(target, address) | |||
| 173 | + | return nil | |||
| 174 | + | } | |||
| 175 | + | return fmt.Errorf("core: resolve symbol %s: %w", symbol, lastErr) | |||
| 176 | + | }, | |||
| 177 | + | ) | |||
| 178 | + | ||||
| 179 | + | // LazyRegister publishes a generated function's native symbol on its first | |||
| 180 | + | // call. Required-library failures panic; optional-library failures return false. | |||
| 181 | + | func LazyRegister(target any, library, symbol string, optional bool) bool { | |||
| 182 | + | if err := defaultLazyResolver.Register(target, library, symbol); err != nil { | |||
| 183 | + | if optional { | |||
| 184 | + | return false | |||
| 185 | + | } | |||
| 186 | + | panic(err) | |||
| 187 | + | } | |||
| 188 | + | return true | |||
| 189 | + | } | |||
| 190 | + | ||||
| 191 | + | // LibraryAvailable reports whether an optional library can be opened. The open | |||
| 192 | + | // result, including failure, is shared with LazyRegister. | |||
| 193 | + | func LibraryAvailable(library string) bool { | |||
| 194 | + | _, err := defaultLazyResolver.open(library) | |||
| 195 | + | return err == nil | |||
| 196 | + | } | |||
| 197 | + | ||||
| 19 | func PuregoSafeRegister(fptr interface{}, libs []uintptr, name string) { | 198 | func PuregoSafeRegister(fptr interface{}, libs []uintptr, name string) { | ||
| 20 | for _, lib := range libs { | 199 | for _, lib := range libs { | ||
| 21 | sym, err := purego.Dlsym(lib, name) | 200 | sym, err := purego.Dlsym(lib, name) | ||
| @@ -53,7 +232,10 @@ var paths = map[string][]string{ | |||||
| 53 | 232 | ||||
| 54 | // names is a lookup from library names to shared object filenames | 233 | // names is a lookup from library names to shared object filenames | ||
| 55 | // This is populated dynamically via SetSharedLibrary | 234 | // This is populated dynamically via SetSharedLibrary | ||
| 56 | - | var names = map[string][]string{} | 235 | + | var ( |
| 236 | + | libraryConfigMu sync.RWMutex | |||
| 237 | + | names = map[string][]string{} | |||
| 238 | + | ) | |||
| 57 | 239 | ||||
| 58 | // pkgConfNames is a lookup from library names to pkg-config library names | 240 | // pkgConfNames is a lookup from library names to pkg-config library names | ||
| 59 | // This is populated dynamically via SetPackageName | 241 | // This is populated dynamically via SetPackageName | ||
| @@ -63,6 +245,8 @@ var pkgConfNames = map[string]string{} | |||||
| 63 | // This is used by the code generator to set package names from GIR files. | 245 | // This is used by the code generator to set package names from GIR files. | ||
| 64 | // It won't override existing entries to preserve defaults. | 246 | // It won't override existing entries to preserve defaults. | ||
| 65 | func SetPackageName(libName, pkgName string) { | 247 | func SetPackageName(libName, pkgName string) { | ||
| 248 | + | libraryConfigMu.Lock() | |||
| 249 | + | defer libraryConfigMu.Unlock() | |||
| 66 | if _, exists := pkgConfNames[libName]; !exists && pkgName != "" { | 250 | if _, exists := pkgConfNames[libName]; !exists && pkgName != "" { | ||
| 67 | pkgConfNames[libName] = pkgName | 251 | pkgConfNames[libName] = pkgName | ||
| 68 | } | 252 | } | ||
| @@ -72,16 +256,22 @@ func SetPackageName(libName, pkgName string) { | |||||
| 72 | // This is used by the code generator to set library names from GIR files. | 256 | // This is used by the code generator to set library names from GIR files. | ||
| 73 | // It won't override existing entries to preserve defaults. | 257 | // It won't override existing entries to preserve defaults. | ||
| 74 | func SetSharedLibraries(libName string, sharedLibs []string) { | 258 | func SetSharedLibraries(libName string, sharedLibs []string) { | ||
| 259 | + | libraryConfigMu.Lock() | |||
| 260 | + | defer libraryConfigMu.Unlock() | |||
| 75 | if _, exists := names[libName]; !exists && len(sharedLibs) > 0 { | 261 | if _, exists := names[libName]; !exists && len(sharedLibs) > 0 { | ||
| 76 | - | names[libName] = sharedLibs | 262 | + | names[libName] = append([]string(nil), sharedLibs...) |
| 77 | } | 263 | } | ||
| 78 | } | 264 | } | ||
| 79 | 265 | ||||
| 80 | // findSos tries to find all shared objects from a path and a library name | 266 | // findSos tries to find all shared objects from a path and a library name | ||
| 81 | // It does this by mapping the library name to all suitable shared object filenames and then trying some suffixes | 267 | // It does this by mapping the library name to all suitable shared object filenames and then trying some suffixes | ||
| 82 | func findSos(path string, name string) []string { | 268 | func findSos(path string, name string) []string { | ||
| 269 | + | libraryConfigMu.RLock() | |||
| 270 | + | libraryNames := append([]string(nil), names[name]...) | |||
| 271 | + | libraryConfigMu.RUnlock() | |||
| 272 | + | ||||
| 83 | sos := []string{} | 273 | sos := []string{} | ||
| 84 | - | for _, n := range names[name] { | 274 | + | for _, n := range libraryNames { |
| 85 | suffixes := []string{"", ".0", ".1", ".2"} | 275 | suffixes := []string{"", ".0", ".1", ".2"} | ||
| 86 | fn := filepath.Join(path, n) | 276 | fn := filepath.Join(path, n) | ||
| 87 | for _, s := range suffixes { | 277 | for _, s := range suffixes { | ||
| @@ -97,7 +287,10 @@ func findSos(path string, name string) []string { | |||||
| 97 | // it does this by running pkg-config --libs-only-L libname | 287 | // it does this by running pkg-config --libs-only-L libname | ||
| 98 | // and then it loops over the directories returned and finds all suitable ones | 288 | // and then it loops over the directories returned and finds all suitable ones | ||
| 99 | func findPkgConf(name string) []string { | 289 | func findPkgConf(name string) []string { | ||
| 100 | - | cmd := exec.Command("pkg-config", "--libs-only-L", pkgConfNames[name]) | 290 | + | libraryConfigMu.RLock() |
| 291 | + | pkgName := pkgConfNames[name] | |||
| 292 | + | libraryConfigMu.RUnlock() | |||
| 293 | + | cmd := exec.Command("pkg-config", "--libs-only-L", pkgName) | |||
| 101 | var out, outerr bytes.Buffer | 294 | var out, outerr bytes.Buffer | ||
| 102 | cmd.Stdout = &out | 295 | cmd.Stdout = &out | ||
| 103 | cmd.Stderr = &outerr | 296 | cmd.Stderr = &outerr | ||