From 51d027f9f11dbf2e88d59537fc1a98b41583dd2d Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 24 Jul 2022 09:58:13 +0800 Subject: [PATCH] mod: clang/pathutil --- cl/blockctx.go | 1 + cl/compile.go | 5 ++++- cl/multifiles.go | 15 ++++----------- clang/pathutil/pathutil.go | 12 ++++++++++++ clang/preprocessor/preprocessor.go | 20 +++++++++++++++++--- proj.go | 22 ++++++++-------------- 6 files changed, 46 insertions(+), 29 deletions(-) create mode 100644 clang/pathutil/pathutil.go diff --git a/cl/blockctx.go b/cl/blockctx.go index 209a9ff..a6889d9 100644 --- a/cl/blockctx.go +++ b/cl/blockctx.go @@ -200,6 +200,7 @@ type blockCtx struct { gblvars map[string]*gox.VarDefs public map[string]string ignored []string + srcdir string srcfile string src []byte file *token.File diff --git a/cl/compile.go b/cl/compile.go index cb67476..99be897 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -4,6 +4,7 @@ import ( "go/token" "go/types" "log" + "path/filepath" "syscall" goast "go/ast" @@ -258,13 +259,15 @@ func loadFile(p *gox.Package, conf *Config, file *ast.Node) (pi *PkgInfo, err er if file.Kind != ast.TranslationUnitDecl { return nil, syscall.EINVAL } + srcfile, _ := filepath.Abs(conf.SrcFile) ctx := &blockCtx{ pkg: p, cb: p.CB(), fset: p.Fset, unnameds: make(map[ast.ID]unnamedType), gblvars: make(map[string]*gox.VarDefs), ignored: conf.Ignored, public: conf.Public, - srcfile: conf.SrcFile, + srcdir: filepath.Dir(srcfile), + srcfile: srcfile, src: conf.Src, bfm: conf.BuiltinFuncMode, testMain: conf.TestMain, diff --git a/cl/multifiles.go b/cl/multifiles.go index 93bcad0..14b2d77 100644 --- a/cl/multifiles.go +++ b/cl/multifiles.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" + "github.com/goplus/c2go/clang/pathutil" ctypes "github.com/goplus/c2go/clang/types" "github.com/goplus/c2go/clang/ast" @@ -26,7 +27,6 @@ type multiFileCtl struct { autopub map[string]none base *int // for anonymous struct/union or static baseOF string // basename of file - baseDir string hasMulti bool inHeader bool // in header file (only valid on hasMulti) inDepPkg bool // in dependent package (only valid on hasMulti) @@ -127,7 +127,7 @@ func (p *blockCtx) logFile(node *ast.Node) { fname = headerGoFile p.inHeader = true p.inDepPkg = p.skipLibcH - f, _ = filepath.Abs(f) // f is related to cwd, not p.baseDir + f = pathutil.Canonical(p.srcdir, f) // f is related to directory of source file for dir, kind := range p.incs { if strings.HasPrefix(f, dir) { suffix := f[len(dir):] @@ -160,13 +160,6 @@ func (p *blockCtx) inSrcFile() bool { return p.hasMulti && !p.inHeader } -func canonical(baseDir string, uri string) string { - if filepath.IsAbs(uri) { - return uri - } - return filepath.Join(baseDir, uri) -} - // ----------------------------------------------------------------------------- type pubName struct { @@ -220,7 +213,7 @@ func (p *depPkgs) init(conf *Config) { } p.incs = make(map[string]int) for _, dir := range conf.Include { - dir = canonical(base, dir) + dir = pathutil.Canonical(base, dir) p.incs[dir] = incInSelf } procDepPkg := conf.ProcDepPkg @@ -239,7 +232,7 @@ func (p *depPkgs) init(conf *Config) { log.Panicln("findIncludeDirs:", err) } for _, dir := range depPkgIncs { - dir = canonical(depPkgDir, dir) + dir = pathutil.Canonical(depPkgDir, dir) p.incs[dir] = incInDeps } pubfile := filepath.Join(depPkgDir, "c2go.a.pub") diff --git a/clang/pathutil/pathutil.go b/clang/pathutil/pathutil.go new file mode 100644 index 0000000..ef23ce3 --- /dev/null +++ b/clang/pathutil/pathutil.go @@ -0,0 +1,12 @@ +package pathutil + +import ( + "path/filepath" +) + +func Canonical(baseDir string, uri string) string { + if filepath.IsAbs(uri) { + return uri + } + return filepath.Join(baseDir, uri) +} diff --git a/clang/preprocessor/preprocessor.go b/clang/preprocessor/preprocessor.go index db59518..bf78f4e 100644 --- a/clang/preprocessor/preprocessor.go +++ b/clang/preprocessor/preprocessor.go @@ -5,6 +5,8 @@ import ( "os" "os/exec" "path/filepath" + + "github.com/goplus/c2go/clang/pathutil" ) const ( @@ -25,16 +27,28 @@ func SetDebug(flags int) { type Config struct { Compiler string // default: clang PPFlag string // default: -E - BaseDir string // base of include searching directory + BaseDir string // base of include searching directory, should be absolute path IncludeDirs []string Defines []string Flags []string } func Do(infile, outfile string, conf *Config) (err error) { + if infile, err = filepath.Abs(infile); err != nil { + return + } + if outfile, err = filepath.Abs(outfile); err != nil { + return + } if conf == nil { conf = new(Config) } + base := conf.BaseDir + if base == "" { + if base, err = os.Getwd(); err != nil { + return + } + } compiler := conf.Compiler if compiler == "" { compiler = "clang" @@ -51,15 +65,15 @@ func Do(infile, outfile string, conf *Config) (err error) { for _, def := range conf.Defines { args = append(args, "-D"+def) } - base := conf.BaseDir for _, inc := range conf.IncludeDirs { - args = append(args, "-I"+filepath.Join(base, inc)) + args = append(args, "-I"+pathutil.Canonical(base, inc)) } args = append(args, infile) if debugExecCmd { log.Println("==> runCmd:", compiler, args) } cmd := exec.Command(compiler, args...) + cmd.Dir = filepath.Dir(infile) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr diff --git a/proj.go b/proj.go index aeea769..a00c189 100644 --- a/proj.go +++ b/proj.go @@ -26,6 +26,7 @@ import ( "github.com/goplus/c2go/cl" "github.com/goplus/c2go/clang/parser" + "github.com/goplus/c2go/clang/pathutil" "github.com/goplus/c2go/clang/preprocessor" "github.com/goplus/gox" "github.com/goplus/gox/cpackages" @@ -75,7 +76,7 @@ type c2goConf struct { cl.Reused `json:"-"` - dir string `json:"-"` + dir string `json:"-"` // should be absolute path public map[string]string `json:"-"` needPkgInfo bool `json:"-"` @@ -96,7 +97,7 @@ func execProj(projfile string, flags int, in *Config) { base, _ := filepath.Split(projfile) conf.needPkgInfo = (flags & FlagDepsAutoGen) != 0 - conf.dir = base + conf.dir, _ = filepath.Abs(base) noSource := len(conf.Source.Dirs) == 0 && len(conf.Source.Files) == 0 if noSource { if len(conf.Target.Cmds) == 0 { @@ -114,7 +115,7 @@ func execProj(projfile string, flags int, in *Config) { check(err) if in != nil && in.SelectFile != "" { - execProjFile(canonical(base, in.SelectFile), &conf, appFlags) + execProjFile(pathutil.Canonical(base, in.SelectFile), &conf, appFlags) return } execProjSource(base, appFlags, &conf) @@ -157,7 +158,7 @@ func execProj(projfile string, flags int, in *Config) { execProjSource(base, appFlags, &conf) if (appFlags & FlagRunTest) != 0 { cmd2 := exec.Command(clangOut) - cmd2.Dir = canonical(base, cmd.Dir) + cmd2.Dir = pathutil.Canonical(base, cmd.Dir) cmd2.Stdout = os.Stdout cmd2.Stderr = os.Stderr fmt.Printf("==> Running %s ...\n", cmd2.Dir) @@ -218,17 +219,17 @@ func execProjSource(base string, flags int, conf *c2goConf) { if recursively { dir = dir[:len(dir)-4] } - execProjDir(canonical(base, dir), conf, flags, recursively) + execProjDir(pathutil.Canonical(base, dir), conf, flags, recursively) } for _, file := range conf.Source.Files { - execProjFile(canonical(base, file), conf, flags) + execProjFile(pathutil.Canonical(base, file), conf, flags) } execProjDone(base, flags, conf) } func execProjDone(base string, flags int, conf *c2goConf) { if pkg := conf.Reused.Pkg(); pkg.IsValid() { - dir := canonical(base, conf.Target.Dir) + dir := pathutil.Canonical(base, conf.Target.Dir) os.MkdirAll(dir, 0777) pkg.ForEachFile(func(fname string, file *gox.File) { gofile := fname @@ -378,10 +379,3 @@ func execProjFile(infile string, conf *c2goConf, flags int) { }) check(err) } - -func canonical(baseDir string, uri string) string { - if filepath.IsAbs(uri) { - return uri - } - return filepath.Join(baseDir, uri) -}