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