github.com/bir3/gocompiler@v0.3.205/src/cmd/cgo/main.go (about)

     1  // Copyright 2009 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  // Cgo; see doc.go for an overview.
     6  
     7  // TODO(rsc):
     8  //	Emit correct line number annotations.
     9  //	Make gc understand the annotations.
    10  
    11  package cgo
    12  
    13  import (
    14  	"github.com/bir3/gocompiler/src/cmd/cgo/flag"
    15  	"fmt"
    16  	"github.com/bir3/gocompiler/src/go/ast"
    17  	"github.com/bir3/gocompiler/src/go/printer"
    18  	"github.com/bir3/gocompiler/src/go/token"
    19  	"github.com/bir3/gocompiler/src/internal/buildcfg"
    20  	"io"
    21  	"os"
    22  	"path/filepath"
    23  	"reflect"
    24  	"runtime"
    25  	"sort"
    26  	"strings"
    27  
    28  	"github.com/bir3/gocompiler/src/cmd/internal/edit"
    29  	"github.com/bir3/gocompiler/src/cmd/internal/notsha256"
    30  	"github.com/bir3/gocompiler/src/cmd/internal/objabi"
    31  	"github.com/bir3/gocompiler/src/cmd/cgo/flag_objabi"
    32  )
    33  
    34  // A Package collects information about the package we're going to write.
    35  type Package struct {
    36  	PackageName string // name of package
    37  	PackagePath string
    38  	PtrSize     int64
    39  	IntSize     int64
    40  	GccOptions  []string
    41  	GccIsClang  bool
    42  	CgoFlags    map[string][]string // #cgo flags (CFLAGS, LDFLAGS)
    43  	Written     map[string]bool
    44  	Name        map[string]*Name // accumulated Name from Files
    45  	ExpFunc     []*ExpFunc       // accumulated ExpFunc from Files
    46  	Decl        []ast.Decl
    47  	GoFiles     []string        // list of Go files
    48  	GccFiles    []string        // list of gcc output files
    49  	Preamble    string          // collected preamble for _cgo_export.h
    50  	typedefs    map[string]bool // type names that appear in the types of the objects we're interested in
    51  	typedefList []typedefInfo
    52  }
    53  
    54  // A typedefInfo is an element on Package.typedefList: a typedef name
    55  // and the position where it was required.
    56  type typedefInfo struct {
    57  	typedef string
    58  	pos     token.Pos
    59  }
    60  
    61  // A File collects information about a single Go input file.
    62  type File struct {
    63  	AST      *ast.File           // parsed AST
    64  	Comments []*ast.CommentGroup // comments from file
    65  	Package  string              // Package name
    66  	Preamble string              // C preamble (doc comment on import "C")
    67  	Ref      []*Ref              // all references to C.xxx in AST
    68  	Calls    []*Call             // all calls to C.xxx in AST
    69  	ExpFunc  []*ExpFunc          // exported functions for this file
    70  	Name     map[string]*Name    // map from Go name to Name
    71  	NamePos  map[*Name]token.Pos // map from Name to position of the first reference
    72  	Edit     *edit.Buffer
    73  }
    74  
    75  func (f *File) offset(p token.Pos) int {
    76  	return fset.Position(p).Offset
    77  }
    78  
    79  func nameKeys(m map[string]*Name) []string {
    80  	var ks []string
    81  	for k := range m {
    82  		ks = append(ks, k)
    83  	}
    84  	sort.Strings(ks)
    85  	return ks
    86  }
    87  
    88  // A Call refers to a call of a C.xxx function in the AST.
    89  type Call struct {
    90  	Call     *ast.CallExpr
    91  	Deferred bool
    92  	Done     bool
    93  }
    94  
    95  // A Ref refers to an expression of the form C.xxx in the AST.
    96  type Ref struct {
    97  	Name    *Name
    98  	Expr    *ast.Expr
    99  	Context astContext
   100  	Done    bool
   101  }
   102  
   103  func (r *Ref) Pos() token.Pos {
   104  	return (*r.Expr).Pos()
   105  }
   106  
   107  var nameKinds = []string{"iconst", "fconst", "sconst", "type", "var", "fpvar", "func", "macro", "not-type"}
   108  
   109  // A Name collects information about C.xxx.
   110  type Name struct {
   111  	Go       string // name used in Go referring to package C
   112  	Mangle   string // name used in generated Go
   113  	C        string // name used in C
   114  	Define   string // #define expansion
   115  	Kind     string // one of the nameKinds
   116  	Type     *Type  // the type of xxx
   117  	FuncType *FuncType
   118  	AddError bool
   119  	Const    string // constant definition
   120  }
   121  
   122  // IsVar reports whether Kind is either "var" or "fpvar"
   123  func (n *Name) IsVar() bool {
   124  	return n.Kind == "var" || n.Kind == "fpvar"
   125  }
   126  
   127  // IsConst reports whether Kind is either "iconst", "fconst" or "sconst"
   128  func (n *Name) IsConst() bool {
   129  	return strings.HasSuffix(n.Kind, "const")
   130  }
   131  
   132  // An ExpFunc is an exported function, callable from C.
   133  // Such functions are identified in the Go input file
   134  // by doc comments containing the line //export ExpName
   135  type ExpFunc struct {
   136  	Func    *ast.FuncDecl
   137  	ExpName string // name to use from C
   138  	Doc     string
   139  }
   140  
   141  // A TypeRepr contains the string representation of a type.
   142  type TypeRepr struct {
   143  	Repr       string
   144  	FormatArgs []interface{}
   145  }
   146  
   147  // A Type collects information about a type in both the C and Go worlds.
   148  type Type struct {
   149  	Size       int64
   150  	Align      int64
   151  	C          *TypeRepr
   152  	Go         ast.Expr
   153  	EnumValues map[string]int64
   154  	Typedef    string
   155  	BadPointer bool // this pointer type should be represented as a uintptr (deprecated)
   156  }
   157  
   158  // A FuncType collects information about a function type in both the C and Go worlds.
   159  type FuncType struct {
   160  	Params []*Type
   161  	Result *Type
   162  	Go     *ast.FuncType
   163  }
   164  
   165  func usage() {
   166  	fmt.Fprint(os.Stderr, "usage: cgo -- [compiler options] file.go ...\n")
   167  	flag.PrintDefaults()
   168  	os.Exit(2)
   169  }
   170  
   171  var ptrSizeMap = map[string]int64{
   172  	"386":      4,
   173  	"alpha":    8,
   174  	"amd64":    8,
   175  	"arm":      4,
   176  	"arm64":    8,
   177  	"loong64":  8,
   178  	"m68k":     4,
   179  	"mips":     4,
   180  	"mipsle":   4,
   181  	"mips64":   8,
   182  	"mips64le": 8,
   183  	"nios2":    4,
   184  	"ppc":      4,
   185  	"ppc64":    8,
   186  	"ppc64le":  8,
   187  	"riscv":    4,
   188  	"riscv64":  8,
   189  	"s390":     4,
   190  	"s390x":    8,
   191  	"sh":       4,
   192  	"shbe":     4,
   193  	"sparc":    4,
   194  	"sparc64":  8,
   195  }
   196  
   197  var intSizeMap = map[string]int64{
   198  	"386":      4,
   199  	"alpha":    8,
   200  	"amd64":    8,
   201  	"arm":      4,
   202  	"arm64":    8,
   203  	"loong64":  8,
   204  	"m68k":     4,
   205  	"mips":     4,
   206  	"mipsle":   4,
   207  	"mips64":   8,
   208  	"mips64le": 8,
   209  	"nios2":    4,
   210  	"ppc":      4,
   211  	"ppc64":    8,
   212  	"ppc64le":  8,
   213  	"riscv":    4,
   214  	"riscv64":  8,
   215  	"s390":     4,
   216  	"s390x":    8,
   217  	"sh":       4,
   218  	"shbe":     4,
   219  	"sparc":    4,
   220  	"sparc64":  8,
   221  }
   222  
   223  var cPrefix string
   224  
   225  var fset = token.NewFileSet()
   226  
   227  var dynobj = flag.String("dynimport", "", "if non-empty, print dynamic import data for that file")
   228  var dynout = flag.String("dynout", "", "write -dynimport output to this file")
   229  var dynpackage = flag.String("dynpackage", "main", "set Go package for -dynimport output")
   230  var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information in -dynimport mode")
   231  
   232  // This flag is for bootstrapping a new Go implementation,
   233  // to generate Go types that match the data layout and
   234  // constant values used in the host's C libraries and system calls.
   235  var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output")
   236  
   237  var srcDir = flag.String("srcdir", "", "source directory")
   238  var objDir = flag.String("objdir", "", "object directory")
   239  var importPath = flag.String("importpath", "", "import path of package being built (for comments in generated files)")
   240  var exportHeader = flag.String("exportheader", "", "where to write export header if any exported functions")
   241  
   242  var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo")
   243  var gccgoprefix = flag.String("gccgoprefix", "", "-fgo-prefix option used with gccgo")
   244  var gccgopkgpath = flag.String("gccgopkgpath", "", "-fgo-pkgpath option used with gccgo")
   245  var gccgoMangler func(string) string
   246  var gccgoDefineCgoIncomplete = flag.Bool("gccgo_define_cgoincomplete", false, "define cgo.Incomplete for older gccgo/GoLLVM")
   247  var importRuntimeCgo = flag.Bool("import_runtime_cgo", true, "import runtime/cgo in generated code")
   248  var importSyscall = flag.Bool("import_syscall", true, "import syscall in generated code")
   249  var trimpath = flag.String("trimpath", "", "applies supplied rewrites or trims prefixes to recorded source file paths")
   250  
   251  var goarch, goos, gomips, gomips64 string
   252  var gccBaseCmd []string
   253  
   254  func Main() {
   255  	flag_objabi.AddVersionFlag() // -V
   256  	flag_objabi.Flagparse(usage)
   257  
   258  	if *gccgoDefineCgoIncomplete {
   259  		if !*gccgo {
   260  			fmt.Fprintf(os.Stderr, "cgo: -gccgo_define_cgoincomplete without -gccgo\n")
   261  			os.Exit(2)
   262  		}
   263  		incomplete = "_cgopackage_Incomplete"
   264  	}
   265  
   266  	if *dynobj != "" {
   267  		// cgo -dynimport is essentially a separate helper command
   268  		// built into the cgo binary. It scans a gcc-produced executable
   269  		// and dumps information about the imported symbols and the
   270  		// imported libraries. The 'go build' rules for cgo prepare an
   271  		// appropriate executable and then use its import information
   272  		// instead of needing to make the linkers duplicate all the
   273  		// specialized knowledge gcc has about where to look for imported
   274  		// symbols and which ones to use.
   275  		dynimport(*dynobj)
   276  		return
   277  	}
   278  
   279  	if *godefs {
   280  		// Generating definitions pulled from header files,
   281  		// to be checked into Go repositories.
   282  		// Line numbers are just noise.
   283  		conf.Mode &^= printer.SourcePos
   284  	}
   285  
   286  	args := flag.Args()
   287  	if len(args) < 1 {
   288  		usage()
   289  	}
   290  
   291  	// Find first arg that looks like a go file and assume everything before
   292  	// that are options to pass to gcc.
   293  	var i int
   294  	for i = len(args); i > 0; i-- {
   295  		if !strings.HasSuffix(args[i-1], ".go") {
   296  			break
   297  		}
   298  	}
   299  	if i == len(args) {
   300  		usage()
   301  	}
   302  
   303  	// Save original command line arguments for the godefs generated comment. Relative file
   304  	// paths in os.Args will be rewritten to absolute file paths in the loop below.
   305  	osArgs := make([]string, len(os.Args))
   306  	copy(osArgs, os.Args[:])
   307  	goFiles := args[i:]
   308  
   309  	for _, arg := range args[:i] {
   310  		if arg == "-fsanitize=thread" {
   311  			tsanProlog = yesTsanProlog
   312  		}
   313  		if arg == "-fsanitize=memory" {
   314  			msanProlog = yesMsanProlog
   315  		}
   316  	}
   317  
   318  	p := newPackage(args[:i])
   319  
   320  	// We need a C compiler to be available. Check this.
   321  	var err error
   322  	gccBaseCmd, err = checkGCCBaseCmd()
   323  	if err != nil {
   324  		fatalf("%v", err)
   325  		os.Exit(2)
   326  	}
   327  
   328  	// Record CGO_LDFLAGS from the environment for external linking.
   329  	if ldflags := os.Getenv("CGO_LDFLAGS"); ldflags != "" {
   330  		args, err := splitQuoted(ldflags)
   331  		if err != nil {
   332  			fatalf("bad CGO_LDFLAGS: %q (%s)", ldflags, err)
   333  		}
   334  		p.addToFlag("LDFLAGS", args)
   335  	}
   336  
   337  	// Need a unique prefix for the global C symbols that
   338  	// we use to coordinate between gcc and ourselves.
   339  	// We already put _cgo_ at the beginning, so the main
   340  	// concern is other cgo wrappers for the same functions.
   341  	// Use the beginning of the notsha256 of the input to disambiguate.
   342  	h := notsha256.New()
   343  	io.WriteString(h, *importPath)
   344  	fs := make([]*File, len(goFiles))
   345  	for i, input := range goFiles {
   346  		if *srcDir != "" {
   347  			input = filepath.Join(*srcDir, input)
   348  		}
   349  
   350  		// Create absolute path for file, so that it will be used in error
   351  		// messages and recorded in debug line number information.
   352  		// This matches the rest of the toolchain. See golang.org/issue/5122.
   353  		if aname, err := filepath.Abs(input); err == nil {
   354  			input = aname
   355  		}
   356  
   357  		b, err := os.ReadFile(input)
   358  		if err != nil {
   359  			fatalf("%s", err)
   360  		}
   361  		if _, err = h.Write(b); err != nil {
   362  			fatalf("%s", err)
   363  		}
   364  
   365  		// Apply trimpath to the file path. The path won't be read from after this point.
   366  		input, _ = objabi.ApplyRewrites(input, *trimpath)
   367  		goFiles[i] = input
   368  
   369  		f := new(File)
   370  		f.Edit = edit.NewBuffer(b)
   371  		f.ParseGo(input, b)
   372  		f.DiscardCgoDirectives()
   373  		fs[i] = f
   374  	}
   375  
   376  	cPrefix = fmt.Sprintf("_%x", h.Sum(nil)[0:6])
   377  
   378  	if *objDir == "" {
   379  		// make sure that _obj directory exists, so that we can write
   380  		// all the output files there.
   381  		os.Mkdir("_obj", 0777)
   382  		*objDir = "_obj"
   383  	}
   384  	*objDir += string(filepath.Separator)
   385  
   386  	for i, input := range goFiles {
   387  		f := fs[i]
   388  		p.Translate(f)
   389  		for _, cref := range f.Ref {
   390  			switch cref.Context {
   391  			case ctxCall, ctxCall2:
   392  				if cref.Name.Kind != "type" {
   393  					break
   394  				}
   395  				old := *cref.Expr
   396  				*cref.Expr = cref.Name.Type.Go
   397  				f.Edit.Replace(f.offset(old.Pos()), f.offset(old.End()), gofmt(cref.Name.Type.Go))
   398  			}
   399  		}
   400  		if nerrors > 0 {
   401  			os.Exit(2)
   402  		}
   403  		p.PackagePath = f.Package
   404  		p.Record(f)
   405  		if *godefs {
   406  			os.Stdout.WriteString(p.godefs(f, osArgs))
   407  		} else {
   408  			p.writeOutput(f, input)
   409  		}
   410  	}
   411  
   412  	if !*godefs {
   413  		p.writeDefs()
   414  	}
   415  	if nerrors > 0 {
   416  		os.Exit(2)
   417  	}
   418  }
   419  
   420  // newPackage returns a new Package that will invoke
   421  // gcc with the additional arguments specified in args.
   422  func newPackage(args []string) *Package {
   423  	goarch = runtime.GOARCH
   424  	if s := os.Getenv("GOARCH"); s != "" {
   425  		goarch = s
   426  	}
   427  	goos = runtime.GOOS
   428  	if s := os.Getenv("GOOS"); s != "" {
   429  		goos = s
   430  	}
   431  	buildcfg.Check()
   432  	gomips = buildcfg.GOMIPS
   433  	gomips64 = buildcfg.GOMIPS64
   434  	ptrSize := ptrSizeMap[goarch]
   435  	if ptrSize == 0 {
   436  		fatalf("unknown ptrSize for $GOARCH %q", goarch)
   437  	}
   438  	intSize := intSizeMap[goarch]
   439  	if intSize == 0 {
   440  		fatalf("unknown intSize for $GOARCH %q", goarch)
   441  	}
   442  
   443  	// Reset locale variables so gcc emits English errors [sic].
   444  	os.Setenv("LANG", "en_US.UTF-8")
   445  	os.Setenv("LC_ALL", "C")
   446  
   447  	p := &Package{
   448  		PtrSize:  ptrSize,
   449  		IntSize:  intSize,
   450  		CgoFlags: make(map[string][]string),
   451  		Written:  make(map[string]bool),
   452  	}
   453  	p.addToFlag("CFLAGS", args)
   454  	return p
   455  }
   456  
   457  // Record what needs to be recorded about f.
   458  func (p *Package) Record(f *File) {
   459  	if p.PackageName == "" {
   460  		p.PackageName = f.Package
   461  	} else if p.PackageName != f.Package {
   462  		error_(token.NoPos, "inconsistent package names: %s, %s", p.PackageName, f.Package)
   463  	}
   464  
   465  	if p.Name == nil {
   466  		p.Name = f.Name
   467  	} else {
   468  		for k, v := range f.Name {
   469  			if p.Name[k] == nil {
   470  				p.Name[k] = v
   471  			} else if p.incompleteTypedef(p.Name[k].Type) {
   472  				p.Name[k] = v
   473  			} else if p.incompleteTypedef(v.Type) {
   474  				// Nothing to do.
   475  			} else if _, ok := nameToC[k]; ok {
   476  				// Names we predefine may appear inconsistent
   477  				// if some files typedef them and some don't.
   478  				// Issue 26743.
   479  			} else if !reflect.DeepEqual(p.Name[k], v) {
   480  				error_(token.NoPos, "inconsistent definitions for C.%s", fixGo(k))
   481  			}
   482  		}
   483  	}
   484  
   485  	if f.ExpFunc != nil {
   486  		p.ExpFunc = append(p.ExpFunc, f.ExpFunc...)
   487  		p.Preamble += "\n" + f.Preamble
   488  	}
   489  	p.Decl = append(p.Decl, f.AST.Decls...)
   490  }
   491  
   492  // incompleteTypedef reports whether t appears to be an incomplete
   493  // typedef definition.
   494  func (p *Package) incompleteTypedef(t *Type) bool {
   495  	return t == nil || (t.Size == 0 && t.Align == -1)
   496  }