internal/graphicsdriver/opengl/gl: bug fix: searching GL libraries failed on some machines

On a Debian machine, LD_LIBRARY_PATH might not be set and libGL.so might
not exist (libGL.so.1 exists instead). In this case, searching for a GL
library fails

This change fixes the issue by not using LD_LIBRARY_PATH and also including
`libGL.so.*` files as candidates for dlopen.

Closes #2539
This commit is contained in:
Hajime Hoshi 2023-01-13 15:05:57 +09:00
parent abd293fae6
commit 72f026e254

View File

@ -40,9 +40,7 @@ import "C"
import (
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"unsafe"
)
@ -52,59 +50,6 @@ var (
libGLES unsafe.Pointer
)
// listLibs returns an appropriate library file paths based on the given library paths and the library name as a prefix.
// Note that the found libraries might not be available e.g. due to architecture mismatches.
func listLibs(libraryPaths []string, libName string) ([]string, error) {
// LD_LIBRARY_PATH might be empty. Use the original name as a candidate.
libNames := []string{libName}
// Look for a library file. In some environments like Steam, a library with the exactly same name might not exist (#2523).
// For example, libGL.so.1 might exist instead of libGL.so.
for _, dir := range libraryPaths {
libs, err := listLibsInDirectory(dir, libName)
if err != nil {
return nil, err
}
if len(libs) == 0 {
continue
}
// The file names are sorted in the alphabetical order.
// TODO: What is the best version to use?
sort.Strings(libs)
libNames = append(libNames, libs...)
}
return libNames, nil
}
// listLibsInDirectory returns library file paths with the given prefix in the directory.
// Note that the found libraries might not be available e.g. due to architecture mismatches.
func listLibsInDirectory(dir string, prefix string) ([]string, error) {
ents, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var files []string
for _, ent := range ents {
if ent.IsDir() {
continue
}
if ent.Name() == prefix {
files = append(files, filepath.Join(dir, ent.Name()))
continue
}
if strings.HasPrefix(ent.Name(), prefix+".") {
files = append(files, filepath.Join(dir, ent.Name()))
continue
}
}
return files, nil
}
func (c *defaultContext) init() error {
var preferES bool
if runtime.GOOS == "android" {
@ -120,15 +65,10 @@ func (c *defaultContext) init() error {
}
}
libraryPaths := filepath.SplitList(os.Getenv("LD_LIBRARY_PATH"))
// Try OpenGL first. OpenGL is preferrable as this doesn't cause context losts.
if !preferES {
names, err := listLibs(libraryPaths, "libGL.so")
if err != nil {
return err
}
for _, name := range names {
// Usually libGL.so or libGL.so.1 is used. libGL.so.2 might exist only on NetBSD.
for _, name := range []string{"libGL.so", "libGL.so.2", "libGL.so.1", "libGL.so.0"} {
cname := C.CString(name)
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
C.free(unsafe.Pointer(cname))
@ -140,11 +80,7 @@ func (c *defaultContext) init() error {
}
// Try OpenGL ES.
names, err := listLibs(libraryPaths, "libGLESv2.so")
if err != nil {
return err
}
for _, name := range names {
for _, name := range []string{"libGLESv2.so", "libGLESv2.so.2", "libGLESv2.so.1", "libGLESv2.so.0"} {
cname := C.CString(name)
lib := C.dlopen(cname, C.RTLD_LAZY|C.RTLD_GLOBAL)
C.free(unsafe.Pointer(cname))