-
Notifications
You must be signed in to change notification settings - Fork 11
use cobra command for llcppg #582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tsingbx
wants to merge
9
commits into
goplus:main
Choose a base branch
from
tsingbx:cpp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8a397d4
add outline for use cobra command for llcppg
tsingbx 9a8e25e
implement gencfg sub command
tsingbx c0fc2d0
implement sub command for gensym
tsingbx 2cad0a2
implement sub command for gensig
tsingbx f1938d8
implement genpkg sub command
tsingbx c0e13b4
rename new llcppg command to llcppgx
tsingbx 45caad9
remove version sub command
tsingbx bda7808
delete default completion sub command
tsingbx fb56e76
check -mod flag and fix type error
tsingbx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| *.so | ||
| *.dylib | ||
| *.DS_Store | ||
| *_autogen.go | ||
|
|
||
| # Test binary, built with `go test -c` | ||
| *.test | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // Package base defines shared basic pieces of the llgo command, | ||
| // in particular logging and the Command structure. | ||
| package base | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/goplus/llcppg/config" | ||
| ) | ||
|
|
||
| // A Command is an implementation of a llcppgx command | ||
| // like llcppgx gensym or llcppgx genpkg. | ||
| type Command struct { | ||
| // Run runs the command. | ||
| // The args are the arguments after the command name. | ||
| Run func(cmd *Command, args []string) | ||
|
|
||
| // UsageLine is the one-line usage message. | ||
| // The words between "llcppgx" and the first flag or argument in the line are taken to be the command name. | ||
| UsageLine string | ||
|
|
||
| // Short is the short description shown in the 'llcppgx help' output. | ||
| Short string | ||
|
|
||
| // Flag is a set of flags specific to this command. | ||
| Flag flag.FlagSet | ||
|
|
||
| // Commands lists the available commands and help topics. | ||
| // The order here is the order in which they are printed by 'llcppgx help'. | ||
| // Note that subcommands are in general best avoided. | ||
| Commands []*Command | ||
| } | ||
|
|
||
| // Llcppg command | ||
| var Llcppg = &Command{ | ||
| UsageLine: "llcppgx", | ||
| Short: `llcppgx aims to be a tool for automatically generating LLGo bindings for C/C++ libraries.`, | ||
| // Commands initialized in package main | ||
| } | ||
|
|
||
| // LongName returns the command's long name: all the words in the usage line between "llcppgx" and a flag or argument, | ||
| func (c *Command) LongName() string { | ||
| name := c.UsageLine | ||
| if i := strings.Index(name, " ["); i >= 0 { | ||
| name = name[:i] | ||
| } | ||
| if name == "llcppgx" { | ||
| return "" | ||
| } | ||
| return strings.TrimPrefix(name, "llcppgx ") | ||
| } | ||
|
|
||
| // Name returns the command's short name: the last word in the usage line before a flag or argument. | ||
| func (c *Command) Name() string { | ||
| name := c.LongName() | ||
| if i := strings.LastIndex(name, " "); i >= 0 { | ||
| name = name[i+1:] | ||
| } | ||
| return name | ||
| } | ||
|
|
||
| // Usage show the command usage. | ||
| func (c *Command) Usage(w io.Writer) { | ||
| fmt.Fprintf(w, "%s\n\nUsage: %s\n", c.Short, c.UsageLine) | ||
|
|
||
| // restore output of flag | ||
| defer c.Flag.SetOutput(c.Flag.Output()) | ||
|
|
||
| c.Flag.SetOutput(w) | ||
| c.Flag.PrintDefaults() | ||
| fmt.Fprintln(w) | ||
| } | ||
|
|
||
| // Runnable reports whether the command can be run; otherwise | ||
| // it is a documentation pseudo-command. | ||
| func (c *Command) Runnable() bool { | ||
| return c.Run != nil | ||
| } | ||
|
|
||
| func RunCmdWithName(cmd *Command, args []string, name string, out *io.PipeWriter) { | ||
| err := cmd.Flag.Parse(args) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| cfgFile := config.LLCPPG_CFG | ||
|
|
||
| bytesOfConf, err := config.MarshalConfigFile(cfgFile) | ||
| Check(err) | ||
|
|
||
| if cmd.Flag.NArg() == 0 { | ||
| args = append(args, "-") | ||
| } | ||
|
|
||
| nameCmd := exec.Command(name, args...) | ||
| nameCmd.Stdin = bytes.NewReader(bytesOfConf) | ||
| nameCmd.Stdout = os.Stdout | ||
| if out != nil { | ||
| nameCmd.Stdout = out | ||
| } | ||
| nameCmd.Stderr = os.Stderr | ||
| Check(nameCmd.Run()) | ||
| } | ||
|
|
||
| func Check(err error) { | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
|
|
||
| // Usage is the usage-reporting function, filled in by package main | ||
| // but here for reference by other packages. | ||
| // | ||
| // flag.Usage func() | ||
|
|
||
| // CmdName - "build", "install", "list", "mod tidy", etc. | ||
| var CmdName string | ||
|
|
||
| // Main runs a command. | ||
| func Main(c *Command, app string, args []string) { | ||
| name := c.UsageLine | ||
| if i := strings.Index(name, " ["); i >= 0 { | ||
| c.UsageLine = app + name[i:] | ||
| } | ||
| c.Run(c, args) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package gencfg | ||
|
|
||
| import "flag" | ||
|
|
||
| var dependencies string | ||
| var extsString string | ||
| var excludes string | ||
| var cpp, help, tab bool | ||
|
|
||
| func addFlags(fs *flag.FlagSet) { | ||
| fs.BoolVar(&cpp, "cpp", false, "if it is C++ lib") | ||
| fs.BoolVar(&help, "help", false, "print help message") | ||
| fs.BoolVar(&tab, "tab", true, "generate .cfg config file with tab indent") | ||
| fs.StringVar(&excludes, "excludes", "", "exclude all header files in subdir of include example -excludes=\"internal impl\"") | ||
| fs.StringVar(&extsString, "exts", ".h", "extra include file extensions for example -exts=\".h .hpp .hh\"") | ||
| fs.StringVar(&dependencies, "deps", "", "deps for autofilling dependencies") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package gencfg | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/goplus/llcppg/cmd/internal/base" | ||
| "github.com/goplus/llcppg/cmd/llcppcfg/gen" | ||
| "github.com/goplus/llcppg/config" | ||
| ) | ||
|
|
||
| var Cmd = &base.Command{ | ||
| UsageLine: "llcppg init", | ||
| Short: "init llcppg.cfg config file", | ||
| } | ||
|
|
||
| func init() { | ||
| Cmd.Run = runCmd | ||
| addFlags(&Cmd.Flag) | ||
| } | ||
|
|
||
| func runCmd(cmd *base.Command, args []string) { | ||
|
|
||
| if err := cmd.Flag.Parse(args); err != nil { | ||
| return | ||
| } | ||
|
|
||
| name := "" | ||
| if len(cmd.Flag.Args()) > 0 { | ||
| name = cmd.Flag.Arg(0) | ||
| } | ||
|
|
||
| if len(name) == 0 { | ||
| return | ||
| } | ||
|
|
||
| exts := strings.Fields(extsString) | ||
| deps := strings.Fields(dependencies) | ||
|
|
||
| excludeSubdirs := []string{} | ||
| if len(excludes) > 0 { | ||
| excludeSubdirs = strings.Fields(excludes) | ||
| } | ||
| var flagMode gen.FlagMode | ||
| if cpp { | ||
| flagMode |= gen.WithCpp | ||
| } | ||
| if tab { | ||
| flagMode |= gen.WithTab | ||
| } | ||
| buf, err := gen.Do(gen.NewConfig(name, flagMode, exts, deps, excludeSubdirs)) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| outFile := config.LLCPPG_CFG | ||
| err = os.WriteFile(outFile, buf, 0600) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package genpkg | ||
|
|
||
| import ( | ||
| "flag" | ||
| ) | ||
|
|
||
| var modulePath string | ||
| var verbose bool | ||
|
|
||
| func addFlags(fs *flag.FlagSet) { | ||
| fs.BoolVar(&verbose, "v", false, "enable verbose output") | ||
| fs.StringVar(&modulePath, "mod", "", "the module path of the generated code,if not set,will not init a new module") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package genpkg | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
|
|
||
| "github.com/goplus/llcppg/cmd/internal/base" | ||
| "github.com/goplus/llcppg/config" | ||
| "golang.org/x/mod/module" | ||
| ) | ||
|
|
||
| var Cmd = &base.Command{ | ||
| UsageLine: "llcppg genpkg", | ||
| Short: "generate a go package by signature information of symbols", | ||
| } | ||
|
|
||
| func init() { | ||
| Cmd.Run = runCmd | ||
| addFlags(&Cmd.Flag) | ||
| } | ||
|
|
||
| func runCmd(cmd *base.Command, args []string) { | ||
| err := cmd.Flag.Parse(args) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| err = module.CheckPath(modulePath) | ||
| base.Check(err) | ||
|
|
||
| cfgFile := config.LLCPPG_CFG | ||
|
|
||
| config.HandleMarshalConfigFile(cfgFile, func(b []byte, err error) { | ||
|
|
||
| base.Check(err) | ||
|
|
||
| r, w := io.Pipe() | ||
|
|
||
| errCh := make(chan error, 1) | ||
| go func() { | ||
| defer w.Close() | ||
| llcppsigfetchCmdArgs := make([]string, 0) | ||
| if verbose { | ||
| llcppsigfetchCmdArgs = append(llcppsigfetchCmdArgs, "-v") | ||
| } | ||
| if cmd.Flag.NArg() == 0 { | ||
| llcppsigfetchCmdArgs = append(llcppsigfetchCmdArgs, "-") | ||
| } | ||
| llcppsigfetchCmd := exec.Command("llcppsigfetch", llcppsigfetchCmdArgs...) | ||
| llcppsigfetchCmd.Stdin = bytes.NewReader(b) | ||
| llcppsigfetchCmd.Stdout = w | ||
| llcppsigfetchCmd.Stderr = os.Stderr | ||
| errCh <- llcppsigfetchCmd.Run() | ||
| }() | ||
|
|
||
| gogensigCmdArgs := make([]string, 0) | ||
| gogensigCmdArgs = append(gogensigCmdArgs, fmt.Sprintf("-mod=%s", modulePath)) | ||
| if verbose { | ||
| gogensigCmdArgs = append(gogensigCmdArgs, "-v") | ||
| } | ||
| gogensigCmd := exec.Command("gogensig", gogensigCmdArgs...) | ||
| gogensigCmd.Stdin = r | ||
| gogensigCmd.Stdout = os.Stdout | ||
| gogensigCmd.Stderr = os.Stderr | ||
| err = gogensigCmd.Run() | ||
| base.Check(err) | ||
|
|
||
| if fetchErr := <-errCh; fetchErr != nil { | ||
| base.Check(fetchErr) | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package gensig | ||
|
|
||
| import "flag" | ||
|
|
||
| var verbose bool | ||
|
|
||
| func addFlags(fs *flag.FlagSet) { | ||
| fs.BoolVar(&verbose, "v", false, "enable verbose output") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package gensig | ||
|
|
||
| import ( | ||
| "github.com/goplus/llcppg/cmd/internal/base" | ||
| ) | ||
|
|
||
| var Cmd = &base.Command{ | ||
| UsageLine: "llcppg gensig", | ||
| Short: "generate signature information of C/C++ symbols", | ||
| } | ||
|
|
||
| func init() { | ||
| Cmd.Run = runCmd | ||
| addFlags(&Cmd.Flag) | ||
| } | ||
|
|
||
| func runCmd(cmd *base.Command, args []string) { | ||
| base.RunCmdWithName(cmd, args, "llcppsigfetch", nil) | ||
| } | ||
tsingbx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package gensym | ||
|
|
||
| import "flag" | ||
|
|
||
| var verbose bool | ||
|
|
||
| func addFlags(fs *flag.FlagSet) { | ||
| fs.BoolVar(&verbose, "v", false, "enable verbose output") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package gensym | ||
|
|
||
| import ( | ||
| "github.com/goplus/llcppg/cmd/internal/base" | ||
| ) | ||
|
|
||
| var Cmd = &base.Command{ | ||
| UsageLine: "llcppg gensym", | ||
| Short: "generate symbol table for a C/C++ library", | ||
| } | ||
|
|
||
| func init() { | ||
| Cmd.Run = runCmd | ||
| addFlags(&Cmd.Flag) | ||
| } | ||
|
|
||
| func runCmd(cmd *base.Command, args []string) { | ||
| base.RunCmdWithName(cmd, args, "llcppsymg", nil) | ||
| } | ||
tsingbx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.