Skip to content

Commit fbd74a8

Browse files
committed
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 <[email protected]>
1 parent 68a98d5 commit fbd74a8

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

test/run.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,15 @@ func compileInDir(runcmd runCmd, dir string, flags []string, localImports bool,
233233
return runcmd(cmd...)
234234
}
235235

236-
func linkFile(runcmd runCmd, goname string) (err error) {
236+
func linkFile(runcmd runCmd, goname string, ldflags []string) (err error) {
237237
pfile := strings.Replace(goname, ".go", ".o", -1)
238238
cmd := []string{goTool(), "tool", "link", "-w", "-o", "a.exe", "-L", "."}
239239
if *linkshared {
240240
cmd = append(cmd, "-linkshared", "-installsuffix=dynlink")
241241
}
242+
if ldflags != nil {
243+
cmd = append(cmd, ldflags...)
244+
}
242245
cmd = append(cmd, pfile)
243246
_, err = runcmd(cmd...)
244247
return
@@ -324,6 +327,18 @@ func goDirFiles(longdir string) (filter []os.FileInfo, err error) {
324327

325328
var packageRE = regexp.MustCompile(`(?m)^package ([\p{Lu}\p{Ll}\w]+)`)
326329

330+
func getPackageNameFromSource(fn string) (string, error) {
331+
data, err := ioutil.ReadFile(fn)
332+
if err != nil {
333+
return "", err
334+
}
335+
pkgname := packageRE.FindStringSubmatch(string(data))
336+
if pkgname == nil {
337+
return "", fmt.Errorf("cannot find package name in %s", fn)
338+
}
339+
return pkgname[1], nil
340+
}
341+
327342
// If singlefilepkgs is set, each file is considered a separate package
328343
// even if the package names are the same.
329344
func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
@@ -335,19 +350,13 @@ func goDirPackages(longdir string, singlefilepkgs bool) ([][]string, error) {
335350
m := make(map[string]int)
336351
for _, file := range files {
337352
name := file.Name()
338-
data, err := ioutil.ReadFile(filepath.Join(longdir, name))
339-
if err != nil {
340-
return nil, err
341-
}
342-
pkgname := packageRE.FindStringSubmatch(string(data))
343-
if pkgname == nil {
344-
return nil, fmt.Errorf("cannot find package name in %s", name)
345-
}
346-
i, ok := m[pkgname[1]]
353+
pkgname, err := getPackageNameFromSource(filepath.Join(longdir, name))
354+
check(err)
355+
i, ok := m[pkgname]
347356
if singlefilepkgs || !ok {
348357
i = len(pkgs)
349358
pkgs = append(pkgs, nil)
350-
m[pkgname[1]] = i
359+
m[pkgname] = i
351360
}
352361
pkgs[i] = append(pkgs[i], name)
353362
}
@@ -502,6 +511,7 @@ func (t *test) run() {
502511
wantError := false
503512
wantAuto := false
504513
singlefilepkgs := false
514+
setpkgpaths := false
505515
localImports := true
506516
f := strings.Fields(action)
507517
if len(f) > 0 {
@@ -540,6 +550,8 @@ func (t *test) run() {
540550
wantError = false
541551
case "-s":
542552
singlefilepkgs = true
553+
case "-P":
554+
setpkgpaths = true
543555
case "-n":
544556
// Do not set relative path for local imports to current dir,
545557
// e.g. do not pass -D . -I . to the compiler.
@@ -765,16 +777,34 @@ func (t *test) run() {
765777
t.err = err
766778
return
767779
}
780+
// Split flags into gcflags and ldflags
781+
ldflags := []string{}
782+
for i, fl := range flags {
783+
if fl == "-ldflags" {
784+
ldflags = flags[i+1:]
785+
flags = flags[0:i]
786+
break
787+
}
788+
}
789+
768790
for i, gofiles := range pkgs {
769-
_, err := compileInDir(runcmd, longdir, flags, localImports, gofiles...)
791+
pflags := []string{}
792+
pflags = append(pflags, flags...)
793+
if setpkgpaths {
794+
fp := filepath.Join(longdir, gofiles[0])
795+
pkgname, serr := getPackageNameFromSource(fp)
796+
check(serr)
797+
pflags = append(pflags, "-p", pkgname)
798+
}
799+
_, err := compileInDir(runcmd, longdir, pflags, localImports, gofiles...)
770800
// Allow this package compilation fail based on conditions below;
771801
// its errors were checked in previous case.
772802
if err != nil && !(wantError && action == "errorcheckandrundir" && i == len(pkgs)-2) {
773803
t.err = err
774804
return
775805
}
776806
if i == len(pkgs)-1 {
777-
err = linkFile(runcmd, gofiles[0])
807+
err = linkFile(runcmd, gofiles[0], ldflags)
778808
if err != nil {
779809
t.err = err
780810
return

0 commit comments

Comments
 (0)