Null-DRM-Official/apps/pkg/modhost/build.go
404errordeveloper f9c28be3f1
All checks were successful
release / build-and-upload (push) Successful in 6m8s
modules-release / build-and-upload (push) Successful in 4m47s
v0.1.1: external modules, publish YAML, proxy reset, binary-first README
- Host discovers/builds modules under modules/ (bbc, rte, tg4)
- module-publish.yaml tracked; local module.yaml gitignored
- drm --modules-dir / DRM_MODULES_DIR
- drm proxy reset; Magisk reinject reliability
- modules-v* Forgejo release workflow + build scripts
- BBC full Explore channel map including S4C
- README quickstart based on release binaries
2026-10-08 23:16:14 +02:00

107 lines
2.6 KiB
Go

package modhost
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// EnsureBuilt returns the path to the module binary, building from Go source
// when needed. Prebuilt .bin/ is used as-is when source is absent.
func EnsureBuilt(m Module) (string, error) {
bin := m.BinPath()
hasSrc := hasGoSource(m.Dir)
binOK := fileExists(bin)
if !hasSrc && !binOK {
return "", fmt.Errorf("module %s: no Go source in %s and no binary at %s", m.Name, m.Dir, bin)
}
if binOK && (!hasSrc || !sourceNewerThan(m.Dir, bin)) {
return bin, nil
}
goBin, err := findGo()
if err != nil {
if binOK {
fmt.Fprintf(os.Stderr, "[!] go not found; using existing %s\n", bin)
return bin, nil
}
return "", fmt.Errorf("module %s needs building but go is not on PATH: %w", m.Name, err)
}
if err := os.MkdirAll(filepath.Dir(bin), 0o755); err != nil {
return "", err
}
fmt.Printf("[*] Building module %s → %s\n", m.Name, bin)
cmd := exec.Command(goBin, "build", "-trimpath", "-ldflags=-s -w", "-o", bin, ".")
cmd.Dir = m.Dir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", fmt.Errorf("go build module %s: %w", m.Name, err)
}
return bin, nil
}
func hasGoSource(dir string) bool {
if fileExists(filepath.Join(dir, "go.mod")) {
return true
}
ents, err := os.ReadDir(dir)
if err != nil {
return false
}
for _, e := range ents {
if !e.IsDir() && strings.HasSuffix(e.Name(), ".go") {
return true
}
}
return false
}
func sourceNewerThan(dir, bin string) bool {
bst, err := os.Stat(bin)
if err != nil {
return true
}
binTime := bst.ModTime()
newer := false
_ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
if info != nil && info.IsDir() && (info.Name() == ".bin" || info.Name() == "vendor") {
return filepath.SkipDir
}
return nil
}
name := info.Name()
if strings.HasSuffix(name, ".go") || name == "go.mod" || name == "go.sum" {
if info.ModTime().After(binTime) {
newer = true
}
}
return nil
})
return newer
}
func findGo() (string, error) {
if p, err := exec.LookPath("go"); err == nil {
return p, nil
}
home, _ := os.UserHomeDir()
candidates := []string{
filepath.Join(home, "go-sdk", "go", "bin", "go"),
filepath.Join(home, "go-sdk", "go", "bin", "go.exe"),
filepath.Join(home, "sdk", "go", "bin", "go"),
`C:\Program Files\Go\bin\go.exe`,
"/usr/local/go/bin/go",
}
for _, c := range candidates {
if st, err := os.Stat(c); err == nil && !st.IsDir() {
return c, nil
}
}
return "", fmt.Errorf("go binary not found (install Go or add it to PATH)")
}