Revert "chore(deps): pin puregotk nil-signal fix"
This reverts commit a161e348e2093172038390b0906448f0ae979a0d.
- commit
- 40378d5e4fd2372d054e49168ab2a6f67aedd0fd
- author
- brice · · 2026-07-14 17:59 UTC
- parent
- 8d647bb
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
905 files · +25263 -27842
M go.mod +1 -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.1-0.20260714170452-0b6b9b901234 | |
| 14 | + | github.com/bnema/puregotk v0.7.0 | |
| 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 | |
| @@ -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.1-0.20260714170452-0b6b9b901234 | 14 | + | github.com/bnema/puregotk v0.7.0 |
| 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 | ||
M go.sum +0 -4 generated file: collapsed
M vendor/github.com/bnema/puregotk/internal/core/core.go +4 -197
| @@ -8,7 +8,6 @@ import ( | |||
| 8 | 8 | "os" | |
| 9 | 9 | "os/exec" | |
| 10 | 10 | "path/filepath" | |
| 11 | - | "reflect" | |
| 12 | 11 | "runtime" | |
| 13 | 12 | "strings" | |
| 14 | 13 | "sync" | |
| @@ -17,184 +16,6 @@ import ( | |||
| 17 | 16 | "github.com/bnema/purego" | |
| 18 | 17 | ) | |
| 19 | 18 | ||
| 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 | - | ||
| 198 | 19 | func PuregoSafeRegister(fptr interface{}, libs []uintptr, name string) { | |
| 199 | 20 | for _, lib := range libs { | |
| 200 | 21 | sym, err := purego.Dlsym(lib, name) | |
| @@ -232,10 +53,7 @@ var paths = map[string][]string{ | |||
| 232 | 53 | ||
| 233 | 54 | // names is a lookup from library names to shared object filenames | |
| 234 | 55 | // This is populated dynamically via SetSharedLibrary | |
| 235 | - | var ( | |
| 236 | - | libraryConfigMu sync.RWMutex | |
| 237 | - | names = map[string][]string{} | |
| 238 | - | ) | |
| 56 | + | var names = map[string][]string{} | |
| 239 | 57 | ||
| 240 | 58 | // pkgConfNames is a lookup from library names to pkg-config library names | |
| 241 | 59 | // This is populated dynamically via SetPackageName | |
| @@ -245,8 +63,6 @@ var pkgConfNames = map[string]string{} | |||
| 245 | 63 | // This is used by the code generator to set package names from GIR files. | |
| 246 | 64 | // It won't override existing entries to preserve defaults. | |
| 247 | 65 | func SetPackageName(libName, pkgName string) { | |
| 248 | - | libraryConfigMu.Lock() | |
| 249 | - | defer libraryConfigMu.Unlock() | |
| 250 | 66 | if _, exists := pkgConfNames[libName]; !exists && pkgName != "" { | |
| 251 | 67 | pkgConfNames[libName] = pkgName | |
| 252 | 68 | } | |
| @@ -256,22 +72,16 @@ func SetPackageName(libName, pkgName string) { | |||
| 256 | 72 | // This is used by the code generator to set library names from GIR files. | |
| 257 | 73 | // It won't override existing entries to preserve defaults. | |
| 258 | 74 | func SetSharedLibraries(libName string, sharedLibs []string) { | |
| 259 | - | libraryConfigMu.Lock() | |
| 260 | - | defer libraryConfigMu.Unlock() | |
| 261 | 75 | if _, exists := names[libName]; !exists && len(sharedLibs) > 0 { | |
| 262 | - | names[libName] = append([]string(nil), sharedLibs...) | |
| 76 | + | names[libName] = sharedLibs | |
| 263 | 77 | } | |
| 264 | 78 | } | |
| 265 | 79 | ||
| 266 | 80 | // findSos tries to find all shared objects from a path and a library name | |
| 267 | 81 | // It does this by mapping the library name to all suitable shared object filenames and then trying some suffixes | |
| 268 | 82 | func findSos(path string, name string) []string { | |
| 269 | - | libraryConfigMu.RLock() | |
| 270 | - | libraryNames := append([]string(nil), names[name]...) | |
| 271 | - | libraryConfigMu.RUnlock() | |
| 272 | - | ||
| 273 | 83 | sos := []string{} | |
| 274 | - | for _, n := range libraryNames { | |
| 84 | + | for _, n := range names[name] { | |
| 275 | 85 | suffixes := []string{"", ".0", ".1", ".2"} | |
| 276 | 86 | fn := filepath.Join(path, n) | |
| 277 | 87 | for _, s := range suffixes { | |
| @@ -287,10 +97,7 @@ func findSos(path string, name string) []string { | |||
| 287 | 97 | // it does this by running pkg-config --libs-only-L libname | |
| 288 | 98 | // and then it loops over the directories returned and finds all suitable ones | |
| 289 | 99 | func findPkgConf(name string) []string { | |
| 290 | - | libraryConfigMu.RLock() | |
| 291 | - | pkgName := pkgConfNames[name] | |
| 292 | - | libraryConfigMu.RUnlock() | |
| 293 | - | cmd := exec.Command("pkg-config", "--libs-only-L", pkgName) | |
| 100 | + | cmd := exec.Command("pkg-config", "--libs-only-L", pkgConfNames[name]) | |
| 294 | 101 | var out, outerr bytes.Buffer | |
| 295 | 102 | cmd.Stdout = &out | |
| 296 | 103 | cmd.Stderr = &outerr | |
| @@ -8,7 +8,6 @@ import ( | |||||
| 8 | "os" | 8 | "os" | ||
| 9 | "os/exec" | 9 | "os/exec" | ||
| 10 | "path/filepath" | 10 | "path/filepath" | ||
| 11 | - | "reflect" | |||
| 12 | "runtime" | 11 | "runtime" | ||
| 13 | "strings" | 12 | "strings" | ||
| 14 | "sync" | 13 | "sync" | ||
| @@ -17,184 +16,6 @@ import ( | |||||
| 17 | "github.com/bnema/purego" | 16 | "github.com/bnema/purego" | ||
| 18 | ) | 17 | ) | ||
| 19 | 18 | ||||
| 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 | - | ||||
| 198 | func PuregoSafeRegister(fptr interface{}, libs []uintptr, name string) { | 19 | func PuregoSafeRegister(fptr interface{}, libs []uintptr, name string) { | ||
| 199 | for _, lib := range libs { | 20 | for _, lib := range libs { | ||
| 200 | sym, err := purego.Dlsym(lib, name) | 21 | sym, err := purego.Dlsym(lib, name) | ||
| @@ -232,10 +53,7 @@ var paths = map[string][]string{ | |||||
| 232 | 53 | ||||
| 233 | // names is a lookup from library names to shared object filenames | 54 | // names is a lookup from library names to shared object filenames | ||
| 234 | // This is populated dynamically via SetSharedLibrary | 55 | // This is populated dynamically via SetSharedLibrary | ||
| 235 | - | var ( | 56 | + | var names = map[string][]string{} |
| 236 | - | libraryConfigMu sync.RWMutex | |||
| 237 | - | names = map[string][]string{} | |||
| 238 | - | ) | |||
| 239 | 57 | ||||
| 240 | // pkgConfNames is a lookup from library names to pkg-config library names | 58 | // pkgConfNames is a lookup from library names to pkg-config library names | ||
| 241 | // This is populated dynamically via SetPackageName | 59 | // This is populated dynamically via SetPackageName | ||
| @@ -245,8 +63,6 @@ var pkgConfNames = map[string]string{} | |||||
| 245 | // This is used by the code generator to set package names from GIR files. | 63 | // This is used by the code generator to set package names from GIR files. | ||
| 246 | // It won't override existing entries to preserve defaults. | 64 | // It won't override existing entries to preserve defaults. | ||
| 247 | func SetPackageName(libName, pkgName string) { | 65 | func SetPackageName(libName, pkgName string) { | ||
| 248 | - | libraryConfigMu.Lock() | |||
| 249 | - | defer libraryConfigMu.Unlock() | |||
| 250 | if _, exists := pkgConfNames[libName]; !exists && pkgName != "" { | 66 | if _, exists := pkgConfNames[libName]; !exists && pkgName != "" { | ||
| 251 | pkgConfNames[libName] = pkgName | 67 | pkgConfNames[libName] = pkgName | ||
| 252 | } | 68 | } | ||
| @@ -256,22 +72,16 @@ func SetPackageName(libName, pkgName string) { | |||||
| 256 | // This is used by the code generator to set library names from GIR files. | 72 | // This is used by the code generator to set library names from GIR files. | ||
| 257 | // It won't override existing entries to preserve defaults. | 73 | // It won't override existing entries to preserve defaults. | ||
| 258 | func SetSharedLibraries(libName string, sharedLibs []string) { | 74 | func SetSharedLibraries(libName string, sharedLibs []string) { | ||
| 259 | - | libraryConfigMu.Lock() | |||
| 260 | - | defer libraryConfigMu.Unlock() | |||
| 261 | if _, exists := names[libName]; !exists && len(sharedLibs) > 0 { | 75 | if _, exists := names[libName]; !exists && len(sharedLibs) > 0 { | ||
| 262 | - | names[libName] = append([]string(nil), sharedLibs...) | 76 | + | names[libName] = sharedLibs |
| 263 | } | 77 | } | ||
| 264 | } | 78 | } | ||
| 265 | 79 | ||||
| 266 | // findSos tries to find all shared objects from a path and a library name | 80 | // findSos tries to find all shared objects from a path and a library name | ||
| 267 | // It does this by mapping the library name to all suitable shared object filenames and then trying some suffixes | 81 | // It does this by mapping the library name to all suitable shared object filenames and then trying some suffixes | ||
| 268 | func findSos(path string, name string) []string { | 82 | func findSos(path string, name string) []string { | ||
| 269 | - | libraryConfigMu.RLock() | |||
| 270 | - | libraryNames := append([]string(nil), names[name]...) | |||
| 271 | - | libraryConfigMu.RUnlock() | |||
| 272 | - | ||||
| 273 | sos := []string{} | 83 | sos := []string{} | ||
| 274 | - | for _, n := range libraryNames { | 84 | + | for _, n := range names[name] { |
| 275 | suffixes := []string{"", ".0", ".1", ".2"} | 85 | suffixes := []string{"", ".0", ".1", ".2"} | ||
| 276 | fn := filepath.Join(path, n) | 86 | fn := filepath.Join(path, n) | ||
| 277 | for _, s := range suffixes { | 87 | for _, s := range suffixes { | ||
| @@ -287,10 +97,7 @@ func findSos(path string, name string) []string { | |||||
| 287 | // it does this by running pkg-config --libs-only-L libname | 97 | // it does this by running pkg-config --libs-only-L libname | ||
| 288 | // and then it loops over the directories returned and finds all suitable ones | 98 | // and then it loops over the directories returned and finds all suitable ones | ||
| 289 | func findPkgConf(name string) []string { | 99 | func findPkgConf(name string) []string { | ||
| 290 | - | libraryConfigMu.RLock() | 100 | + | cmd := exec.Command("pkg-config", "--libs-only-L", pkgConfNames[name]) |
| 291 | - | pkgName := pkgConfNames[name] | |||
| 292 | - | libraryConfigMu.RUnlock() | |||
| 293 | - | cmd := exec.Command("pkg-config", "--libs-only-L", pkgName) | |||
| 294 | var out, outerr bytes.Buffer | 101 | var out, outerr bytes.Buffer | ||
| 295 | cmd.Stdout = &out | 102 | cmd.Stdout = &out | ||
| 296 | cmd.Stderr = &outerr | 103 | cmd.Stderr = &outerr | ||