Enable linking tools, shared libraries against libLLVM
[oota-llvm.git] / tools / llvm-go / llvm-go.go
index c5c3fd244cadfd553f7720d95b1d52b3a49c5069..ed79ca67b89d3743e1fbc4a18986b7f2c3722071 100644 (file)
@@ -24,6 +24,11 @@ import (
        "strings"
 )
 
+const (
+       linkmodeComponentLibs = "component-libs"
+       linkmodeDylib         = "dylib"
+)
+
 type pkg struct {
        llvmpath, pkgpath string
 }
@@ -66,11 +71,12 @@ var components = []string{
 func llvmConfig(args ...string) string {
        configpath := os.Getenv("LLVM_CONFIG")
        if configpath == "" {
-               // strip llvm-go, add llvm-config
-               configpath = os.Args[0][:len(os.Args[0])-7] + "llvm-config"
+               bin, _ := filepath.Split(os.Args[0])
+               configpath = filepath.Join(bin, "llvm-config")
        }
 
        cmd := exec.Command(configpath, args...)
+       cmd.Stderr = os.Stderr
        out, err := cmd.Output()
        if err != nil {
                panic(err.Error())
@@ -78,11 +84,21 @@ func llvmConfig(args ...string) string {
 
        outstr := string(out)
        outstr = strings.TrimSuffix(outstr, "\n")
-       return strings.Replace(outstr, "\n", " ", -1)
+       outstr = strings.Replace(outstr, "\n", " ", -1)
+       return outstr
 }
 
-func llvmFlags() compilerFlags {
-       ldflags := llvmConfig(append([]string{"--ldflags", "--libs", "--system-libs"}, components...)...)
+func llvmFlags(linkmode string) compilerFlags {
+       ldflags := llvmConfig("--ldflags")
+       switch linkmode {
+       case linkmodeComponentLibs:
+               ldflags += " " + llvmConfig(append([]string{"--libs"}, components...)...)
+       case linkmodeDylib:
+               ldflags += " -lLLVM"
+       default:
+               panic("invalid linkmode: " + linkmode)
+       }
+       ldflags += " " + llvmConfig("--system-libs")
        if runtime.GOOS != "darwin" {
                // OS X doesn't like -rpath with cgo. See:
                // https://code.google.com/p/go/issues/detail?id=7293
@@ -117,8 +133,8 @@ func printComponents() {
        fmt.Println(strings.Join(components, " "))
 }
 
-func printConfig() {
-       flags := llvmFlags()
+func printConfig(linkmode string) {
+       flags := llvmFlags(linkmode)
 
        fmt.Printf(`// +build !byollvm
 
@@ -137,7 +153,7 @@ type (run_build_sh int)
 `, flags.cpp, flags.cxx, flags.ld)
 }
 
-func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags string) {
+func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode string) {
        args = addTag(args, "byollvm")
 
        srcdir := llvmConfig("--src-root")
@@ -166,7 +182,7 @@ func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, l
        newgopathlist = append(newgopathlist, filepath.SplitList(os.Getenv("GOPATH"))...)
        newgopath := strings.Join(newgopathlist, string(filepath.ListSeparator))
 
-       flags := llvmFlags()
+       flags := llvmFlags(linkmode)
 
        newenv := []string{
                "CC=" + cc,
@@ -178,7 +194,7 @@ func runGoWithLLVMEnv(args []string, cc, cxx, gocmd, llgo, cppflags, cxxflags, l
                "PATH=" + newpath,
        }
        if llgo != "" {
-               newenv = append(newenv, "GCCGO=" + llgo)
+               newenv = append(newenv, "GCCGO="+llgo)
        }
 
        for _, v := range os.Environ() {
@@ -234,45 +250,44 @@ func main() {
        ldflags := os.Getenv("CGO_LDFLAGS")
        gocmd := "go"
        llgo := ""
+       linkmode := linkmodeComponentLibs
+
+       flags := []struct {
+               name string
+               dest *string
+       }{
+               {"cc", &cc},
+               {"cxx", &cxx},
+               {"go", &gocmd},
+               {"llgo", &llgo},
+               {"cppflags", &cppflags},
+               {"ldflags", &ldflags},
+               {"linkmode", &linkmode},
+       }
 
        args := os.Args[1:]
-       DONE: for {
-               switch {
-               case len(args) == 0:
+LOOP:
+       for {
+               if len(args) == 0 {
                        usage()
-               case strings.HasPrefix(args[0], "cc="):
-                       cc = args[0][3:]
-                       args = args[1:]
-               case strings.HasPrefix(args[0], "cxx="):
-                       cxx = args[0][4:]
-                       args = args[1:]
-               case strings.HasPrefix(args[0], "go="):
-                       gocmd = args[0][3:]
-                       args = args[1:]
-               case strings.HasPrefix(args[0], "llgo="):
-                       llgo = args[0][5:]
-                       args = args[1:]
-               case strings.HasPrefix(args[0], "cppflags="):
-                       cppflags = args[0][9:]
-                       args = args[1:]
-               case strings.HasPrefix(args[0], "cxxflags="):
-                       cxxflags = args[0][9:]
-                       args = args[1:]
-               case strings.HasPrefix(args[0], "ldflags="):
-                       ldflags = args[0][8:]
-                       args = args[1:]
-               default:
-                       break DONE
                }
+               for _, flag := range flags {
+                       if strings.HasPrefix(args[0], flag.name+"=") {
+                               *flag.dest = args[0][len(flag.name)+1:]
+                               args = args[1:]
+                               continue LOOP
+                       }
+               }
+               break
        }
 
        switch args[0] {
        case "build", "get", "install", "run", "test":
-               runGoWithLLVMEnv(args, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags)
+               runGoWithLLVMEnv(args, cc, cxx, gocmd, llgo, cppflags, cxxflags, ldflags, linkmode)
        case "print-components":
                printComponents()
        case "print-config":
-               printConfig()
+               printConfig(linkmode)
        default:
                usage()
        }