mirror of https://github.com/golang/go.git
test: support -ldflags for "rundir" tests, new -P option
For "rundir" tests, allow users to add in linker flags as well as compiler flags, e.g. // rundir -m -ldflags -w The directive above will pass "-m" to the compiler on each package compilation and "-w" to the linker for the final link. In addition, if "-P" is specified with 'rundir', then for each compile pass in "-p <X>" to set the packagepath explicitly, which is closer to how the compiler is run by 'go build'. Change-Id: I04720011a89d1bd8dcb4f2ccb4af1d74f6a01da1 Reviewed-on: https://go-review.googlesource.com/c/go/+/168977 Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
parent
68a98d5279
commit
fbd74a8922
56
test/run.go
56
test/run.go
|
|
@ -233,12 +233,15 @@ func compileInDir(runcmd runCmd, dir string, flags []string, localImports bool,
|
||||||
return runcmd(cmd...)
|
return runcmd(cmd...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func linkFile(runcmd runCmd, goname string) (err error) {
|
func linkFile(runcmd runCmd, goname string, ldflags []string) (err error) {
|
||||||
pfile := strings.Replace(goname, ".go", ".o", -1)
|
pfile := strings.Replace(goname, ".go", ".o", -1)
|
||||||
cmd := []string{goTool(), "tool", "link", "-w", "-o", "a.exe", "-L", "."}
|
cmd := []string{goTool(), "tool", "link", "-w", "-o", "a.exe", "-L", "."}
|
||||||
if *linkshared {
|
if *linkshared {
|
||||||
cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
|
cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
|
||||||
}
|
}
|
||||||
|
if ldflags != nil {
|
||||||
|
cmd = append(cmd, ldflags...)
|
||||||
|
}
|
||||||
cmd = append(cmd, pfile)
|
cmd = append(cmd, pfile)
|
||||||
_, err = runcmd(cmd...)
|
_, err = runcmd(cmd...)
|
||||||
return
|
return
|
||||||
|
|
@ -324,6 +327,18 @@ func goDirFiles(longdir string) (filter []os.FileInfo, err error) {
|
||||||
|
|
||||||
var packageRE = regexp.MustCompile(`(?m)^package ([\p{Lu}\p{Ll}\w]+)`)
|
var packageRE = regexp.MustCompile(`(?m)^package ([\p{Lu}\p{Ll}\w]+)`)
|
||||||
|
|
||||||
|
func getPackageNameFromSource(fn string) (string, error) {
|
||||||
|
data, err := ioutil.ReadFile(fn)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
pkgname := packageRE.FindStringSubmatch(string(data))
|
||||||
|
if pkgname == nil {
|
||||||
|
return "", fmt.Errorf("cannot find package name in %s", fn)
|
||||||
|
}
|
||||||
|
return pkgname[1], nil
|
||||||
|
}
|
||||||
|
|
||||||
// If singlefilepkgs is set, each file is considered a separate package
|
// If singlefilepkgs is set, each file is considered a separate package
|
||||||
// even if the package names are the same.
|
// even if the package names are the same.
|
||||||
func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
|
func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
|
||||||
|
|
@ -335,19 +350,13 @@ func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
|
||||||
m := make(map[string]int)
|
m := make(map[string]int)
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
name := file.Name()
|
name := file.Name()
|
||||||
data, err := ioutil.ReadFile(filepath.Join(longdir, name))
|
pkgname, err := getPackageNameFromSource(filepath.Join(longdir, name))
|
||||||
if err != nil {
|
check(err)
|
||||||
return nil, err
|
i, ok := m[pkgname]
|
||||||
}
|
|
||||||
pkgname := packageRE.FindStringSubmatch(string(data))
|
|
||||||
if pkgname == nil {
|
|
||||||
return nil, fmt.Errorf("cannot find package name in %s", name)
|
|
||||||
}
|
|
||||||
i, ok := m[pkgname[1]]
|
|
||||||
if singlefilepkgs || !ok {
|
if singlefilepkgs || !ok {
|
||||||
i = len(pkgs)
|
i = len(pkgs)
|
||||||
pkgs = append(pkgs, nil)
|
pkgs = append(pkgs, nil)
|
||||||
m[pkgname[1]] = i
|
m[pkgname] = i
|
||||||
}
|
}
|
||||||
pkgs[i] = append(pkgs[i], name)
|
pkgs[i] = append(pkgs[i], name)
|
||||||
}
|
}
|
||||||
|
|
@ -502,6 +511,7 @@ func (t *test) run() {
|
||||||
wantError := false
|
wantError := false
|
||||||
wantAuto := false
|
wantAuto := false
|
||||||
singlefilepkgs := false
|
singlefilepkgs := false
|
||||||
|
setpkgpaths := false
|
||||||
localImports := true
|
localImports := true
|
||||||
f := strings.Fields(action)
|
f := strings.Fields(action)
|
||||||
if len(f) > 0 {
|
if len(f) > 0 {
|
||||||
|
|
@ -540,6 +550,8 @@ func (t *test) run() {
|
||||||
wantError = false
|
wantError = false
|
||||||
case "-s":
|
case "-s":
|
||||||
singlefilepkgs = true
|
singlefilepkgs = true
|
||||||
|
case "-P":
|
||||||
|
setpkgpaths = true
|
||||||
case "-n":
|
case "-n":
|
||||||
// Do not set relative path for local imports to current dir,
|
// Do not set relative path for local imports to current dir,
|
||||||
// e.g. do not pass -D . -I . to the compiler.
|
// e.g. do not pass -D . -I . to the compiler.
|
||||||
|
|
@ -765,8 +777,26 @@ func (t *test) run() {
|
||||||
t.err = err
|
t.err = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// Split flags into gcflags and ldflags
|
||||||
|
ldflags := []string{}
|
||||||
|
for i, fl := range flags {
|
||||||
|
if fl == "-ldflags" {
|
||||||
|
ldflags = flags[i+1:]
|
||||||
|
flags = flags[0:i]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for i, gofiles := range pkgs {
|
for i, gofiles := range pkgs {
|
||||||
_, err := compileInDir(runcmd, longdir, flags, localImports, gofiles...)
|
pflags := []string{}
|
||||||
|
pflags = append(pflags, flags...)
|
||||||
|
if setpkgpaths {
|
||||||
|
fp := filepath.Join(longdir, gofiles[0])
|
||||||
|
pkgname, serr := getPackageNameFromSource(fp)
|
||||||
|
check(serr)
|
||||||
|
pflags = append(pflags, "-p", pkgname)
|
||||||
|
}
|
||||||
|
_, err := compileInDir(runcmd, longdir, pflags, localImports, gofiles...)
|
||||||
// Allow this package compilation fail based on conditions below;
|
// Allow this package compilation fail based on conditions below;
|
||||||
// its errors were checked in previous case.
|
// its errors were checked in previous case.
|
||||||
if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
|
if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
|
||||||
|
|
@ -774,7 +804,7 @@ func (t *test) run() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if i == len(pkgs)-1 {
|
if i == len(pkgs)-1 {
|
||||||
err = linkFile(runcmd, gofiles[0])
|
err = linkFile(runcmd, gofiles[0], ldflags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.err = err
|
t.err = err
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue