From 9a79078441059e9a15f40cd50c217af84f65c268 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Fri, 16 May 2025 17:12:56 +0800 Subject: [PATCH] clang.Cmd.CheckLinkArgs compiles to temp file and remove --- xtool/clang/clang.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/xtool/clang/clang.go b/xtool/clang/clang.go index d9dfd8cc..9ac3974d 100644 --- a/xtool/clang/clang.go +++ b/xtool/clang/clang.go @@ -105,16 +105,33 @@ func (p *Cmd) Exec(args ...string) error { return cmd.Run() } -func (p *Cmd) CheckLinkArgs(cmdArgs []string) error { - nul := "/dev/null" - if runtime.GOOS == "windows" { - nul = "NUL" +func (p *Cmd) CheckLinkArgs(cmdArgs []string, wasm bool) error { + // Create a temporary file with appropriate extension + extension := "" + if wasm { + extension = ".wasm" + } else if runtime.GOOS == "windows" { + extension = ".exe" } + + tmpFile, err := os.CreateTemp("", "llgo_check*"+extension) + if err != nil { + return fmt.Errorf("failed to create temporary file: %w", err) + } + tmpFile.Close() + tmpPath := tmpFile.Name() + + // Make sure to delete the temporary file when done + defer os.Remove(tmpPath) + + // Set up compilation arguments args := append([]string{}, cmdArgs...) - args = append(args, []string{"-x", "c", "-o", nul, "-"}...) + args = append(args, []string{"-x", "c", "-o", tmpPath, "-"}...) src := "int main() {return 0;}" srcIn := strings.NewReader(src) p.Stdin = srcIn + + // Execute the command return p.execWithFlags([]string{"LDFLAGS", "CCFLAGS"}, args...) }