// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gccgoimporter

import (
	
	
	
	
	
	
)

// Information about a specific installation of gccgo.
type GccgoInstallation struct {
	// Version of gcc (e.g. 4.8.0).
	GccVersion string

	// Target triple (e.g. x86_64-unknown-linux-gnu).
	TargetTriple string

	// Built-in library paths used by this installation.
	LibPaths []string
}

// Ask the driver at the given path for information for this GccgoInstallation.
// The given arguments are passed directly to the call of the driver.
func ( *GccgoInstallation) ( string,  ...string) ( error) {
	 := append([]string{"-###", "-S", "-x", "go", "-"}, ...)
	 := exec.Command(, ...)
	,  := .StderrPipe()
	if  != nil {
		return
	}

	 = .Start()
	if  != nil {
		return
	}

	 := bufio.NewScanner()
	for .Scan() {
		 := .Text()
		switch {
		case strings.HasPrefix(, "Target: "):
			.TargetTriple = [8:]

		case [0] == ' ':
			 := strings.Fields()
			for ,  := range [1:] {
				if strings.HasPrefix(, "-L") {
					.LibPaths = append(.LibPaths, [2:])
				}
			}
		}
	}

	 = append([]string{"-dumpversion"}, ...)
	,  := exec.Command(, ...).Output()
	if  != nil {
		return
	}
	.GccVersion = strings.TrimSpace(string())

	return
}

// Return the list of export search paths for this GccgoInstallation.
func ( *GccgoInstallation) () ( []string) {
	for ,  := range .LibPaths {
		 := filepath.Join(, "go", .GccVersion)
		,  := os.Stat()
		if  != nil || !.IsDir() {
			continue
		}
		 = append(, )

		 = filepath.Join(, .TargetTriple)
		,  = os.Stat()
		if  != nil || !.IsDir() {
			continue
		}
		 = append(, )
	}

	 = append(, .LibPaths...)

	return
}

// Return an importer that searches incpaths followed by the gcc installation's
// built-in search paths and the current directory.
func ( *GccgoInstallation) ( []string,  map[*types.Package]InitData) Importer {
	return GetImporter(append(append(, .SearchPaths()...), "."), )
}