OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [go/] [build/] [path.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors.  All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package build
6
 
7
import (
8
        "errors"
9
        "fmt"
10
        "os"
11
        "path/filepath"
12
        "runtime"
13
)
14
 
15
// Path is a validated list of Trees derived from $GOROOT and $GOPATH at init.
16
var Path []*Tree
17
 
18
// Tree describes a Go source tree, either $GOROOT or one from $GOPATH.
19
type Tree struct {
20
        Path   string
21
        Goroot bool
22
}
23
 
24
func newTree(p string) (*Tree, error) {
25
        if !filepath.IsAbs(p) {
26
                return nil, errors.New("must be absolute")
27
        }
28
        ep, err := filepath.EvalSymlinks(p)
29
        if err != nil {
30
                return nil, err
31
        }
32
        return &Tree{Path: ep}, nil
33
}
34
 
35
// SrcDir returns the tree's package source directory.
36
func (t *Tree) SrcDir() string {
37
        if t.Goroot {
38
                return filepath.Join(t.Path, "src", "pkg")
39
        }
40
        return filepath.Join(t.Path, "src")
41
}
42
 
43
// PkgDir returns the tree's package object directory.
44
func (t *Tree) PkgDir() string {
45
        goos, goarch := runtime.GOOS, runtime.GOARCH
46
        if e := os.Getenv("GOOS"); e != "" {
47
                goos = e
48
        }
49
        if e := os.Getenv("GOARCH"); e != "" {
50
                goarch = e
51
        }
52
        return filepath.Join(t.Path, "pkg", goos+"_"+goarch)
53
}
54
 
55
// BinDir returns the tree's binary executable directory.
56
func (t *Tree) BinDir() string {
57
        if t.Goroot {
58
                if gobin := os.Getenv("GOBIN"); gobin != "" {
59
                        return filepath.Clean(gobin)
60
                }
61
        }
62
        return filepath.Join(t.Path, "bin")
63
}
64
 
65
// HasSrc returns whether the given package's
66
// source can be found inside this Tree.
67
func (t *Tree) HasSrc(pkg string) bool {
68
        fi, err := os.Stat(filepath.Join(t.SrcDir(), pkg))
69
        if err != nil {
70
                return false
71
        }
72
        return fi.IsDir()
73
}
74
 
75
// HasPkg returns whether the given package's
76
// object file can be found inside this Tree.
77
func (t *Tree) HasPkg(pkg string) bool {
78
        fi, err := os.Stat(filepath.Join(t.PkgDir(), pkg+".a"))
79
        if err != nil {
80
                return false
81
        }
82
        return !fi.IsDir()
83
}
84
 
85
var (
86
        ErrNotFound     = errors.New("package could not be found locally")
87
        ErrTreeNotFound = errors.New("no valid GOROOT or GOPATH could be found")
88
)
89
 
90
// FindTree takes an import or filesystem path and returns the
91
// tree where the package source should be and the package import path.
92
func FindTree(path string) (tree *Tree, pkg string, err error) {
93
        if isLocalPath(path) {
94
                if path, err = filepath.Abs(path); err != nil {
95
                        return
96
                }
97
                if path, err = filepath.EvalSymlinks(path); err != nil {
98
                        return
99
                }
100
                for _, t := range Path {
101
                        tpath := t.SrcDir() + string(filepath.Separator)
102
                        if !filepath.HasPrefix(path, tpath) {
103
                                continue
104
                        }
105
                        tree = t
106
                        pkg = filepath.ToSlash(path[len(tpath):])
107
                        return
108
                }
109
                err = fmt.Errorf("path %q not inside a GOPATH", path)
110
                return
111
        }
112
        tree = defaultTree
113
        pkg = filepath.ToSlash(path)
114
        for _, t := range Path {
115
                if t.HasSrc(pkg) {
116
                        tree = t
117
                        return
118
                }
119
        }
120
        if tree == nil {
121
                err = ErrTreeNotFound
122
        } else {
123
                err = ErrNotFound
124
        }
125
        return
126
}
127
 
128
// isLocalPath returns whether the given path is local (/foo ./foo ../foo . ..)
129
// Windows paths that starts with drive letter (c:\foo c:foo) are considered local.
130
func isLocalPath(s string) bool {
131
        const sep = string(filepath.Separator)
132
        return s == "." || s == ".." ||
133
                filepath.HasPrefix(s, sep) ||
134
                filepath.HasPrefix(s, "."+sep) || filepath.HasPrefix(s, ".."+sep) ||
135
                filepath.VolumeName(s) != ""
136
}
137
 
138
var (
139
        // argument lists used by the build's gc and ld methods
140
        gcImportArgs []string
141
        ldImportArgs []string
142
 
143
        // default tree for remote packages
144
        defaultTree *Tree
145
)
146
 
147
// set up Path: parse and validate GOROOT and GOPATH variables
148
func init() {
149
        root := runtime.GOROOT()
150
        t, err := newTree(root)
151
        if err == nil {
152
                t.Goroot = true
153
                Path = []*Tree{t}
154
        }
155
 
156
        for _, p := range filepath.SplitList(os.Getenv("GOPATH")) {
157
                if p == "" {
158
                        continue
159
                }
160
                t, err := newTree(p)
161
                if err != nil {
162
                        continue
163
                }
164
 
165
                Path = append(Path, t)
166
                gcImportArgs = append(gcImportArgs, "-I", t.PkgDir())
167
                ldImportArgs = append(ldImportArgs, "-L", t.PkgDir())
168
 
169
                // select first GOPATH entry as default
170
                if defaultTree == nil {
171
                        defaultTree = t
172
                }
173
        }
174
 
175
        // use GOROOT if no valid GOPATH specified
176
        if defaultTree == nil && len(Path) > 0 {
177
                defaultTree = Path[0]
178
        }
179
}

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.