|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package docker |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +// systemFreeTypePath returns the path to the system libfreetype.so.6, or "" if |
| 14 | +// none is found. It globs the common (multiarch) library locations so it works |
| 15 | +// on amd64, arm64, and other Linux distros without a hardcoded arch path. |
| 16 | +func systemFreeTypePath() string { |
| 17 | + patterns := []string{ |
| 18 | + "/usr/lib/*/libfreetype.so.6", // Debian/Ubuntu multiarch (x86_64, aarch64, …) |
| 19 | + "/usr/lib/libfreetype.so.6", |
| 20 | + "/lib/*/libfreetype.so.6", |
| 21 | + "/usr/local/lib/libfreetype.so.6", |
| 22 | + } |
| 23 | + for _, pat := range patterns { |
| 24 | + matches, _ := filepath.Glob(pat) |
| 25 | + for _, m := range matches { |
| 26 | + if fi, err := os.Stat(m); err == nil && !fi.IsDir() { |
| 27 | + return m |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + return "" |
| 32 | +} |
| 33 | + |
| 34 | +// skiaFreeTypeLib returns the system libfreetype to preload to work around the |
| 35 | +// libSkiaSharp "undefined symbol: FT_Get_BDF_Property" crash, or "" when the |
| 36 | +// workaround does not apply. |
| 37 | +// |
| 38 | +// Root cause: mx/mxbuild run under the Temurin JVM, whose bundled libfreetype |
| 39 | +// is stripped and lacks FT_Get_BDF_Property. Skia then loads the JVM's FreeType |
| 40 | +// instead of the system one (which has the symbol) and aborts. Preloading the |
| 41 | +// system libfreetype makes it load first, fixing mx build/run/check while |
| 42 | +// keeping Skia working (unlike moving libSkiaSharp.so aside, which disables it). |
| 43 | +// |
| 44 | +// Returns "" on non-Linux hosts or when no system libfreetype is found, so |
| 45 | +// callers leave the environment untouched everywhere the bug can't occur. |
| 46 | +func skiaFreeTypeLib() string { |
| 47 | + if runtime.GOOS != "linux" { |
| 48 | + return "" |
| 49 | + } |
| 50 | + return systemFreeTypePath() |
| 51 | +} |
| 52 | + |
| 53 | +// PrepareMxCommand injects the FreeType LD_PRELOAD workaround into cmd's |
| 54 | +// environment before it runs an mx/mxbuild binary. It is a no-op when the |
| 55 | +// workaround does not apply (non-Linux, no system libfreetype) or when an |
| 56 | +// LD_PRELOAD already references libfreetype (e.g. the user exported it). When a |
| 57 | +// non-libfreetype LD_PRELOAD is already present, the system libfreetype is |
| 58 | +// prepended so it loads first. |
| 59 | +func PrepareMxCommand(cmd *exec.Cmd) { |
| 60 | + lib := skiaFreeTypeLib() |
| 61 | + if lib == "" { |
| 62 | + return |
| 63 | + } |
| 64 | + env := cmd.Env |
| 65 | + if env == nil { |
| 66 | + env = os.Environ() |
| 67 | + } |
| 68 | + if merged, changed := injectLDPreload(env, lib); changed { |
| 69 | + cmd.Env = merged |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// injectLDPreload returns env with lib added to LD_PRELOAD, and whether it |
| 74 | +// changed anything. If an LD_PRELOAD already references libfreetype it is left |
| 75 | +// untouched (changed=false); a non-libfreetype LD_PRELOAD gets lib prepended so |
| 76 | +// it loads first. env is not mutated; a copy is returned when changed. |
| 77 | +func injectLDPreload(env []string, lib string) (result []string, changed bool) { |
| 78 | + existing := "" |
| 79 | + idx := -1 |
| 80 | + for i, e := range env { |
| 81 | + if v, ok := strings.CutPrefix(e, "LD_PRELOAD="); ok { |
| 82 | + existing = v |
| 83 | + idx = i |
| 84 | + } |
| 85 | + } |
| 86 | + if strings.Contains(existing, "libfreetype.so") { |
| 87 | + return env, false |
| 88 | + } |
| 89 | + value := lib |
| 90 | + if existing != "" { |
| 91 | + value = lib + ":" + existing |
| 92 | + } |
| 93 | + entry := "LD_PRELOAD=" + value |
| 94 | + out := append([]string(nil), env...) |
| 95 | + if idx >= 0 { |
| 96 | + out[idx] = entry |
| 97 | + } else { |
| 98 | + out = append(out, entry) |
| 99 | + } |
| 100 | + return out, true |
| 101 | +} |
0 commit comments