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