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
84 changes: 32 additions & 52 deletions cmd/llcppcfg/gen/cfg.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gen

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand All @@ -16,15 +15,11 @@ import (
)

type Config struct {
name string
flag FlagMode
exts []string
deps []string
excludeSubdirs []string
}

func NewConfig(name string, flag FlagMode, exts, deps, excludeSubdirs []string) *Config {
return &Config{name: name, flag: flag, exts: exts, deps: deps, excludeSubdirs: excludeSubdirs}
Name string
IsCpp bool
Exts []string
Deps []string
ExcludeSubdirs []string
}

type llcppCfgKey string
Expand All @@ -34,13 +29,6 @@ const (
cfgCflagsKey llcppCfgKey = "cflags"
)

type FlagMode int

const (
WithTab FlagMode = 1 << iota
WithCpp
)

type emptyStringError struct {
name string
}
Expand Down Expand Up @@ -189,25 +177,6 @@ func parseCFlagsEntry(cflags, cflag string, exts []string, excludeSubdirs []stri
return &cflagEntry
}

func sortIncludes(expandCflags string, cfg *llcppg.Config, exts []string, excludeSubdirs []string) {
list := strings.Fields(expandCflags)
includeList := NewIncludeList()
for i, cflag := range list {
pCflagEntry := parseCFlagsEntry(expandCflags, cflag, exts, excludeSubdirs)
includeList.AddCflagEntry(i, pCflagEntry)
}
cfg.Include = includeList.include
}

func newLLCppgConfig(name string, flag FlagMode) *llcppg.Config {
cfg := llcppg.NewDefault()
cfg.Name = name
cfg.CFlags = fmt.Sprintf("$(pkg-config --cflags %s)", name)
cfg.Libs = fmt.Sprintf("$(pkg-config --libs %s)", name)
cfg.Cplusplus = (flag&WithCpp != 0)
return cfg
}

func NormalizePackageName(name string) string {
fields := strings.FieldsFunc(name, func(r rune) bool {
return !unicode.IsLetter(r) && r != '_' && !unicode.IsDigit(r)
Expand All @@ -220,24 +189,35 @@ func NormalizePackageName(name string) string {
return strings.Join(fields, "_")
}

func (c *Config) toLLCppg() *llcppg.Config {
cfg := llcppg.NewDefault()
cfg.Name = NormalizePackageName(c.Name)
cfg.CFlags = fmt.Sprintf("$(pkg-config --cflags %s)", c.Name)
cfg.Libs = fmt.Sprintf("$(pkg-config --libs %s)", c.Name)
cfg.Cplusplus = c.IsCpp
cfg.Deps = c.Deps

expandCFlags := ExpandName(c.Name, "", cfgCflagsKey)
cfg.Include = c.sortIncludes(expandCFlags, c.Exts, c.ExcludeSubdirs)

return cfg
}

func (c *Config) sortIncludes(expandCflags string, exts []string, excludeSubdirs []string) []string {
list := strings.Fields(expandCflags)
includeList := NewIncludeList()
for i, cflag := range list {
pCflagEntry := parseCFlagsEntry(expandCflags, cflag, exts, excludeSubdirs)
includeList.AddCflagEntry(i, pCflagEntry)
}
return includeList.include
}

func Do(genCfg *Config) ([]byte, error) {
if len(genCfg.name) == 0 {
if len(genCfg.Name) == 0 {
return nil, newEmptyStringError("name")
}
cfg := newLLCppgConfig(genCfg.name, genCfg.flag)
expandCFlags := ExpandName(genCfg.name, "", cfgCflagsKey)
sortIncludes(expandCFlags, cfg, genCfg.exts, genCfg.excludeSubdirs)
cfg.Name = NormalizePackageName(cfg.Name)
cfg.Deps = genCfg.deps
cfg := genCfg.toLLCppg()

buf := bytes.NewBuffer([]byte{})
jsonEncoder := json.NewEncoder(buf)
if genCfg.flag&WithTab != 0 {
jsonEncoder.SetIndent("", "\t")
}
err := jsonEncoder.Encode(cfg)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
return json.MarshalIndent(cfg, "", " ")
}
16 changes: 9 additions & 7 deletions cmd/llcppcfg/gen/cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func Test_sortIncludes(t *testing.T) {
[]string{".h"},
[]string{},
},
[]string{},
nil,
},
{
"deps/case4_recircle",
Expand All @@ -464,7 +464,8 @@ func Test_sortIncludes(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sortIncludes(tt.args.expandCflags, tt.args.cfg, tt.args.exts, tt.args.excludeSubdirs)
c := &Config{Exts: tt.args.exts, ExcludeSubdirs: tt.args.excludeSubdirs}
tt.args.cfg.Include = c.sortIncludes(tt.args.expandCflags, tt.args.exts, tt.args.excludeSubdirs)
if !reflect.DeepEqual(tt.args.cfg.Include, tt.wantInclude) {
t.Errorf("sortIncludes() = %v, want %v", tt.args.cfg.Include, tt.wantInclude)
}
Expand All @@ -474,8 +475,8 @@ func Test_sortIncludes(t *testing.T) {

func TestNewLLCppConfig(t *testing.T) {
type args struct {
name string
flag FlagMode
name string
isCpp bool
}
tests := []struct {
name string
Expand All @@ -486,7 +487,7 @@ func TestNewLLCppConfig(t *testing.T) {
"libcjson",
args{
"libcjson",
WithTab,
false,
},
&llcppg.Config{
Name: "libcjson",
Expand All @@ -499,8 +500,9 @@ func TestNewLLCppConfig(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := newLLCppgConfig(tt.args.name, tt.args.flag); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewLLCppgConfig() = %v, want %v", got, tt.want)
c := &Config{Name: tt.args.name, IsCpp: tt.args.isCpp}
if got := c.toLLCppg(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewLLCppgConfig() = %#v, want %#v", got, tt.want)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/llcppcfg/gen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type IncludeList struct {
}

func NewIncludeList() *IncludeList {
return &IncludeList{include: make([]string, 0), absPathMap: make(map[string]struct{}), relPathMap: make(map[string]struct{})}
return &IncludeList{absPathMap: make(map[string]struct{}), relPathMap: make(map[string]struct{})}
}

func (p *IncludeList) AddCflagEntry(index int, entry *CflagEntry) {
Expand Down
19 changes: 9 additions & 10 deletions cmd/llcppcfg/llcppcfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ usage: llcppcfg [-cpp|-tab|-excludes|-exts|-help] libname`)

func main() {
var dependencies string
var cpp, help, tab bool
var cpp, help bool
flag.BoolVar(&cpp, "cpp", false, "if it is c++ lib")
flag.BoolVar(&help, "help", false, "print help message")
flag.BoolVar(&tab, "tab", true, "generate .cfg config file with tab indent")
extsString := ""
flag.StringVar(&extsString, "exts", ".h", "extra include file extensions for example -exts=\".h .hpp .hh\"")
excludes := ""
Expand All @@ -44,14 +43,14 @@ func main() {
if len(excludes) > 0 {
excludeSubdirs = strings.Fields(excludes)
}
var flag gen.FlagMode
if cpp {
flag |= gen.WithCpp
}
if tab {
flag |= gen.WithTab
}
buf, err := gen.Do(gen.NewConfig(name, flag, exts, deps, excludeSubdirs))

buf, err := gen.Do(&gen.Config{
Name: name,
Exts: exts,
Deps: deps,
IsCpp: cpp,
ExcludeSubdirs: excludeSubdirs,
})
if err != nil {
panic(err)
}
Expand Down