gllvm/shared/compiler.go

221 lines
6.3 KiB
Go
Raw Normal View History

package shared
2017-06-22 01:34:56 +02:00
import (
2017-06-29 21:38:04 +02:00
"io"
"io/ioutil"
2017-06-29 21:38:04 +02:00
"os"
"path"
"path/filepath"
"runtime"
2017-06-29 21:38:04 +02:00
"strings"
"sync"
2017-06-22 01:34:56 +02:00
)
2017-06-30 02:39:37 +02:00
type bitcodeToObjectLink struct {
2017-06-29 21:38:04 +02:00
bcPath string
objPath string
}
2017-07-06 02:42:55 +02:00
//Compile wraps a call to the compiler with the given args.
func Compile(args []string, compiler string) (exitCode int) {
exitCode = 0
//in the configureOnly case we have to know the exit code of the compile
//because that is how configure figures out what it can and cannot do.
2017-07-01 00:09:30 +02:00
2017-07-06 02:42:55 +02:00
var ok = true
2017-07-10 22:01:21 +02:00
var compilerExecName = GetCompilerExecName(compiler)
var configureOnly bool
if LLVMConfigureOnly != "" {
configureOnly = true
}
var pr = parse(args)
var wg sync.WaitGroup
2017-07-05 21:52:06 +02:00
// If configure only or print only are set, just execute the compiler
if configureOnly || pr.IsPrintOnly {
wg.Add(1)
go execCompile(compilerExecName, pr, &wg, &ok)
wg.Wait()
if !ok {
exitCode = 1
}
// Else try to build bitcode as well
} else {
2017-06-30 02:39:37 +02:00
var bcObjLinks []bitcodeToObjectLink
var newObjectFiles []string
wg.Add(2)
go execCompile(compilerExecName, pr, &wg, &ok)
go buildAndAttachBitcode(compilerExecName, pr, &bcObjLinks, &newObjectFiles, &wg)
wg.Wait()
2017-07-01 00:09:30 +02:00
//grok the exit code
if !ok {
exitCode = 1
} else {
// When objects and bitcode are built we can attach bitcode paths
// to object files and link
for _, link := range bcObjLinks {
attachBitcodePathToObject(link.bcPath, link.objPath)
}
if !pr.IsCompileOnly {
compileTimeLinkFiles(compilerExecName, pr, newObjectFiles)
}
}
}
return
2017-06-22 21:14:00 +02:00
}
// Compiles bitcode files and mutates the list of bc->obj links to perform + the list of
// new object files to link
2017-06-30 02:46:30 +02:00
func buildAndAttachBitcode(compilerExecName string, pr parserResult, bcObjLinks *[]bitcodeToObjectLink, newObjectFiles *[]string, wg *sync.WaitGroup) {
defer (*wg).Done()
// If nothing to do, exit silently
if !pr.IsEmitLLVM && !pr.IsAssembly && !pr.IsAssembleOnly &&
!(pr.IsDependencyOnly && !pr.IsCompileOnly) && !pr.IsPreprocessOnly {
var hidden = !pr.IsCompileOnly
if len(pr.InputFiles) == 1 && pr.IsCompileOnly {
var srcFile = pr.InputFiles[0]
objFile, bcFile := getArtifactNames(pr, 0, hidden)
buildBitcodeFile(compilerExecName, pr, srcFile, bcFile)
2017-06-30 02:39:37 +02:00
*bcObjLinks = append(*bcObjLinks, bitcodeToObjectLink{bcPath: bcFile, objPath: objFile})
} else {
for i, srcFile := range pr.InputFiles {
objFile, bcFile := getArtifactNames(pr, i, hidden)
if hidden {
buildObjectFile(compilerExecName, pr, srcFile, objFile)
*newObjectFiles = append(*newObjectFiles, objFile)
}
if strings.HasSuffix(srcFile, ".bc") {
2017-06-30 02:39:37 +02:00
*bcObjLinks = append(*bcObjLinks, bitcodeToObjectLink{bcPath: srcFile, objPath: objFile})
} else {
buildBitcodeFile(compilerExecName, pr, srcFile, bcFile)
2017-06-30 02:39:37 +02:00
*bcObjLinks = append(*bcObjLinks, bitcodeToObjectLink{bcPath: bcFile, objPath: objFile})
}
}
}
}
return
2017-06-22 21:14:00 +02:00
}
func attachBitcodePathToObject(bcFile, objFile string) {
// We can only attach a bitcode path to certain file types
switch filepath.Ext(objFile) {
2017-06-29 21:38:04 +02:00
case
".o",
".lo",
".os",
".So",
".po":
// Store bitcode path to temp file
var absBcPath, _ = filepath.Abs(bcFile)
2017-06-29 21:38:04 +02:00
tmpContent := []byte(absBcPath + "\n")
tmpFile, err := ioutil.TempFile("", "gllvm")
if err != nil {
LogFatal("attachBitcodePathToObject: %v\n", err)
}
defer os.Remove(tmpFile.Name())
if _, err := tmpFile.Write(tmpContent); err != nil {
LogFatal("attachBitcodePathToObject: %v\n", err)
}
if err := tmpFile.Close(); err != nil {
LogFatal("attachBitcodePathToObject: %v\n", err)
}
// Let's write the bitcode section
var attachCmd string
var attachCmdArgs []string
if runtime.GOOS == "darwin" {
attachCmd = "ld"
2017-06-30 20:54:59 +02:00
attachCmdArgs = []string{"-r", "-keep_private_externs", objFile, "-sectcreate", DarwinSegmentName, DarwinSectionName, tmpFile.Name(), "-o", objFile}
} else {
attachCmd = "objcopy"
2017-06-30 20:54:59 +02:00
attachCmdArgs = []string{"--add-section", ELFSectionName + "=" + tmpFile.Name(), objFile}
}
// Run the attach command and ignore errors
2017-06-30 21:17:27 +02:00
execCmd(attachCmd, attachCmdArgs, "")
// Copy bitcode file to store, if necessary
if bcStorePath := LLVMBitcodeStorePath; bcStorePath != "" {
destFilePath := path.Join(bcStorePath, getHashedPath(absBcPath))
in, _ := os.Open(absBcPath)
defer in.Close()
out, _ := os.Create(destFilePath)
2017-06-29 21:38:04 +02:00
defer out.Close()
io.Copy(out, in)
out.Sync()
}
}
2017-06-22 21:14:00 +02:00
}
2017-06-30 02:46:30 +02:00
func compileTimeLinkFiles(compilerExecName string, pr parserResult, objFiles []string) {
var outputFile = pr.OutputFilename
if outputFile == "" {
outputFile = "a.out"
}
args := pr.LinkArgs
args = append(args, objFiles...)
args = append(args, "-o", outputFile)
success, err := execCmd(compilerExecName, args, "")
if !success {
LogFatal("Failed to link: %v.", err)
}
2017-06-22 21:14:00 +02:00
}
// Tries to build the specified source file to object
2017-06-30 02:46:30 +02:00
func buildObjectFile(compilerExecName string, pr parserResult, srcFile string, objFile string) {
args := pr.CompileArgs[:]
args = append(args, srcFile, "-c", "-o", objFile)
success, err := execCmd(compilerExecName, args, "")
if !success {
LogFatal("Failed to build object file for %s because: %v\n", srcFile, err)
}
2017-06-22 21:14:00 +02:00
}
// Tries to build the specified source file to bitcode
2017-06-30 02:46:30 +02:00
func buildBitcodeFile(compilerExecName string, pr parserResult, srcFile string, bcFile string) {
args := pr.CompileArgs[:]
args = append(args, "-emit-llvm", "-c", srcFile, "-o", bcFile)
success, err := execCmd(compilerExecName, args, "")
if !success {
LogFatal("Failed to build bitcode file for %s because: %v\n", srcFile, err)
}
2017-06-22 21:14:00 +02:00
}
// Tries to build object file
func execCompile(compilerExecName string, pr parserResult, wg *sync.WaitGroup, ok *bool) {
defer (*wg).Done()
success, err := execCmd(compilerExecName, pr.InputList, "")
2017-06-30 21:17:27 +02:00
if !success {
LogError("Failed to compile using given arguments: %v\n", err)
*ok = false
2017-07-01 00:09:30 +02:00
}
2017-06-22 21:14:00 +02:00
}
2017-07-10 22:01:21 +02:00
// GetCompilerExecName returns the full path of the executable
func GetCompilerExecName(compiler string) string {
2017-07-06 02:42:55 +02:00
switch compiler {
case "clang":
if LLVMCCName != "" {
return filepath.Join(LLVMToolChainBinDir, LLVMCCName)
}
2017-07-06 02:42:55 +02:00
return filepath.Join(LLVMToolChainBinDir, compiler)
case "clang++":
if LLVMCXXName != "" {
return filepath.Join(LLVMToolChainBinDir, LLVMCXXName)
}
2017-07-06 02:42:55 +02:00
return filepath.Join(LLVMToolChainBinDir, compiler)
default:
2017-07-06 02:42:55 +02:00
LogFatal("The compiler %s is not supported by this tool.", compiler)
return ""
}
2017-06-22 01:34:56 +02:00
}