github.com/atrn/dcc@v0.0.0-20220806184050-4470d2553272/compiler.go (about)

     1  // dcc - dependency-driven C/C++ compiler front end
     2  //
     3  // Copyright © A.Newman 2015.
     4  //
     5  // This source code is released under version 2 of the  GNU Public License.
     6  // See the file LICENSE for details.
     7  //
     8  
     9  package main
    10  
    11  import (
    12  	"io"
    13  	"log"
    14  	"strings"
    15  )
    16  
    17  // The Compiler interface defines the interface to a compiler
    18  // that can generate dependency information.
    19  //
    20  // The interface exists to accomonodate the differences between
    21  // gcc-style dependency generation (as done by gcc, clang and icc)
    22  // and the approach taken with Microsoft C++ (parsing the output
    23  // of its /showIncludes switch).
    24  type Compiler interface {
    25  	// Return a name for the compiler.
    26  	//
    27  	Name() string
    28  
    29  	// Compile the source file named by source using the supplied options
    30  	// and create an object file named object and a dependencies file
    31  	// called deps.
    32  	//
    33  	Compile(source, object, deps string, options []string, w io.Writer) error
    34  
    35  	// Read a compiler-generated depdencies file and return the dependent filenames.
    36  	//
    37  	ReadDependencies(path string) (string, []string, error)
    38  
    39  	// Return the command line options used to name the ouput executable
    40  	// file when the compiler (driver) is used to link a program
    41  	DefineExecutableArgs(exeName string) []string
    42  }
    43  
    44  // GetCompiler is a factory function to return a value that implements
    45  // the Compiler interface.
    46  func GetCompiler(name string) Compiler {
    47  	switch name {
    48  	case "cl", "cl.exe":
    49  		return NewMsvcCompiler()
    50  	case "cc", "c++", "gcc", "g++", "clang", "clang++", "icc", "icpc":
    51  		return NewGccStyleCompiler(name)
    52  	default:
    53  		if strings.Contains(name, "gcc") || strings.Contains(name, "clang") {
    54  			return NewGccStyleCompiler(name)
    55  		}
    56  		if strings.Contains(name, "g++") || strings.Contains(name, "clang++") {
    57  			return NewGccStyleCompiler(name)
    58  		}
    59  	}
    60  	log.Fatalf("%s: unsupported compiler", name)
    61  	return nil
    62  }