Skip to content

Commit

Permalink
Merge pull request #148 from tsingbx/llcppgtest/flag
Browse files Browse the repository at this point in the history
llcppgtest: add -exts & -excludes flag
  • Loading branch information
luoliwoshang authored Jan 13, 2025
2 parents 2ed9d71 + aeddb1a commit 08d3a9b
Showing 1 changed file with 75 additions and 28 deletions.
103 changes: 75 additions & 28 deletions cmd/llcppgtest/llcppgtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ import (
"github.com/goplus/llcppg/cmd/llcppgtest/demo"
)

type FlagName string

const (
RFlagName FlagName = "r"
RandFlagName FlagName = "rand"
AFlagName FlagName = "a"
AllFlagName FlagName = "all"
VFlagName FlagName = "v"
VfetchFlagName FlagName = "vfetch"
VsymgFlagName FlagName = "vsymg"
CppFlagName FlagName = "cpp"
ExtsFlagName FlagName = "exts"
ExcludesFlagName FlagName = "excludes"
HFlagName FlagName = "h"
HelpFlagName FlagName = "help"
DemosFlagName FlagName = "demos"
DemoFlagName FlagName = "demo"
)

func RunCommandWithOut(out *io.PipeWriter, dir, cmdName string, args ...string) {
defer out.Close()
cmd := exec.Command(cmdName, args...)
Expand Down Expand Up @@ -84,6 +103,8 @@ const (
withCpp runPkgMode = 1 << iota
withSigfetchVerbose
withSymgVerbose
withExts
withExcludes
)

type runAppMode int
Expand All @@ -96,20 +117,36 @@ const (
runDemo
)

func runPkgs(pkgs []string, runMode runPkgMode) {
type RunConfig struct {
runMode runPkgMode
exts string
excludes string
}

func NewRunConfig(runMode runPkgMode, exts, excludes string) *RunConfig {
return &RunConfig{runMode: runMode, exts: exts, excludes: excludes}
}

func runPkgs(pkgs []string, cfg *RunConfig) {
wd, _ := os.Getwd()
wg := sync.WaitGroup{}
wg.Add(len(pkgs))
llcppcfgArg := []string{}
if runMode&withCpp != 0 {
llcppcfgArg = append(llcppcfgArg, "-cpp")
if cfg.runMode&withCpp != 0 {
llcppcfgArg = append(llcppcfgArg, fmt.Sprintf("-%s", CppFlagName))
}
if cfg.runMode&withExts != 0 {
llcppcfgArg = append(llcppcfgArg, fmt.Sprintf("-%s=%s", ExtsFlagName, cfg.exts))
}
if cfg.runMode&withExcludes != 0 {
llcppcfgArg = append(llcppcfgArg, fmt.Sprintf("-%s=%s", ExcludesFlagName, cfg.excludes))
}
llcppgArg := []string{}
if runMode&withSigfetchVerbose != 0 {
llcppgArg = append(llcppgArg, "-vfetch")
if cfg.runMode&withSigfetchVerbose != 0 {
llcppgArg = append(llcppgArg, fmt.Sprintf("-%s", VfetchFlagName))
}
if runMode&withSymgVerbose != 0 {
llcppgArg = append(llcppgArg, "-vsym")
if cfg.runMode&withSymgVerbose != 0 {
llcppgArg = append(llcppgArg, fmt.Sprintf("-%s", VsymgFlagName))
}
runs := make([]string, 0)
for _, pkg := range pkgs {
Expand All @@ -133,43 +170,51 @@ func randIndex(maxInt int) int {
return r.Intn(maxInt)
}

func runPkg(runMode runPkgMode) {
func runPkg(cfg *RunConfig) {
pkgs := getPkgs()
idx := randIndex(len(pkgs))
pkg := pkgs[idx]
fmt.Printf("***start test %s\n", pkg)
runPkgs([]string{pkg}, runMode)
runPkgs([]string{pkg}, cfg)
}

func printHelp() {
helpString := `llcppgtest is used to test llcppg
usage: llcppgtest [-r|-rand|-a|-all] [-v|-vfetch|-vsym] [-cpp] [-h|-help] pkgname
llcppgtest -demos <path> # test all first-level demo directories
llcppgtest -demo <path> # test specific demo directory`
helpString := fmt.Sprintf(`
llcppgtest is used to test llcppg
usage: llcppgtest [-%s|-%s|-%s|-%s] [-%s|-%s|-%s] [-%s|-%s|-%s] [-%s|-%s] pkgname
llcppgtest -%s <path> # test all first-level demo directories
llcppgtest -%s <path> # test specific demo directory
`, RFlagName, RandFlagName, AFlagName, AllFlagName, VFlagName,
VfetchFlagName, VsymgFlagName, CppFlagName, ExtsFlagName,
ExcludesFlagName, HFlagName, HelpFlagName, DemosFlagName, DemoFlagName)
fmt.Println(helpString)
flag.PrintDefaults()
}

func main() {
rand := false
flag.BoolVar(&rand, "r", false, "same as -rand")
flag.BoolVar(&rand, "rand", false, "select one pkg of pkg-config --list-all to test")
flag.BoolVar(&rand, string(RFlagName), false, "same as -rand")
flag.BoolVar(&rand, string(RandFlagName), false, "select one pkg of pkg-config --list-all to test")
all := false
flag.BoolVar(&all, "a", false, "same as -all")
flag.BoolVar(&all, "all", false, "test all pkgs of pkg-config --list-all")
flag.BoolVar(&all, string(AFlagName), false, "same as -all")
flag.BoolVar(&all, string(AllFlagName), false, "test all pkgs of pkg-config --list-all")
v := false
flag.BoolVar(&v, "v", false, "enable verbose of llcppsigfetch and llcppsymg")
flag.BoolVar(&v, string(VFlagName), false, "enable verbose of llcppsigfetch and llcppsymg")
vSig := false
flag.BoolVar(&vSig, "vfetch", false, "enable verbose of llcppsigfetch")
flag.BoolVar(&vSig, string(VfetchFlagName), false, "enable verbose of llcppsigfetch")
vSym := false
flag.BoolVar(&vSym, "vsym", false, "enable verbose of llcppsymg")
flag.BoolVar(&vSym, string(VsymgFlagName), false, "enable verbose of llcppsymg")
cpp := false
flag.BoolVar(&cpp, "cpp", false, "if it is a cpp library")
flag.BoolVar(&cpp, string(CppFlagName), false, "if it is a cpp library")
exts := ""
flag.StringVar(&exts, string(ExtsFlagName), ".h", "for all headers with ext of exts to generate .go, for example -exts=\".h .hh .cpp .hpp\"")
excludes := ""
flag.StringVar(&excludes, string(ExcludesFlagName), "", "for all internal implementation directors that you want to excludes from -I include director to handle. For example -excludes=\"internal impl\"")
help := false
flag.BoolVar(&help, "h", false, "print help message")
flag.BoolVar(&help, "help", false, "print help message")
demosPath := flag.String("demos", "", "test all first-level demo directories in the specified path")
demoPath := flag.String("demo", "", "test the specified demo directory")
flag.BoolVar(&help, string(HFlagName), false, "print help message")
flag.BoolVar(&help, string(HelpFlagName), false, "print help message")
demosPath := flag.String(string(DemosFlagName), "", "test all first-level demo directories in the specified path")
demoPath := flag.String(string(DemoFlagName), "", "test the specified demo directory")
flag.Parse()

if help || len(os.Args) == 1 {
Expand Down Expand Up @@ -207,12 +252,14 @@ func main() {
appMode = runDemo
}

cfg := NewRunConfig(runPkgMode(appMode), exts, excludes)

switch {
case appMode == runRand:
runPkg(runPkgMode(runMode))
runPkg(cfg)
case appMode == runAll:
pkgs := getPkgs()
runPkgs(pkgs, runPkgMode(runMode))
runPkgs(pkgs, cfg)
case appMode == runDemos:
demo.RunAllGenPkgDemos(*demosPath)
case appMode == runDemo:
Expand All @@ -221,7 +268,7 @@ func main() {
if len(flag.Args()) > 0 {
arg := flag.Arg(0)
fmt.Printf("***start test %s\n", arg)
runPkgs([]string{arg}, runPkgMode(runMode))
runPkgs([]string{arg}, cfg)
} else {
printHelp()
}
Expand Down

0 comments on commit 08d3a9b

Please sign in to comment.