github.com/miolini/go@v0.0.0-20160405192216-fca68c8cb408/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":    4,
   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  	p := newPackage(args[:i])
   231  
   232  	// Record CGO_LDFLAGS from the environment for external linking.
   233  	if ldflags := os.Getenv("CGO_LDFLAGS"); ldflags != "" {
   234  		args, err := splitQuoted(ldflags)
   235  		if err != nil {
   236  			fatalf("bad CGO_LDFLAGS: %q (%s)", ldflags, err)
   237  		}
   238  		p.addToFlag("LDFLAGS", args)
   239  	}
   240  
   241  	// Need a unique prefix for the global C symbols that
   242  	// we use to coordinate between gcc and ourselves.
   243  	// We already put _cgo_ at the beginning, so the main
   244  	// concern is other cgo wrappers for the same functions.
   245  	// Use the beginning of the md5 of the input to disambiguate.
   246  	h := md5.New()
   247  	for _, input := range goFiles {
   248  		f, err := os.Open(input)
   249  		if err != nil {
   250  			fatalf("%s", err)
   251  		}
   252  		io.Copy(h, f)
   253  		f.Close()
   254  	}
   255  	cPrefix = fmt.Sprintf("_%x", h.Sum(nil)[0:6])
   256  
   257  	fs := make([]*File, len(goFiles))
   258  	for i, input := range goFiles {
   259  		f := new(File)
   260  		f.ReadGo(input)
   261  		f.DiscardCgoDirectives()
   262  		fs[i] = f
   263  	}
   264  
   265  	if *objDir == "" {
   266  		// make sure that _obj directory exists, so that we can write
   267  		// all the output files there.
   268  		os.Mkdir("_obj", 0777)
   269  		*objDir = "_obj"
   270  	}
   271  	*objDir += string(filepath.Separator)
   272  
   273  	for i, input := range goFiles {
   274  		f := fs[i]
   275  		p.Translate(f)
   276  		for _, cref := range f.Ref {
   277  			switch cref.Context {
   278  			case "call", "call2":
   279  				if cref.Name.Kind != "type" {
   280  					break
   281  				}
   282  				*cref.Expr = cref.Name.Type.Go
   283  			}
   284  		}
   285  		if nerrors > 0 {
   286  			os.Exit(2)
   287  		}
   288  		p.PackagePath = f.Package
   289  		p.Record(f)
   290  		if *godefs {
   291  			os.Stdout.WriteString(p.godefs(f, input))
   292  		} else {
   293  			p.writeOutput(f, input)
   294  		}
   295  	}
   296  
   297  	if !*godefs {
   298  		p.writeDefs()
   299  	}
   300  	if nerrors > 0 {
   301  		os.Exit(2)
   302  	}
   303  }
   304  
   305  // newPackage returns a new Package that will invoke
   306  // gcc with the additional arguments specified in args.
   307  func newPackage(args []string) *Package {
   308  	goarch = runtime.GOARCH
   309  	if s := os.Getenv("GOARCH"); s != "" {
   310  		goarch = s
   311  	}
   312  	goos = runtime.GOOS
   313  	if s := os.Getenv("GOOS"); s != "" {
   314  		goos = s
   315  	}
   316  	ptrSize := ptrSizeMap[goarch]
   317  	if ptrSize == 0 {
   318  		fatalf("unknown ptrSize for $GOARCH %q", goarch)
   319  	}
   320  	intSize := intSizeMap[goarch]
   321  	if intSize == 0 {
   322  		fatalf("unknown intSize for $GOARCH %q", goarch)
   323  	}
   324  
   325  	// Reset locale variables so gcc emits English errors [sic].
   326  	os.Setenv("LANG", "en_US.UTF-8")
   327  	os.Setenv("LC_ALL", "C")
   328  
   329  	p := &Package{
   330  		PtrSize:  ptrSize,
   331  		IntSize:  intSize,
   332  		CgoFlags: make(map[string][]string),
   333  		Written:  make(map[string]bool),
   334  	}
   335  	p.addToFlag("CFLAGS", args)
   336  	return p
   337  }
   338  
   339  // Record what needs to be recorded about f.
   340  func (p *Package) Record(f *File) {
   341  	if p.PackageName == "" {
   342  		p.PackageName = f.Package
   343  	} else if p.PackageName != f.Package {
   344  		error_(token.NoPos, "inconsistent package names: %s, %s", p.PackageName, f.Package)
   345  	}
   346  
   347  	if p.Name == nil {
   348  		p.Name = f.Name
   349  	} else {
   350  		for k, v := range f.Name {
   351  			if p.Name[k] == nil {
   352  				p.Name[k] = v
   353  			} else if !reflect.DeepEqual(p.Name[k], v) {
   354  				error_(token.NoPos, "inconsistent definitions for C.%s", fixGo(k))
   355  			}
   356  		}
   357  	}
   358  
   359  	if f.ExpFunc != nil {
   360  		p.ExpFunc = append(p.ExpFunc, f.ExpFunc...)
   361  		p.Preamble += "\n" + f.Preamble
   362  	}
   363  	p.Decl = append(p.Decl, f.AST.Decls...)
   364  }