]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/cmd/go/internal/work/gccgo.go
cmd/go: don't collect package CGOLDFLAGS when using gccgo
[thirdparty/gcc.git] / libgo / go / cmd / go / internal / work / gccgo.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package work
6
7 import (
8 "fmt"
9 exec "internal/execabs"
10 "os"
11 "path/filepath"
12 "strings"
13 "sync"
14
15 "cmd/go/internal/base"
16 "cmd/go/internal/cfg"
17 "cmd/go/internal/fsys"
18 "cmd/go/internal/load"
19 "cmd/go/internal/str"
20 "cmd/internal/pkgpath"
21 )
22
23 // The Gccgo toolchain.
24
25 type gccgoToolchain struct{}
26
27 var GccgoName, GccgoBin string
28 var gccgoErr error
29
30 func init() {
31 GccgoName = cfg.Getenv("GCCGO")
32 if GccgoName == "" {
33 GccgoName = cfg.DefaultGCCGO(cfg.Goos, cfg.Goarch)
34 }
35 GccgoBin, gccgoErr = exec.LookPath(GccgoName)
36 }
37
38 func (gccgoToolchain) compiler() string {
39 checkGccgoBin()
40 return GccgoBin
41 }
42
43 func (gccgoToolchain) linker() string {
44 checkGccgoBin()
45 return GccgoBin
46 }
47
48 func (gccgoToolchain) ar() string {
49 ar := cfg.Getenv("AR")
50 if ar == "" {
51 ar = "ar"
52 }
53 return ar
54 }
55
56 func checkGccgoBin() {
57 if gccgoErr == nil {
58 return
59 }
60 fmt.Fprintf(os.Stderr, "cmd/go: gccgo: %s\n", gccgoErr)
61 base.SetExitStatus(2)
62 base.Exit()
63 }
64
65 func (tools gccgoToolchain) gc(b *Builder, a *Action, archive string, importcfg, embedcfg []byte, symabis string, asmhdr bool, gofiles []string) (ofile string, output []byte, err error) {
66 p := a.Package
67 objdir := a.Objdir
68 out := "_go_.o"
69 ofile = objdir + out
70 gcargs := []string{"-g"}
71 gcargs = append(gcargs, b.gccArchArgs()...)
72 gcargs = append(gcargs, "-fdebug-prefix-map="+b.WorkDir+"=/tmp/go-build")
73 gcargs = append(gcargs, "-gno-record-gcc-switches")
74 if pkgpath := gccgoPkgpath(p); pkgpath != "" {
75 gcargs = append(gcargs, "-fgo-pkgpath="+pkgpath)
76 }
77 if p.Internal.LocalPrefix != "" {
78 gcargs = append(gcargs, "-fgo-relative-import-path="+p.Internal.LocalPrefix)
79 }
80
81 args := str.StringList(tools.compiler(), "-c", "-O2", gcargs, "-o", ofile, forcedGccgoflags)
82 if importcfg != nil {
83 if b.gccSupportsFlag(args[:1], "-fgo-importcfg=/dev/null") {
84 if err := b.writeFile(objdir+"importcfg", importcfg); err != nil {
85 return "", nil, err
86 }
87 args = append(args, "-fgo-importcfg="+objdir+"importcfg")
88 } else {
89 root := objdir + "_importcfgroot_"
90 if err := buildImportcfgSymlinks(b, root, importcfg); err != nil {
91 return "", nil, err
92 }
93 args = append(args, "-I", root)
94 }
95 }
96 if embedcfg != nil && b.gccSupportsFlag(args[:1], "-fgo-embedcfg=/dev/null") {
97 if err := b.writeFile(objdir+"embedcfg", embedcfg); err != nil {
98 return "", nil, err
99 }
100 args = append(args, "-fgo-embedcfg="+objdir+"embedcfg")
101 }
102
103 if b.gccSupportsFlag(args[:1], "-ffile-prefix-map=a=b") {
104 if cfg.BuildTrimpath {
105 args = append(args, "-ffile-prefix-map="+base.Cwd()+"=.")
106 args = append(args, "-ffile-prefix-map="+b.WorkDir+"=/tmp/go-build")
107 }
108 if fsys.OverlayFile != "" {
109 for _, name := range gofiles {
110 absPath := mkAbs(p.Dir, name)
111 overlayPath, ok := fsys.OverlayPath(absPath)
112 if !ok {
113 continue
114 }
115 toPath := absPath
116 // gccgo only applies the last matching rule, so also handle the case where
117 // BuildTrimpath is true and the path is relative to base.Cwd().
118 if cfg.BuildTrimpath && str.HasFilePathPrefix(toPath, base.Cwd()) {
119 toPath = "." + toPath[len(base.Cwd()):]
120 }
121 args = append(args, "-ffile-prefix-map="+overlayPath+"="+toPath)
122 }
123 }
124 }
125
126 args = append(args, a.Package.Internal.Gccgoflags...)
127 for _, f := range gofiles {
128 f := mkAbs(p.Dir, f)
129 // Overlay files if necessary.
130 // See comment on gctoolchain.gc about overlay TODOs
131 f, _ = fsys.OverlayPath(f)
132 args = append(args, f)
133 }
134
135 output, err = b.runOut(a, p.Dir, nil, args)
136 return ofile, output, err
137 }
138
139 // buildImportcfgSymlinks builds in root a tree of symlinks
140 // implementing the directives from importcfg.
141 // This serves as a temporary transition mechanism until
142 // we can depend on gccgo reading an importcfg directly.
143 // (The Go 1.9 and later gc compilers already do.)
144 func buildImportcfgSymlinks(b *Builder, root string, importcfg []byte) error {
145 for lineNum, line := range strings.Split(string(importcfg), "\n") {
146 lineNum++ // 1-based
147 line = strings.TrimSpace(line)
148 if line == "" {
149 continue
150 }
151 if line == "" || strings.HasPrefix(line, "#") {
152 continue
153 }
154 var verb, args string
155 if i := strings.Index(line, " "); i < 0 {
156 verb = line
157 } else {
158 verb, args = line[:i], strings.TrimSpace(line[i+1:])
159 }
160 var before, after string
161 if i := strings.Index(args, "="); i >= 0 {
162 before, after = args[:i], args[i+1:]
163 }
164 switch verb {
165 default:
166 base.Fatalf("importcfg:%d: unknown directive %q", lineNum, verb)
167 case "packagefile":
168 if before == "" || after == "" {
169 return fmt.Errorf(`importcfg:%d: invalid packagefile: syntax is "packagefile path=filename": %s`, lineNum, line)
170 }
171 archive := gccgoArchive(root, before)
172 if err := b.Mkdir(filepath.Dir(archive)); err != nil {
173 return err
174 }
175 if err := b.Symlink(after, archive); err != nil {
176 return err
177 }
178 case "importmap":
179 if before == "" || after == "" {
180 return fmt.Errorf(`importcfg:%d: invalid importmap: syntax is "importmap old=new": %s`, lineNum, line)
181 }
182 beforeA := gccgoArchive(root, before)
183 afterA := gccgoArchive(root, after)
184 if err := b.Mkdir(filepath.Dir(beforeA)); err != nil {
185 return err
186 }
187 if err := b.Mkdir(filepath.Dir(afterA)); err != nil {
188 return err
189 }
190 if err := b.Symlink(afterA, beforeA); err != nil {
191 return err
192 }
193 case "packageshlib":
194 return fmt.Errorf("gccgo -importcfg does not support shared libraries")
195 }
196 }
197 return nil
198 }
199
200 func (tools gccgoToolchain) asm(b *Builder, a *Action, sfiles []string) ([]string, error) {
201 p := a.Package
202 var ofiles []string
203 for _, sfile := range sfiles {
204 base := filepath.Base(sfile)
205 ofile := a.Objdir + base[:len(base)-len(".s")] + ".o"
206 ofiles = append(ofiles, ofile)
207 sfile, _ = fsys.OverlayPath(mkAbs(p.Dir, sfile))
208 defs := []string{"-D", "GOOS_" + cfg.Goos, "-D", "GOARCH_" + cfg.Goarch}
209 if pkgpath := tools.gccgoCleanPkgpath(b, p); pkgpath != "" {
210 defs = append(defs, `-D`, `GOPKGPATH=`+pkgpath)
211 }
212 defs = tools.maybePIC(defs)
213 defs = append(defs, b.gccArchArgs()...)
214 err := b.run(a, p.Dir, p.ImportPath, nil, tools.compiler(), "-xassembler-with-cpp", "-I", a.Objdir, "-c", "-o", ofile, defs, sfile)
215 if err != nil {
216 return nil, err
217 }
218 }
219 return ofiles, nil
220 }
221
222 func (gccgoToolchain) symabis(b *Builder, a *Action, sfiles []string) (string, error) {
223 return "", nil
224 }
225
226 func gccgoArchive(basedir, imp string) string {
227 end := filepath.FromSlash(imp + ".a")
228 afile := filepath.Join(basedir, end)
229 // add "lib" to the final element
230 return filepath.Join(filepath.Dir(afile), "lib"+filepath.Base(afile))
231 }
232
233 func (tools gccgoToolchain) pack(b *Builder, a *Action, afile string, ofiles []string) error {
234 p := a.Package
235 objdir := a.Objdir
236 var absOfiles []string
237 for _, f := range ofiles {
238 absOfiles = append(absOfiles, mkAbs(objdir, f))
239 }
240 var arArgs []string
241 if cfg.Goos == "aix" && cfg.Goarch == "ppc64" {
242 // AIX puts both 32-bit and 64-bit objects in the same archive.
243 // Tell the AIX "ar" command to only care about 64-bit objects.
244 arArgs = []string{"-X64"}
245 }
246 absAfile := mkAbs(objdir, afile)
247 // Try with D modifier first, then without if that fails.
248 output, err := b.runOut(a, p.Dir, nil, tools.ar(), arArgs, "rcD", absAfile, absOfiles)
249 if err != nil {
250 return b.run(a, p.Dir, p.ImportPath, nil, tools.ar(), arArgs, "rc", absAfile, absOfiles)
251 }
252
253 if len(output) > 0 {
254 // Show the output if there is any even without errors.
255 b.showOutput(a, p.Dir, p.ImportPath, b.processOutput(output))
256 }
257
258 return nil
259 }
260
261 func (tools gccgoToolchain) link(b *Builder, root *Action, out, importcfg string, allactions []*Action, buildmode, desc string) error {
262 // gccgo needs explicit linking with all package dependencies,
263 // and all LDFLAGS from cgo dependencies.
264 afiles := []string{}
265 shlibs := []string{}
266 ldflags := b.gccArchArgs()
267 cgoldflags := []string{}
268 usesCgo := false
269 cxx := false
270 objc := false
271 fortran := false
272 if root.Package != nil {
273 cxx = len(root.Package.CXXFiles) > 0 || len(root.Package.SwigCXXFiles) > 0
274 objc = len(root.Package.MFiles) > 0
275 fortran = len(root.Package.FFiles) > 0
276 }
277
278 readCgoFlags := func(flagsFile string) error {
279 flags, err := os.ReadFile(flagsFile)
280 if err != nil {
281 return err
282 }
283 const ldflagsPrefix = "_CGO_LDFLAGS="
284 for _, line := range strings.Split(string(flags), "\n") {
285 if strings.HasPrefix(line, ldflagsPrefix) {
286 line = line[len(ldflagsPrefix):]
287 quote := byte(0)
288 start := true
289 var nl []byte
290 for len(line) > 0 {
291 b := line[0]
292 line = line[1:]
293 if quote == 0 && (b == ' ' || b == '\t') {
294 if len(nl) > 0 {
295 cgoldflags = append(cgoldflags, string(nl))
296 nl = nil
297 }
298 start = true
299 continue
300 } else if b == '"' || b == '\'' {
301 quote = b
302 } else if b == quote {
303 quote = 0
304 } else if quote == 0 && start && b == '-' && strings.HasPrefix(line, "g") {
305 line = line[1:]
306 continue
307 } else if quote == 0 && start && b == '-' && strings.HasPrefix(line, "O") {
308 for len(line) > 0 && line[0] != ' ' && line[0] != '\t' {
309 line = line[1:]
310 }
311 continue
312 }
313 nl = append(nl, b)
314 start = false
315 }
316 if len(nl) > 0 {
317 cgoldflags = append(cgoldflags, string(nl))
318 }
319 }
320 }
321 return nil
322 }
323
324 var arArgs []string
325 if cfg.Goos == "aix" && cfg.Goarch == "ppc64" {
326 // AIX puts both 32-bit and 64-bit objects in the same archive.
327 // Tell the AIX "ar" command to only care about 64-bit objects.
328 arArgs = []string{"-X64"}
329 }
330
331 newID := 0
332 readAndRemoveCgoFlags := func(archive string) (string, error) {
333 newID++
334 newArchive := root.Objdir + fmt.Sprintf("_pkg%d_.a", newID)
335 if err := b.copyFile(newArchive, archive, 0666, false); err != nil {
336 return "", err
337 }
338 if cfg.BuildN {
339 // TODO(rsc): We could do better about showing the right _cgo_flags even in -n mode.
340 // Either the archive is already built and we can read them out,
341 // or we're printing commands to build the archive and can
342 // forward the _cgo_flags directly to this step.
343 b.Showcmd("", "ar d %s _cgo_flags", newArchive)
344 return "", nil
345 }
346 err := b.run(root, root.Objdir, desc, nil, tools.ar(), arArgs, "x", newArchive, "_cgo_flags")
347 if err != nil {
348 return "", err
349 }
350 err = b.run(root, ".", desc, nil, tools.ar(), arArgs, "d", newArchive, "_cgo_flags")
351 if err != nil {
352 return "", err
353 }
354 err = readCgoFlags(filepath.Join(root.Objdir, "_cgo_flags"))
355 if err != nil {
356 return "", err
357 }
358 return newArchive, nil
359 }
360
361 // If using -linkshared, find the shared library deps.
362 haveShlib := make(map[string]bool)
363 targetBase := filepath.Base(root.Target)
364 if cfg.BuildLinkshared {
365 for _, a := range root.Deps {
366 p := a.Package
367 if p == nil || p.Shlib == "" {
368 continue
369 }
370
371 // The .a we are linking into this .so
372 // will have its Shlib set to this .so.
373 // Don't start thinking we want to link
374 // this .so into itself.
375 base := filepath.Base(p.Shlib)
376 if base != targetBase {
377 haveShlib[base] = true
378 }
379 }
380 }
381
382 // Arrange the deps into afiles and shlibs.
383 addedShlib := make(map[string]bool)
384 for _, a := range root.Deps {
385 p := a.Package
386 if p != nil && p.Shlib != "" && haveShlib[filepath.Base(p.Shlib)] {
387 // This is a package linked into a shared
388 // library that we will put into shlibs.
389 continue
390 }
391
392 if haveShlib[filepath.Base(a.Target)] {
393 // This is a shared library we want to link against.
394 if !addedShlib[a.Target] {
395 shlibs = append(shlibs, a.Target)
396 addedShlib[a.Target] = true
397 }
398 continue
399 }
400
401 if p != nil {
402 target := a.built
403 if p.UsesCgo() || p.UsesSwig() {
404 var err error
405 target, err = readAndRemoveCgoFlags(target)
406 if err != nil {
407 continue
408 }
409 }
410
411 afiles = append(afiles, target)
412 }
413 }
414
415 for _, a := range allactions {
416 if a.Package == nil {
417 continue
418 }
419 if len(a.Package.CgoFiles) > 0 {
420 usesCgo = true
421 }
422 if a.Package.UsesSwig() {
423 usesCgo = true
424 }
425 if len(a.Package.CXXFiles) > 0 || len(a.Package.SwigCXXFiles) > 0 {
426 cxx = true
427 }
428 if len(a.Package.MFiles) > 0 {
429 objc = true
430 }
431 if len(a.Package.FFiles) > 0 {
432 fortran = true
433 }
434 }
435
436 wholeArchive := []string{"-Wl,--whole-archive"}
437 noWholeArchive := []string{"-Wl,--no-whole-archive"}
438 if cfg.Goos == "aix" {
439 wholeArchive = nil
440 noWholeArchive = nil
441 }
442 ldflags = append(ldflags, wholeArchive...)
443 ldflags = append(ldflags, afiles...)
444 ldflags = append(ldflags, noWholeArchive...)
445
446 ldflags = append(ldflags, cgoldflags...)
447 ldflags = append(ldflags, envList("CGO_LDFLAGS", "")...)
448 if cfg.Goos != "aix" {
449 ldflags = str.StringList("-Wl,-(", ldflags, "-Wl,-)")
450 }
451
452 if root.buildID != "" {
453 // On systems that normally use gold or the GNU linker,
454 // use the --build-id option to write a GNU build ID note.
455 switch cfg.Goos {
456 case "android", "dragonfly", "linux", "netbsd":
457 ldflags = append(ldflags, fmt.Sprintf("-Wl,--build-id=0x%x", root.buildID))
458 }
459 }
460
461 var rLibPath string
462 if cfg.Goos == "aix" {
463 rLibPath = "-Wl,-blibpath="
464 } else {
465 rLibPath = "-Wl,-rpath="
466 }
467 for _, shlib := range shlibs {
468 ldflags = append(
469 ldflags,
470 "-L"+filepath.Dir(shlib),
471 rLibPath+filepath.Dir(shlib),
472 "-l"+strings.TrimSuffix(
473 strings.TrimPrefix(filepath.Base(shlib), "lib"),
474 ".so"))
475 }
476
477 var realOut string
478 goLibBegin := str.StringList(wholeArchive, "-lgolibbegin", noWholeArchive)
479 switch buildmode {
480 case "exe":
481 if usesCgo && cfg.Goos == "linux" {
482 ldflags = append(ldflags, "-Wl,-E")
483 }
484
485 case "c-archive":
486 // Link the Go files into a single .o, and also link
487 // in -lgolibbegin.
488 //
489 // We need to use --whole-archive with -lgolibbegin
490 // because it doesn't define any symbols that will
491 // cause the contents to be pulled in; it's just
492 // initialization code.
493 //
494 // The user remains responsible for linking against
495 // -lgo -lpthread -lm in the final link. We can't use
496 // -r to pick them up because we can't combine
497 // split-stack and non-split-stack code in a single -r
498 // link, and libgo picks up non-split-stack code from
499 // libffi.
500 ldflags = append(ldflags, "-Wl,-r", "-nostdlib")
501 ldflags = append(ldflags, goLibBegin...)
502
503 if nopie := b.gccNoPie([]string{tools.linker()}); nopie != "" {
504 ldflags = append(ldflags, nopie)
505 }
506
507 // We are creating an object file, so we don't want a build ID.
508 if root.buildID == "" {
509 ldflags = b.disableBuildID(ldflags)
510 }
511
512 realOut = out
513 out = out + ".o"
514
515 case "c-shared":
516 ldflags = append(ldflags, "-shared", "-nostdlib")
517 ldflags = append(ldflags, goLibBegin...)
518 ldflags = append(ldflags, "-lgo", "-lgcc_s", "-lgcc", "-lc", "-lgcc")
519
520 case "shared":
521 if cfg.Goos != "aix" {
522 ldflags = append(ldflags, "-zdefs")
523 }
524 ldflags = append(ldflags, "-shared", "-nostdlib", "-lgo", "-lgcc_s", "-lgcc", "-lc")
525
526 case "pie":
527 ldflags = append(ldflags, "-pie")
528
529 default:
530 base.Fatalf("-buildmode=%s not supported for gccgo", buildmode)
531 }
532
533 switch buildmode {
534 case "exe", "c-shared":
535 if cxx {
536 ldflags = append(ldflags, "-lstdc++")
537 }
538 if objc {
539 ldflags = append(ldflags, "-lobjc")
540 }
541 if fortran {
542 fc := cfg.Getenv("FC")
543 if fc == "" {
544 fc = "gfortran"
545 }
546 // support gfortran out of the box and let others pass the correct link options
547 // via CGO_LDFLAGS
548 if strings.Contains(fc, "gfortran") {
549 ldflags = append(ldflags, "-lgfortran")
550 }
551 }
552 }
553
554 if err := b.run(root, ".", desc, nil, tools.linker(), "-o", out, ldflags, forcedGccgoflags, root.Package.Internal.Gccgoflags); err != nil {
555 return err
556 }
557
558 switch buildmode {
559 case "c-archive":
560 if err := b.run(root, ".", desc, nil, tools.ar(), arArgs, "rc", realOut, out); err != nil {
561 return err
562 }
563 }
564 return nil
565 }
566
567 func (tools gccgoToolchain) ld(b *Builder, root *Action, out, importcfg, mainpkg string) error {
568 return tools.link(b, root, out, importcfg, root.Deps, ldBuildmode, root.Package.ImportPath)
569 }
570
571 func (tools gccgoToolchain) ldShared(b *Builder, root *Action, toplevelactions []*Action, out, importcfg string, allactions []*Action) error {
572 return tools.link(b, root, out, importcfg, allactions, "shared", out)
573 }
574
575 func (tools gccgoToolchain) cc(b *Builder, a *Action, ofile, cfile string) error {
576 p := a.Package
577 inc := filepath.Join(cfg.GOROOT, "pkg", "include")
578 cfile = mkAbs(p.Dir, cfile)
579 defs := []string{"-D", "GOOS_" + cfg.Goos, "-D", "GOARCH_" + cfg.Goarch}
580 defs = append(defs, b.gccArchArgs()...)
581 if pkgpath := tools.gccgoCleanPkgpath(b, p); pkgpath != "" {
582 defs = append(defs, `-D`, `GOPKGPATH="`+pkgpath+`"`)
583 }
584 compiler := envList("CC", cfg.DefaultCC(cfg.Goos, cfg.Goarch))
585 if b.gccSupportsFlag(compiler, "-fsplit-stack") {
586 defs = append(defs, "-fsplit-stack")
587 }
588 defs = tools.maybePIC(defs)
589 if b.gccSupportsFlag(compiler, "-ffile-prefix-map=a=b") {
590 defs = append(defs, "-ffile-prefix-map="+base.Cwd()+"=.")
591 defs = append(defs, "-ffile-prefix-map="+b.WorkDir+"=/tmp/go-build")
592 } else if b.gccSupportsFlag(compiler, "-fdebug-prefix-map=a=b") {
593 defs = append(defs, "-fdebug-prefix-map="+b.WorkDir+"=/tmp/go-build")
594 }
595 if b.gccSupportsFlag(compiler, "-gno-record-gcc-switches") {
596 defs = append(defs, "-gno-record-gcc-switches")
597 }
598 return b.run(a, p.Dir, p.ImportPath, nil, compiler, "-Wall", "-g",
599 "-I", a.Objdir, "-I", inc, "-o", ofile, defs, "-c", cfile)
600 }
601
602 // maybePIC adds -fPIC to the list of arguments if needed.
603 func (tools gccgoToolchain) maybePIC(args []string) []string {
604 switch cfg.BuildBuildmode {
605 case "c-archive", "c-shared", "shared", "plugin":
606 args = append(args, "-fPIC")
607 }
608 return args
609 }
610
611 func gccgoPkgpath(p *load.Package) string {
612 if p.Internal.Build.IsCommand() && !p.Internal.ForceLibrary {
613 return ""
614 }
615 if p.ImportPath == "main" && p.Internal.ForceLibrary {
616 return "testmain"
617 }
618 return p.ImportPath
619 }
620
621 var gccgoToSymbolFuncOnce sync.Once
622 var gccgoToSymbolFunc func(string) string
623
624 func (tools gccgoToolchain) gccgoCleanPkgpath(b *Builder, p *load.Package) string {
625 gccgoToSymbolFuncOnce.Do(func() {
626 if cfg.BuildN {
627 gccgoToSymbolFunc = func(s string) string { return s }
628 return
629 }
630 fn, err := pkgpath.ToSymbolFunc(tools.compiler(), b.WorkDir)
631 if err != nil {
632 fmt.Fprintf(os.Stderr, "cmd/go: %v\n", err)
633 base.SetExitStatus(2)
634 base.Exit()
635 }
636 gccgoToSymbolFunc = fn
637 })
638
639 return gccgoToSymbolFunc(gccgoPkgpath(p))
640 }