main: use GOPATH from the environment

Be more compatible with the Go toolchain by setting GOPATH in the same
way. This makes it possible to flash and run examples from the standard
GOPATH instead of only from the source tree.
Этот коммит содержится в:
Ayke van Laethem 2018-09-29 22:30:45 +02:00
родитель 5bf058a0a6
коммит b6db84e916
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
3 изменённых файлов: 34 добавлений и 1 удалений

Просмотреть файл

@ -34,6 +34,7 @@ type Config struct {
DumpSSA bool // dump Go SSA, for compiler debugging
Debug bool // add debug symbols for gdb
RootDir string // GOROOT for TinyGo
GOPATH string // GOPATH, like `go env GOPATH`
BuildTags []string // build tags for TinyGo (empty means {runtime.GOOS/runtime.GOARCH})
}
@ -151,6 +152,15 @@ func (c *Compiler) Module() llvm.Module {
func (c *Compiler) Compile(mainPath string) error {
tripleSplit := strings.Split(c.Triple, "-")
// Prefix the GOPATH with the system GOROOT, as GOROOT is already set to
// the TinyGo root.
gopath := c.GOPATH
if gopath == "" {
gopath = runtime.GOROOT()
} else {
gopath = runtime.GOROOT() + string(filepath.ListSeparator) + gopath
}
config := loader.Config{
TypeChecker: types.Config{
Sizes: &StdSizes{
@ -163,7 +173,7 @@ func (c *Compiler) Compile(mainPath string) error {
GOARCH: tripleSplit[0],
GOOS: tripleSplit[2],
GOROOT: c.RootDir,
GOPATH: runtime.GOROOT(),
GOPATH: gopath,
CgoEnabled: true,
UseAllFiles: false,
Compiler: "gc", // must be one of the recognized compilers

Просмотреть файл

@ -22,6 +22,7 @@ func Compile(pkgName, outpath string, spec *TargetSpec, printIR, dumpSSA, debug
Debug: debug,
DumpSSA: dumpSSA,
RootDir: sourceDir(),
GOPATH: getGopath(),
BuildTags: spec.BuildTags,
}
c, err := compiler.NewCompiler(pkgName, config)
@ -198,6 +199,7 @@ func Flash(pkgName, target, port string, printIR, dumpSSA, debug bool, printSize
func Run(pkgName string) error {
config := compiler.Config{
RootDir: sourceDir(),
GOPATH: getGopath(),
}
c, err := compiler.NewCompiler(pkgName, config)
if err != nil {

Просмотреть файл

@ -58,3 +58,24 @@ func sourceDir() string {
_, path, _, _ := runtime.Caller(0)
return filepath.Dir(path)
}
func getGopath() string {
gopath := os.Getenv("GOPATH")
if gopath != "" {
return gopath
}
// fallback
var home string
if runtime.GOOS == "windows" {
home = os.Getenv("USERPROFILE")
} else {
home = os.Getenv("HOME")
}
if home == "" {
// This is very unlikely, so panic here.
// Not the nicest solution, however.
panic("no $HOME or %USERPROFILE% found")
}
return filepath.Join(home, "go")
}