blob: 12f0d73b4cc48117f3eadc35ea3e254be4e2c2cc [file] [log] [blame]
Peter Collingbourne244ecf52014-10-23 02:33:23 +00001//===-- llvm-go.go - go tool wrapper for LLVM -----------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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".
12//
13//===----------------------------------------------------------------------===//
14
15package main
16
17import (
18 "fmt"
19 "io/ioutil"
20 "os"
21 "os/exec"
22 "path/filepath"
23 "runtime"
24 "strings"
25)
26
Andrew Wilkins92113962015-09-01 03:14:31 +000027const (
28 linkmodeComponentLibs = "component-libs"
29 linkmodeDylib = "dylib"
30)
31
Peter Collingbourne244ecf52014-10-23 02:33:23 +000032type pkg struct {
33 llvmpath, pkgpath string
34}
35
36var packages = []pkg{
37 {"bindings/go/llvm", "llvm.org/llvm/bindings/go/llvm"},
Peter Collingbournea4f0bd72014-11-27 00:15:21 +000038 {"tools/llgo", "llvm.org/llgo"},
Peter Collingbourne244ecf52014-10-23 02:33:23 +000039}
40
41type compilerFlags struct {
42 cpp, cxx, ld string
43}
44
45var components = []string{
46 "all-targets",
47 "analysis",
48 "asmparser",
49 "asmprinter",
50 "bitreader",
51 "bitwriter",
52 "codegen",
53 "core",
Zachary Turner82af9432015-01-30 18:07:45 +000054 "debuginfodwarf",
Peter Collingbourne244ecf52014-10-23 02:33:23 +000055 "executionengine",
56 "instrumentation",
57 "interpreter",
58 "ipo",
59 "irreader",
60 "linker",
61 "mc",
62 "mcjit",
63 "objcarcopts",
64 "option",
65 "profiledata",
66 "scalaropts",
67 "support",
68 "target",
69}
70
71func llvmConfig(args ...string) string {
72 configpath := os.Getenv("LLVM_CONFIG")
73 if configpath == "" {
Andrew Wilkins92113962015-09-01 03:14:31 +000074 bin, _ := filepath.Split(os.Args[0])
75 configpath = filepath.Join(bin, "llvm-config")
Peter Collingbourne244ecf52014-10-23 02:33:23 +000076 }
77
78 cmd := exec.Command(configpath, args...)
Andrew Wilkins92113962015-09-01 03:14:31 +000079 cmd.Stderr = os.Stderr
Peter Collingbourne244ecf52014-10-23 02:33:23 +000080 out, err := cmd.Output()
81 if err != nil {
82 panic(err.Error())
83 }
84
85 outstr := string(out)
86 outstr = strings.TrimSuffix(outstr, "\n")
Andrew Wilkins92113962015-09-01 03:14:31 +000087 outstr = strings.Replace(outstr, "\n", " ", -1)
88 return outstr
Peter Collingbourne244ecf52014-10-23 02:33:23 +000089}
90
Andrew Wilkinsdfd60882016-01-20 04:03:09 +000091func llvmFlags() compilerFlags {
Andrew Wilkins7ab4dc72016-01-21 02:33:39 +000092 args := append([]string{"--ldflags", "--libs", "--system-libs"}, components...)
93 ldflags := llvmConfig(args...)
Peter Collingbourne244ecf52014-10-23 02:33:23 +000094 if runtime.GOOS != "darwin" {
95 // OS X doesn't like -rpath with cgo. See:
96 // https://code.google.com/p/go/issues/detail?id=7293
97 ldflags = "-Wl,-rpath," + llvmConfig("--libdir") + " " + ldflags
98 }
99 return compilerFlags{
100 cpp: llvmConfig("--cppflags"),
101 cxx: "-std=c++11",
102 ld: ldflags,
103 }
104}
105
106func addTag(args []string, tag string) []string {
107 args = append([]string{}, args...)
108 addedTag := false
109 for i, a := range args {
110 if strings.HasPrefix(a, "-tags=") {
111 args[i] = a + " " + tag
112 addedTag = true
113 } else if a == "-tags" && i+1 < len(args) {
114 args[i+1] = args[i+1] + " " + tag
115 addedTag = true
116 }
117 }
118 if !addedTag {
119 args = append([]string{args[0], "-tags", tag}, args[1:]...)
120 }
121 return args
122}
123
124func printComponents() {
125 fmt.Println(strings.Join(components, " "))
126}
127
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000128func printConfig() {
129 flags := llvmFlags()
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000130
131 fmt.Printf(`// +build !byollvm
132
133// This file is generated by llvm-go, do not edit.
134
135package llvm
136
137/*
138#cgo CPPFLAGS: %s
139#cgo CXXFLAGS: %s
140#cgo LDFLAGS: %s
141*/
142import "C"
143
144type (run_build_sh int)
145`, flags.cpp, flags.cxx, flags.ld)
146}
147
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000148func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags string) {
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000149 args = addTag(args, "byollvm")
150
151 srcdir := llvmConfig("--src-root")
152
153 tmpgopath, err := ioutil.TempDir("", "gopath")
154 if err != nil {
155 panic(err.Error())
156 }
157
158 for _, p := range packages {
159 path := filepath.Join(tmpgopath, "src", p.pkgpath)
160 err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
161 if err != nil {
162 panic(err.Error())
163 }
164
165 err = os.Symlink(filepath.Join(srcdir, p.llvmpath), path)
166 if err != nil {
167 panic(err.Error())
168 }
169 }
170
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000171 newpath := os.Getenv("PATH")
172
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000173 newgopathlist := []string{tmpgopath}
174 newgopathlist = append(newgopathlist, filepath.SplitList(os.Getenv("GOPATH"))...)
175 newgopath := strings.Join(newgopathlist, string(filepath.ListSeparator))
176
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000177 flags := llvmFlags()
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000178
179 newenv := []string{
180 "CC=" + cc,
181 "CXX=" + cxx,
182 "CGO_CPPFLAGS=" + flags.cpp + " " + cppflags,
183 "CGO_CXXFLAGS=" + flags.cxx + " " + cxxflags,
184 "CGO_LDFLAGS=" + flags.ld + " " + ldflags,
185 "GOPATH=" + newgopath,
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000186 "PATH=" + newpath,
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000187 }
Peter Collingbourne46f4b482015-02-14 01:45:57 +0000188 if llgo != "" {
Andrew Wilkins92113962015-09-01 03:14:31 +0000189 newenv = append(newenv, "GCCGO="+llgo)
Peter Collingbourne46f4b482015-02-14 01:45:57 +0000190 }
191
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000192 for _, v := range os.Environ() {
193 if !strings.HasPrefix(v, "CC=") &&
194 !strings.HasPrefix(v, "CXX=") &&
195 !strings.HasPrefix(v, "CGO_CPPFLAGS=") &&
196 !strings.HasPrefix(v, "CGO_CXXFLAGS=") &&
197 !strings.HasPrefix(v, "CGO_LDFLAGS=") &&
Peter Collingbourne46f4b482015-02-14 01:45:57 +0000198 !strings.HasPrefix(v, "GCCGO=") &&
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000199 !strings.HasPrefix(v, "GOPATH=") &&
200 !strings.HasPrefix(v, "PATH=") {
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000201 newenv = append(newenv, v)
202 }
203 }
204
Peter Collingbourne55707082015-02-14 01:45:56 +0000205 gocmdpath, err := exec.LookPath(gocmd)
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000206 if err != nil {
207 panic(err.Error())
208 }
209
Peter Collingbourne55707082015-02-14 01:45:56 +0000210 proc, err := os.StartProcess(gocmdpath, append([]string{gocmd}, args...),
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000211 &os.ProcAttr{
212 Env: newenv,
213 Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
214 })
215 if err != nil {
216 panic(err.Error())
217 }
218 ps, err := proc.Wait()
219 if err != nil {
220 panic(err.Error())
221 }
222
223 os.RemoveAll(tmpgopath)
224
225 if !ps.Success() {
226 os.Exit(1)
227 }
228}
229
230func usage() {
231 fmt.Println(`Usage: llvm-go subcommand [flags]
232
233Available subcommands: build get install run test print-components print-config`)
234 os.Exit(0)
235}
236
237func main() {
238 cc := os.Getenv("CC")
239 cxx := os.Getenv("CXX")
240 cppflags := os.Getenv("CGO_CPPFLAGS")
241 cxxflags := os.Getenv("CGO_CXXFLAGS")
242 ldflags := os.Getenv("CGO_LDFLAGS")
Peter Collingbourne55707082015-02-14 01:45:56 +0000243 gocmd := "go"
Peter Collingbournea4f0bd72014-11-27 00:15:21 +0000244 llgo := ""
Andrew Wilkins92113962015-09-01 03:14:31 +0000245
246 flags := []struct {
247 name string
248 dest *string
249 }{
250 {"cc", &cc},
251 {"cxx", &cxx},
252 {"go", &gocmd},
253 {"llgo", &llgo},
254 {"cppflags", &cppflags},
255 {"ldflags", &ldflags},
Andrew Wilkins92113962015-09-01 03:14:31 +0000256 }
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000257
258 args := os.Args[1:]
Andrew Wilkins92113962015-09-01 03:14:31 +0000259LOOP:
260 for {
261 if len(args) == 0 {
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000262 usage()
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000263 }
Andrew Wilkins92113962015-09-01 03:14:31 +0000264 for _, flag := range flags {
265 if strings.HasPrefix(args[0], flag.name+"=") {
266 *flag.dest = args[0][len(flag.name)+1:]
267 args = args[1:]
268 continue LOOP
269 }
270 }
271 break
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000272 }
273
274 switch args[0] {
275 case "build", "get", "install", "run", "test":
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000276 runGoWithLLVMEnv(args, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags)
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000277 case "print-components":
278 printComponents()
279 case "print-config":
Andrew Wilkinsdfd60882016-01-20 04:03:09 +0000280 printConfig()
Peter Collingbourne244ecf52014-10-23 02:33:23 +0000281 default:
282 usage()
283 }
284}