Skip to content

Commit fb56e76

Browse files
committed
check -mod flag and fix type error
1 parent bda7808 commit fb56e76

6 files changed

Lines changed: 46 additions & 12 deletions

File tree

cmd/internal/base/base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func RunCmdWithName(cmd *Command, args []string, name string, out *io.PipeWriter
121121
nameCmd.Stdout = out
122122
}
123123
nameCmd.Stderr = os.Stderr
124-
nameCmd.Run()
124+
Check(nameCmd.Run())
125125
}
126126

127127
func Check(err error) {

cmd/internal/gencfg/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func addFlags(fs *flag.FlagSet) {
1111
fs.BoolVar(&cpp, "cpp", false, "if it is C++ lib")
1212
fs.BoolVar(&help, "help", false, "print help message")
1313
fs.BoolVar(&tab, "tab", true, "generate .cfg config file with tab indent")
14-
fs.StringVar(&excludes, "excludes", "", "exclude all header files in subdir of include expamle -excludes=\"internal impl\"")
14+
fs.StringVar(&excludes, "excludes", "", "exclude all header files in subdir of include example -excludes=\"internal impl\"")
1515
fs.StringVar(&extsString, "exts", ".h", "extra include file extensions for example -exts=\".h .hpp .hh\"")
1616
fs.StringVar(&dependencies, "deps", "", "deps for autofilling dependencies")
1717
}

cmd/internal/genpkg/genpkg.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/goplus/llcppg/cmd/internal/base"
1111
"github.com/goplus/llcppg/config"
12+
"golang.org/x/mod/module"
1213
)
1314

1415
var Cmd = &base.Command{
@@ -27,6 +28,9 @@ func runCmd(cmd *base.Command, args []string) {
2728
return
2829
}
2930

31+
err = module.CheckPath(modulePath)
32+
base.Check(err)
33+
3034
cfgFile := config.LLCPPG_CFG
3135

3236
config.HandleMarshalConfigFile(cfgFile, func(b []byte, err error) {
@@ -35,6 +39,7 @@ func runCmd(cmd *base.Command, args []string) {
3539

3640
r, w := io.Pipe()
3741

42+
errCh := make(chan error, 1)
3843
go func() {
3944
defer w.Close()
4045
llcppsigfetchCmdArgs := make([]string, 0)
@@ -48,14 +53,11 @@ func runCmd(cmd *base.Command, args []string) {
4853
llcppsigfetchCmd.Stdin = bytes.NewReader(b)
4954
llcppsigfetchCmd.Stdout = w
5055
llcppsigfetchCmd.Stderr = os.Stderr
51-
err = llcppsigfetchCmd.Run()
52-
base.Check(err)
56+
errCh <- llcppsigfetchCmd.Run()
5357
}()
5458

5559
gogensigCmdArgs := make([]string, 0)
56-
if len(modulePath) > 0 {
57-
gogensigCmdArgs = append(gogensigCmdArgs, fmt.Sprintf("-mod=%s", modulePath))
58-
}
60+
gogensigCmdArgs = append(gogensigCmdArgs, fmt.Sprintf("-mod=%s", modulePath))
5961
if verbose {
6062
gogensigCmdArgs = append(gogensigCmdArgs, "-v")
6163
}
@@ -65,5 +67,9 @@ func runCmd(cmd *base.Command, args []string) {
6567
gogensigCmd.Stderr = os.Stderr
6668
err = gogensigCmd.Run()
6769
base.Check(err)
70+
71+
if fetchErr := <-errCh; fetchErr != nil {
72+
base.Check(fetchErr)
73+
}
6874
})
6975
}

cmd/llcppgx/main_app.gox

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
short `
22
llcppgx aims to be a tool for automatically generating LLGo bindings for C/C++ libraries.
33

4-
In order to convert C/C++ library to go package, you should do the steps as folows:
4+
In order to convert C/C++ library to go package, you should do the steps as follows:
55

66
1、llcppgx init <libname>
7-
For exampe, run 'llcppgx init libcjson', it will init llcppg.cfg for libcjson library.
7+
For example, run 'llcppgx init libcjson', it will init llcppg.cfg for libcjson library.
88
In order to successfully convert the C/C++ library, you should carefully inspect and edit llcppg.cfg.
99

1010
2、llcppgx gensym
11-
It will generates and edits the symbol table for the libcjson library.
11+
It will generate and edits the symbol table for the libcjson library.
1212

1313
3、llcppgx genpkg -mod "github.com/goplus/cjson"
14-
It will enerates a go package for the libcjson library.
14+
It will generate a go package for the libcjson library.
1515
`

config/parse.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/json"
55
"fmt"
66
"os"
7+
"path/filepath"
8+
"strings"
79

810
"github.com/goplus/llgo/xtool/env"
911
)
@@ -37,7 +39,33 @@ func (c *Config) UnmarshalJSON(data []byte) error {
3739
return nil
3840
}
3941

42+
func isPathWithinCurrentDirectory(targetPath string) (bool, error) {
43+
// Get the absolute path of the current working directory
44+
cwd, err := os.Getwd()
45+
if err != nil {
46+
return false, fmt.Errorf("failed to get current working directory: %w", err)
47+
}
48+
49+
// Get the absolute path of the target path
50+
absTargetPath, err := filepath.Abs(targetPath)
51+
if err != nil {
52+
return false, fmt.Errorf("failed to get absolute path for target: %w", err)
53+
}
54+
55+
// Clean both paths for consistent comparison
56+
cleanedCwd := filepath.Clean(cwd)
57+
cleanedAbsTargetPath := filepath.Clean(absTargetPath)
58+
59+
// Check if the cleaned absolute target path starts with the cleaned absolute current directory path
60+
return strings.HasPrefix(cleanedAbsTargetPath, cleanedCwd), nil
61+
}
62+
4063
func ParseConfigFile(cfgFile string) (*Config, error) {
64+
ok, _ := isPathWithinCurrentDirectory(cfgFile)
65+
if !ok {
66+
return nil, fmt.Errorf("ParseConfigFile:%s is not within current directory", cfgFile)
67+
}
68+
4169
openCfgFile, err := os.Open(cfgFile)
4270
if err != nil {
4371
return nil, err

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ require (
1111
github.com/qiniu/x v1.15.1
1212
)
1313

14-
require golang.org/x/mod v0.27.0 // indirect
14+
require golang.org/x/mod v0.27.0

0 commit comments

Comments
 (0)