From a9ce03858d657f4bab5cae242cfba7cfc1c144de Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 27 Nov 2014 00:15:21 +0000 Subject: [PATCH] Teach LLVM about llgo subproject. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222860 91177308-0d34-0410-b5e6-96231b3b80d8 --- cmake/modules/AddLLVM.cmake | 25 +++++++++++++++++++++++++ tools/CMakeLists.txt | 1 + tools/llvm-go/llvm-go.go | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/cmake/modules/AddLLVM.cmake b/cmake/modules/AddLLVM.cmake index bc26f067166..2ef73fb8e5d 100644 --- a/cmake/modules/AddLLVM.cmake +++ b/cmake/modules/AddLLVM.cmake @@ -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 diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index fd761ecfe43..5905baf9813 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -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) diff --git a/tools/llvm-go/llvm-go.go b/tools/llvm-go/llvm-go.go index 47f948150f0..cf520bd90e9 100644 --- a/tools/llvm-go/llvm-go.go +++ b/tools/llvm-go/llvm-go.go @@ -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": -- 2.34.1