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