~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/cmd/goinstall/path.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// 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 main
6
 
 
7
 
import (
8
 
        "fmt"
9
 
        "log"
10
 
        "os"
11
 
        "path/filepath"
12
 
        "runtime"
13
 
        "strings"
14
 
)
15
 
 
16
 
var (
17
 
        gopath      []*pkgroot
18
 
        imports     []string
19
 
        defaultRoot *pkgroot // default root for remote packages
20
 
)
21
 
 
22
 
// set up gopath: parse and validate GOROOT and GOPATH variables
23
 
func init() {
24
 
        root := runtime.GOROOT()
25
 
        p, err := newPkgroot(root)
26
 
        if err != nil {
27
 
                log.Fatalf("Invalid GOROOT %q: %v", root, err)
28
 
        }
29
 
        p.goroot = true
30
 
        gopath = []*pkgroot{p}
31
 
 
32
 
        for _, p := range filepath.SplitList(os.Getenv("GOPATH")) {
33
 
                if p == "" {
34
 
                        continue
35
 
                }
36
 
                r, err := newPkgroot(p)
37
 
                if err != nil {
38
 
                        log.Printf("Invalid GOPATH %q: %v", p, err)
39
 
                        continue
40
 
                }
41
 
                gopath = append(gopath, r)
42
 
                imports = append(imports, r.pkgDir())
43
 
 
44
 
                // select first GOPATH entry as default
45
 
                if defaultRoot == nil {
46
 
                        defaultRoot = r
47
 
                }
48
 
        }
49
 
 
50
 
        // use GOROOT if no valid GOPATH specified
51
 
        if defaultRoot == nil {
52
 
                defaultRoot = gopath[0]
53
 
        }
54
 
}
55
 
 
56
 
type pkgroot struct {
57
 
        path   string
58
 
        goroot bool // TODO(adg): remove this once Go tree re-organized
59
 
}
60
 
 
61
 
func newPkgroot(p string) (*pkgroot, os.Error) {
62
 
        if !filepath.IsAbs(p) {
63
 
                return nil, os.NewError("must be absolute")
64
 
        }
65
 
        ep, err := filepath.EvalSymlinks(p)
66
 
        if err != nil {
67
 
                return nil, err
68
 
        }
69
 
        return &pkgroot{path: ep}, nil
70
 
}
71
 
 
72
 
func (r *pkgroot) srcDir() string {
73
 
        if r.goroot {
74
 
                return filepath.Join(r.path, "src", "pkg")
75
 
        }
76
 
        return filepath.Join(r.path, "src")
77
 
}
78
 
 
79
 
func (r *pkgroot) pkgDir() string {
80
 
        goos, goarch := runtime.GOOS, runtime.GOARCH
81
 
        if e := os.Getenv("GOOS"); e != "" {
82
 
                goos = e
83
 
        }
84
 
        if e := os.Getenv("GOARCH"); e != "" {
85
 
                goarch = e
86
 
        }
87
 
        return filepath.Join(r.path, "pkg", goos+"_"+goarch)
88
 
}
89
 
 
90
 
func (r *pkgroot) binDir() string {
91
 
        return filepath.Join(r.path, "bin")
92
 
}
93
 
 
94
 
func (r *pkgroot) hasSrcDir(name string) bool {
95
 
        fi, err := os.Stat(filepath.Join(r.srcDir(), name))
96
 
        if err != nil {
97
 
                return false
98
 
        }
99
 
        return fi.IsDirectory()
100
 
}
101
 
 
102
 
func (r *pkgroot) hasPkg(name string) bool {
103
 
        fi, err := os.Stat(filepath.Join(r.pkgDir(), name+".a"))
104
 
        if err != nil {
105
 
                return false
106
 
        }
107
 
        return fi.IsRegular()
108
 
        // TODO(adg): check object version is consistent
109
 
}
110
 
 
111
 
 
112
 
var ErrPackageNotFound = os.NewError("package could not be found locally")
113
 
 
114
 
// findPackageRoot takes an import or filesystem path and returns the
115
 
// root where the package source should be and the package import path.
116
 
func findPackageRoot(path string) (root *pkgroot, pkg string, err os.Error) {
117
 
        if isLocalPath(path) {
118
 
                if path, err = filepath.Abs(path); err != nil {
119
 
                        return
120
 
                }
121
 
                for _, r := range gopath {
122
 
                        rpath := r.srcDir() + string(filepath.Separator)
123
 
                        if !strings.HasPrefix(path, rpath) {
124
 
                                continue
125
 
                        }
126
 
                        root = r
127
 
                        pkg = path[len(rpath):]
128
 
                        return
129
 
                }
130
 
                err = fmt.Errorf("path %q not inside a GOPATH", path)
131
 
                return
132
 
        }
133
 
        root = defaultRoot
134
 
        pkg = path
135
 
        for _, r := range gopath {
136
 
                if r.hasSrcDir(path) {
137
 
                        root = r
138
 
                        return
139
 
                }
140
 
        }
141
 
        err = ErrPackageNotFound
142
 
        return
143
 
}
144
 
 
145
 
// Is this a local path?  /foo ./foo ../foo . ..
146
 
func isLocalPath(s string) bool {
147
 
        const sep = string(filepath.Separator)
148
 
        return strings.HasPrefix(s, sep) || strings.HasPrefix(s, "."+sep) || strings.HasPrefix(s, ".."+sep) || s == "." || s == ".."
149
 
}