Skip to content

Commit 4d6b08d

Browse files
committed
cmd/compile: add -importcfg to specify import resolution
Allows reading -importmap options from a file instead of putting them all on the command line, and adds the ability to specify the file location of specific packages. In effect, -importcfg is a generalization of and supersedes -importmap, -importsuffix, and -I. Of course, those flags will continue to be supported, for compatibility with other tools. Having this flag in Go 1.9 will let us try some experiments involving package management without needing guinea pigs to build a custom Go toolchain. This flag also helps with #14271 at some later point. For #20579. Change-Id: If005dbc2b01d8fd16cbfd3687dfbe82499f4bc56 Reviewed-on: https://go-review.googlesource.com/44850 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent e5646b2 commit 4d6b08d

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

src/cmd/compile/internal/gc/main.go

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"flag"
1919
"fmt"
2020
"io"
21+
"io/ioutil"
2122
"log"
2223
"os"
2324
"path"
@@ -195,6 +196,7 @@ func Main(archInit func(*Arch)) {
195196
objabi.Flagcount("h", "halt on error", &Debug['h'])
196197
objabi.Flagcount("i", "debug line number stack", &Debug['i'])
197198
objabi.Flagfn1("importmap", "add `definition` of the form source=actual to import map", addImportMap)
199+
objabi.Flagfn1("importcfg", "read import configuration from `file`", readImportCfg)
198200
flag.StringVar(&flag_installsuffix, "installsuffix", "", "set pkg directory `suffix`")
199201
objabi.Flagcount("j", "debug runtime-initialized variables", &Debug['j'])
200202
objabi.Flagcount("l", "disable inlining", &Debug['l'])
@@ -671,7 +673,10 @@ func writebench(filename string) error {
671673
return f.Close()
672674
}
673675

674-
var importMap = map[string]string{}
676+
var (
677+
importMap = map[string]string{}
678+
packageFile map[string]string // nil means not in use
679+
)
675680

676681
func addImportMap(s string) {
677682
if strings.Count(s, "=") != 1 {
@@ -685,6 +690,47 @@ func addImportMap(s string) {
685690
importMap[source] = actual
686691
}
687692

693+
func readImportCfg(file string) {
694+
packageFile = map[string]string{}
695+
data, err := ioutil.ReadFile(file)
696+
if err != nil {
697+
log.Fatalf("-importcfg: %v", err)
698+
}
699+
700+
for lineNum, line := range strings.Split(string(data), "\n") {
701+
lineNum++ // 1-based
702+
line = strings.TrimSpace(line)
703+
if line == "" || strings.HasPrefix(line, "#") {
704+
continue
705+
}
706+
707+
var verb, args string
708+
if i := strings.Index(line, " "); i < 0 {
709+
verb = line
710+
} else {
711+
verb, args = line[:i], strings.TrimSpace(line[i+1:])
712+
}
713+
var before, after string
714+
if i := strings.Index(args, "="); i >= 0 {
715+
before, after = args[:i], args[i+1:]
716+
}
717+
switch verb {
718+
default:
719+
log.Fatalf("%s:%d: unknown directive %q", file, lineNum, verb)
720+
case "importmap":
721+
if before == "" || after == "" {
722+
log.Fatalf(`%s:%d: invalid importmap: syntax is "importmap old=new"`, file, lineNum)
723+
}
724+
importMap[before] = after
725+
case "packagefile":
726+
if before == "" || after == "" {
727+
log.Fatalf(`%s:%d: invalid packagefile: syntax is "packagefile path=filename"`, file, lineNum)
728+
}
729+
packageFile[before] = after
730+
}
731+
}
732+
}
733+
688734
func saveerrors() {
689735
nsavederrors += nerrors
690736
nerrors = 0
@@ -745,6 +791,11 @@ func findpkg(name string) (file string, ok bool) {
745791
return "", false
746792
}
747793

794+
if packageFile != nil {
795+
file, ok = packageFile[name]
796+
return file, ok
797+
}
798+
748799
// try .a before .6. important for building libraries:
749800
// if there is an array.6 in the array.a library,
750801
// want to find all of array.a, not just array.6.
@@ -767,6 +818,11 @@ func findpkg(name string) (file string, ok bool) {
767818
return "", false
768819
}
769820

821+
if packageFile != nil {
822+
file, ok = packageFile[name]
823+
return file, ok
824+
}
825+
770826
for _, dir := range idirs {
771827
file = fmt.Sprintf("%s/%s.a", dir, name)
772828
if _, err := os.Stat(file); err == nil {
@@ -969,8 +1025,13 @@ func importfile(f *Val) *types.Pkg {
9691025
}
9701026

9711027
// assume files move (get installed) so don't record the full path
972-
// (e.g., for file "/Users/foo/go/pkg/darwin_amd64/math.a" record "math.a")
973-
Ctxt.AddImport(file[len(file)-len(path_)-len(pkgSuffix):])
1028+
if packageFile != nil {
1029+
// If using a packageFile map, assume path_ can be recorded directly.
1030+
Ctxt.AddImport(path_)
1031+
} else {
1032+
// For file "/Users/foo/go/pkg/darwin_amd64/math.a" record "math.a".
1033+
Ctxt.AddImport(file[len(file)-len(path_)-len(pkgSuffix):])
1034+
}
9741035

9751036
// In the importfile, if we find:
9761037
// $$\n (textual format): not supported anymore

0 commit comments

Comments
 (0)