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