github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/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 gmp.go for an overview.
     6  
     7  // TODO(rsc):
     8  //	Emit correct line number annotations.
     9  //	Make gc understand the annotations.
    10  
    11  package main
    12  
    13  import (
    14  	"crypto/md5"
    15  	"flag"
    16  	"fmt"
    17  	"go/ast"
    18  	"go/printer"
    19  	"go/token"
    20  	"io"
    21  	"os"
    22  	"path/filepath"
    23  	"reflect"
    24  	"runtime"
    25  	"sort"
    26  	"strings"
    27  )
    28  
    29  // A Package collects information about the package we're going to write.
    30  type Package struct {
    31  	PackageName string // name of package
    32  	PackagePath string
    33  	PtrSize     int64
    34  	IntSize     int64
    35  	GccOptions  []string
    36  	GccIsClang  bool
    37  	CgoFlags    map[string][]string // #cgo flags (CFLAGS, LDFLAGS)
    38  	Written     map[string]bool
    39  	Name        map[string]*Name // accumulated Name from Files
    40  	ExpFunc     []*ExpFunc       // accumulated ExpFunc from Files
    41  	Decl        []ast.Decl
    42  	GoFiles     []string // list of Go files
    43  	GccFiles    []string // list of gcc output files
    44  	Preamble    string   // collected preamble for _cgo_export.h
    45  	CgoChecks   []string // see unsafeCheckPointerName
    46  }
    47  
    48  // A File collects information about a single Go input file.
    49  type File struct {
    50  	AST      *ast.File           // parsed AST
    51  	Comments []*ast.CommentGroup // comments from file
    52  	Package  string              // Package name
    53  	Preamble string              // C preamble (doc comment on import "C")
    54  	Ref      []*Ref              // all references to C.xxx in AST
    55  	Calls    []*ast.CallExpr     // all calls to C.xxx in AST
    56  	ExpFunc  []*ExpFunc          // exported functions for this file
    57  	Name     map[string]*Name    // map from Go name to Name
    58  }
    59  
    60  func nameKeys(m map[string]*Name) []string {
    61  	var ks []string
    62  	for k := range m {
    63  		ks = append(ks, k)
    64  	}
    65  	sort.Strings(ks)
    66  	return ks
    67  }
    68  
    69  // A Ref refers to an expression of the form C.xxx in the AST.
    70  type Ref struct {
    71  	Name    *Name
    72  	Expr    *ast.Expr
    73  	Context string // "type", "expr", "call", or "call2"
    74  }
    75  
    76  func (r *Ref) Pos() token.Pos {
    77  	return (*r.Expr).Pos()
    78  }
    79  
    80  // A Name collects information about C.xxx.
    81  type Name struct {
    82  	Go       string // name used in Go referring to package C
    83  	Mangle   string // name used in generated Go
    84  	C        string // name used in C
    85  	Define   string // #define expansion
    86  	Kind     string // "const", "type", "var", "fpvar", "func", "not-type"
    87  	Type     *Type  // the type of xxx
    88  	FuncType *FuncType
    89  	AddError bool
    90  	Const    string // constant definition
    91  }
    92  
    93  // IsVar reports whether Kind is either "var" or "fpvar"
    94  func (n *Name) IsVar() bool {
    95  	return n.Kind == "var" || n.Kind == "fpvar"
    96  }
    97  
    98  // A ExpFunc is an exported function, callable from C.
    99  // Such functions are identified in the Go input file
   100  // by doc comments containing the line //export ExpName
   101  type ExpFunc struct {
   102  	Func    *ast.FuncDecl
   103  	ExpName string // name to use from C
   104  	Doc     string
   105  }
   106  
   107  // A TypeRepr contains the string representation of a type.
   108  type TypeRepr struct {
   109  	Repr       string
   110  	FormatArgs []interface{}
   111  }
   112  
   113  // A Type collects information about a type in both the C and Go worlds.
   114  type Type struct {
   115  	Size       int64
   116  	Align      int64
   117  	C          *TypeRepr
   118  	Go         ast.Expr
   119  	EnumValues map[string]int64
   120  	Typedef    string
   121  }
   122  
   123  // A FuncType collects information about a function type in both the C and Go worlds.
   124  type FuncType struct {
   125  	Params []*Type
   126  	Result *Type
   127  	Go     *ast.FuncType
   128  }
   129  
   130  func usage() {
   131  	fmt.Fprint(os.Stderr, "usage: cgo -- [compiler options] file.go ...\n")
   132  	flag.PrintDefaults()
   133  	os.Exit(2)
   134  }
   135  
   136  var ptrSizeMap = map[string]int64{
   137  	"386":      4,
   138  	"amd64":    8,
   139  	"arm":      4,
   140  	"arm64":    8,
   141  	"mips64":   8,
   142  	"mips64le": 8,
   143  	"ppc64":    8,
   144  	"ppc64le":  8,
   145  	"s390":     4,
   146  	"s390x":    8,
   147  }
   148  
   149  var intSizeMap = map[string]int64{
   150  	"386":      4,
   151  	"amd64":    8,
   152  	"arm":      4,
   153  	"arm64":    8,
   154  	"mips64":   8,
   155  	"mips64le": 8,
   156  	"ppc64":    8,
   157  	"ppc64le":  8,
   158  	"s390":     4,
   159  	"s390x":    8,
   160  }
   161  
   162  var cPrefix string
   163  
   164  var fset = token.NewFileSet()
   165  
   166  var dynobj = flag.String("dynimport", "", "if non-empty, print dynamic import data for that file")
   167  var dynout = flag.String("dynout", "", "write -dynimport output to this file")
   168  var dynpackage = flag.String("dynpackage", "main", "set Go package for -dynimport output")
   169  var dynlinker = flag.Bool("dynlinker", false, "record dynamic linker information in -dynimport mode")
   170  
   171  // This flag is for bootstrapping a new Go implementation,
   172  // to generate Go types that match the data layout and
   173  // constant values used in the host's C libraries and system calls.
   174  var godefs = flag.Bool("godefs", false, "for bootstrap: write Go definitions for C file to standard output")
   175  
   176  var objDir = flag.String("objdir", "", "object directory")
   177  var importPath = flag.String("importpath", "", "import path of package being built (for comments in generated files)")
   178  var exportHeader = flag.String("exportheader", "", "where to write export header if any exported functions")
   179  
   180  var gccgo = flag.Bool("gccgo", false, "generate files for use with gccgo")
   181  var gccgoprefix = flag.String("gccgoprefix", "", "-fgo-prefix option used with gccgo")
   182  var gccgopkgpath = flag.String("gccgopkgpath", "", "-fgo-pkgpath option used with gccgo")
   183  var importRuntimeCgo = flag.Bool("import_runtime_cgo", true, "import runtime/cgo in generated code")
   184  var importSyscall = flag.Bool("import_syscall", true, "import syscall in generated code")
   185  var goarch, goos string
   186  
   187  func main() {
   188  	flag.Usage = usage
   189  	flag.Parse()
   190  
   191  	if *dynobj != "" {
   192  		// cgo -dynimport is essentially a separate helper command
   193  		// built into the cgo binary. It scans a gcc-produced executable
   194  		// and dumps information about the imported symbols and the
   195  		// imported libraries. The 'go build' rules for cgo prepare an
   196  		// appropriate executable and then use its import information
   197  		// instead of needing to make the linkers duplicate all the
   198  		// specialized knowledge gcc has about where to look for imported
   199  		// symbols and which ones to use.
   200  		dynimport(*dynobj)
   201  		return
   202  	}
   203  
   204  	if *godefs {
   205  		// Generating definitions pulled from header files,
   206  		// to be checked into Go repositories.
   207  		// Line numbers are just noise.
   208  		conf.Mode &^= printer.SourcePos
   209  	}
   210  
   211  	args := flag.Args()
   212  	if len(args) < 1 {
   213  		usage()
   214  	}
   215  
   216  	// Find first arg that looks like a go file and assume everything before
   217  	// that are options to pass to gcc.
   218  	var i int
   219  	for i = len(args); i > 0; i-- {
   220  		if !strings.HasSuffix(args[i-1], ".go") {
   221  			break
   222  		}
   223  	}
   224  	if i == len(args) {
   225  		usage()
   226  	}
   227  
   228  	goFiles := args[i:]
   229  
   230  	for _, arg := range args[:i] {
   231  		if arg == "-fsanitize=thread" {
   232  			tsanProlog = yesTsanProlog
   233  		}
   234  	}
   235  
   236  	p := newPackage(args[:i])
   237  
   238  	// Record CGO_LDFLAGS from the environment for external linking.
   239  	if ldflags := os.Getenv("CGO_LDFLAGS"); ldflags != "" {
   240  		args, err := splitQuoted(ldflags)
   241  		if err != nil {
   242  			fatalf("bad CGO_LDFLAGS: %q (%s)", ldflags, err)
   243  		}
   244  		p.addToFlag("LDFLAGS", args)
   245  	}
   246  
   247  	// Need a unique prefix for the global C symbols that
   248  	// we use to coordinate between gcc and ourselves.
   249  	// We already put _cgo_ at the beginning, so the main
   250  	// concern is other cgo wrappers for the same functions.
   251  	// Use the beginning of the md5 of the input to disambiguate.
   252  	h := md5.New()
   253  	for _, input := range goFiles {
   254  		f, err := os.Open(input)
   255  		if err != nil {
   256  			fatalf("%s", err)
   257  		}
   258  		io.Copy(h, f)
   259  		f.Close()
   260  	}
   261  	cPrefix = fmt.Sprintf("_%x", h.Sum(nil)[0:6])
   262  
   263  	fs := make([]*File, len(goFiles))
   264  	for i, input := range goFiles {
   265  		f := new(File)
   266  		f.ReadGo(input)
   267  		f.DiscardCgoDirectives()
   268  		fs[i] = f
   269  	}
   270  
   271  	if *objDir == "" {
   272  		// make sure that _obj directory exists, so that we can write
   273  		// all the output files there.
   274  		os.Mkdir("_obj", 0777)
   275  		*objDir = "_obj"
   276  	}
   277  	*objDir += string(filepath.Separator)
   278  
   279  	for i, input := range goFiles {
   280  		f := fs[i]
   281  		p.Translate(f)
   282  		for _, cref := range f.Ref {
   283  			switch cref.Context {
   284  			case "call", "call2":
   285  				if cref.Name.Kind != "type" {
   286  					break
   287  				}
   288  				*cref.Expr = cref.Name.Type.Go
   289  			}
   290  		}
   291  		if nerrors > 0 {
   292  			os.Exit(2)
   293  		}
   294  		p.PackagePath = f.Package
   295  		p.Record(f)
   296  		if *godefs {
   297  			os.Stdout.WriteString(p.godefs(f, input))
   298  		} else {
   299  			p.writeOutput(f, input)
   300  		}
   301  	}
   302  
   303  	if !*godefs {
   304  		p.writeDefs()
   305  	}
   306  	if nerrors > 0 {
   307  		os.Exit(2)
   308  	}
   309  }
   310  
   311  // newPackage returns a new Package that will invoke
   312  // gcc with the additional arguments specified in args.
   313  func newPackage(args []string) *Package {
   314  	goarch = runtime.GOARCH
   315  	if s := os.Getenv("GOARCH"); s != "" {
   316  		goarch = s
   317  	}
   318  	goos = runtime.GOOS
   319  	if s := os.Getenv("GOOS"); s != "" {
   320  		goos = s
   321  	}
   322  	ptrSize := ptrSizeMap[goarch]
   323  	if ptrSize == 0 {
   324  		fatalf("unknown ptrSize for $GOARCH %q", goarch)
   325  	}
   326  	intSize := intSizeMap[goarch]
   327  	if intSize == 0 {
   328  		fatalf("unknown intSize for $GOARCH %q", goarch)
   329  	}
   330  
   331  	// Reset locale variables so gcc emits English errors [sic].
   332  	os.Setenv("LANG", "en_US.UTF-8")
   333  	os.Setenv("LC_ALL", "C")
   334  
   335  	p := &Package{
   336  		PtrSize:  ptrSize,
   337  		IntSize:  intSize,
   338  		CgoFlags: make(map[string][]string),
   339  		Written:  make(map[string]bool),
   340  	}
   341  	p.addToFlag("CFLAGS", args)
   342  	return p
   343  }
   344  
   345  // Record what needs to be recorded about f.
   346  func (p *Package) Record(f *File) {
   347  	if p.PackageName == "" {
   348  		p.PackageName = f.Package
   349  	} else if p.PackageName != f.Package {
   350  		error_(token.NoPos, "inconsistent package names: %s, %s", p.PackageName, f.Package)
   351  	}
   352  
   353  	if p.Name == nil {
   354  		p.Name = f.Name
   355  	} else {
   356  		for k, v := range f.Name {
   357  			if p.Name[k] == nil {
   358  				p.Name[k] = v
   359  			} else if !reflect.DeepEqual(p.Name[k], v) {
   360  				error_(token.NoPos, "inconsistent definitions for C.%s", fixGo(k))
   361  			}
   362  		}
   363  	}
   364  
   365  	if f.ExpFunc != nil {
   366  		p.ExpFunc = append(p.ExpFunc, f.ExpFunc...)
   367  		p.Preamble += "\n" + f.Preamble
   368  	}
   369  	p.Decl = append(p.Decl, f.AST.Decls...)
   370  }