Source file src/cmd/cgo/util.go

     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  package main
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"go/token"
    11  	"os"
    12  	"os/exec"
    13  	"slices"
    14  )
    15  
    16  // run runs the command argv, feeding in stdin on standard input.
    17  // It returns the output to standard output and standard error.
    18  // ok indicates whether the command exited successfully.
    19  func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
    20  	if i := slices.Index(argv, "-xc"); i >= 0 && argv[len(argv)-1] == "-" {
    21  		// Some compilers have trouble with standard input.
    22  		// Others have trouble with -xc.
    23  		// Avoid both problems by writing a file with a .c extension.
    24  		f, err := os.CreateTemp("", "cgo-gcc-input-")
    25  		if err != nil {
    26  			fatalf("%s", err)
    27  		}
    28  		name := f.Name()
    29  		f.Close()
    30  		if err := os.WriteFile(name+".c", stdin, 0666); err != nil {
    31  			os.Remove(name)
    32  			fatalf("%s", err)
    33  		}
    34  		defer os.Remove(name)
    35  		defer os.Remove(name + ".c")
    36  
    37  		// Build new argument list without -xc and trailing -.
    38  		new := append(argv[:i:i], argv[i+1:len(argv)-1]...)
    39  
    40  		// Since we are going to write the file to a temporary directory,
    41  		// we will need to add -I . explicitly to the command line:
    42  		// any #include "foo" before would have looked in the current
    43  		// directory as the directory "holding" standard input, but now
    44  		// the temporary directory holds the input.
    45  		// We've also run into compilers that reject "-I." but allow "-I", ".",
    46  		// so be sure to use two arguments.
    47  		// This matters mainly for people invoking cgo -godefs by hand.
    48  		new = append(new, "-I", ".")
    49  
    50  		// Finish argument list with path to C file.
    51  		new = append(new, name+".c")
    52  
    53  		argv = new
    54  		stdin = nil
    55  	}
    56  
    57  	p := exec.Command(argv[0], argv[1:]...)
    58  	p.Stdin = bytes.NewReader(stdin)
    59  	var bout, berr bytes.Buffer
    60  	p.Stdout = &bout
    61  	p.Stderr = &berr
    62  	// Disable escape codes in clang error messages.
    63  	p.Env = append(os.Environ(), "TERM=dumb")
    64  	err := p.Run()
    65  	if _, ok := err.(*exec.ExitError); err != nil && !ok {
    66  		fatalf("exec %s: %s", argv[0], err)
    67  	}
    68  	ok = p.ProcessState.Success()
    69  	stdout, stderr = bout.Bytes(), berr.Bytes()
    70  	return
    71  }
    72  
    73  func lineno(pos token.Pos) string {
    74  	return fset.Position(pos).String()
    75  }
    76  
    77  // Die with an error message.
    78  func fatalf(msg string, args ...interface{}) {
    79  	// If we've already printed other errors, they might have
    80  	// caused the fatal condition. Assume they're enough.
    81  	if nerrors == 0 {
    82  		fmt.Fprintf(os.Stderr, "cgo: "+msg+"\n", args...)
    83  	}
    84  	os.Exit(2)
    85  }
    86  
    87  var nerrors int
    88  
    89  func error_(pos token.Pos, msg string, args ...interface{}) {
    90  	nerrors++
    91  	if pos.IsValid() {
    92  		fmt.Fprintf(os.Stderr, "%s: ", fset.Position(pos).String())
    93  	} else {
    94  		fmt.Fprintf(os.Stderr, "cgo: ")
    95  	}
    96  	fmt.Fprintf(os.Stderr, msg, args...)
    97  	fmt.Fprintf(os.Stderr, "\n")
    98  }
    99  
   100  func creat(name string) *os.File {
   101  	f, err := os.Create(name)
   102  	if err != nil {
   103  		fatalf("%s", err)
   104  	}
   105  	return f
   106  }
   107  

View as plain text