From 71eba833f9765f5e44241f66ab4d17f1de4a1d6f Mon Sep 17 00:00:00 2001 From: tsingbx Date: Fri, 10 Jan 2025 13:20:36 +0800 Subject: [PATCH] remove old implementation and redundant code --- cmd/llcppcfg/llcppcfg.go | 11 +- cmd/llcppcfg/llcppgcfg/cfg.go | 121 +------ cmd/llcppcfg/llcppgcfg/cfg_test.go | 328 +----------------- .../llcppgcfg/cfg_test_data/cjson/llcppg.cfg | 4 +- cmd/llcppcfg/llcppgcfg/types.go | 13 - cmd/llcppcfg/llcppgcfg/types_test.go | 28 -- cmdout/cmdout.go | 6 +- 7 files changed, 38 insertions(+), 473 deletions(-) diff --git a/cmd/llcppcfg/llcppcfg.go b/cmd/llcppcfg/llcppcfg.go index e853cf2e..0c5a7a0a 100644 --- a/cmd/llcppcfg/llcppcfg.go +++ b/cmd/llcppcfg/llcppcfg.go @@ -11,16 +11,15 @@ import ( func printHelp() { log.Println(`llcppcfg is to generate llcppg.cfg file. -usage: llcppcfg [-cpp|-help|-expand|-sort|-excludes] libname`) +usage: llcppcfg [-cpp|-tab|-excludes|-exts|-help] libname`) flag.PrintDefaults() } func main() { - var cpp, help, expand, sortByDep bool + var cpp, help, tab bool flag.BoolVar(&cpp, "cpp", false, "if it is c++ lib") flag.BoolVar(&help, "help", false, "print help message") - flag.BoolVar(&expand, "expand", false, "expand pkg-config command to result") - flag.BoolVar(&sortByDep, "sort", true, "expand every cflag and list it's include files and sort include files by dependency") + 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 := "" @@ -45,8 +44,8 @@ func main() { if cpp { flag |= llcppgcfg.WithCpp } - if sortByDep { - flag |= llcppgcfg.WithSort + if tab { + flag |= llcppgcfg.WithTab } buf, err := llcppgcfg.GenCfg(name, flag, exts, excludeSubdirs) if err != nil { diff --git a/cmd/llcppcfg/llcppgcfg/cfg.go b/cmd/llcppcfg/llcppgcfg/cfg.go index 2ed8aa50..d028f856 100644 --- a/cmd/llcppcfg/llcppgcfg/cfg.go +++ b/cmd/llcppcfg/llcppgcfg/cfg.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "io/fs" - "log" "path/filepath" "sort" "strings" @@ -15,13 +14,18 @@ import ( "github.com/goplus/llcppg/types" ) +type llcppCfgKey string + +const ( + cfgLibsKey llcppCfgKey = "libs" + cfgCflagsKey llcppCfgKey = "cflags" +) + type FlagMode int const ( - WithSort FlagMode = 1 << iota - WithNoTab + WithTab FlagMode = 1 << iota WithCpp - WithExpand ) type emptyStringError struct { @@ -59,96 +63,11 @@ func isExcludeDir(relPath string, excludeSubdirs []string) bool { return false } -func doExpandCflags(str string, excludeSubdirs []string, fn func(s string) bool) ([]string, string) { - list := strings.Fields(str) - contains := make(map[string]string, 0) - for _, l := range list { - trimStr := strings.TrimPrefix(l, "-I") - trimStr += string(filepath.Separator) - err := filepath.WalkDir(trimStr, func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - if d.IsDir() { - return nil - } - if !fn(d.Name()) { - return nil - } - _, ok := contains[path] - if !ok { - relPath, errRel := filepath.Rel(trimStr, path) - if errRel != nil { - return errRel - } - if isExcludeDir(relPath, excludeSubdirs) { - return nil - } - contains[path] = relPath - } - return nil - }) - if err != nil { - log.Println(err) - } - } - - includes := make([]string, 0) - includeMap := make(map[string]struct{}) - for path, relPath := range contains { - includeDir, found := strings.CutSuffix(path, relPath) - if found { - includeMap[includeDir] = struct{}{} - } - includes = append(includes, relPath) - } - var flagsBuilder strings.Builder - for include := range includeMap { - if flagsBuilder.Len() > 0 { - flagsBuilder.WriteRune(' ') - } - flagsBuilder.WriteString("-I" + include) - } - flags := flagsBuilder.String() - return includes, flags -} - -func ExpandName(name string, dir string, libsOrCflags string) (expand string, org string) { - originString := fmt.Sprintf("$(pkg-config --%s %s)", libsOrCflags, name) +func ExpandName(name string, dir string, cfgKey llcppCfgKey) string { + originString := fmt.Sprintf("$(pkg-config --%s %s)", cfgKey, name) return cmdout.ExpandString(originString, dir) } -func ExpandLibsName(name string, dir string) (expand string, org string) { - return ExpandName(name, dir, "libs") -} - -func ExpandCflags(originCFlags string, exts []string, excludeDirs []string) (includes []string, expand string, org string) { - cflags, orgCflags := cmdout.ExpandString(originCFlags, "") - expandIncludes, expandCflags := doExpandCflags(cflags, excludeDirs, func(s string) bool { - ext := filepath.Ext(s) - for _, e := range exts { - if e == ext { - return true - } - } - return false - }) - if len(expandCflags) > 0 { - cflags = expandCflags - } - return expandIncludes, cflags, orgCflags -} - -func ExpandCFlagsName(name string, exts []string, excludeDirs []string) (includes []string, expand string, org string) { - originCFlags := fmt.Sprintf("$(pkg-config --cflags %s)", name) - return ExpandCflags(originCFlags, exts, excludeDirs) -} - -func expandCFlagsAndLibs(name string, cfg *LLCppConfig, dir string, exts []string, excludeDirs []string) { - cfg.Include, cfg.CFlags, _ = ExpandCFlagsName(name, exts, excludeDirs) - cfg.Libs, _ = ExpandLibsName(name, dir) -} - func findDepSlice(lines []string) ([]string, string) { objFileString := "" iStart := 0 @@ -297,28 +216,14 @@ func GenCfg(name string, flag FlagMode, exts []string, excludeSubdirs []string) return nil, newEmptyStringError("name") } cfg := NewLLCppConfig(name, flag) - if flag&WithExpand != 0 { - if flag&WithSort != 0 { - cfg.CFlags, _ = ExpandName(name, "", "cflags") - cfg.Libs, _ = ExpandName(name, "", "libs") - sortIncludes(cfg.CFlags, cfg, exts, excludeSubdirs) - } else { - expandCFlagsAndLibs(name, cfg, "", exts, excludeSubdirs) - } - } else { - if flag&WithSort != 0 { - expandCFlags, _ := ExpandName(name, "", "cflags") - sortIncludes(expandCFlags, cfg, exts, excludeSubdirs) - } else { - cfg.Include, _, cfg.CFlags = ExpandCFlagsName(name, exts, excludeSubdirs) - } - } + expandCFlags := ExpandName(name, "", cfgCflagsKey) + sortIncludes(expandCFlags, cfg, exts, excludeSubdirs) cfg.Name = NormalizePackageName(cfg.Name) buf := bytes.NewBuffer([]byte{}) jsonEncoder := json.NewEncoder(buf) - if flag&WithNoTab == 0 { + if flag&WithTab != 0 { jsonEncoder.SetIndent("", "\t") } err := jsonEncoder.Encode(cfg) diff --git a/cmd/llcppcfg/llcppgcfg/cfg_test.go b/cmd/llcppcfg/llcppgcfg/cfg_test.go index bf9d594f..877c5a1c 100644 --- a/cmd/llcppcfg/llcppgcfg/cfg_test.go +++ b/cmd/llcppcfg/llcppgcfg/cfg_test.go @@ -7,7 +7,6 @@ import ( "path/filepath" "reflect" "runtime" - "sort" "strings" "testing" ) @@ -119,296 +118,42 @@ func Test_isExcludeDir(t *testing.T) { } } -func Test_doExpandCflags(t *testing.T) { - cflags, _ := newCflags("cfg_test_data/libtasn1/include/") - type args struct { - str string - excludeSubdirs []string - fn func(s string) bool - } - tests := []struct { - name string - args args - want []string - want1 string - }{ - { - "h", - args{ - cflags, - []string{"internal"}, - func(s string) bool { - ext := filepath.Ext(s) - return ext == ".h" - }, - }, - []string{ - "libtasn1.h", - }, - cflags, - }, - { - "hh", - args{ - cflags, - []string{"internal"}, - func(s string) bool { - ext := filepath.Ext(s) - return ext == ".hh" - }, - }, - []string{ - "b.hh", - }, - cflags, - }, - { - "hh&h", - args{ - cflags, - []string{"internal"}, - func(s string) bool { - ext := filepath.Ext(s) - return ext == ".hh" || ext == ".h" - }, - }, - []string{ - "b.hh", - "libtasn1.h", - }, - cflags, - }, - { - "hh&h-no-excludes", - args{ - cflags, - []string{}, - func(s string) bool { - ext := filepath.Ext(s) - return ext == ".hh" || ext == ".h" - }, - }, - []string{ - "b.hh", - "internal/a.h", - "libtasn1.h", - }, - cflags, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, got1 := doExpandCflags(tt.args.str, tt.args.excludeSubdirs, tt.args.fn) - sort.Strings(got) - sort.Strings(tt.want) - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("doExpandCflags() got = %v, want %v", got, tt.want) - } - if got1 != tt.want1 { - t.Errorf("doExpandCflags() got1 = %v, want %v", got1, tt.want1) - } - }) - } -} - func TestExpandName(t *testing.T) { type args struct { - name string - dir string - libsOrCflags string + name string + dir string + cfgKey llcppCfgKey } tests := []struct { name string args args wantExpandPrefix string - wantOrg string }{ { "cflags", args{ "libcjson", "", - "cflags", + cfgCflagsKey, }, "-I/", - "$(pkg-config --cflags libcjson)", }, { "libs", args{ "libcjson", "", - "libs", + cfgLibsKey, }, "-", - "$(pkg-config --libs libcjson)", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - gotExpand, gotOrg := ExpandName(tt.args.name, tt.args.dir, tt.args.libsOrCflags) + gotExpand := ExpandName(tt.args.name, tt.args.dir, tt.args.cfgKey) if !strings.HasPrefix(gotExpand, tt.wantExpandPrefix) { t.Errorf("ExpandName() gotExpand = %v, want %v", gotExpand, tt.wantExpandPrefix) } - if gotOrg != tt.wantOrg { - t.Errorf("ExpandName() gotOrg = %v, want %v", gotOrg, tt.wantOrg) - } - }) - } -} - -func TestExpandLibsName(t *testing.T) { - type args struct { - name string - dir string - } - tests := []struct { - name string - args args - wantExpandPrefix string - wantOrg string - }{ - { - "", - args{ - "libcjson", - "", - }, - "-", - "$(pkg-config --libs libcjson)", - }, - } - for _, tt := range tests { - t.Run(tt.args.name, func(t *testing.T) { - gotExpand, gotOrg := ExpandLibsName(tt.args.name, tt.args.dir) - if !strings.HasPrefix(gotExpand, tt.wantExpandPrefix) { - t.Errorf("ExpandLibsName() gotExpand = %v, want %v", gotExpand, tt.wantExpandPrefix) - } - if gotOrg != tt.wantOrg { - t.Errorf("ExpandLibsName() gotOrg = %v, want %v", gotOrg, tt.wantOrg) - } - }) - } -} - -func TestExpandCflags(t *testing.T) { - type args struct { - originCFlags string - exts []string - excludeDirs []string - } - tests := []struct { - name string - args args - wantIncludes []string - wantExpandPrefix string - wantOrg string - }{ - { - "libcjson", - args{ - "$(pkg-config --cflags libcjson)", - []string{".h"}, - []string{""}, - }, - []string{"cJSON_Utils.h", "cJSON.h"}, - "-I/", - "$(pkg-config --cflags libcjson)", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gotIncludes, gotExpand, gotOrg := ExpandCflags(tt.args.originCFlags, tt.args.exts, tt.args.excludeDirs) - sort.Strings(gotIncludes) - sort.Strings(tt.wantIncludes) - if !equalIncludes(gotIncludes, tt.wantIncludes) { - t.Errorf("ExpandCflags() gotIncludes = %v, want %v", gotIncludes, tt.wantIncludes) - - } - if !strings.HasPrefix(gotExpand, tt.wantExpandPrefix) { - t.Errorf("ExpandCflags() gotExpand = %v, want %v", gotExpand, tt.wantExpandPrefix) - } - if gotOrg != tt.wantOrg { - t.Errorf("ExpandCflags() gotOrg = %v, want %v", gotOrg, tt.wantOrg) - } - }) - } -} - -func TestExpandCFlagsName(t *testing.T) { - type args struct { - name string - exts []string - excludeDirs []string - } - tests := []struct { - name string - args args - wantIncludes []string - wantExpandPrefix string - wantOrg string - }{ - { - "libcjson", - args{ - "libcjson", - []string{".h"}, - []string{}, - }, - []string{"cjson/cJSON_Utils.h", "cjson/cJSON.h"}, - "-I/", - "$(pkg-config --cflags libcjson)", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gotIncludes, gotExpand, gotOrg := ExpandCFlagsName(tt.args.name, tt.args.exts, tt.args.excludeDirs) - sort.Strings(gotIncludes) - sort.Strings(tt.wantIncludes) - if !equalIncludes(gotIncludes, tt.wantIncludes) { - t.Errorf("ExpandCFlagsName() gotIncludes = %v, want %v", gotIncludes, tt.wantIncludes) - } - if !strings.HasPrefix(gotExpand, tt.wantExpandPrefix) { - t.Errorf("ExpandCFlagsName() gotExpand = %v, want %v", gotExpand, tt.wantExpandPrefix) - } - if gotOrg != tt.wantOrg { - t.Errorf("ExpandCFlagsName() gotOrg = %v, want %v", gotOrg, tt.wantOrg) - } - }) - } -} - -func Test_expandCFlagsAndLibs(t *testing.T) { - config := NewLLCppConfig("libcjson", WithSort) - type args struct { - name string - cfg *LLCppConfig - dir string - exts []string - excludeDirs []string - } - tests := []struct { - name string - args args - }{ - { - "libcjson", - args{ - "libcjson", - config, - "", - []string{}, - []string{}, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - expandCFlagsAndLibs(tt.args.name, tt.args.cfg, tt.args.dir, tt.args.exts, tt.args.excludeDirs) - if !strings.HasPrefix(config.CFlags, "-I") || - !strings.HasPrefix(config.Libs, "-") { - t.Errorf("%s expand cflags and libs fail", tt.args.name) - } }) } } @@ -742,7 +487,7 @@ func TestNewLLCppConfig(t *testing.T) { "libcjson", args{ "libcjson", - WithSort, + WithTab, }, &LLCppConfig{ Name: "libcjson", @@ -822,6 +567,7 @@ func TestGenCfg(t *testing.T) { if runtime.GOOS == "linux" { return } + _, cjsonCfgFilePath := newCflags("cfg_test_data/cjson/llcppg.cfg") _, bdwgcCfgFilePath := newCflags("cfg_test_data/bdw-gc/llcppg.cfg") _, libffiCfgFilePath := newCflags("cfg_test_data/libffi/llcppg.cfg") @@ -841,18 +587,18 @@ func TestGenCfg(t *testing.T) { "libcjson", args{ "libcjson", - WithSort, + WithTab, []string{".h"}, []string{}, }, - bytes.NewBufferString("{\n\t\"name\": \"libcjson\",\n\t\"cflags\": \"$(pkg-config --cflags libcjson)\",\n\t\"libs\": \"$(pkg-config --libs libcjson)\",\n\t\"include\": [\n\t\t\"cjson/cJSON_Utils.h\",\n\t\t\"cjson/cJSON.h\",\n\t\t\"cJSON_Utils.h\",\n\t\t\"cJSON.h\"\n\t],\n\t\"deps\": null,\n\t\"trimPrefixes\": [],\n\t\"cplusplus\": false\n}\n"), + readFile(cjsonCfgFilePath), false, }, { "bdw-gc", args{ "bdw-gc", - WithSort, + WithTab, []string{".h"}, []string{}, }, @@ -863,7 +609,7 @@ func TestGenCfg(t *testing.T) { "libffi", args{ "libffi", - WithSort, + WithTab, []string{".h"}, []string{}, }, @@ -874,35 +620,13 @@ func TestGenCfg(t *testing.T) { "empty_name", args{ "", - WithSort, + WithTab, []string{".h"}, []string{}, }, nil, true, }, - { - "expand", - args{ - "libcjson", - WithSort | WithExpand, - []string{".h"}, - []string{}, - }, - nil, - false, - }, - { - "expand_not_sort", - args{ - "libcjson", - WithExpand, - []string{".h"}, - []string{}, - }, - nil, - false, - }, { "normal_not_sort", args{ @@ -922,14 +646,8 @@ func TestGenCfg(t *testing.T) { t.Errorf("GenCfg() error = %v, wantErr %v", err, tt.wantErr) return } - if tt.args.flag&WithExpand != 0 { - if got.Len() <= 0 { - t.Errorf("GenCfg() = %v, want expaned", got) - } - } else { - if tt.args.flag&WithSort != 0 && !reflect.DeepEqual(got, tt.want) { - t.Errorf("GenCfg() = %v, want %v", got, tt.want) - } + if tt.args.flag&WithTab != 0 && !reflect.DeepEqual(got, tt.want) { + t.Errorf("GenCfg() = %v, want %v", got, tt.want) } }) } @@ -954,22 +672,6 @@ func joinPath(dir, rel string) string { return path } -func equalIncludes(gotIncludes, wantIncludes []string) bool { - if len(gotIncludes) != len(wantIncludes) { - return false - } - for i := range gotIncludes { - got := gotIncludes[i] - want := wantIncludes[i] - _, gotFile := filepath.Split(got) - _, wantFile := filepath.Split(want) - if gotFile != wantFile { - return false - } - } - return true -} - func readFile(filepath string) *bytes.Buffer { buf, err := os.ReadFile(filepath) if err != nil { diff --git a/cmd/llcppcfg/llcppgcfg/cfg_test_data/cjson/llcppg.cfg b/cmd/llcppcfg/llcppgcfg/cfg_test_data/cjson/llcppg.cfg index 745d6a7d..182754dc 100644 --- a/cmd/llcppcfg/llcppgcfg/cfg_test_data/cjson/llcppg.cfg +++ b/cmd/llcppcfg/llcppgcfg/cfg_test_data/cjson/llcppg.cfg @@ -1,8 +1,10 @@ { "name": "libcjson", - "cflags": "-Icfg_test_data/cjson/include", + "cflags": "$(pkg-config --cflags libcjson)", "libs": "$(pkg-config --libs libcjson)", "include": [ + "cjson/cJSON_Utils.h", + "cjson/cJSON.h", "cJSON_Utils.h", "cJSON.h" ], diff --git a/cmd/llcppcfg/llcppgcfg/types.go b/cmd/llcppcfg/llcppgcfg/types.go index 967d0b0d..e03a302a 100644 --- a/cmd/llcppcfg/llcppgcfg/types.go +++ b/cmd/llcppcfg/llcppgcfg/types.go @@ -57,16 +57,3 @@ type CflagEntry struct { func (c *CflagEntry) String() string { return fmt.Sprintf("{Include:%s, ObjFiles:%v}", c.Include, c.ObjFiles) } - -func removeDups[T comparable](s []T) []T { - m := make(map[T]struct{}) - r := make([]T, 0) - for _, ss := range s { - _, ok := m[ss] - if !ok { - m[ss] = struct{}{} - r = append(r, ss) - } - } - return r -} diff --git a/cmd/llcppcfg/llcppgcfg/types_test.go b/cmd/llcppcfg/llcppgcfg/types_test.go index 8e32abe0..753f25a4 100644 --- a/cmd/llcppcfg/llcppgcfg/types_test.go +++ b/cmd/llcppcfg/llcppgcfg/types_test.go @@ -1,7 +1,6 @@ package llcppgcfg import ( - "reflect" "testing" ) @@ -134,33 +133,6 @@ func TestCflagEntry_String(t *testing.T) { } } -func Test_removeDups(t *testing.T) { - type args[TT comparable] struct { - s []TT - } - type CaseType[TT comparable] struct { - name string - args args[TT] - want []TT - } - tests := []CaseType[int]{ - { - "ints", - args[int]{ - []int{1, 1, 7, 7, 7, 2, 3, 5, 6, 6}, - }, - []int{1, 7, 2, 3, 5, 6}, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := removeDups(tt.args.s); !reflect.DeepEqual(got, tt.want) { - t.Errorf("removeDups() = %v, want %v", got, tt.want) - } - }) - } -} - func TestObjFile_IsEqual(t *testing.T) { type args struct { o *ObjFile diff --git a/cmdout/cmdout.go b/cmdout/cmdout.go index 11006f1c..8742f632 100644 --- a/cmdout/cmdout.go +++ b/cmdout/cmdout.go @@ -30,8 +30,7 @@ func NewExecCommand(cmdStr string, args ...string) *exec.Cmd { return exec.Command(cmdStr, args...) } -func ExpandString(str string, dir string) (expand string, org string) { - org = str +func ExpandString(str string, dir string) string { str = strings.ReplaceAll(str, "(", "{") str = strings.ReplaceAll(str, ")", "}") expandStr := os.Expand(str, func(s string) string { @@ -45,8 +44,7 @@ func ExpandString(str string, dir string) (expand string, org string) { } return outString }) - expand = strings.TrimSpace(expandStr) - return expand, org + return strings.TrimSpace(expandStr) } type nilError struct {