compiler: rename import path if it lies in TINYGOPATH
This allows importing (for example) both "github.com/tinygo-org/tinygo/src/machine" and "machine" without issues. The former is renamed to just "machine".
Этот коммит содержится в:
родитель
ec87811420
коммит
66aca428ba
2 изменённых файлов: 27 добавлений и 16 удалений
|
@ -26,6 +26,9 @@ func init() {
|
||||||
llvm.InitializeAllAsmPrinters()
|
llvm.InitializeAllAsmPrinters()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The TinyGo import path.
|
||||||
|
const tinygoPath = "github.com/tinygo-org/tinygo"
|
||||||
|
|
||||||
// Configure the compiler.
|
// Configure the compiler.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Triple string // LLVM target triple, e.g. x86_64-unknown-linux-gnu (empty string means default)
|
Triple string // LLVM target triple, e.g. x86_64-unknown-linux-gnu (empty string means default)
|
||||||
|
@ -198,22 +201,29 @@ func (c *Compiler) Compile(mainPath string) []error {
|
||||||
Compiler: "gc", // must be one of the recognized compilers
|
Compiler: "gc", // must be one of the recognized compilers
|
||||||
BuildTags: append([]string{"tinygo", "gc." + c.selectGC()}, c.BuildTags...),
|
BuildTags: append([]string{"tinygo", "gc." + c.selectGC()}, c.BuildTags...),
|
||||||
},
|
},
|
||||||
ShouldOverlay: func(path string) bool {
|
OverlayPath: func(path string) string {
|
||||||
|
// Return the (overlay) import path when it should be overlaid, and
|
||||||
|
// "" if it should not.
|
||||||
|
if strings.HasPrefix(path, tinygoPath+"/src/") {
|
||||||
|
// Avoid issues with packages that are imported twice, one from
|
||||||
|
// GOPATH and one from TINYGOPATH.
|
||||||
|
path = path[len(tinygoPath+"/src/"):]
|
||||||
|
}
|
||||||
switch path {
|
switch path {
|
||||||
case "machine", "os", "reflect", "runtime", "runtime/volatile", "sync":
|
case "machine", "os", "reflect", "runtime", "runtime/volatile", "sync":
|
||||||
return true
|
return path
|
||||||
default:
|
default:
|
||||||
if strings.HasPrefix(path, "device/") || strings.HasPrefix(path, "examples/") {
|
if strings.HasPrefix(path, "device/") || strings.HasPrefix(path, "examples/") {
|
||||||
return true
|
return path
|
||||||
} else if path == "syscall" {
|
} else if path == "syscall" {
|
||||||
for _, tag := range c.BuildTags {
|
for _, tag := range c.BuildTags {
|
||||||
if tag == "avr" || tag == "cortexm" || tag == "darwin" {
|
if tag == "avr" || tag == "cortexm" || tag == "darwin" {
|
||||||
return true
|
return path
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return ""
|
||||||
},
|
},
|
||||||
TypeChecker: types.Config{
|
TypeChecker: types.Config{
|
||||||
Sizes: &StdSizes{
|
Sizes: &StdSizes{
|
||||||
|
|
|
@ -16,16 +16,16 @@ import (
|
||||||
|
|
||||||
// Program holds all packages and some metadata about the program as a whole.
|
// Program holds all packages and some metadata about the program as a whole.
|
||||||
type Program struct {
|
type Program struct {
|
||||||
Build *build.Context
|
Build *build.Context
|
||||||
OverlayBuild *build.Context
|
OverlayBuild *build.Context
|
||||||
ShouldOverlay func(path string) bool
|
OverlayPath func(path string) string
|
||||||
Packages map[string]*Package
|
Packages map[string]*Package
|
||||||
sorted []*Package
|
sorted []*Package
|
||||||
fset *token.FileSet
|
fset *token.FileSet
|
||||||
TypeChecker types.Config
|
TypeChecker types.Config
|
||||||
Dir string // current working directory (for error reporting)
|
Dir string // current working directory (for error reporting)
|
||||||
TINYGOROOT string // root of the TinyGo installation or root of the source code
|
TINYGOROOT string // root of the TinyGo installation or root of the source code
|
||||||
CFlags []string
|
CFlags []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Package holds a loaded package, its imports, and its parsed files.
|
// Package holds a loaded package, its imports, and its parsed files.
|
||||||
|
@ -48,8 +48,9 @@ func (p *Program) Import(path, srcDir string) (*Package, error) {
|
||||||
|
|
||||||
// Load this package.
|
// Load this package.
|
||||||
ctx := p.Build
|
ctx := p.Build
|
||||||
if p.ShouldOverlay(path) {
|
if newPath := p.OverlayPath(path); newPath != "" {
|
||||||
ctx = p.OverlayBuild
|
ctx = p.OverlayBuild
|
||||||
|
path = newPath
|
||||||
}
|
}
|
||||||
buildPkg, err := ctx.Import(path, srcDir, build.ImportComment)
|
buildPkg, err := ctx.Import(path, srcDir, build.ImportComment)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче