github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gotools/go/gccgoimporter/gccgoinstallation.go (about) 1 // Copyright 2013 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 gccgoimporter 6 7 import ( 8 "bufio" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "strings" 13 14 "llvm.org/llgo/third_party/gotools/go/types" 15 ) 16 17 // Information about a specific installation of gccgo. 18 type GccgoInstallation struct { 19 // Version of gcc (e.g. 4.8.0). 20 GccVersion string 21 22 // Target triple (e.g. x86_64-unknown-linux-gnu). 23 TargetTriple string 24 25 // Built-in library paths used by this installation. 26 LibPaths []string 27 } 28 29 // Ask the driver at the given path for information for this GccgoInstallation. 30 func (inst *GccgoInstallation) InitFromDriver(gccgoPath string) (err error) { 31 cmd := exec.Command(gccgoPath, "-###", "-S", "-x", "go", "-") 32 stderr, err := cmd.StderrPipe() 33 if err != nil { 34 return 35 } 36 37 err = cmd.Start() 38 if err != nil { 39 return 40 } 41 42 scanner := bufio.NewScanner(stderr) 43 for scanner.Scan() { 44 line := scanner.Text() 45 switch { 46 case strings.HasPrefix(line, "Target: "): 47 inst.TargetTriple = line[8:] 48 49 case line[0] == ' ': 50 args := strings.Fields(line) 51 for _, arg := range args[1:] { 52 if strings.HasPrefix(arg, "-L") { 53 inst.LibPaths = append(inst.LibPaths, arg[2:]) 54 } 55 } 56 } 57 } 58 59 stdout, err := exec.Command(gccgoPath, "-dumpversion").Output() 60 if err != nil { 61 return 62 } 63 inst.GccVersion = strings.TrimSpace(string(stdout)) 64 65 return 66 } 67 68 // Return the list of export search paths for this GccgoInstallation. 69 func (inst *GccgoInstallation) SearchPaths() (paths []string) { 70 for _, lpath := range inst.LibPaths { 71 spath := filepath.Join(lpath, "go", inst.GccVersion) 72 fi, err := os.Stat(spath) 73 if err != nil || !fi.IsDir() { 74 continue 75 } 76 paths = append(paths, spath) 77 78 spath = filepath.Join(spath, inst.TargetTriple) 79 fi, err = os.Stat(spath) 80 if err != nil || !fi.IsDir() { 81 continue 82 } 83 paths = append(paths, spath) 84 } 85 86 paths = append(paths, inst.LibPaths...) 87 88 return 89 } 90 91 // Return an importer that searches incpaths followed by the gcc installation's 92 // built-in search paths and the current directory. 93 func (inst *GccgoInstallation) GetImporter(incpaths []string, initmap map[*types.Package]InitData) types.Importer { 94 return GetImporter(append(append(incpaths, inst.SearchPaths()...), "."), initmap) 95 }