@@ -1397,18 +1397,19 @@ func (b *Builder) build(a *Action) (err error) {
1397
1397
if p1 .ImportPath == "unsafe" {
1398
1398
continue
1399
1399
}
1400
+ if p1 .Internal .Pkgfile == "" {
1401
+ // This happens for gccgo-internal packages like runtime.
1402
+ continue
1403
+ }
1400
1404
// TODO(rsc): runtime/internal/sys appears twice sometimes,
1401
1405
// because of the blind append in ../load/pkg.go that
1402
1406
// claims to fix issue 13655. That's probably not the right fix.
1403
1407
// Look into that.
1404
1408
fmt .Fprintf (& icfg , "packagefile %s=%s\n " , p1 .ImportPath , p1 .Internal .Pkgfile )
1405
1409
}
1406
- if err := b .writeFile (objdir + "importcfg" , icfg .Bytes ()); err != nil {
1407
- return err
1408
- }
1409
1410
1410
1411
// Compile Go.
1411
- ofile , out , err := BuildToolchain .gc (b , a .Package , a .Objpkg , objdir , objdir + "importcfg" , len (sfiles ) > 0 , gofiles )
1412
+ ofile , out , err := BuildToolchain .gc (b , a .Package , a .Objpkg , objdir , icfg . Bytes () , len (sfiles ) > 0 , gofiles )
1412
1413
if len (out ) > 0 {
1413
1414
b .showOutput (a .Package .Dir , a .Package .ImportPath , b .processOutput (out ))
1414
1415
if err != nil {
@@ -2174,7 +2175,7 @@ func mkAbs(dir, f string) string {
2174
2175
type toolchain interface {
2175
2176
// gc runs the compiler in a specific directory on a set of files
2176
2177
// and returns the name of the generated output file.
2177
- gc (b * Builder , p * load.Package , archive , objdir , importcfg string , asmhdr bool , gofiles []string ) (ofile string , out []byte , err error )
2178
+ gc (b * Builder , p * load.Package , archive , objdir string , importcfg [] byte , asmhdr bool , gofiles []string ) (ofile string , out []byte , err error )
2178
2179
// cc runs the toolchain's C compiler in a directory on a C file
2179
2180
// to produce an output file.
2180
2181
cc (b * Builder , p * load.Package , objdir , ofile , cfile string ) error
@@ -2213,7 +2214,7 @@ func (noToolchain) linker() string {
2213
2214
return ""
2214
2215
}
2215
2216
2216
- func (noToolchain ) gc (b * Builder , p * load.Package , archive , objdir , importcfg string , asmhdr bool , gofiles []string ) (ofile string , out []byte , err error ) {
2217
+ func (noToolchain ) gc (b * Builder , p * load.Package , archive , objdir string , importcfg [] byte , asmhdr bool , gofiles []string ) (ofile string , out []byte , err error ) {
2217
2218
return "" , nil , noCompiler ()
2218
2219
}
2219
2220
@@ -2253,7 +2254,7 @@ func (gcToolchain) linker() string {
2253
2254
return base .Tool ("link" )
2254
2255
}
2255
2256
2256
- func (gcToolchain ) gc (b * Builder , p * load.Package , archive , objdir , importcfg string , asmhdr bool , gofiles []string ) (ofile string , output []byte , err error ) {
2257
+ func (gcToolchain ) gc (b * Builder , p * load.Package , archive , objdir string , importcfg [] byte , asmhdr bool , gofiles []string ) (ofile string , output []byte , err error ) {
2257
2258
if archive != "" {
2258
2259
ofile = archive
2259
2260
} else {
@@ -2303,14 +2304,6 @@ func (gcToolchain) gc(b *Builder, p *load.Package, archive, objdir, importcfg st
2303
2304
gcargs = append (gcargs , "-dwarf=false" )
2304
2305
}
2305
2306
2306
- for _ , path := range p .Imports {
2307
- if i := strings .LastIndex (path , "/vendor/" ); i >= 0 {
2308
- gcargs = append (gcargs , "-importmap" , path [i + len ("/vendor/" ):]+ "=" + path )
2309
- } else if strings .HasPrefix (path , "vendor/" ) {
2310
- gcargs = append (gcargs , "-importmap" , path [len ("vendor/" ):]+ "=" + path )
2311
- }
2312
- }
2313
-
2314
2307
gcflags := buildGcflags
2315
2308
if compilingRuntime {
2316
2309
// Remove -N, if present.
@@ -2327,8 +2320,11 @@ func (gcToolchain) gc(b *Builder, p *load.Package, archive, objdir, importcfg st
2327
2320
}
2328
2321
}
2329
2322
args := []interface {}{cfg .BuildToolexec , base .Tool ("compile" ), "-o" , ofile , "-trimpath" , b .WorkDir , gcflags , gcargs , "-D" , p .Internal .LocalPrefix }
2330
- if importcfg != "" {
2331
- args = append (args , "-importcfg" , importcfg )
2323
+ if importcfg != nil {
2324
+ if err := b .writeFile (objdir + "importcfg" , importcfg ); err != nil {
2325
+ return "" , nil , err
2326
+ }
2327
+ args = append (args , "-importcfg" , objdir + "importcfg" )
2332
2328
}
2333
2329
if ofile == archive {
2334
2330
args = append (args , "-pack" )
@@ -2703,7 +2699,7 @@ func checkGccgoBin() {
2703
2699
os .Exit (2 )
2704
2700
}
2705
2701
2706
- func (tools gccgoToolchain ) gc (b * Builder , p * load.Package , archive , objdir , importcfg string , asmhdr bool , gofiles []string ) (ofile string , output []byte , err error ) {
2702
+ func (tools gccgoToolchain ) gc (b * Builder , p * load.Package , archive , objdir string , importcfg [] byte , asmhdr bool , gofiles []string ) (ofile string , output []byte , err error ) {
2707
2703
out := "_go_.o"
2708
2704
ofile = objdir + out
2709
2705
gcargs := []string {"-g" }
@@ -2716,8 +2712,19 @@ func (tools gccgoToolchain) gc(b *Builder, p *load.Package, archive, objdir, imp
2716
2712
}
2717
2713
2718
2714
args := str .StringList (tools .compiler (), "-c" , gcargs , "-o" , ofile )
2719
- if importcfg != "" {
2720
- args = append (args , "-importcfg" , importcfg )
2715
+ if importcfg != nil {
2716
+ if b .gccSupportsFlag (args [:1 ], "-fgo-importcfg=/dev/null" ) {
2717
+ if err := b .writeFile (objdir + "importcfg" , importcfg ); err != nil {
2718
+ return "" , nil , err
2719
+ }
2720
+ args = append (args , "-fgo-importcfg=" + objdir + "importcfg" )
2721
+ } else {
2722
+ root := objdir + "_importcfgroot_"
2723
+ if err := buildImportcfgSymlinks (b , root , importcfg ); err != nil {
2724
+ return "" , nil , err
2725
+ }
2726
+ args = append (args , "-I" , root )
2727
+ }
2721
2728
}
2722
2729
args = append (args , buildGccgoflags ... )
2723
2730
for _ , f := range gofiles {
@@ -2728,6 +2735,67 @@ func (tools gccgoToolchain) gc(b *Builder, p *load.Package, archive, objdir, imp
2728
2735
return ofile , output , err
2729
2736
}
2730
2737
2738
+ // buildImportcfgSymlinks builds in root a tree of symlinks
2739
+ // implementing the directives from importcfg.
2740
+ // This serves as a temporary transition mechanism until
2741
+ // we can depend on gccgo reading an importcfg directly.
2742
+ // (The Go 1.9 and later gc compilers already do.)
2743
+ func buildImportcfgSymlinks (b * Builder , root string , importcfg []byte ) error {
2744
+ for lineNum , line := range strings .Split (string (importcfg ), "\n " ) {
2745
+ lineNum ++ // 1-based
2746
+ line = strings .TrimSpace (line )
2747
+ if line == "" {
2748
+ continue
2749
+ }
2750
+ if line == "" || strings .HasPrefix (line , "#" ) {
2751
+ continue
2752
+ }
2753
+ var verb , args string
2754
+ if i := strings .Index (line , " " ); i < 0 {
2755
+ verb = line
2756
+ } else {
2757
+ verb , args = line [:i ], strings .TrimSpace (line [i + 1 :])
2758
+ }
2759
+ var before , after string
2760
+ if i := strings .Index (args , "=" ); i >= 0 {
2761
+ before , after = args [:i ], args [i + 1 :]
2762
+ }
2763
+ switch verb {
2764
+ default :
2765
+ base .Fatalf ("importcfg:%d: unknown directive %q" , lineNum , verb )
2766
+ case "packagefile" :
2767
+ if before == "" || after == "" {
2768
+ return fmt .Errorf (`importcfg:%d: invalid packagefile: syntax is "packagefile path=filename": %s` , lineNum , line )
2769
+ }
2770
+ archive := gccgoArchive (root , before )
2771
+ if err := b .Mkdir (filepath .Dir (archive )); err != nil {
2772
+ return err
2773
+ }
2774
+ if err := os .Symlink (after , archive ); err != nil {
2775
+ return err
2776
+ }
2777
+ case "importmap" :
2778
+ if before == "" || after == "" {
2779
+ return fmt .Errorf (`importcfg:%d: invalid importmap: syntax is "importmap old=new": %s` , lineNum , line )
2780
+ }
2781
+ beforeA := gccgoArchive (root , before )
2782
+ afterA := gccgoArchive (root , after )
2783
+ if err := b .Mkdir (filepath .Dir (beforeA )); err != nil {
2784
+ return err
2785
+ }
2786
+ if err := b .Mkdir (filepath .Dir (afterA )); err != nil {
2787
+ return err
2788
+ }
2789
+ if err := os .Symlink (afterA , beforeA ); err != nil {
2790
+ return err
2791
+ }
2792
+ case "packageshlib" :
2793
+ return fmt .Errorf ("gccgo -importcfg does not support shared libraries" )
2794
+ }
2795
+ }
2796
+ return nil
2797
+ }
2798
+
2731
2799
func (tools gccgoToolchain ) asm (b * Builder , p * load.Package , objdir string , sfiles []string ) ([]string , error ) {
2732
2800
var ofiles []string
2733
2801
for _ , sfile := range sfiles {
@@ -2749,7 +2817,11 @@ func (tools gccgoToolchain) asm(b *Builder, p *load.Package, objdir string, sfil
2749
2817
}
2750
2818
2751
2819
func (gccgoToolchain ) Pkgpath (basedir string , p * load.Package ) string {
2752
- end := filepath .FromSlash (p .ImportPath + ".a" )
2820
+ return gccgoArchive (basedir , p .ImportPath )
2821
+ }
2822
+
2823
+ func gccgoArchive (basedir , imp string ) string {
2824
+ end := filepath .FromSlash (imp + ".a" )
2753
2825
afile := filepath .Join (basedir , end )
2754
2826
// add "lib" to the final element
2755
2827
return filepath .Join (filepath .Dir (afile ), "lib" + filepath .Base (afile ))
@@ -3653,7 +3725,7 @@ func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) {
3653
3725
3654
3726
p := load .GoFilesPackage (srcs )
3655
3727
3656
- if _ , _ , e := BuildToolchain .gc (b , p , "" , objdir , "" , false , srcs ); e != nil {
3728
+ if _ , _ , e := BuildToolchain .gc (b , p , "" , objdir , nil , false , srcs ); e != nil {
3657
3729
return "32" , nil
3658
3730
}
3659
3731
return "64" , nil
0 commit comments