build: fix llvm-readelf call

This commit is contained in:
Li Jie
2025-11-17 15:11:06 +08:00
parent 8e5b34057e
commit faa5330b69
2 changed files with 74 additions and 5 deletions

View File

@@ -4,16 +4,16 @@ import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/goplus/llgo/xtool/env/llvm"
)
type sectionKind int
@@ -132,7 +132,10 @@ func reportBinarySize(path, format, level string, pkgs []Package) error {
}
func collectBinarySize(path string, pkgs []Package, level string) (*sizeReport, error) {
cmd := exec.Command("llvm-readelf", "--all", path)
cmd, err := llvm.New("").Readelf("--elf-output-style=LLVM", "--all", path)
if err != nil {
return nil, fmt.Errorf("llvm-readelf: %w", err)
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
stdout, err := cmd.StdoutPipe()
@@ -574,8 +577,14 @@ func ensureSizeReporting(conf *Config) error {
default:
return fmt.Errorf("invalid size level %q (valid: full,module,package)", conf.SizeLevel)
}
if _, err := exec.LookPath("llvm-readelf"); err != nil {
return errors.New("llvm-readelf not found in PATH")
cmd, err := llvm.New("").Readelf("--version")
if err != nil {
return fmt.Errorf("llvm-readelf not available: %w", err)
}
cmd.Stdout = io.Discard
cmd.Stderr = io.Discard
if err := cmd.Run(); err != nil {
return fmt.Errorf("llvm-readelf not available: %w", err)
}
return nil
}

View File

@@ -17,9 +17,11 @@
package llvm
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"github.com/goplus/llgo/internal/env"
@@ -108,4 +110,62 @@ func (e *Env) InstallNameTool() *install_name_tool.Cmd {
return install_name_tool.New(bin)
}
// Readelf returns a command to execute llvm-readelf with given arguments.
func (e *Env) Readelf(args ...string) (*exec.Cmd, error) {
path, err := e.toolPath("llvm-readelf")
if err != nil {
return nil, err
}
return exec.Command(path, args...), nil
}
func (e *Env) toolPath(base string) (string, error) {
if tool := searchTool(e.binDir, base); tool != "" {
return tool, nil
}
if tool, err := exec.LookPath(base); err == nil {
return tool, nil
}
if tool := searchToolInPath(base); tool != "" {
return tool, nil
}
return "", fmt.Errorf("%s not found", base)
}
func searchTool(dir, base string) string {
if dir == "" {
return ""
}
candidate := filepath.Join(dir, base)
if isExecutable(candidate) {
return candidate
}
pattern := filepath.Join(dir, base+"-*")
matches, _ := filepath.Glob(pattern)
sort.Sort(sort.Reverse(sort.StringSlice(matches)))
for _, match := range matches {
if isExecutable(match) {
return match
}
}
return ""
}
func searchToolInPath(base string) string {
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
if tool := searchTool(dir, base); tool != "" {
return tool
}
}
return ""
}
func isExecutable(path string) bool {
if path == "" {
return false
}
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}
// -----------------------------------------------------------------------------