Merge pull request #1032 from cpunion/fix-linking

Remove LLVM runtime depencendy, better c toolchain integration
This commit is contained in:
xushiwei
2025-03-20 15:47:43 +08:00
committed by GitHub
5 changed files with 34 additions and 7 deletions

View File

@@ -27,7 +27,7 @@ jobs:
id: lychee id: lychee
uses: lycheeverse/lychee-action@v2 uses: lycheeverse/lychee-action@v2
with: with:
args: README.md args: --max-concurrency 3 --retry-wait-time 15 README.md
remote_install: remote_install:
strategy: strategy:

View File

@@ -11,8 +11,8 @@ import (
) )
const ( const (
LLGoPackage = "link: $(llvm-config --ldflags --libs); -lunwind" LLGoPackage = "link"
LLGoFiles = "$(llvm-config --cflags): _wrap/debug.c" LLGoFiles = "_wrap/debug.c"
) )
type Info struct { type Info struct {

View File

@@ -488,7 +488,7 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, linkArgs
if verbose { if verbose {
fmt.Fprintln(os.Stderr, "clang", args) fmt.Fprintln(os.Stderr, "clang", args)
} }
err = ctx.env.Clang().Exec(args...) err = ctx.env.Clang().Link(args...)
check(err) check(err)
if IsRpathChangeEnabled() && runtime.GOOS == "darwin" { if IsRpathChangeEnabled() && runtime.GOOS == "darwin" {
@@ -870,7 +870,7 @@ func clFile(ctx *context, args []string, cFile, expFile string, procFile func(li
if verbose { if verbose {
fmt.Fprintln(os.Stderr, "clang", args) fmt.Fprintln(os.Stderr, "clang", args)
} }
err := ctx.env.Clang().Exec(args...) err := ctx.env.Clang().Compile(args...)
check(err) check(err)
procFile(llFile) procFile(llFile)
} }

View File

@@ -11,8 +11,8 @@ import (
) )
const ( const (
LLGoPackage = "link: $(llvm-config --ldflags --libs); -lunwind" LLGoPackage = "link"
LLGoFiles = "$(llvm-config --cflags): _wrap/debug.c" LLGoFiles = "_wrap/debug.c"
) )
type Info struct { type Info struct {

View File

@@ -20,6 +20,7 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"strings"
) )
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@@ -40,6 +41,32 @@ func New(app string) *Cmd {
return &Cmd{app, os.Stdout, os.Stderr} return &Cmd{app, os.Stdout, os.Stderr}
} }
func (p *Cmd) Compile(args ...string) error {
// Parse CFLAGS environment variable into separate arguments
cflags := strings.Fields(os.Getenv("CFLAGS"))
if len(cflags) > 0 {
// Create a new slice with capacity for all arguments
newArgs := make([]string, 0, len(cflags)+len(args))
newArgs = append(newArgs, cflags...)
newArgs = append(newArgs, args...)
args = newArgs
}
return p.Exec(args...)
}
func (p *Cmd) Link(args ...string) error {
// Parse LDFLAGS environment variable into separate arguments
ldflags := strings.Fields(os.Getenv("LDFLAGS"))
if len(ldflags) > 0 {
// Create a new slice with capacity for all arguments
newArgs := make([]string, 0, len(ldflags)+len(args))
newArgs = append(newArgs, ldflags...)
newArgs = append(newArgs, args...)
args = newArgs
}
return p.Exec(args...)
}
// Exec executes a clang command. // Exec executes a clang command.
func (p *Cmd) Exec(args ...string) error { func (p *Cmd) Exec(args ...string) error {
cmd := exec.Command(p.app, args...) cmd := exec.Command(p.app, args...)