Skip to content

Commit 024b1e3

Browse files
authored
llcppcfg/feat: add -deps for autofilling (#248)
* feat: add -deps for autofilling fix: libxslt test fix: libxslt test fix: libxslt test * fix: libxslt test fix: libxslt test * chore: add llcppcfg test * chore: add linux llcppcfg test * chore: fix test * chore: fix test * chore: fix test * chore: fix test * chore: fix test * chore: fix test * chore: add comment for linux case * chore: adjust dir * chore: adjust dir * chore: adjust dir * chore: adjust test * chore: reset flags * chore: add GenCfg test * chore: remove GenCfg test
1 parent bfd5a7c commit 024b1e3

File tree

12 files changed

+290
-96
lines changed

12 files changed

+290
-96
lines changed

cmd/llcppcfg/llcppcfg.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ usage: llcppcfg [-cpp|-tab|-excludes|-exts|-help] libname`)
1616
}
1717

1818
func main() {
19+
var dependencies string
1920
var cpp, help, tab bool
2021
flag.BoolVar(&cpp, "cpp", false, "if it is c++ lib")
2122
flag.BoolVar(&help, "help", false, "print help message")
@@ -24,6 +25,7 @@ func main() {
2425
flag.StringVar(&extsString, "exts", ".h", "extra include file extensions for example -exts=\".h .hpp .hh\"")
2526
excludes := ""
2627
flag.StringVar(&excludes, "excludes", "", "exclude all header files in subdir of include expamle -excludes=\"internal impl\"")
28+
flag.StringVar(&dependencies, "deps", "", "deps for autofilling dependencies")
2729
flag.Usage = printHelp
2830
flag.Parse()
2931
if help || len(os.Args) <= 1 {
@@ -36,6 +38,8 @@ func main() {
3638
}
3739

3840
exts := strings.Fields(extsString)
41+
deps := strings.Fields(dependencies)
42+
3943
excludeSubdirs := []string{}
4044
if len(excludes) > 0 {
4145
excludeSubdirs = strings.Fields(excludes)
@@ -47,13 +51,13 @@ func main() {
4751
if tab {
4852
flag |= llcppgcfg.WithTab
4953
}
50-
buf, err := llcppgcfg.GenCfg(llcppgcfg.NewGenConfig(name, flag, exts, excludeSubdirs))
54+
buf, err := llcppgcfg.GenCfg(llcppgcfg.NewGenConfig(name, flag, exts, deps, excludeSubdirs))
5155
if err != nil {
52-
log.Fatal(err)
56+
panic(err)
5357
}
5458
outFile := "./llcppg.cfg"
5559
err = os.WriteFile(outFile, buf.Bytes(), 0600)
5660
if err != nil {
57-
log.Fatal(err)
61+
panic(err)
5862
}
5963
}

cmd/llcppcfg/llcppcfg_test.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"flag"
6+
"os"
7+
"path/filepath"
8+
"runtime"
9+
"strings"
10+
"testing"
11+
)
12+
13+
func recoverFn(fn func()) (ret any) {
14+
defer func() {
15+
ret = recover()
16+
}()
17+
fn()
18+
return
19+
}
20+
21+
func readFile(filepath string) *bytes.Buffer {
22+
buf, err := os.ReadFile(filepath)
23+
if err != nil {
24+
return bytes.NewBufferString("")
25+
}
26+
return bytes.NewBuffer(buf)
27+
}
28+
29+
func TestLLCppcfg(t *testing.T) {
30+
31+
llcppgFileName := filepath.Join("macos", "llcppg.cfg")
32+
if runtime.GOOS == "linux" {
33+
// cuurently, due to llcppcfg recognizing system path fail, all includes are empty for temporary tests.
34+
// TODO(ghl): fix it
35+
llcppgFileName = filepath.Join("linux", "llcppg.cfg")
36+
}
37+
38+
cjsonCfgFilePath := filepath.Join("llcppgcfg", "cfg_test_data", "cjson", "conf", llcppgFileName)
39+
bdwgcCfgFilePath := filepath.Join("llcppgcfg", "cfg_test_data", "bdw-gc", "conf", llcppgFileName)
40+
libffiCfgFilePath := filepath.Join("llcppgcfg", "cfg_test_data", "libffi", "conf", llcppgFileName)
41+
libxsltCfgFilePath := filepath.Join("llcppgcfg", "cfg_test_data", "libxslt", "conf", llcppgFileName)
42+
43+
type args struct {
44+
name string
45+
tab string
46+
exts []string
47+
deps []string
48+
excludeSubdirs []string
49+
}
50+
tests := []struct {
51+
name string
52+
args args
53+
want *bytes.Buffer
54+
wantErr bool
55+
}{
56+
{
57+
"libcjson",
58+
args{
59+
"libcjson",
60+
"true",
61+
[]string{".h"},
62+
[]string{},
63+
[]string{},
64+
},
65+
readFile(cjsonCfgFilePath),
66+
false,
67+
},
68+
{
69+
"bdw-gc",
70+
args{
71+
"bdw-gc",
72+
"true",
73+
[]string{".h"},
74+
[]string{},
75+
[]string{},
76+
},
77+
readFile(bdwgcCfgFilePath),
78+
false,
79+
},
80+
{
81+
"libxslt",
82+
args{
83+
"libxslt",
84+
"true",
85+
[]string{".h"},
86+
[]string{"c/os", "github.com/goplus/llpkg/[email protected]"},
87+
[]string{},
88+
},
89+
readFile(libxsltCfgFilePath),
90+
false,
91+
},
92+
{
93+
"libffi",
94+
args{
95+
"libffi",
96+
"true",
97+
[]string{".h"},
98+
[]string{},
99+
[]string{},
100+
},
101+
readFile(libffiCfgFilePath),
102+
false,
103+
},
104+
{
105+
"empty_name",
106+
args{
107+
"",
108+
"true",
109+
[]string{".h"},
110+
[]string{},
111+
[]string{},
112+
},
113+
nil,
114+
true,
115+
},
116+
{
117+
"normal_not_sort",
118+
args{
119+
"libcjson",
120+
"false",
121+
[]string{".h"},
122+
[]string{},
123+
[]string{},
124+
},
125+
nil,
126+
false,
127+
},
128+
}
129+
for _, tt := range tests {
130+
t.Run(tt.name, func(t *testing.T) {
131+
os.Args = []string{
132+
"llcppcfg",
133+
}
134+
if len(tt.args.deps) > 0 {
135+
os.Args = append(os.Args, "-deps", strings.Join(tt.args.deps, " "))
136+
}
137+
if len(tt.args.excludeSubdirs) > 0 {
138+
os.Args = append(os.Args, "-excludes", strings.Join(tt.args.excludeSubdirs, " "))
139+
}
140+
if len(tt.args.exts) > 0 {
141+
os.Args = append(os.Args, "-exts", strings.Join(tt.args.exts, " "))
142+
}
143+
os.Args = append(os.Args, tt.args.name)
144+
145+
// reset flags for the next test
146+
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
147+
148+
ret := recoverFn(main)
149+
if ret != nil {
150+
if !tt.wantErr {
151+
t.Errorf("%v", ret)
152+
}
153+
return
154+
}
155+
defer os.Remove("llcppg.cfg")
156+
if tt.want == nil {
157+
return
158+
}
159+
b, err := os.ReadFile("llcppg.cfg")
160+
if err != nil {
161+
t.Error(err)
162+
return
163+
}
164+
if !bytes.Equal(b, tt.want.Bytes()) {
165+
t.Errorf("unexpected content: want %s got %s", tt.want.String(), string(b))
166+
}
167+
})
168+
}
169+
}

cmd/llcppcfg/llcppgcfg/cfg.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ type GenConfig struct {
2020
name string
2121
flag FlagMode
2222
exts []string
23+
deps []string
2324
excludeSubdirs []string
2425
}
2526

26-
func NewGenConfig(name string, flag FlagMode, exts, excludeSubdirs []string) *GenConfig {
27-
return &GenConfig{name: name, flag: flag, exts: exts, excludeSubdirs: excludeSubdirs}
27+
func NewGenConfig(name string, flag FlagMode, exts, deps, excludeSubdirs []string) *GenConfig {
28+
return &GenConfig{name: name, flag: flag, exts: exts, deps: deps, excludeSubdirs: excludeSubdirs}
2829
}
2930

3031
type llcppCfgKey string
@@ -231,6 +232,8 @@ func GenCfg(genCfg *GenConfig) (*bytes.Buffer, error) {
231232
expandCFlags := ExpandName(genCfg.name, "", cfgCflagsKey)
232233
sortIncludes(expandCFlags, cfg, genCfg.exts, genCfg.excludeSubdirs)
233234
cfg.Name = NormalizePackageName(cfg.Name)
235+
cfg.Deps = genCfg.deps
236+
234237
buf := bytes.NewBuffer([]byte{})
235238
jsonEncoder := json.NewEncoder(buf)
236239
if genCfg.flag&WithTab != 0 {

cmd/llcppcfg/llcppgcfg/cfg_test.go

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"path/filepath"
99
"reflect"
10-
"runtime"
1110
"strings"
1211
"testing"
1312

@@ -566,96 +565,6 @@ func TestNormalizePackageName(t *testing.T) {
566565
}
567566
}
568567

569-
func TestGenCfg(t *testing.T) {
570-
if runtime.GOOS == "linux" {
571-
return
572-
}
573-
_, cjsonCfgFilePath := newCflags("cfg_test_data/cjson/llcppg.cfg")
574-
_, bdwgcCfgFilePath := newCflags("cfg_test_data/bdw-gc/llcppg.cfg")
575-
_, libffiCfgFilePath := newCflags("cfg_test_data/libffi/llcppg.cfg")
576-
577-
type args struct {
578-
name string
579-
flag FlagMode
580-
exts []string
581-
excludeSubdirs []string
582-
}
583-
tests := []struct {
584-
name string
585-
args args
586-
want *bytes.Buffer
587-
wantErr bool
588-
}{
589-
{
590-
"libcjson",
591-
args{
592-
"libcjson",
593-
WithTab,
594-
[]string{".h"},
595-
[]string{},
596-
},
597-
readFile(cjsonCfgFilePath),
598-
false,
599-
},
600-
{
601-
"bdw-gc",
602-
args{
603-
"bdw-gc",
604-
WithTab,
605-
[]string{".h"},
606-
[]string{},
607-
},
608-
readFile(bdwgcCfgFilePath),
609-
false,
610-
},
611-
{
612-
"libffi",
613-
args{
614-
"libffi",
615-
WithTab,
616-
[]string{".h"},
617-
[]string{},
618-
},
619-
readFile(libffiCfgFilePath),
620-
false,
621-
},
622-
{
623-
"empty_name",
624-
args{
625-
"",
626-
WithTab,
627-
[]string{".h"},
628-
[]string{},
629-
},
630-
nil,
631-
true,
632-
},
633-
{
634-
"normal_not_sort",
635-
args{
636-
"libcjson",
637-
0,
638-
[]string{".h"},
639-
[]string{},
640-
},
641-
nil,
642-
false,
643-
},
644-
}
645-
for _, tt := range tests {
646-
t.Run(tt.name, func(t *testing.T) {
647-
got, err := GenCfg(NewGenConfig(tt.args.name, tt.args.flag, tt.args.exts, tt.args.excludeSubdirs))
648-
if (err != nil) != tt.wantErr {
649-
t.Errorf("GenCfg() error = %v, wantErr %v", err, tt.wantErr)
650-
return
651-
}
652-
if tt.args.flag&WithTab != 0 && !reflect.DeepEqual(got, tt.want) {
653-
t.Errorf("GenCfg() = %v, want %v", got, tt.want)
654-
}
655-
})
656-
}
657-
}
658-
659568
func newCflags(relDir string) (cflags string, path string) {
660569
dir, file := filepath.Split(relDir)
661570
wd, _ := os.Getwd()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "bdw_gc",
3+
"cflags": "$(pkg-config --cflags bdw-gc)",
4+
"libs": "$(pkg-config --libs bdw-gc)",
5+
"include": [],
6+
"trimPrefixes": [],
7+
"cplusplus": false,
8+
"deps": [],
9+
"keepUnderScore": false,
10+
"impl": [
11+
{
12+
"files": [],
13+
"cond": {
14+
"os": [],
15+
"arch": []
16+
}
17+
}
18+
],
19+
"mix": false
20+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "libcjson",
3+
"cflags": "$(pkg-config --cflags libcjson)",
4+
"libs": "$(pkg-config --libs libcjson)",
5+
"include": [
6+
"cJSON_Utils.h",
7+
"cJSON.h"
8+
],
9+
"trimPrefixes": [],
10+
"cplusplus": false,
11+
"deps": [],
12+
"keepUnderScore": false,
13+
"impl": [
14+
{
15+
"files": [],
16+
"cond": {
17+
"os": [],
18+
"arch": []
19+
}
20+
}
21+
],
22+
"mix": false
23+
}

0 commit comments

Comments
 (0)