Skip to content

Commit

Permalink
internal/code: Add darwin/amd64 fallback env for package loading.
Browse files Browse the repository at this point in the history
Previously, packages were only loaded with the default linux/amd64
environment. For most packages, that worked well. However, e.g.,
a macOS-only package would have all of its Go files excluded due to
unmet build constraints.

This change adds a secondary darwin/amd64 environment as a fallback
to try if all Go files are excluded due to unmet build constraints.

This was needed for the macOS-only dmitri.shuralyov.com/gpu/mtl package.
  • Loading branch information
dmitshur committed Jul 11, 2018
1 parent 0457bc7 commit 0c473b6
Showing 1 changed file with 67 additions and 39 deletions.
106 changes: 67 additions & 39 deletions internal/code/code.go
Expand Up @@ -207,48 +207,76 @@ func walkRepository(gitDir, repoRoot string) ([]*Directory, error) {
// from filesystem fs in directory dir. // from filesystem fs in directory dir.
// It returns a nil Package if the directory doesn't contain a Go package. // It returns a nil Package if the directory doesn't contain a Go package.
func loadPackage(fs vfs.FileSystem, dir, importPath string) (*Package, error) { func loadPackage(fs vfs.FileSystem, dir, importPath string) (*Package, error) {
bctx := build.Context{ for _, env := range [...]struct{ GOOS, GOARCH string }{
GOOS: "linux", {"linux", "amd64"},
GOARCH: "amd64", {"darwin", "amd64"},
CgoEnabled: true, } {
Compiler: build.Default.Compiler, bctx := build.Context{
ReleaseTags: build.Default.ReleaseTags, GOOS: env.GOOS,
GOARCH: env.GOARCH,
CgoEnabled: true,
Compiler: build.Default.Compiler,
ReleaseTags: build.Default.ReleaseTags,


JoinPath: path.Join, JoinPath: path.Join,
SplitPathList: splitPathList, SplitPathList: splitPathList,
IsAbsPath: path.IsAbs, IsAbsPath: path.IsAbs,
IsDir: func(path string) bool { IsDir: func(path string) bool {
fi, err := fs.Stat(path) fi, err := fs.Stat(path)
return err == nil && fi.IsDir() return err == nil && fi.IsDir()
}, },
HasSubdir: hasSubdir, HasSubdir: hasSubdir,
ReadDir: func(dir string) ([]os.FileInfo, error) { return fs.ReadDir(dir) }, ReadDir: func(dir string) ([]os.FileInfo, error) { return fs.ReadDir(dir) },
OpenFile: func(path string) (io.ReadCloser, error) { return fs.Open(path) }, OpenFile: func(path string) (io.ReadCloser, error) { return fs.Open(path) },
} }
p, err := bctx.ImportDir(dir, 0) p, err := bctx.ImportDir(dir, 0)
if _, ok := err.(*build.NoGoError); ok { if _, ok := err.(*build.NoGoError); ok {
// This directory doesn't contain a package. if buildConstraintsExcludeAll(p) {
return nil, nil // Try again with a different environment.
} else if err != nil { continue
return nil, err }
// This directory doesn't contain a package.
break
} else if err != nil {
return nil, err
}
// TODO: Automate this.
doc := p.Doc
switch importPath {
case "dmitri.shuralyov.com/text/kebabcase", "dmitri.shuralyov.com/kebabcase":
doc += "\n\nReference: https://en.wikipedia.org/wiki/Naming_convention_(programming)#Multiple-word_identifiers."
case "dmitri.shuralyov.com/scratch/image/jpeg":
doc += "\n\nJPEG is defined in ITU-T T.81: http://www.w3.org/Graphics/JPEG/itu-t81.pdf."
case "dmitri.shuralyov.com/scratch/image/png":
doc += "\n\nThe PNG specification is at http://www.w3.org/TR/PNG/."
case "dmitri.shuralyov.com/font/woff2":
doc += "\n\nThe WOFF2 font packaging format is specified at https://www.w3.org/TR/WOFF2/."
case "dmitri.shuralyov.com/gpu/mtl":
doc += "\n\nThis package is in very early stages of development. The API will change when opportunities for improvement are discovered; it is not yet frozen. Less than 20% of the Metal API surface is implemented. Current functionality is sufficient to render very basic geometry."
case "dmitri.shuralyov.com/gpu/mtl/example/hellotriangle":
doc += " It writes the frame to a triangle.png file in current working directory."
}
return &Package{
Name: p.Name,
Synopsis: p.Doc,
DocHTML: docHTML(doc),
}, nil
} }
// TODO: Automate this. // This directory doesn't contain a package.
doc := p.Doc return nil, nil
switch importPath { }
case "dmitri.shuralyov.com/text/kebabcase", "dmitri.shuralyov.com/kebabcase":
doc += "\n\nReference: https://en.wikipedia.org/wiki/Naming_convention_(programming)#Multiple-word_identifiers." // buildConstraintsExcludeAll reports whether Go files exist in p,
case "dmitri.shuralyov.com/scratch/image/jpeg": // but they were ignored due to build constraints.
doc += "\n\nJPEG is defined in ITU-T T.81: http://www.w3.org/Graphics/JPEG/itu-t81.pdf." func buildConstraintsExcludeAll(p *build.Package) bool {
case "dmitri.shuralyov.com/scratch/image/png": // Count files beginning with _ and ., which we will pretend don't exist at all.
doc += "\n\nThe PNG specification is at http://www.w3.org/TR/PNG/." dummy := 0
case "dmitri.shuralyov.com/font/woff2": for _, name := range p.IgnoredGoFiles {
doc += "\n\nThe WOFF2 font packaging format is specified at https://www.w3.org/TR/WOFF2/." if strings.HasPrefix(name, "_") || strings.HasPrefix(name, ".") {
dummy++
}
} }
return &Package{ return len(p.IgnoredGoFiles) > dummy
Name: p.Name,
Synopsis: p.Doc,
DocHTML: docHTML(doc),
}, nil
} }


func splitPathList(list string) []string { return strings.Split(list, ":") } func splitPathList(list string) []string { return strings.Split(list, ":") }
Expand Down

0 comments on commit 0c473b6

Please sign in to comment.