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