本文整理汇总了Golang中go/build.Context.GOARCH方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.GOARCH方法的具体用法?Golang Context.GOARCH怎么用?Golang Context.GOARCH使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类go/build.Context
的用法示例。
在下文中一共展示了Context.GOARCH方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: build
func (b *builder) build(srcs []*source) (*Package, error) {
b.pdoc.Updated = time.Now().UTC()
references := make(map[string]bool)
b.srcs = make(map[string]*source)
for _, src := range srcs {
if strings.HasSuffix(src.name, ".go") {
b.srcs[src.name] = src
} else {
addReferences(references, src.data)
fn := strings.ToLower(src.name)
if fn == "readme" || strings.HasPrefix(fn, "readme.") {
if b.pdoc.ReadmeFiles == nil {
b.pdoc.ReadmeFiles = make(map[string][]byte)
}
b.pdoc.ReadmeFiles[src.name] = src.data
}
}
}
for r := range references {
b.pdoc.References = append(b.pdoc.References, r)
}
if len(b.srcs) == 0 {
return b.pdoc, nil
}
b.fset = token.NewFileSet()
// Find the package and associated files.
ctxt := build.Context{
GOOS: "linux",
GOARCH: "amd64",
CgoEnabled: true,
ReleaseTags: build.Default.ReleaseTags,
JoinPath: path.Join,
IsAbsPath: path.IsAbs,
SplitPathList: func(list string) []string { return strings.Split(list, ":") },
IsDir: func(path string) bool { panic("unexpected") },
HasSubdir: func(root, dir string) (rel string, ok bool) { panic("unexpected") },
ReadDir: func(dir string) (fi []os.FileInfo, err error) { return b.readDir(dir) },
OpenFile: func(path string) (r io.ReadCloser, err error) { return b.openFile(path) },
Compiler: "gc",
}
var err error
var bpkg *build.Package
for _, env := range goEnvs {
ctxt.GOOS = env.GOOS
ctxt.GOARCH = env.GOARCH
bpkg, err = ctxt.ImportDir("/", 0)
if _, ok := err.(*build.NoGoError); !ok {
break
}
}
if err != nil {
if _, ok := err.(*build.NoGoError); !ok {
b.pdoc.Errors = append(b.pdoc.Errors, err.Error())
}
return b.pdoc, nil
}
// Parse the Go files
files := make(map[string]*ast.File)
names := append(bpkg.GoFiles, bpkg.CgoFiles...)
sort.Strings(names)
b.pdoc.Files = make([]*File, len(names))
for i, name := range names {
file, err := parser.ParseFile(b.fset, name, b.srcs[name].data, parser.ParseComments)
if err != nil {
b.pdoc.Errors = append(b.pdoc.Errors, err.Error())
continue
}
src := b.srcs[name]
src.index = i
b.pdoc.Files[i] = &File{Name: name, URL: src.browseURL}
b.pdoc.SourceSize += len(src.data)
files[name] = file
}
apkg, _ := ast.NewPackage(b.fset, files, simpleImporter, nil)
// Find examples in the test files.
names = append(bpkg.TestGoFiles, bpkg.XTestGoFiles...)
sort.Strings(names)
b.pdoc.TestFiles = make([]*File, len(names))
for i, name := range names {
file, err := parser.ParseFile(b.fset, name, b.srcs[name].data, parser.ParseComments)
if err != nil {
b.pdoc.Errors = append(b.pdoc.Errors, err.Error())
continue
}
//.........这里部分代码省略.........
示例2: Build
// Build generates documentation from given source files through 'WalkType'.
func (w *Walker) Build(wr *WalkRes) (*Package, error) {
ctxt := build.Context{
CgoEnabled: true,
ReleaseTags: build.Default.ReleaseTags,
BuildTags: build.Default.BuildTags,
Compiler: "gc",
}
if w.Pdoc.PkgDecl == nil {
w.Pdoc.PkgDecl = &PkgDecl{}
}
// Check 'WalkType'.
switch wr.WalkType {
case WT_Local:
// Check root path.
if len(wr.RootPath) == 0 {
return nil, errors.New("WT_Local: empty root path")
} else if !com.IsDir(wr.RootPath) {
return nil, errors.New("WT_Local: cannot find specific directory or it's a file")
}
w.setLocalContext(&ctxt)
return nil, errors.New("Hasn't supported yet!")
case WT_Memory:
// Convert source files.
w.SrcFiles = make(map[string]*Source)
w.Pdoc.Readme = make(map[string][]byte)
for _, src := range wr.Srcs {
srcName := strings.ToLower(src.Name()) // For readme comparation.
switch {
case strings.HasSuffix(src.Name(), ".go"):
w.SrcFiles[src.Name()] = src
case len(w.Pdoc.Tag) > 0 || (wr.WalkMode&WM_NoReadme != 0):
// This means we are not on the latest version of the code,
// so we do not collect the README files.
continue
case strings.HasPrefix(srcName, "readme_zh") || strings.HasPrefix(srcName, "readme_cn"):
w.Pdoc.Readme["zh"] = src.Data()
case strings.HasPrefix(srcName, "readme"):
w.Pdoc.Readme["en"] = src.Data()
}
}
// Check source files.
if w.SrcFiles == nil {
return nil, errors.New("WT_Memory: no Go source file")
}
w.setMemoryContext(&ctxt)
default:
return nil, errors.New("Hasn't supported yet!")
}
var err error
var bpkg *build.Package
for _, env := range goEnvs {
ctxt.GOOS = env.GOOS
ctxt.GOARCH = env.GOARCH
bpkg, err = ctxt.ImportDir(w.Pdoc.ImportPath, 0)
// Continue if there are no Go source files; we still want the directory info.
_, nogo := err.(*build.NoGoError)
if err != nil {
if nogo {
err = nil
} else {
return nil, errors.New("hv.Walker.Build -> ImportDir: " + err.Error())
}
}
}
w.Pdoc.IsCmd = bpkg.IsCommand()
w.Pdoc.Synopsis = synopsis(bpkg.Doc)
w.Pdoc.Imports = bpkg.Imports
w.Pdoc.IsCgo = w.isCgo()
w.Pdoc.TestImports = bpkg.TestImports
// Check depth.
if wr.WalkDepth <= WD_Imports {
return w.Pdoc, nil
}
w.Fset = token.NewFileSet()
// Parse the Go files
files := make(map[string]*ast.File)
for _, name := range append(bpkg.GoFiles, bpkg.CgoFiles...) {
file, err := parser.ParseFile(w.Fset, name, w.SrcFiles[name].Data(), parser.ParseComments)
if err != nil {
return nil, errors.New("hv.Walker.Build -> parse Go files: " + err.Error())
continue
}
w.Pdoc.Files = append(w.Pdoc.Files, w.SrcFiles[name])
w.Pdoc.SourceSize += int64(len(w.SrcFiles[name].Data()))
files[name] = file
}
//.........这里部分代码省略.........
示例3: newPackage
func newPackage(dir *gosrc.Directory) (*Package, error) {
pkg := &Package{
Updated: time.Now().UTC(),
LineFmt: dir.LineFmt,
ImportPath: dir.ImportPath,
ProjectRoot: dir.ProjectRoot,
ProjectName: dir.ProjectName,
ProjectURL: dir.ProjectURL,
BrowseURL: dir.BrowseURL,
Etag: PackageVersion + "-" + dir.Etag,
VCS: dir.VCS,
DeadEndFork: dir.DeadEndFork,
Subdirectories: dir.Subdirectories,
}
var b builder
b.srcs = make(map[string]*source)
references := make(map[string]bool)
for _, file := range dir.Files {
if strings.HasSuffix(file.Name, ".go") {
gosrc.OverwriteLineComments(file.Data)
b.srcs[file.Name] = &source{name: file.Name, browseURL: file.BrowseURL, data: file.Data}
} else {
addReferences(references, file.Data)
}
}
for r := range references {
pkg.References = append(pkg.References, r)
}
if len(b.srcs) == 0 {
return pkg, nil
}
b.fset = token.NewFileSet()
// Find the package and associated files.
ctxt := build.Context{
GOOS: "linux",
GOARCH: "amd64",
CgoEnabled: true,
ReleaseTags: build.Default.ReleaseTags,
BuildTags: build.Default.BuildTags,
Compiler: "gc",
}
var err error
var bpkg *build.Package
for _, env := range goEnvs {
ctxt.GOOS = env.GOOS
ctxt.GOARCH = env.GOARCH
// TODO(garyburd): Change second argument to build.ImportComment when
// gddo is upgraded to Go 1.4.
bpkg, err = dir.Import(&ctxt, 0 /* build.ImportComment */)
if _, ok := err.(*build.NoGoError); !ok {
break
}
}
if err != nil {
if _, ok := err.(*build.NoGoError); !ok {
pkg.Errors = append(pkg.Errors, err.Error())
}
return pkg, nil
}
/*
TODO(garyburd): This block of code uses the import comment feature
added in Go 1.4. Uncomment this block when gddo upgraded to Go 1.4.
Also, change the second argument to dir.Import above from 0 to
build.ImportComment.
if bpkg.ImportComment != "" && bpkg.ImportComment != dir.ImportPath {
return nil, gosrc.NotFoundError{
Message: "not at canonical import path",
Redirect: bpkg.ImportComment,
}
}
*/
// Parse the Go files
files := make(map[string]*ast.File)
names := append(bpkg.GoFiles, bpkg.CgoFiles...)
sort.Strings(names)
pkg.Files = make([]*File, len(names))
for i, name := range names {
file, err := parser.ParseFile(b.fset, name, b.srcs[name].data, parser.ParseComments)
if err != nil {
pkg.Errors = append(pkg.Errors, err.Error())
} else {
files[name] = file
}
src := b.srcs[name]
src.index = i
pkg.Files[i] = &File{Name: name, URL: src.browseURL}
pkg.SourceSize += len(src.data)
//.........这里部分代码省略.........
示例4: getPackage
func getPackage(pkgpath string) (*gobuild.Package, error) {
var ctx gobuild.Context = gobuild.Default
ctx.GOARCH = GOARCH
ctx.GOOS = GOOS
ctx.BuildTags = append(ctx.BuildTags[:], "llgo")
//ctx.Compiler = "llgo"
// Attempt to find an overlay package path,
// which we'll use in ReadDir below.
overlayentries := make(map[string]bool)
overlaypkgpath := llgoPkgPrefix + pkgpath
overlaypkg, err := ctx.Import(overlaypkgpath, "", gobuild.FindOnly)
if err != nil {
overlaypkg = nil
}
// ReadDir is overridden to return a fake ".s"
// file for each ".ll" file in the directory.
ctx.ReadDir = func(dir string) (fi []os.FileInfo, err error) {
fi, err = ioutil.ReadDir(dir)
if err != nil {
return nil, err
}
entries := make(map[string]os.FileInfo)
for _, info := range fi {
entries[info.Name()] = info
}
// Overlay all files in the overlay package dir.
// If we find any .ll files, replace the suffix
// with .s.
if overlaypkg != nil {
fi, err = ioutil.ReadDir(overlaypkg.Dir)
}
if err == nil {
// Check for .ll files in the overlay dir if
// we have one, else in the standard package dir.
for _, info := range fi {
name := info.Name()
if strings.HasSuffix(name, ".ll") {
name = name[:len(name)-3] + ".s"
info = &renamedFileInfo{info, name}
}
overlayentries[name] = true
entries[name] = info
}
}
fi = make([]os.FileInfo, 0, len(entries))
for _, info := range entries {
fi = append(fi, info)
}
return fi, nil
}
// OpenFile is overridden to return the contents
// of the ".ll" file found in ReadDir above. The
// returned ReadCloser is wrapped to transform
// LLVM IR comments to use "//", as expected by
// go/build when looking for build tags.
ctx.OpenFile = func(path string) (io.ReadCloser, error) {
base := filepath.Base(path)
overlay := overlayentries[base]
if overlay {
if overlaypkg != nil {
path = filepath.Join(overlaypkg.Dir, base)
}
if strings.HasSuffix(path, ".s") {
path := path[:len(path)-2] + ".ll"
var r io.ReadCloser
var err error
r, err = os.Open(path)
if err == nil {
r = build.NewLLVMIRReader(r)
}
return r, err
}
}
return os.Open(path)
}
pkg, err := ctx.Import(pkgpath, "", 0)
if err != nil {
return nil, err
} else {
if overlaypkg == nil {
overlaypkg = pkg
}
for i, filename := range pkg.GoFiles {
pkgdir := pkg.Dir
if overlayentries[filename] {
pkgdir = overlaypkg.Dir
}
pkg.GoFiles[i] = path.Join(pkgdir, filename)
}
for i, filename := range pkg.CFiles {
pkgdir := pkg.Dir
if overlayentries[filename] {
pkgdir = overlaypkg.Dir
}
pkg.CFiles[i] = path.Join(pkgdir, filename)
}
//.........这里部分代码省略.........