diff --git a/cmd/llcppcfg/gen/cfg.go b/cmd/llcppcfg/gen/cfg.go index f0993bab3..dbe80e0d6 100644 --- a/cmd/llcppcfg/gen/cfg.go +++ b/cmd/llcppcfg/gen/cfg.go @@ -1,7 +1,6 @@ package gen import ( - "bytes" "encoding/json" "errors" "fmt" @@ -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 @@ -34,13 +29,6 @@ const ( cfgCflagsKey llcppCfgKey = "cflags" ) -type FlagMode int - -const ( - WithTab FlagMode = 1 << iota - WithCpp -) - type emptyStringError struct { name string } @@ -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) @@ -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, "", " ") } diff --git a/cmd/llcppcfg/gen/cfg_test.go b/cmd/llcppcfg/gen/cfg_test.go index 7a5d5ed77..922317eb2 100644 --- a/cmd/llcppcfg/gen/cfg_test.go +++ b/cmd/llcppcfg/gen/cfg_test.go @@ -442,7 +442,7 @@ func Test_sortIncludes(t *testing.T) { []string{".h"}, []string{}, }, - []string{}, + nil, }, { "deps/case4_recircle", @@ -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) } @@ -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 @@ -486,7 +487,7 @@ func TestNewLLCppConfig(t *testing.T) { "libcjson", args{ "libcjson", - WithTab, + false, }, &llcppg.Config{ Name: "libcjson", @@ -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) } }) } diff --git a/cmd/llcppcfg/gen/types.go b/cmd/llcppcfg/gen/types.go index e1820022c..844b3701b 100644 --- a/cmd/llcppcfg/gen/types.go +++ b/cmd/llcppcfg/gen/types.go @@ -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) { diff --git a/cmd/llcppcfg/llcppcfg.go b/cmd/llcppcfg/llcppcfg.go index ba892cc8d..812d13d4c 100644 --- a/cmd/llcppcfg/llcppcfg.go +++ b/cmd/llcppcfg/llcppcfg.go @@ -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 := "" @@ -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) }