diff --git a/compiler/compiler.go b/compiler/compiler.go index 3a588b98..33ad092a 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -26,6 +26,9 @@ func init() { llvm.InitializeAllAsmPrinters() } +// The TinyGo import path. +const tinygoPath = "github.com/tinygo-org/tinygo" + // Configure the compiler. type Config struct { 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 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 { case "machine", "os", "reflect", "runtime", "runtime/volatile", "sync": - return true + return path default: if strings.HasPrefix(path, "device/") || strings.HasPrefix(path, "examples/") { - return true + return path } else if path == "syscall" { for _, tag := range c.BuildTags { if tag == "avr" || tag == "cortexm" || tag == "darwin" { - return true + return path } } } } - return false + return "" }, TypeChecker: types.Config{ Sizes: &StdSizes{ diff --git a/loader/loader.go b/loader/loader.go index cf7cf2c7..7b20a925 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -16,16 +16,16 @@ import ( // Program holds all packages and some metadata about the program as a whole. type Program struct { - Build *build.Context - OverlayBuild *build.Context - ShouldOverlay func(path string) bool - Packages map[string]*Package - sorted []*Package - fset *token.FileSet - TypeChecker types.Config - Dir string // current working directory (for error reporting) - TINYGOROOT string // root of the TinyGo installation or root of the source code - CFlags []string + Build *build.Context + OverlayBuild *build.Context + OverlayPath func(path string) string + Packages map[string]*Package + sorted []*Package + fset *token.FileSet + TypeChecker types.Config + Dir string // current working directory (for error reporting) + TINYGOROOT string // root of the TinyGo installation or root of the source code + CFlags []string } // 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. ctx := p.Build - if p.ShouldOverlay(path) { + if newPath := p.OverlayPath(path); newPath != "" { ctx = p.OverlayBuild + path = newPath } buildPkg, err := ctx.Import(path, srcDir, build.ImportComment) if err != nil {