github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/cmd/cc-wrapper/main.go (about)

     1  //===- main.go - Clang compiler wrapper for building libgo ----------------===//
     2  //
     3  // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
     4  // See https://llvm.org/LICENSE.txt for license information.
     5  // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
     6  //
     7  //===----------------------------------------------------------------------===//
     8  //
     9  // This is a wrapper for Clang that passes invocations with -fdump-go-spec to
    10  // GCC, and rewrites -fplan9-extensions to -fms-extensions. It is intended to
    11  // go away once libgo's build no longer uses these flags.
    12  //
    13  //===----------------------------------------------------------------------===//
    14  
    15  package main
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"os/exec"
    21  	"strings"
    22  )
    23  
    24  func runproc(name string, argv []string) {
    25  	path, err := exec.LookPath(name)
    26  	if err != nil {
    27  		fmt.Fprintf(os.Stderr, "cc-wrapper: could not find %s: %v\n", name, err)
    28  		os.Exit(1)
    29  	}
    30  
    31  	proc, err := os.StartProcess(path, append([]string{name}, argv...), &os.ProcAttr{
    32  		Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
    33  	})
    34  	if err != nil {
    35  		fmt.Fprintf(os.Stderr, "cc-wrapper: could not start %s: %v\n", name, err)
    36  		os.Exit(1)
    37  	}
    38  
    39  	state, err := proc.Wait()
    40  	if err != nil {
    41  		fmt.Fprintf(os.Stderr, "cc-wrapper: could not wait for %s: %v\n", name, err)
    42  		os.Exit(1)
    43  	}
    44  
    45  	if state.Success() {
    46  		os.Exit(0)
    47  	} else {
    48  		os.Exit(1)
    49  	}
    50  }
    51  
    52  func main() {
    53  	newargs := make([]string, len(os.Args)-1)
    54  	for i, arg := range os.Args[1:] {
    55  		switch {
    56  		case strings.HasPrefix(arg, "-fdump-go-spec"):
    57  			runproc("gcc", os.Args[1:])
    58  
    59  		case arg == "-fplan9-extensions":
    60  			newargs[i] = "-fms-extensions"
    61  			newargs = append(newargs, "-Wno-microsoft")
    62  
    63  		default:
    64  			newargs[i] = arg
    65  		}
    66  	}
    67  
    68  	ccargs := strings.Split(os.Getenv("REAL_CC"), "@SPACE@")
    69  	runproc(ccargs[0], append(ccargs[1:], newargs...))
    70  }