Merge pull request #1147 from luoliwoshang/c++

cl:compile with clang++
This commit is contained in:
xushiwei
2025-06-12 12:54:13 +08:00
committed by GitHub
6 changed files with 27 additions and 8 deletions

View File

@@ -34,7 +34,7 @@ runs:
echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-${{inputs.llvm-version}} main" | sudo tee /etc/apt/sources.list.d/llvm.list echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-${{inputs.llvm-version}} main" | sudo tee /etc/apt/sources.list.d/llvm.list
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -
sudo apt-get update sudo apt-get update
sudo apt-get install -y llvm-${{inputs.llvm-version}}-dev clang-${{inputs.llvm-version}} libclang-${{inputs.llvm-version}}-dev lld-${{inputs.llvm-version}} pkg-config libgc-dev libssl-dev zlib1g-dev libffi-dev libcjson-dev libunwind-dev libuv1-dev sudo apt-get install -y llvm-${{inputs.llvm-version}}-dev clang-${{inputs.llvm-version}} libclang-${{inputs.llvm-version}}-dev lld-${{inputs.llvm-version}} pkg-config libgc-dev libssl-dev zlib1g-dev libffi-dev libcjson-dev libunwind-${{inputs.llvm-version}}-dev libuv1-dev libc++-${{inputs.llvm-version}}-dev
echo "PATH=/usr/lib/llvm-${{inputs.llvm-version}}/bin:$PATH" >> $GITHUB_ENV echo "PATH=/usr/lib/llvm-${{inputs.llvm-version}}/bin:$PATH" >> $GITHUB_ENV
# Install optional deps for demos. # Install optional deps for demos.

View File

@@ -70,11 +70,6 @@ jobs:
with: with:
go-version: ${{matrix.go}} go-version: ${{matrix.go}}
- name: _xtool build tests
run: |
cd _xtool
llgo build -v ./...
- name: Test demos - name: Test demos
run: | run: |
# TODO(lijie): force python3-embed to be linked with python-3.12-embed # TODO(lijie): force python3-embed to be linked with python-3.12-embed
@@ -88,6 +83,11 @@ jobs:
export PKG_CONFIG_PATH=$pcdir export PKG_CONFIG_PATH=$pcdir
bash .github/workflows/test_demo.sh bash .github/workflows/test_demo.sh
- name: _xtool build tests
run: |
cd _xtool
llgo build -v ./...
- name: Show test result - name: Show test result
run: cat result.md run: cat result.md

12
_demo/cppstr/cppstr.go Normal file
View File

@@ -0,0 +1,12 @@
package main
import (
"github.com/goplus/lib/c"
"github.com/goplus/lib/cpp/std"
)
func main() {
s := std.Str("Hello world\n")
c.Printf(s.CStr())
print(s.Str(), s.Size(), "\n")
}

View File

@@ -953,6 +953,13 @@ func clFiles(ctx *context, files string, pkg *packages.Package, procFile func(li
func clFile(ctx *context, args []string, cFile, expFile string, procFile func(linkFile string), verbose bool) { func clFile(ctx *context, args []string, cFile, expFile string, procFile func(linkFile string), verbose bool) {
llFile := expFile + filepath.Base(cFile) + ".ll" llFile := expFile + filepath.Base(cFile) + ".ll"
ext := filepath.Ext(cFile)
// default clang++ will use c++ to compile c file,will cause symbol be mangled
if ext == ".c" {
args = append(args, "-x", "c")
}
args = append(args, "-emit-llvm", "-S", "-o", llFile, "-c", cFile) args = append(args, "-emit-llvm", "-S", "-o", llFile, "-c", cFile)
args = append(args, ctx.crossCompile.CCFLAGS...) args = append(args, ctx.crossCompile.CCFLAGS...)
args = append(args, ctx.crossCompile.CFLAGS...) args = append(args, ctx.crossCompile.CFLAGS...)

View File

@@ -9,7 +9,7 @@ import (
) )
const ( const (
LLGoFiles = "_wrap/debug.c" LLGoFiles = "$(llvm-config --cflags): _wrap/debug.c"
) )
type Info struct { type Info struct {

View File

@@ -72,7 +72,7 @@ func (e *Env) BinDir() string { return e.binDir }
// Clang returns a new [clang.Cmd] instance. // Clang returns a new [clang.Cmd] instance.
func (e *Env) Clang() *clang.Cmd { func (e *Env) Clang() *clang.Cmd {
bin := filepath.Join(e.BinDir(), "clang") bin := filepath.Join(e.BinDir(), "clang++")
return clang.New(bin) return clang.New(bin)
} }