Skip to content

Commit

Permalink
add expandCfgByName
Browse files Browse the repository at this point in the history
  • Loading branch information
tsingbx committed Dec 11, 2024
1 parent a5ec323 commit b1ede78
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
9 changes: 2 additions & 7 deletions cmd/llcppcfg/llcppcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ package main

import (
"flag"
"fmt"
"log"
"os"

"github.com/goplus/llcppg/cmd/llcppcfg/llcppgcfg"
)

func customLog() {
fmt.Println(`llcppcfg is to generate llcppg.cfg file.
usage: llcppcfg [-cpp|-help|-expand] libname`)
}

func printHelp() {
customLog()
log.Println(`llcppcfg is to generate llcppg.cfg file.
usage: llcppcfg [-cpp|-help|-expand] libname`)
flag.PrintDefaults()
}

Expand Down
54 changes: 33 additions & 21 deletions cmd/llcppcfg/llcppgcfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package llcppgcfg
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/fs"
"log"
Expand All @@ -17,20 +16,32 @@ import (

type LLCppConfig types.Config

type NilCmdError struct {
type NilError struct {
}

func (p *NilCmdError) Error() string {
return "nil cmd"
func (p *NilError) Error() string {
return "nil error"
}

func NewNilCmdError() *NilCmdError {
return &NilCmdError{}
func NewNilError() *NilError {
return &NilError{}
}

type EmptyStringError struct {
name string
}

func (p *EmptyStringError) Error() string {
return fmt.Sprintf("%s can't be empty", p.name)
}

func NewEmptyStringError(name string) *EmptyStringError {
return &EmptyStringError{name: name}
}

func CmdOutString(cmd *exec.Cmd) (string, error) {
if cmd == nil {
return "", NewNilCmdError()
return "", NewNilError()
}
outBuf := bytes.NewBufferString("")
cmd.Stdin = os.Stdin
Expand Down Expand Up @@ -82,12 +93,11 @@ func doExpandCflags(str string, fn func(s string) bool) ([]string, string) {
}
_, ok := contains[path]
if !ok {
relPath, err := filepath.Rel(trimStr, path)
if err == nil {
contains[path] = relPath
} else {
panic(err)
relPath, errRel := filepath.Rel(trimStr, path)
if errRel != nil {
return errRel
}
contains[path] = relPath
}
return nil
})
Expand Down Expand Up @@ -138,29 +148,31 @@ func ExpandCFlagsName(name string) ([]string, string) {
return ExpandCflags(originCFlags)
}

func NewLLCppConfig(name string, isCpp bool, expand bool) *LLCppConfig {
func expandCFlagsAndLibs(name string, cfg *LLCppConfig) {
cfg.Include, cfg.CFlags = ExpandCFlagsName(name)
cfg.Libs = ExpandLibsName(name)
}

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{}
if expand {
cfg.Include, cfg.CFlags = ExpandCFlagsName(name)
cfg.Libs = ExpandLibsName(name)
return cfg
}
cfg.Include, _ = ExpandCFlagsName(name)
return cfg
}

func GenCfg(name string, cpp bool, expand bool) (*bytes.Buffer, error) {
if len(name) == 0 {
return nil, errors.New("name can't be empty")
return nil, NewEmptyStringError("name")
}
cfg := NewLLCppConfig(name, cpp)
if expand {
expandCFlagsAndLibs(name, cfg)
}
cfg := NewLLCppConfig(name, cpp, expand)
buf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(buf)
jsonEncoder.SetIndent("", "\t")
Expand Down

0 comments on commit b1ede78

Please sign in to comment.