1 //===-- llvm-go.go - go tool wrapper for LLVM -----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tool lets us build LLVM components within the tree by setting up a
11 // $GOPATH that resembles a tree fetched in the normal way with "go get".
13 //===----------------------------------------------------------------------===//
28 linkmodeComponentLibs = "component-libs"
29 linkmodeDylib = "dylib"
33 llvmpath, pkgpath string
37 {"bindings/go/llvm", "llvm.org/llvm/bindings/go/llvm"},
38 {"tools/llgo", "llvm.org/llgo"},
41 type compilerFlags struct {
45 var components = []string{
71 func llvmConfig(args ...string) string {
72 configpath := os.Getenv("LLVM_CONFIG")
74 bin, _ := filepath.Split(os.Args[0])
75 configpath = filepath.Join(bin, "llvm-config")
78 cmd := exec.Command(configpath, args...)
79 cmd.Stderr = os.Stderr
80 out, err := cmd.Output()
86 outstr = strings.TrimSuffix(outstr, "\n")
87 outstr = strings.Replace(outstr, "\n", " ", -1)
91 func llvmFlags(linkmode string) compilerFlags {
92 ldflags := llvmConfig("--ldflags")
94 case linkmodeComponentLibs:
95 ldflags += " " + llvmConfig(append([]string{"--libs"}, components...)...)
99 panic("invalid linkmode: " + linkmode)
101 ldflags += " " + llvmConfig("--system-libs")
102 if runtime.GOOS != "darwin" {
103 // OS X doesn't like -rpath with cgo. See:
104 // https://code.google.com/p/go/issues/detail?id=7293
105 ldflags = "-Wl,-rpath," + llvmConfig("--libdir") + " " + ldflags
107 return compilerFlags{
108 cpp: llvmConfig("--cppflags"),
114 func addTag(args []string, tag string) []string {
115 args = append([]string{}, args...)
117 for i, a := range args {
118 if strings.HasPrefix(a, "-tags=") {
119 args[i] = a + " " + tag
121 } else if a == "-tags" && i+1 < len(args) {
122 args[i+1] = args[i+1] + " " + tag
127 args = append([]string{args[0], "-tags", tag}, args[1:]...)
132 func printComponents() {
133 fmt.Println(strings.Join(components, " "))
136 func printConfig(linkmode string) {
137 flags := llvmFlags(linkmode)
139 fmt.Printf(`// +build !byollvm
141 // This file is generated by llvm-go, do not edit.
152 type (run_build_sh int)
153 `, flags.cpp, flags.cxx, flags.ld)
156 func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode string) {
157 args = addTag(args, "byollvm")
159 srcdir := llvmConfig("--src-root")
161 tmpgopath, err := ioutil.TempDir("", "gopath")
166 for _, p := range packages {
167 path := filepath.Join(tmpgopath, "src", p.pkgpath)
168 err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
173 err = os.Symlink(filepath.Join(srcdir, p.llvmpath), path)
179 newpath := os.Getenv("PATH")
181 newgopathlist := []string{tmpgopath}
182 newgopathlist = append(newgopathlist, filepath.SplitList(os.Getenv("GOPATH"))...)
183 newgopath := strings.Join(newgopathlist, string(filepath.ListSeparator))
185 flags := llvmFlags(linkmode)
190 "CGO_CPPFLAGS=" + flags.cpp + " " + cppflags,
191 "CGO_CXXFLAGS=" + flags.cxx + " " + cxxflags,
192 "CGO_LDFLAGS=" + flags.ld + " " + ldflags,
193 "GOPATH=" + newgopath,
197 newenv = append(newenv, "GCCGO="+llgo)
200 for _, v := range os.Environ() {
201 if !strings.HasPrefix(v, "CC=") &&
202 !strings.HasPrefix(v, "CXX=") &&
203 !strings.HasPrefix(v, "CGO_CPPFLAGS=") &&
204 !strings.HasPrefix(v, "CGO_CXXFLAGS=") &&
205 !strings.HasPrefix(v, "CGO_LDFLAGS=") &&
206 !strings.HasPrefix(v, "GCCGO=") &&
207 !strings.HasPrefix(v, "GOPATH=") &&
208 !strings.HasPrefix(v, "PATH=") {
209 newenv = append(newenv, v)
213 gocmdpath, err := exec.LookPath(gocmd)
218 proc, err := os.StartProcess(gocmdpath, append([]string{gocmd}, args...),
221 Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
226 ps, err := proc.Wait()
231 os.RemoveAll(tmpgopath)
239 fmt.Println(`Usage: llvm-go subcommand [flags]
241 Available subcommands: build get install run test print-components print-config`)
246 cc := os.Getenv("CC")
247 cxx := os.Getenv("CXX")
248 cppflags := os.Getenv("CGO_CPPFLAGS")
249 cxxflags := os.Getenv("CGO_CXXFLAGS")
250 ldflags := os.Getenv("CGO_LDFLAGS")
253 linkmode := linkmodeComponentLibs
263 {"cppflags", &cppflags},
264 {"ldflags", &ldflags},
265 {"linkmode", &linkmode},
274 for _, flag := range flags {
275 if strings.HasPrefix(args[0], flag.name+"=") {
276 *flag.dest = args[0][len(flag.name)+1:]
285 case "build", "get", "install", "run", "test":
286 runGoWithLLVMEnv(args, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode)
287 case "print-components":
290 printConfig(linkmode)