Teach LLVM about llgo subproject.
authorPeter Collingbourne <peter@pcc.me.uk>
Thu, 27 Nov 2014 00:15:21 +0000 (00:15 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Thu, 27 Nov 2014 00:15:21 +0000 (00:15 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222860 91177308-0d34-0410-b5e6-96231b3b80d8

cmake/modules/AddLLVM.cmake
tools/CMakeLists.txt
tools/llvm-go/llvm-go.go

index bc26f0671668b889f031cd5988a919d65da7a0d4..2ef73fb8e5d9bc79fb834ce4282cb66750a99ab9 100644 (file)
@@ -603,6 +603,31 @@ function(add_unittest test_suite test_name)
   endif ()
 endfunction()
 
+function(llvm_add_go_executable binary pkgpath)
+  cmake_parse_arguments(ARG "ALL" "" "DEPENDS;GOFLAGS" ${ARGN})
+
+  if(LLVM_BINDINGS MATCHES "go")
+    # FIXME: This should depend only on the libraries Go needs.
+    get_property(llvmlibs GLOBAL PROPERTY LLVM_LIBS)
+    set(binpath ${CMAKE_BINARY_DIR}/bin/${binary}${CMAKE_EXECUTABLE_SUFFIX})
+    set(cc "${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}")
+    set(cxx "${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}")
+    set(ldflags "${CMAKE_EXE_LINKER_FLAGS}")
+    add_custom_command(OUTPUT ${binpath}
+      COMMAND ${CMAKE_BINARY_DIR}/bin/llvm-go "cc=${cc}" "cxx=${cxx}" "ldflags=${ldflags}"
+              ${ARG_GOFLAGS} build -o ${binpath} ${pkgpath}
+      DEPENDS llvm-config ${CMAKE_BINARY_DIR}/bin/llvm-go${CMAKE_EXECUTABLE_SUFFIX}
+              ${llvmlibs} ${ARG_DEPENDS}
+      COMMENT "Building Go executable ${binary}"
+      VERBATIM)
+    if (ARG_ALL)
+      add_custom_target(${binary} ALL DEPENDS ${binpath})
+    else()
+      add_custom_target(${binary} DEPENDS ${binpath})
+    endif()
+  endif()
+endfunction()
+
 # This function provides an automatic way to 'configure'-like generate a file
 # based on a set of common and custom variables, specifically targeting the
 # variables needed for the 'lit.site.cfg' files. This function bundles the
index fd761ecfe439905fbed233024e54f163224926e8..5905baf98138775bc4da713320587a7139f739b1 100644 (file)
@@ -81,6 +81,7 @@ else()
 endif()
 
 add_llvm_external_project(clang)
+add_llvm_external_project(llgo)
 
 if( NOT LLVM_INCLUDE_TOOLS STREQUAL "bootstrap-only" )
   add_llvm_external_project(lld)
index 47f948150f056e91c66128303c245db58f852f16..cf520bd90e9ddeb85c7d8f44a0e21abf0b36f1a3 100644 (file)
@@ -30,6 +30,7 @@ type pkg struct {
 
 var packages = []pkg{
        {"bindings/go/llvm", "llvm.org/llvm/bindings/go/llvm"},
+       {"tools/llgo", "llvm.org/llgo"},
 }
 
 type compilerFlags struct {
@@ -136,7 +137,7 @@ type (run_build_sh int)
 `, flags.cpp, flags.cxx, flags.ld)
 }
 
-func runGoWithLLVMEnv(args []string, cc, cxx, cppflags, cxxflags, ldflags string) {
+func runGoWithLLVMEnv(args []string, cc, cxx, llgo, cppflags, cxxflags, ldflags string) {
        args = addTag(args, "byollvm")
 
        srcdir := llvmConfig("--src-root")
@@ -159,6 +160,28 @@ func runGoWithLLVMEnv(args []string, cc, cxx, cppflags, cxxflags, ldflags string
                }
        }
 
+       newpath := os.Getenv("PATH")
+
+       if llgo != "" {
+               bindir := filepath.Join(tmpgopath, "bin")
+
+               err = os.MkdirAll(bindir, os.ModePerm)
+               if err != nil {
+                       panic(err.Error())
+               }
+
+               err = os.Symlink(llgo, filepath.Join(bindir, "gccgo"))
+               if err != nil {
+                       panic(err.Error())
+               }
+
+               newpathlist := []string{bindir}
+               newpathlist = append(newpathlist, filepath.SplitList(newpath)...)
+               newpath = strings.Join(newpathlist, string(filepath.ListSeparator))
+
+               args = append([]string{args[0], "-compiler", "gccgo"}, args[1:]...)
+       }
+
        newgopathlist := []string{tmpgopath}
        newgopathlist = append(newgopathlist, filepath.SplitList(os.Getenv("GOPATH"))...)
        newgopath := strings.Join(newgopathlist, string(filepath.ListSeparator))
@@ -172,6 +195,7 @@ func runGoWithLLVMEnv(args []string, cc, cxx, cppflags, cxxflags, ldflags string
                "CGO_CXXFLAGS=" + flags.cxx + " " + cxxflags,
                "CGO_LDFLAGS=" + flags.ld + " " + ldflags,
                "GOPATH=" + newgopath,
+               "PATH=" + newpath,
        }
        for _, v := range os.Environ() {
                if !strings.HasPrefix(v, "CC=") &&
@@ -179,7 +203,8 @@ func runGoWithLLVMEnv(args []string, cc, cxx, cppflags, cxxflags, ldflags string
                        !strings.HasPrefix(v, "CGO_CPPFLAGS=") &&
                        !strings.HasPrefix(v, "CGO_CXXFLAGS=") &&
                        !strings.HasPrefix(v, "CGO_LDFLAGS=") &&
-                       !strings.HasPrefix(v, "GOPATH=") {
+                       !strings.HasPrefix(v, "GOPATH=") &&
+                       !strings.HasPrefix(v, "PATH=") {
                        newenv = append(newenv, v)
                }
        }
@@ -222,6 +247,7 @@ func main() {
        cppflags := os.Getenv("CGO_CPPFLAGS")
        cxxflags := os.Getenv("CGO_CXXFLAGS")
        ldflags := os.Getenv("CGO_LDFLAGS")
+       llgo := ""
 
        args := os.Args[1:]
        DONE: for {
@@ -234,6 +260,9 @@ func main() {
                case strings.HasPrefix(args[0], "cxx="):
                        cxx = args[0][4:]
                        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:]
@@ -250,7 +279,7 @@ func main() {
 
        switch args[0] {
        case "build", "get", "install", "run", "test":
-               runGoWithLLVMEnv(args, cc, cxx, cppflags, cxxflags, ldflags)
+               runGoWithLLVMEnv(args, cc, cxx, llgo, cppflags, cxxflags, ldflags)
        case "print-components":
                printComponents()
        case "print-config":