Changeset View
Changeset View
Standalone View
Standalone View
tools/llvm-go/llvm-go.go
Show First 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | func llvmConfig(args ...string) string { | ||||
} | } | ||||
outstr := string(out) | outstr := string(out) | ||||
outstr = strings.TrimSuffix(outstr, "\n") | outstr = strings.TrimSuffix(outstr, "\n") | ||||
outstr = strings.Replace(outstr, "\n", " ", -1) | outstr = strings.Replace(outstr, "\n", " ", -1) | ||||
return outstr | return outstr | ||||
} | } | ||||
func llvmFlags(linkmode string) compilerFlags { | func llvmFlags(linkmode, dylibName string) compilerFlags { | ||||
ldflags := llvmConfig("--ldflags") | ldflags := llvmConfig("--ldflags") | ||||
switch linkmode { | switch linkmode { | ||||
case linkmodeComponentLibs: | case linkmodeComponentLibs: | ||||
ldflags += " " + llvmConfig(append([]string{"--libs"}, components...)...) | ldflags += " " + llvmConfig(append([]string{"--libs"}, components...)...) | ||||
case linkmodeDylib: | case linkmodeDylib: | ||||
ldflags += " -lLLVM" | ldflags += " -l" + dylibName | ||||
default: | default: | ||||
panic("invalid linkmode: " + linkmode) | panic("invalid linkmode: " + linkmode) | ||||
} | } | ||||
ldflags += " " + llvmConfig("--system-libs") | ldflags += " " + llvmConfig("--system-libs") | ||||
if runtime.GOOS != "darwin" { | if runtime.GOOS != "darwin" { | ||||
// OS X doesn't like -rpath with cgo. See: | // OS X doesn't like -rpath with cgo. See: | ||||
// https://code.google.com/p/go/issues/detail?id=7293 | // https://code.google.com/p/go/issues/detail?id=7293 | ||||
ldflags = "-Wl,-rpath," + llvmConfig("--libdir") + " " + ldflags | ldflags = "-Wl,-rpath," + llvmConfig("--libdir") + " " + ldflags | ||||
Show All 22 Lines | func addTag(args []string, tag string) []string { | ||||
} | } | ||||
return args | return args | ||||
} | } | ||||
func printComponents() { | func printComponents() { | ||||
fmt.Println(strings.Join(components, " ")) | fmt.Println(strings.Join(components, " ")) | ||||
} | } | ||||
func printConfig(linkmode string) { | func printConfig(linkmode, dylibName string) { | ||||
flags := llvmFlags(linkmode) | flags := llvmFlags(linkmode, dylibName) | ||||
fmt.Printf(`// +build !byollvm | fmt.Printf(`// +build !byollvm | ||||
// This file is generated by llvm-go, do not edit. | // This file is generated by llvm-go, do not edit. | ||||
package llvm | package llvm | ||||
/* | /* | ||||
#cgo CPPFLAGS: %s | #cgo CPPFLAGS: %s | ||||
#cgo CXXFLAGS: %s | #cgo CXXFLAGS: %s | ||||
#cgo LDFLAGS: %s | #cgo LDFLAGS: %s | ||||
*/ | */ | ||||
import "C" | import "C" | ||||
type (run_build_sh int) | type (run_build_sh int) | ||||
`, flags.cpp, flags.cxx, flags.ld) | `, flags.cpp, flags.cxx, flags.ld) | ||||
} | } | ||||
func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode string) { | func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode, dylibName string) { | ||||
args = addTag(args, "byollvm") | args = addTag(args, "byollvm") | ||||
srcdir := llvmConfig("--src-root") | srcdir := llvmConfig("--src-root") | ||||
tmpgopath, err := ioutil.TempDir("", "gopath") | tmpgopath, err := ioutil.TempDir("", "gopath") | ||||
if err != nil { | if err != nil { | ||||
panic(err.Error()) | panic(err.Error()) | ||||
} | } | ||||
Show All 12 Lines | func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode, dylibName string) { | ||||
} | } | ||||
newpath := os.Getenv("PATH") | newpath := os.Getenv("PATH") | ||||
newgopathlist := []string{tmpgopath} | newgopathlist := []string{tmpgopath} | ||||
newgopathlist = append(newgopathlist, filepath.SplitList(os.Getenv("GOPATH"))...) | newgopathlist = append(newgopathlist, filepath.SplitList(os.Getenv("GOPATH"))...) | ||||
newgopath := strings.Join(newgopathlist, string(filepath.ListSeparator)) | newgopath := strings.Join(newgopathlist, string(filepath.ListSeparator)) | ||||
flags := llvmFlags(linkmode) | flags := llvmFlags(linkmode, dylibName) | ||||
newenv := []string{ | newenv := []string{ | ||||
"CC=" + cc, | "CC=" + cc, | ||||
"CXX=" + cxx, | "CXX=" + cxx, | ||||
"CGO_CPPFLAGS=" + flags.cpp + " " + cppflags, | "CGO_CPPFLAGS=" + flags.cpp + " " + cppflags, | ||||
"CGO_CXXFLAGS=" + flags.cxx + " " + cxxflags, | "CGO_CXXFLAGS=" + flags.cxx + " " + cxxflags, | ||||
"CGO_LDFLAGS=" + flags.ld + " " + ldflags, | "CGO_LDFLAGS=" + flags.ld + " " + ldflags, | ||||
"GOPATH=" + newgopath, | "GOPATH=" + newgopath, | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | func main() { | ||||
cc := os.Getenv("CC") | cc := os.Getenv("CC") | ||||
cxx := os.Getenv("CXX") | cxx := os.Getenv("CXX") | ||||
cppflags := os.Getenv("CGO_CPPFLAGS") | cppflags := os.Getenv("CGO_CPPFLAGS") | ||||
cxxflags := os.Getenv("CGO_CXXFLAGS") | cxxflags := os.Getenv("CGO_CXXFLAGS") | ||||
ldflags := os.Getenv("CGO_LDFLAGS") | ldflags := os.Getenv("CGO_LDFLAGS") | ||||
gocmd := "go" | gocmd := "go" | ||||
llgo := "" | llgo := "" | ||||
linkmode := linkmodeComponentLibs | linkmode := linkmodeComponentLibs | ||||
dylibName := "LLVM" | |||||
flags := []struct { | flags := []struct { | ||||
name string | name string | ||||
dest *string | dest *string | ||||
}{ | }{ | ||||
{"cc", &cc}, | {"cc", &cc}, | ||||
{"cxx", &cxx}, | {"cxx", &cxx}, | ||||
{"go", &gocmd}, | {"go", &gocmd}, | ||||
{"llgo", &llgo}, | {"llgo", &llgo}, | ||||
{"cppflags", &cppflags}, | {"cppflags", &cppflags}, | ||||
{"ldflags", &ldflags}, | {"ldflags", &ldflags}, | ||||
{"linkmode", &linkmode}, | {"linkmode", &linkmode}, | ||||
{"dylib", &dylibName}, | |||||
} | } | ||||
args := os.Args[1:] | args := os.Args[1:] | ||||
LOOP: | LOOP: | ||||
for { | for { | ||||
if len(args) == 0 { | if len(args) == 0 { | ||||
usage() | usage() | ||||
} | } | ||||
for _, flag := range flags { | for _, flag := range flags { | ||||
if strings.HasPrefix(args[0], flag.name+"=") { | if strings.HasPrefix(args[0], flag.name+"=") { | ||||
*flag.dest = args[0][len(flag.name)+1:] | *flag.dest = args[0][len(flag.name)+1:] | ||||
args = args[1:] | args = args[1:] | ||||
continue LOOP | continue LOOP | ||||
} | } | ||||
} | } | ||||
break | break | ||||
} | } | ||||
switch args[0] { | switch args[0] { | ||||
case "build", "get", "install", "run", "test": | case "build", "get", "install", "run", "test": | ||||
runGoWithLLVMEnv(args, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode) | runGoWithLLVMEnv(args, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode, dylibName) | ||||
case "print-components": | case "print-components": | ||||
printComponents() | printComponents() | ||||
case "print-config": | case "print-config": | ||||
printConfig(linkmode) | printConfig(linkmode, dylibName) | ||||
default: | default: | ||||
usage() | usage() | ||||
} | } | ||||
} | } |