Commit 17bebd3c authored by Russ Cox's avatar Russ Cox

go/build: handle cgo, //build comments

R=adg
CC=golang-dev
https://golang.org/cl/5018044
parent ce008f8c
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
"go/token" "go/token"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath" // use for file system paths
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
...@@ -190,7 +190,7 @@ func install(pkg, parent string) { ...@@ -190,7 +190,7 @@ func install(pkg, parent string) {
}() }()
// Don't allow trailing '/' // Don't allow trailing '/'
if _, f := filepath.Split(pkg); f == "" { if strings.HasSuffix(pkg, "/") {
errorf("%s should not have trailing '/'\n", pkg) errorf("%s should not have trailing '/'\n", pkg)
return return
} }
...@@ -225,16 +225,17 @@ func install(pkg, parent string) { ...@@ -225,16 +225,17 @@ func install(pkg, parent string) {
terrorf(tree, "%s: %v\n", pkg, err) terrorf(tree, "%s: %v\n", pkg, err)
return return
} }
dir := filepath.Join(tree.SrcDir(), pkg) dir := filepath.Join(tree.SrcDir(), filepath.FromSlash(pkg))
// Install prerequisites. // Install prerequisites.
dirInfo, err := build.ScanDir(dir, parent == "") dirInfo, err := build.ScanDir(dir)
if err != nil { if err != nil {
terrorf(tree, "%s: %v\n", pkg, err) terrorf(tree, "%s: %v\n", pkg, err)
return return
} }
if len(dirInfo.GoFiles)+len(dirInfo.CgoFiles) == 0 { // We reserve package main to identify commands.
terrorf(tree, "%s: package has no files\n", pkg) if parent != "" && dirInfo.Package == "main" {
terrorf(tree, "%s: found only package main in %s; cannot import", pkg, dir)
return return
} }
for _, p := range dirInfo.Imports { for _, p := range dirInfo.Imports {
......
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
"bytes" "bytes"
"go/build" "go/build"
"os" "os"
"path/filepath" "path" // use for import paths
"strings" "strings"
"template" "template"
) )
...@@ -44,10 +44,10 @@ func makeMakefile(dir, pkg string, tree *build.Tree, isCmd bool) ([]byte, os.Err ...@@ -44,10 +44,10 @@ func makeMakefile(dir, pkg string, tree *build.Tree, isCmd bool) ([]byte, os.Err
targDir := tree.PkgDir() targDir := tree.PkgDir()
if isCmd { if isCmd {
// use the last part of the package name for targ // use the last part of the package name for targ
_, targ = filepath.Split(pkg) _, targ = path.Split(pkg)
targDir = tree.BinDir() targDir = tree.BinDir()
} }
dirInfo, err := build.ScanDir(dir, isCmd) dirInfo, err := build.ScanDir(dir)
if err != nil { if err != nil {
return nil, err return nil, err
} }
......
...@@ -165,7 +165,7 @@ func setEnvironment() { ...@@ -165,7 +165,7 @@ func setEnvironment() {
func getTestFileNames() { func getTestFileNames() {
names := fileNames names := fileNames
if len(names) == 0 { if len(names) == 0 {
info, err := build.ScanDir(".", true) info, err := build.ScanDir(".")
if err != nil { if err != nil {
Fatalf("scanning directory: %v", err) Fatalf("scanning directory: %v", err)
} }
......
...@@ -27,7 +27,7 @@ var buildPkgs = []struct { ...@@ -27,7 +27,7 @@ var buildPkgs = []struct {
&DirInfo{ &DirInfo{
GoFiles: []string{"pkgtest.go"}, GoFiles: []string{"pkgtest.go"},
SFiles: []string{"sqrt_" + runtime.GOARCH + ".s"}, SFiles: []string{"sqrt_" + runtime.GOARCH + ".s"},
PkgName: "pkgtest", Package: "pkgtest",
Imports: []string{"os"}, Imports: []string{"os"},
TestImports: []string{"fmt", "pkgtest"}, TestImports: []string{"fmt", "pkgtest"},
TestGoFiles: sortstr([]string{"sqrt_test.go", "sqrt_" + runtime.GOARCH + "_test.go"}), TestGoFiles: sortstr([]string{"sqrt_test.go", "sqrt_" + runtime.GOARCH + "_test.go"}),
...@@ -38,7 +38,7 @@ var buildPkgs = []struct { ...@@ -38,7 +38,7 @@ var buildPkgs = []struct {
"go/build/cmdtest", "go/build/cmdtest",
&DirInfo{ &DirInfo{
GoFiles: []string{"main.go"}, GoFiles: []string{"main.go"},
PkgName: "main", Package: "main",
Imports: []string{"go/build/pkgtest"}, Imports: []string{"go/build/pkgtest"},
}, },
}, },
...@@ -48,7 +48,9 @@ var buildPkgs = []struct { ...@@ -48,7 +48,9 @@ var buildPkgs = []struct {
CgoFiles: []string{"cgotest.go"}, CgoFiles: []string{"cgotest.go"},
CFiles: []string{"cgotest.c"}, CFiles: []string{"cgotest.c"},
Imports: []string{"C", "unsafe"}, Imports: []string{"C", "unsafe"},
PkgName: "cgotest", Package: "cgotest",
CgoLDFLAGS: []string{"-lregexp"},
CgoPkgConfig: []string{"cairo", "moscow"},
}, },
}, },
} }
...@@ -56,11 +58,11 @@ var buildPkgs = []struct { ...@@ -56,11 +58,11 @@ var buildPkgs = []struct {
const cmdtestOutput = "3" const cmdtestOutput = "3"
func TestBuild(t *testing.T) { func TestBuild(t *testing.T) {
var ctxt = Context{GOOS: "darwin", GOARCH: "amd64"}
for _, tt := range buildPkgs { for _, tt := range buildPkgs {
tree := Path[0] // Goroot tree := Path[0] // Goroot
dir := filepath.Join(tree.SrcDir(), tt.dir) dir := filepath.Join(tree.SrcDir(), tt.dir)
info, err := ctxt.ScanDir(dir)
info, err := ScanDir(dir, true)
if err != nil { if err != nil {
t.Errorf("ScanDir(%#q): %v", tt.dir, err) t.Errorf("ScanDir(%#q): %v", tt.dir, err)
continue continue
...@@ -70,6 +72,13 @@ func TestBuild(t *testing.T) { ...@@ -70,6 +72,13 @@ func TestBuild(t *testing.T) {
continue continue
} }
if tt.dir == "go/build/cgotest" {
// Don't actually run cgo.
// Among other things our test depends
// on pkg-config, which is not present on all systems.
continue
}
s, err := Build(tree, tt.dir, info) s, err := Build(tree, tt.dir, info)
if err != nil { if err != nil {
t.Errorf("Build(%#q): %v", tt.dir, err) t.Errorf("Build(%#q): %v", tt.dir, err)
......
...@@ -6,6 +6,9 @@ package cgotest ...@@ -6,6 +6,9 @@ package cgotest
/* /*
char* greeting = "hello, world"; char* greeting = "hello, world";
#cgo darwin/amd64 LDFLAGS: -lregexp
#cgo linux CFLAGS: -m32
#cgo pkg-config: cairo moscow
*/ */
// #include "cgotest.h" // #include "cgotest.h"
import "C" import "C"
......
This diff is collapsed.
...@@ -55,8 +55,8 @@ var tests = []GoodFileTest{ ...@@ -55,8 +55,8 @@ var tests = []GoodFileTest{
func TestGoodOSArch(t *testing.T) { func TestGoodOSArch(t *testing.T) {
for _, test := range tests { for _, test := range tests {
if DefaultContext.goodOSArch(test.name) != test.result { if DefaultContext.goodOSArchFile(test.name) != test.result {
t.Fatalf("goodOSArch(%q) != %v", test.name, test.result) t.Fatalf("goodOSArchFile(%q) != %v", test.name, test.result)
} }
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment