Commit 7bcc6a16 authored by Russ Cox's avatar Russ Cox

cmd/compile: add -importmap option

The -importmap option takes an argument of the form old=new
and specifies that import "old" should be interpreted as if it said
import "new". The option may be repeated to specify multiple mappings.

This option is here to support the go command's new -vendor flag.

Change-Id: I31b4ed4249b549982a720bf61bb230462b33c59b
Reviewed-on: https://go-review.googlesource.com/10922Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent f5d494bb
...@@ -54,6 +54,9 @@ Flags: ...@@ -54,6 +54,9 @@ Flags:
Remove the limit on the number of errors reported (default limit is 10). Remove the limit on the number of errors reported (default limit is 10).
-h -h
Halt with a stack trace at the first error detected. Halt with a stack trace at the first error detected.
-importmap old=new
Interpret import "old" as import "new" during compilation.
The option may be repeated to add multiple mappings.
-installsuffix suffix -installsuffix suffix
Look for packages in $GOROOT/pkg/$GOOS_$GOARCH_suffix Look for packages in $GOROOT/pkg/$GOOS_$GOARCH_suffix
instead of $GOROOT/pkg/$GOOS_$GOARCH. instead of $GOROOT/pkg/$GOOS_$GOARCH.
......
...@@ -214,6 +214,7 @@ func Main() { ...@@ -214,6 +214,7 @@ func Main() {
obj.Flagcount("g", "debug code generation", &Debug['g']) obj.Flagcount("g", "debug code generation", &Debug['g'])
obj.Flagcount("h", "halt on error", &Debug['h']) obj.Flagcount("h", "halt on error", &Debug['h'])
obj.Flagcount("i", "debug line number stack", &Debug['i']) obj.Flagcount("i", "debug line number stack", &Debug['i'])
obj.Flagfn1("importmap", "add `definition` of the form source=actual to import map", addImportMap)
obj.Flagstr("installsuffix", "set pkg directory `suffix`", &flag_installsuffix) obj.Flagstr("installsuffix", "set pkg directory `suffix`", &flag_installsuffix)
obj.Flagcount("j", "debug runtime-initialized variables", &Debug['j']) obj.Flagcount("j", "debug runtime-initialized variables", &Debug['j'])
obj.Flagcount("l", "disable inlining", &Debug['l']) obj.Flagcount("l", "disable inlining", &Debug['l'])
...@@ -501,6 +502,20 @@ func Main() { ...@@ -501,6 +502,20 @@ func Main() {
Flusherrors() Flusherrors()
} }
var importMap = map[string]string{}
func addImportMap(s string) {
if strings.Count(s, "=") != 1 {
log.Fatal("-importmap argument must be of the form source=actual")
}
i := strings.Index(s, "=")
source, actual := s[:i], s[i+1:]
if source == "" || actual == "" {
log.Fatal("-importmap argument must be of the form source=actual; source and actual must be non-empty")
}
importMap[source] = actual
}
func saveerrors() { func saveerrors() {
nsavederrors += nerrors nsavederrors += nerrors
nerrors = 0 nerrors = 0
...@@ -687,6 +702,11 @@ func importfile(f *Val, line int) { ...@@ -687,6 +702,11 @@ func importfile(f *Val, line int) {
} }
path_ := f.U.(string) path_ := f.U.(string)
if mapped, ok := importMap[path_]; ok {
path_ = mapped
}
if islocalname(path_) { if islocalname(path_) {
if path_[0] == '/' { if path_[0] == '/' {
Yyerror("import path cannot be absolute path") Yyerror("import path cannot be absolute path")
......
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