Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions cmd/internal/base/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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 (
"flag"
"fmt"
"io"
"strings"
)

// A Command is an implementation of a xgo command
// like xgo export or xgo install.
Comment thread
tsingbx marked this conversation as resolved.
Outdated
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 "gop" and the first flag or argument in the line are taken to be the command name.
Comment thread
tsingbx marked this conversation as resolved.
Outdated
UsageLine string

// Short is the short description shown in the 'gop 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 'gop help'.
// Note that subcommands are in general best avoided.
Commands []*Command
}
Comment thread
tsingbx marked this conversation as resolved.
Outdated

// Llcppg command
var Llcppg = &Command{
UsageLine: "llcppg",
Short: `llcppg aims to be a tool for automatically generating LLGo bindings for C/C++ libraries, enhancing the experience of integrating LLGo with C!`,
// Commands initialized in package main
}

// LongName returns the command's long name: all the words in the usage line between "gop" and a flag or argument,
Comment thread
tsingbx marked this conversation as resolved.
Outdated
func (c *Command) LongName() string {
name := c.UsageLine
if i := strings.Index(name, " ["); i >= 0 {
name = name[:i]
}
if name == "llcppg" {
return ""
}
return strings.TrimPrefix(name, "llcppg ")
}

// 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
}

// 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)
}
82 changes: 82 additions & 0 deletions cmd/internal/base/pass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package base

import (
"flag"
"fmt"
"strings"
)

type stringValue struct {
p *PassArgs
name string
}

func (p *stringValue) String() string {
return ""
}

func (p *stringValue) Set(v string) error {
p.p.Args = append(p.p.Args, fmt.Sprintf("-%v=%v", p.name, v))
Comment thread
tsingbx marked this conversation as resolved.
Outdated
return nil
}

type boolValue struct {
p *PassArgs
name string
}

func (p *boolValue) String() string {
return ""
}

func (p *boolValue) Set(v string) error {
p.p.Args = append(p.p.Args, fmt.Sprintf("-%v=%v", p.name, v))
return nil
}

func (p *boolValue) IsBoolFlag() bool {
return true
}

type PassArgs struct {
Args []string
Flag *flag.FlagSet
}

func (p *PassArgs) Tags() string {
for _, v := range p.Args {
if strings.HasPrefix(v, "-tags=") {
return v[6:]
}
}
return ""
}

func (p *PassArgs) Var(names ...string) {
for _, name := range names {
p.Flag.Var(&stringValue{p: p, name: name}, name, "")
}
}

func (p *PassArgs) Bool(names ...string) {
for _, name := range names {
p.Flag.Var(&boolValue{p: p, name: name}, name, "")
}
}

func NewPassArgs(flag *flag.FlagSet) *PassArgs {
p := &PassArgs{Flag: flag}
Comment thread
tsingbx marked this conversation as resolved.
Outdated
return p
}

func PassBuildFlags(cmd *Command) *PassArgs {
p := NewPassArgs(&cmd.Flag)
p.Bool("n", "x")
p.Bool("a")
p.Bool("linkshared", "race", "msan", "asan",
"trimpath", "work")
p.Var("p", "asmflags", "compiler",
"gcflags", "gccgoflags", "installsuffix",
"ldflags", "pkgdir", "toolexec", "buildvcs")
return p
}
20 changes: 20 additions & 0 deletions cmd/internal/gencfg/gencfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gencfg

import (
"fmt"

"github.com/goplus/llcppg/cmd/internal/base"
)

var Cmd = &base.Command{
UsageLine: "llcppg gencfg",
Short: "generate llcpp.cfg",
Comment thread
tsingbx marked this conversation as resolved.
Outdated
}

func init() {
Cmd.Run = runCmd
}

func runCmd(cmd *base.Command, args []string) {
fmt.Printf("todo generate llcpp.cfg")
Comment thread
tsingbx marked this conversation as resolved.
Outdated
}
Comment thread
tsingbx marked this conversation as resolved.
20 changes: 20 additions & 0 deletions cmd/internal/genpkg/genpkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package genpkg

import (
"fmt"

"github.com/goplus/llcppg/cmd/internal/base"
)

var Cmd = &base.Command{
UsageLine: "llcppg genpkg",
Short: "generate a go package by signature information of symbols",
}

func init() {
Cmd.Run = runCmd
}

func runCmd(cmd *base.Command, args []string) {
fmt.Printf("todo genpkg")
}
Comment thread
tsingbx marked this conversation as resolved.
20 changes: 20 additions & 0 deletions cmd/internal/gensig/gensig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gensig

import (
"fmt"

"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
}

func runCmd(cmd *base.Command, args []string) {
fmt.Printf("todo gensig")
}
Comment thread
tsingbx marked this conversation as resolved.
20 changes: 20 additions & 0 deletions cmd/internal/gensym/gensym.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gensym

import (
"fmt"

"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
}

func runCmd(cmd *base.Command, args []string) {
fmt.Printf("todo gensym")
}
Comment thread
tsingbx marked this conversation as resolved.
20 changes: 20 additions & 0 deletions cmd/internal/runtest/runtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package runtest

import (
"fmt"

"github.com/goplus/llcppg/cmd/internal/base"
)

var Cmd = &base.Command{
UsageLine: "llcppg runtest",
Short: "run test for llcppg",
}

func init() {
Cmd.Run = runCmd
}

func runCmd(cmd *base.Command, args []string) {
fmt.Printf("todo runtest")
}
Comment thread
tsingbx marked this conversation as resolved.
Outdated
20 changes: 20 additions & 0 deletions cmd/internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package version

import (
"fmt"

"github.com/goplus/llcppg/cmd/internal/base"
)

var Cmd = &base.Command{
UsageLine: "llcppg version",
Short: "Print llcppg version",
}

func init() {
Cmd.Run = runCmd
}

func runCmd(cmd *base.Command, args []string) {
fmt.Printf("todo print version")
}
Comment thread
tsingbx marked this conversation as resolved.
11 changes: 11 additions & 0 deletions cmd/llcppg/gencfg_cmd.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import (
self "github.com/goplus/llcppg/cmd/internal/gencfg"
)

use "gencfg"

short "generate llcpp.cfg"

run args => {
self.Cmd.Run self.Cmd, args
}
11 changes: 11 additions & 0 deletions cmd/llcppg/genpkg_cmd.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import (
self "github.com/goplus/llcppg/cmd/internal/genpkg"
)

use "genpkg"

short "generate a go package by signature information of symbols"

run args => {
self.Cmd.Run self.Cmd, args
}
11 changes: 11 additions & 0 deletions cmd/llcppg/gensig_cmd.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import (
self "github.com/goplus/llcppg/cmd/internal/gensig"
)

use "gensig"

short "generate signature information of C/C++ symbols"

run args => {
self.Cmd.Run self.Cmd, args
}
11 changes: 11 additions & 0 deletions cmd/llcppg/gensym_cmd.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import (
self "github.com/goplus/llcppg/cmd/internal/gensym"
)

use "gensym"

short "generate symbol table for a C/C++ library"

run args => {
self.Cmd.Run self.Cmd, args
}
Binary file added cmd/llcppg/llcppg
Binary file not shown.
1 change: 1 addition & 0 deletions cmd/llcppg/main_app.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
short `llcppg aims to be a tool for automatically generating LLGo bindings for C/C++ libraries, enhancing the experience of integrating LLGo with C!`
11 changes: 11 additions & 0 deletions cmd/llcppg/runtest_cmd.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import (
self "github.com/goplus/llcppg/cmd/internal/runtest"
)

use "runtest"

short "run test for llcppg"

run args => {
self.Cmd.Run self.Cmd, args
}
11 changes: 11 additions & 0 deletions cmd/llcppg/version_cmd.gox
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import (
self "github.com/goplus/llcppg/cmd/internal/version"
)

use "version"

short "Print llcppg version"

run args => {
self.Cmd.Run self.Cmd, args
}
Loading
Loading