From f2f93c7f5faf3d05b3e4e1bb21dd6ec4f3893c28 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 11 Jun 2025 17:01:12 +0800 Subject: [PATCH] cl:compile with clang++ --- internal/build/build.go | 2 ++ .../clite/bitcast/_cast/{cast.c => cast.cpp} | 18 ++++++++---------- runtime/internal/clite/bitcast/bitcast.go | 2 +- .../clite/debug/_wrap/{debug.c => debug.cpp} | 15 +++++++-------- runtime/internal/clite/debug/debug.go | 2 +- xtool/clang/clang.go | 3 +++ xtool/env/llvm/llvm.go | 2 +- 7 files changed, 23 insertions(+), 21 deletions(-) rename runtime/internal/clite/bitcast/_cast/{cast.c => cast.cpp} (61%) rename runtime/internal/clite/debug/_wrap/{debug.c => debug.cpp} (78%) diff --git a/internal/build/build.go b/internal/build/build.go index 654b3746..1bc6078a 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -490,6 +490,8 @@ func linkMainPkg(ctx *context, pkg *packages.Package, pkgs []*aPackage, conf *Co } linkArgs = append(linkArgs, exargs...) + fmt.Fprintf(os.Stderr, "linkArgs: %v\n", linkArgs) + err = compileAndLinkLLFiles(ctx, app, llFiles, linkArgs, verbose) check(err) diff --git a/runtime/internal/clite/bitcast/_cast/cast.c b/runtime/internal/clite/bitcast/_cast/cast.cpp similarity index 61% rename from runtime/internal/clite/bitcast/_cast/cast.c rename to runtime/internal/clite/bitcast/_cast/cast.cpp index ea567ce5..ae4d6707 100644 --- a/runtime/internal/clite/bitcast/_cast/cast.c +++ b/runtime/internal/clite/bitcast/_cast/cast.cpp @@ -1,35 +1,33 @@ -typedef union -{ +typedef union { double d; float f; long v; long long ll; } castUnion; -double llgoToFloat64(long long v) -{ +extern "C" { + +double llgoToFloat64(long long v) { castUnion k; k.ll = v; return k.d; } -float llgoToFloat32(int v) -{ +float llgoToFloat32(int v) { castUnion k; k.v = v; return k.f; } -long long llgoFromFloat64(double v) -{ +long long llgoFromFloat64(double v) { castUnion k; k.d = v; return k.ll; } -int llgoFromFloat32(float v) -{ +int llgoFromFloat32(float v) { castUnion k; k.f = v; return k.v; } +} \ No newline at end of file diff --git a/runtime/internal/clite/bitcast/bitcast.go b/runtime/internal/clite/bitcast/bitcast.go index 3fa7f6fa..07a79515 100644 --- a/runtime/internal/clite/bitcast/bitcast.go +++ b/runtime/internal/clite/bitcast/bitcast.go @@ -19,7 +19,7 @@ package bitcast import _ "unsafe" const ( - LLGoFiles = "_cast/cast.c" + LLGoFiles = "_cast/cast.cpp" LLGoPackage = "link" ) diff --git a/runtime/internal/clite/debug/_wrap/debug.c b/runtime/internal/clite/debug/_wrap/debug.cpp similarity index 78% rename from runtime/internal/clite/debug/_wrap/debug.c rename to runtime/internal/clite/debug/_wrap/debug.cpp index 32d87903..441cd971 100644 --- a/runtime/internal/clite/debug/_wrap/debug.c +++ b/runtime/internal/clite/debug/_wrap/debug.cpp @@ -9,13 +9,11 @@ #include #include -void *llgo_address() { - return __builtin_return_address(0); -} +extern "C" { -int llgo_addrinfo(void *addr, Dl_info *info) { - return dladdr(addr, info); -} +void *llgo_address() { return __builtin_return_address(0); } + +int llgo_addrinfo(void *addr, Dl_info *info) { return dladdr(addr, info); } void llgo_stacktrace(int skip, void *ctx, int (*fn)(void *ctx, void *pc, void *offset, void *sp, char *name)) { unw_cursor_t cursor; @@ -33,9 +31,10 @@ void llgo_stacktrace(int skip, void *ctx, int (*fn)(void *ctx, void *pc, void *o if (unw_get_reg(&cursor, UNW_REG_IP, &pc) == 0) { unw_get_proc_name(&cursor, fname, sizeof(fname), &offset); unw_get_reg(&cursor, UNW_REG_SP, &sp); - if (fn(ctx, (void*)pc, (void*)offset, (void*)sp, fname) == 0) { + if (fn(ctx, (void *)pc, (void *)offset, (void *)sp, fname) == 0) { return; } } } -} \ No newline at end of file +} +} diff --git a/runtime/internal/clite/debug/debug.go b/runtime/internal/clite/debug/debug.go index f762cb4d..2a02b718 100644 --- a/runtime/internal/clite/debug/debug.go +++ b/runtime/internal/clite/debug/debug.go @@ -9,7 +9,7 @@ import ( ) const ( - LLGoFiles = "_wrap/debug.c" + LLGoFiles = "_wrap/debug.cpp" ) type Info struct { diff --git a/xtool/clang/clang.go b/xtool/clang/clang.go index 9ac3974d..78ca3e18 100644 --- a/xtool/clang/clang.go +++ b/xtool/clang/clang.go @@ -91,6 +91,7 @@ func (p *Cmd) execWithFlags(flags []string, args ...string) error { if p.Env != nil { cmd.Env = p.Env } + fmt.Fprintln(os.Stderr, cmd.String()) return cmd.Run() } @@ -131,6 +132,8 @@ func (p *Cmd) CheckLinkArgs(cmdArgs []string, wasm bool) error { srcIn := strings.NewReader(src) p.Stdin = srcIn + fmt.Fprintf(os.Stderr, "args: %v\n", args) + // Execute the command return p.execWithFlags([]string{"LDFLAGS", "CCFLAGS"}, args...) } diff --git a/xtool/env/llvm/llvm.go b/xtool/env/llvm/llvm.go index 82d0d13e..a34757c9 100644 --- a/xtool/env/llvm/llvm.go +++ b/xtool/env/llvm/llvm.go @@ -72,7 +72,7 @@ func (e *Env) BinDir() string { return e.binDir } // Clang returns a new [clang.Cmd] instance. func (e *Env) Clang() *clang.Cmd { - bin := filepath.Join(e.BinDir(), "clang") + bin := filepath.Join(e.BinDir(), "clang++") return clang.New(bin) }