Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
mod: clang/pathutil
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jul 24, 2022
1 parent efb5909 commit 51d027f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
1 change: 1 addition & 0 deletions cl/blockctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"go/token"
"go/types"
"log"
"path/filepath"
"syscall"

goast "go/ast"
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 4 additions & 11 deletions cl/multifiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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):]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions clang/pathutil/pathutil.go
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 17 additions & 3 deletions clang/preprocessor/preprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"os/exec"
"path/filepath"

"github.com/goplus/c2go/clang/pathutil"
)

const (
Expand All @@ -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"
Expand All @@ -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
Expand Down
22 changes: 8 additions & 14 deletions proj.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:"-"`

Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit 51d027f

Please sign in to comment.