Skip to content

Commit

Permalink
fill Include header files
Browse files Browse the repository at this point in the history
  • Loading branch information
tsingbx committed Oct 8, 2024
1 parent 0745242 commit f8532f7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 48 deletions.
91 changes: 91 additions & 0 deletions chore/llcppcfg/cfg/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package cfg

import (
"bytes"
"encoding/json"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
)

type LLCppConfig struct {
Name string `json:"name"`
Cflags string `json:"cflags"`
Include []string `json:"include"`
Libs string `json:"libs"`
TrimPrefixes []string `json:"trimPrefixes"`
Cplusplus bool `json:"cplusplus"`
}

func CmdOutString(cmd *exec.Cmd) (string, error) {
outBuf := bytes.NewBufferString("")
cmd.Stdin = os.Stdin
cmd.Stdout = outBuf
err := cmd.Run()
if err != nil {
return outBuf.String(), err
}
return outBuf.String(), nil
}

func NewLLCppConfig(name string, isCpp bool) *LLCppConfig {
cfg := &LLCppConfig{
Name: name,
}
cfg.Cflags = fmt.Sprintf("${pkg-config --cflags %s}", name)
cfg.Libs = fmt.Sprintf("${pkg-config --libs %s}", name)
cfg.TrimPrefixes = []string{}
cfg.Cplusplus = isCpp
cfg.Include = []string{}
cflags := os.Expand(cfg.Cflags, func(s string) string {
args := strings.Fields(s)
if len(args) <= 0 {
return ""
}
outString, err := CmdOutString(exec.Command(args[0], args[1:]...))
if err != nil {
return ""
}
return outString
})
includes := strings.FieldsFunc(cflags, func(r rune) bool {
return r == '\n'
})
for _, include := range includes {
trimInclude := strings.TrimPrefix(include, "-I")
trimInclude += "/"
filepath.WalkDir(trimInclude, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
ext := filepath.Ext(d.Name())
if ext != ".h" && ext != ".hpp" {
return nil
}
cfg.Include = append(cfg.Include, path)
return nil
})
}
return cfg
}

func GenCfg(name string) (*bytes.Buffer, error) {
if len(name) <= 0 {
return nil, fmt.Errorf("name can't be empty")
}
cfg := NewLLCppConfig(name, true)
buf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(buf)
jsonEncoder.SetIndent("", "\t")
err := jsonEncoder.Encode(cfg)
if err != nil {
return nil, err
}
return buf, nil
}
43 changes: 3 additions & 40 deletions chore/llcppcfg/llcppcfg.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,20 @@
package main

import (
"bytes"
"encoding/json"
"flag"
"fmt"
"log"
"os"
)

type LLCppConfig struct {
Name string `json:"name"`
Cflags string `json:"cflags"`
Include []string `json:"include"`
Libs string `json:"libs"`
TrimPrefixes []string `json:"trimPrefixes"`
Cplusplus bool `json:"cplusplus"`
}

func NewLLCppConfig(name string, isCpp bool) *LLCppConfig {
cfg := &LLCppConfig{
Name: name,
}
cfg.Cflags = fmt.Sprintf("$(pkg-config --cflags %s)", name)
cfg.Libs = fmt.Sprintf("$(pkg-config --libs %s)", name)
cfg.TrimPrefixes = []string{}
cfg.Cplusplus = isCpp
cfg.Include = []string{}
return cfg
}

func GenCfg(name string) (*bytes.Buffer, error) {
if len(name) <= 0 {
return nil, fmt.Errorf("name can't be empty")
}
cfg := NewLLCppConfig(name, true)
buf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(buf)
jsonEncoder.SetIndent("", "\t")
err := jsonEncoder.Encode(cfg)
if err != nil {
return nil, err
}
return buf, nil
}
"github.com/goplus/llgo/chore/llcppcfg/cfg"
)

func main() {
flag.Parse()
name := ""
if len(flag.Args()) > 0 {
name = flag.Arg(0)
}
buf, err := GenCfg(name)
buf, err := cfg.GenCfg(name)
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 0 additions & 8 deletions chore/llcppcfg/llcppg.cfg

This file was deleted.

0 comments on commit f8532f7

Please sign in to comment.