Skip to content

Commit 8a397d4

Browse files
committed
add outline for use cobra command for llcppg
1 parent 7955d98 commit 8a397d4

File tree

20 files changed

+540
-0
lines changed

20 files changed

+540
-0
lines changed

cmd/internal/base/base.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Package base defines shared basic pieces of the llgo command,
18+
// in particular logging and the Command structure.
19+
package base
20+
21+
import (
22+
"flag"
23+
"fmt"
24+
"io"
25+
"strings"
26+
)
27+
28+
// A Command is an implementation of a xgo command
29+
// like xgo export or xgo install.
30+
type Command struct {
31+
// Run runs the command.
32+
// The args are the arguments after the command name.
33+
Run func(cmd *Command, args []string)
34+
35+
// UsageLine is the one-line usage message.
36+
// The words between "gop" and the first flag or argument in the line are taken to be the command name.
37+
UsageLine string
38+
39+
// Short is the short description shown in the 'gop help' output.
40+
Short string
41+
42+
// Flag is a set of flags specific to this command.
43+
Flag flag.FlagSet
44+
45+
// Commands lists the available commands and help topics.
46+
// The order here is the order in which they are printed by 'gop help'.
47+
// Note that subcommands are in general best avoided.
48+
Commands []*Command
49+
}
50+
51+
// Llcppg command
52+
var Llcppg = &Command{
53+
UsageLine: "llcppg",
54+
Short: `llcppg aims to be a tool for automatically generating LLGo bindings for C/C++ libraries, enhancing the experience of integrating LLGo with C!`,
55+
// Commands initialized in package main
56+
}
57+
58+
// LongName returns the command's long name: all the words in the usage line between "gop" and a flag or argument,
59+
func (c *Command) LongName() string {
60+
name := c.UsageLine
61+
if i := strings.Index(name, " ["); i >= 0 {
62+
name = name[:i]
63+
}
64+
if name == "llcppg" {
65+
return ""
66+
}
67+
return strings.TrimPrefix(name, "llcppg ")
68+
}
69+
70+
// Name returns the command's short name: the last word in the usage line before a flag or argument.
71+
func (c *Command) Name() string {
72+
name := c.LongName()
73+
if i := strings.LastIndex(name, " "); i >= 0 {
74+
name = name[i+1:]
75+
}
76+
return name
77+
}
78+
79+
// Usage show the command usage.
80+
func (c *Command) Usage(w io.Writer) {
81+
fmt.Fprintf(w, "%s\n\nUsage: %s\n", c.Short, c.UsageLine)
82+
83+
// restore output of flag
84+
defer c.Flag.SetOutput(c.Flag.Output())
85+
86+
c.Flag.SetOutput(w)
87+
c.Flag.PrintDefaults()
88+
fmt.Fprintln(w)
89+
}
90+
91+
// Runnable reports whether the command can be run; otherwise
92+
// it is a documentation pseudo-command.
93+
func (c *Command) Runnable() bool {
94+
return c.Run != nil
95+
}
96+
97+
// Usage is the usage-reporting function, filled in by package main
98+
// but here for reference by other packages.
99+
//
100+
// flag.Usage func()
101+
102+
// CmdName - "build", "install", "list", "mod tidy", etc.
103+
var CmdName string
104+
105+
// Main runs a command.
106+
func Main(c *Command, app string, args []string) {
107+
name := c.UsageLine
108+
if i := strings.Index(name, " ["); i >= 0 {
109+
c.UsageLine = app + name[i:]
110+
}
111+
c.Run(c, args)
112+
}

cmd/internal/base/pass.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package base
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
type stringValue struct {
10+
p *PassArgs
11+
name string
12+
}
13+
14+
func (p *stringValue) String() string {
15+
return ""
16+
}
17+
18+
func (p *stringValue) Set(v string) error {
19+
p.p.Args = append(p.p.Args, fmt.Sprintf("-%v=%v", p.name, v))
20+
return nil
21+
}
22+
23+
type boolValue struct {
24+
p *PassArgs
25+
name string
26+
}
27+
28+
func (p *boolValue) String() string {
29+
return ""
30+
}
31+
32+
func (p *boolValue) Set(v string) error {
33+
p.p.Args = append(p.p.Args, fmt.Sprintf("-%v=%v", p.name, v))
34+
return nil
35+
}
36+
37+
func (p *boolValue) IsBoolFlag() bool {
38+
return true
39+
}
40+
41+
type PassArgs struct {
42+
Args []string
43+
Flag *flag.FlagSet
44+
}
45+
46+
func (p *PassArgs) Tags() string {
47+
for _, v := range p.Args {
48+
if strings.HasPrefix(v, "-tags=") {
49+
return v[6:]
50+
}
51+
}
52+
return ""
53+
}
54+
55+
func (p *PassArgs) Var(names ...string) {
56+
for _, name := range names {
57+
p.Flag.Var(&stringValue{p: p, name: name}, name, "")
58+
}
59+
}
60+
61+
func (p *PassArgs) Bool(names ...string) {
62+
for _, name := range names {
63+
p.Flag.Var(&boolValue{p: p, name: name}, name, "")
64+
}
65+
}
66+
67+
func NewPassArgs(flag *flag.FlagSet) *PassArgs {
68+
p := &PassArgs{Flag: flag}
69+
return p
70+
}
71+
72+
func PassBuildFlags(cmd *Command) *PassArgs {
73+
p := NewPassArgs(&cmd.Flag)
74+
p.Bool("n", "x")
75+
p.Bool("a")
76+
p.Bool("linkshared", "race", "msan", "asan",
77+
"trimpath", "work")
78+
p.Var("p", "asmflags", "compiler",
79+
"gcflags", "gccgoflags", "installsuffix",
80+
"ldflags", "pkgdir", "toolexec", "buildvcs")
81+
return p
82+
}

cmd/internal/gencfg/gencfg.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package gencfg
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/goplus/llcppg/cmd/internal/base"
7+
)
8+
9+
var Cmd = &base.Command{
10+
UsageLine: "llcppg gencfg",
11+
Short: "generate llcpp.cfg",
12+
}
13+
14+
func init() {
15+
Cmd.Run = runCmd
16+
}
17+
18+
func runCmd(cmd *base.Command, args []string) {
19+
fmt.Printf("todo generate llcpp.cfg")
20+
}

cmd/internal/genpkg/genpkg.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package genpkg
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/goplus/llcppg/cmd/internal/base"
7+
)
8+
9+
var Cmd = &base.Command{
10+
UsageLine: "llcppg genpkg",
11+
Short: "generate a go package by signature information of symbols",
12+
}
13+
14+
func init() {
15+
Cmd.Run = runCmd
16+
}
17+
18+
func runCmd(cmd *base.Command, args []string) {
19+
fmt.Printf("todo genpkg")
20+
}

cmd/internal/gensig/gensig.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package gensig
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/goplus/llcppg/cmd/internal/base"
7+
)
8+
9+
var Cmd = &base.Command{
10+
UsageLine: "llcppg gensig",
11+
Short: "generate signature information of C/C++ symbols",
12+
}
13+
14+
func init() {
15+
Cmd.Run = runCmd
16+
}
17+
18+
func runCmd(cmd *base.Command, args []string) {
19+
fmt.Printf("todo gensig")
20+
}

cmd/internal/gensym/gensym.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package gensym
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/goplus/llcppg/cmd/internal/base"
7+
)
8+
9+
var Cmd = &base.Command{
10+
UsageLine: "llcppg gensym",
11+
Short: "generate symbol table for a C/C++ library",
12+
}
13+
14+
func init() {
15+
Cmd.Run = runCmd
16+
}
17+
18+
func runCmd(cmd *base.Command, args []string) {
19+
fmt.Printf("todo gensym")
20+
}

cmd/internal/runtest/runtest.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package runtest
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/goplus/llcppg/cmd/internal/base"
7+
)
8+
9+
var Cmd = &base.Command{
10+
UsageLine: "llcppg runtest",
11+
Short: "run test for llcppg",
12+
}
13+
14+
func init() {
15+
Cmd.Run = runCmd
16+
}
17+
18+
func runCmd(cmd *base.Command, args []string) {
19+
fmt.Printf("todo runtest")
20+
}

cmd/internal/version/version.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/goplus/llcppg/cmd/internal/base"
7+
)
8+
9+
var Cmd = &base.Command{
10+
UsageLine: "llcppg version",
11+
Short: "Print llcppg version",
12+
}
13+
14+
func init() {
15+
Cmd.Run = runCmd
16+
}
17+
18+
func runCmd(cmd *base.Command, args []string) {
19+
fmt.Printf("todo print version")
20+
}

cmd/llcppg/gencfg_cmd.gox

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import (
2+
self "github.com/goplus/llcppg/cmd/internal/gencfg"
3+
)
4+
5+
use "gencfg"
6+
7+
short "generate llcpp.cfg"
8+
9+
run args => {
10+
self.Cmd.Run self.Cmd, args
11+
}

cmd/llcppg/genpkg_cmd.gox

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import (
2+
self "github.com/goplus/llcppg/cmd/internal/genpkg"
3+
)
4+
5+
use "genpkg"
6+
7+
short "generate a go package by signature information of symbols"
8+
9+
run args => {
10+
self.Cmd.Run self.Cmd, args
11+
}

0 commit comments

Comments
 (0)