Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Merge branch 'main' of https://github.com/goplus/llcppg into third
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
6 changes: 2 additions & 4 deletions _cmptest/testdata/libxml2/2.13.6/libxml2/xmlstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ func (recv_ *Char) StrncatNew(str2 *Char, len c.Int) *Char {
return nil
}

// llgo:link (*Char).StrPrintf C.xmlStrPrintf
func (recv_ *Char) StrPrintf(len c.Int, msg *c.Char, __llgo_va_list ...interface{}) c.Int {
return 0
}
//go:linkname StrPrintf C.xmlStrPrintf
func StrPrintf(buf *Char, len c.Int, msg *c.Char, __llgo_va_list ...interface{}) c.Int

// llgo:link (*Char).StrVPrintf C.xmlStrVPrintf
func (recv_ *Char) StrVPrintf(len c.Int, msg *c.Char, ap c.VaList) c.Int {
Expand Down
3 changes: 3 additions & 0 deletions _cmptest/testdata/sqlite3/3.49.1/sqlite3/llcppg.pub
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ sqlite3_context Context
sqlite3_destructor_type DestructorType
sqlite3_file File
sqlite3_filename Filename
sqlite3_index_constraint IndexConstraint
sqlite3_index_constraint_usage IndexConstraintUsage
sqlite3_index_info IndexInfo
sqlite3_index_orderby IndexOrderby
sqlite3_int64 Int64
sqlite3_io_methods IoMethods
sqlite3_loadext_entry LoadextEntry
Expand Down
30 changes: 16 additions & 14 deletions _cmptest/testdata/sqlite3/3.49.1/sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,10 +1084,8 @@ func Config(__llgo_arg_0 c.Int, __llgo_va_list ...interface{}) c.Int
** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
** the call is considered successful.
*/
// llgo:link (*Sqlite3).DbConfig C.sqlite3_db_config
func (recv_ *Sqlite3) DbConfig(op c.Int, __llgo_va_list ...interface{}) c.Int {
return 0
}
//go:linkname DbConfig C.sqlite3_db_config
func DbConfig(__llgo_arg_0 *Sqlite3, op c.Int, __llgo_va_list ...interface{}) c.Int

type MemMethods struct {
XMalloc c.Pointer
Expand Down Expand Up @@ -5691,15 +5689,22 @@ type Module struct {
}

type IndexConstraint struct {
Unused [8]uint8
IColumn c.Int
Op c.Char
Usable c.Char
ITermOffset c.Int
}

type IndexOrderby struct {
Unused [8]uint8
IColumn c.Int
Desc c.Char
}

/* Outputs */

type IndexConstraintUsage struct {
Unused [8]uint8
ArgvIndex c.Int
Omit c.Char
}

/*
Expand Down Expand Up @@ -6449,9 +6454,8 @@ func (recv_ *Str) StrFinish() *c.Char {
** is recorded in the [sqlite3_str] object and can be recovered by a
** subsequent call to [sqlite3_str_errcode(X)].
*/
// llgo:link (*Str).StrAppendf C.sqlite3_str_appendf
func (recv_ *Str) StrAppendf(zFormat *c.Char, __llgo_va_list ...interface{}) {
}
//go:linkname StrAppendf C.sqlite3_str_appendf
func StrAppendf(__llgo_arg_0 *Str, zFormat *c.Char, __llgo_va_list ...interface{})

// llgo:link (*Str).StrVappendf C.sqlite3_str_vappendf
func (recv_ *Str) StrVappendf(zFormat *c.Char, __llgo_arg_1 c.VaList) {
Expand Down Expand Up @@ -7284,10 +7288,8 @@ func (recv_ *Sqlite3) WalCheckpointV2(zDb *c.Char, eMode c.Int, pnLog *c.Int, pn
** of parameters after C depend on which [virtual table configuration option]
** is used.
*/
// llgo:link (*Sqlite3).VtabConfig C.sqlite3_vtab_config
func (recv_ *Sqlite3) VtabConfig(op c.Int, __llgo_va_list ...interface{}) c.Int {
return 0
}
//go:linkname VtabConfig C.sqlite3_vtab_config
func VtabConfig(__llgo_arg_0 *Sqlite3, op c.Int, __llgo_va_list ...interface{}) c.Int

/*
** CAPI3REF: Determine The Virtual Table Conflict Policy
Expand Down
11 changes: 10 additions & 1 deletion _xtool/internal/clang/clang.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package clang

import (
"errors"
"os"
"path/filepath"
"strings"
"unsafe"

"github.com/goplus/lib/c"
Expand All @@ -28,7 +30,14 @@ const TEMP_FILE = "temp.h"
func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit, error) {
// default use the c/c++ standard of clang; c:gnu17 c++:gnu++17
// https://clang.llvm.org/docs/CommandGuide/clang.html
allArgs := clangtool.WithSysRoot(append(defaultArgs(config.IsCpp), config.Args...))
var allArgs []string

if env := os.Getenv("TARGET"); env != "" {
if strings.Contains(env, "xtensa") {
allArgs = append(allArgs, "-D__XTENSA__")
}
}
allArgs = append(allArgs, clangtool.WithSysRoot(append(defaultArgs(config.IsCpp), config.Args...))...)

cArgs := make([]*c.Char, len(allArgs))
for i, arg := range allArgs {
Expand Down
12 changes: 10 additions & 2 deletions _xtool/internal/clangtool/clangtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,16 @@ func defaultArgs(isCpp bool) []string {
func sysRoot() ([]string, error) {
var output bytes.Buffer

cmds := []string{"-E", "-v", "-x", "c"}

env := os.Getenv("TARGET")
if env != "" {
cmds = append(cmds, strings.Fields(env)...)
}

cmds = append(cmds, "/dev/null")
// -x dones't matter, we don't care, just get the isysroot
cmd := exec.Command("clang", "-E", "-v", "-x", "c", "/dev/null")
cmd := exec.Command("clang", cmds...)
cmd.Stderr = &output

cmd.Run()
Expand Down Expand Up @@ -105,5 +113,5 @@ func ParseSystemPath(output string) ([]string, error) {
return nil, fmt.Errorf("failed to find any sysRoot path")
}

return result, nil
return append(result, "-nostdlibinc", "-nostdlib"), nil
}
66 changes: 66 additions & 0 deletions _xtool/internal/clangtool/inclusion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package clangtool

import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)

type Config struct {
HeaderFileName string
ComposedHeaderFile string
CompileArgs []string
IsCpp bool
}

func GetInclusions(conf *Config, fn func(fileName string, depth int)) error {
if conf.HeaderFileName == "" && conf.ComposedHeaderFile == "" {
return errors.New("failed to get inclusion: no header file")
}
file := conf.ComposedHeaderFile
if file == "" {
tmpFile, err := os.CreateTemp("", "inclusion")
if err != nil {
return err
}
defer os.Remove(tmpFile.Name())

inc := fmt.Sprintf("#include <%s>", conf.HeaderFileName)
_, err = tmpFile.Write([]byte(inc))
if err != nil {
return err
}

file = tmpFile.Name()
}

args := defaultArgs(conf.IsCpp)
args = append(args, "-H", "-E")
args = append(args, conf.CompileArgs...)
args = append(args, file)

var buf bytes.Buffer

cmd := exec.Command("clang", args...)
cmd.Stderr = &buf
err := cmd.Run()
if err != nil {
return errors.New(buf.String())
}

br := bufio.NewScanner(&buf)

for br.Scan() {
strs := strings.Split(br.Text(), " ")
if len(strs) == 2 {
fn(filepath.Clean(strs[1]), strings.Count(strs[0], "."))
}
}

return nil
}
62 changes: 29 additions & 33 deletions _xtool/internal/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"path/filepath"
"strings"

"github.com/goplus/lib/c/clang"
clangutils "github.com/goplus/llcppg/_xtool/internal/clang"
"github.com/goplus/llcppg/_xtool/internal/clangtool"
)

Expand Down Expand Up @@ -52,50 +50,48 @@ func PkgHfileInfo(conf *Config) *PkgHfilesInfo {
if err != nil {
panic(err)
}
defer os.Remove(outfile.Name())
// defer os.Remove(outfile.Name())

inters := make(map[string]struct{})
others := []string{} // impl & third

if env := os.Getenv("TARGET"); env != "" {
conf.Args = append(conf.Args, strings.Fields(env)...)
}

retrieveInterfaceFn := func(filename string, depth int) {
if depth == 1 {
info.Inters = append(info.Inters, filename)
inters[filename] = struct{}{}
}
}

retrieveComposedHeadersFn := func(filename string, depth int) {
// not in the first level include maybe impl or third hfile
_, inter := inters[filename]
if depth > 1 && !inter {
others = append(others, filename)
}
}

for _, f := range conf.Includes {
content := "#include <" + f + ">"
index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{
File: content,
Temp: true,
Args: conf.Args,
})
err := clangtool.GetInclusions(&clangtool.Config{
HeaderFileName: f,
CompileArgs: conf.Args,
}, retrieveInterfaceFn)
if err != nil {
panic(err)
}
clangutils.GetInclusions(unit, func(inced clang.File, incins []clang.SourceLocation) {
if len(incins) == 1 {
filename := filepath.Clean(clang.GoString(inced.FileName()))
info.Inters = append(info.Inters, filename)
inters[filename] = struct{}{}
}
})
unit.Dispose()
index.Dispose()
}

clangtool.ComposeIncludes(conf.Includes, outfile.Name())
index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{
File: outfile.Name(),
Temp: false,
Args: conf.Args,
})
defer unit.Dispose()
defer index.Dispose()
err = clangtool.GetInclusions(&clangtool.Config{
ComposedHeaderFile: outfile.Name(),
CompileArgs: conf.Args,
}, retrieveComposedHeadersFn)
if err != nil {
panic(err)
}
clangutils.GetInclusions(unit, func(inced clang.File, incins []clang.SourceLocation) {
// not in the first level include maybe impl or third hfile
filename := filepath.Clean(clang.GoString(inced.FileName()))
_, inter := inters[filename]
if len(incins) > 1 && !inter {
others = append(others, filename)
}
})

if conf.Mix {
info.Thirds = others
Expand Down
Loading