gllvm/compiler.go

184 lines
5.8 KiB
Go
Raw Normal View History

2017-06-22 01:34:56 +02:00
package main
import (
"os"
2017-06-22 22:48:40 +02:00
"io/ioutil"
2017-06-22 21:14:00 +02:00
"log"
"strings"
2017-06-22 22:48:40 +02:00
"path"
"path/filepath"
"runtime"
"io"
2017-06-22 01:34:56 +02:00
)
func compile(args []string) {
if len(args) < 1 {
2017-06-22 21:14:00 +02:00
log.Fatal("You must precise which compiler to use.")
2017-06-22 01:34:56 +02:00
}
var compilerName = args[0]
2017-06-22 21:14:00 +02:00
var compilerExecName = getCompilerExecName(compilerName)
var configureOnly bool
if os.Getenv(CONFIGURE_ONLY) != "" {
configureOnly = true
}
2017-06-22 01:34:56 +02:00
args = args[1:]
var pr = parse(args)
2017-06-22 21:14:00 +02:00
// If configure only is set, try to execute normal compiling command then exit silently
if configureOnly {
execCompile(compilerExecName, pr)
os.Exit(0)
}
// Else try to build objects and bitcode
buildAndAttachBitcode(compilerExecName, pr)
}
// Compiles bitcode files and attach path to the object files
func buildAndAttachBitcode(compilerExecName string, pr ParserResult) {
// If nothing to do, exit silently
2017-06-23 00:42:19 +02:00
if pr.IsEmitLLVM || pr.IsAssembly || pr.IsAssembleOnly ||
2017-06-22 21:14:00 +02:00
(pr.IsDependencyOnly && !pr.IsCompileOnly) || pr.IsPreprocessOnly {
os.Exit(0)
}
var newObjectFiles []string
var hidden = !pr.IsCompileOnly
if len(pr.InputFiles) == 1 && pr.IsCompileOnly {
var srcFile = pr.InputFiles[0]
objFile, bcFile := getArtifactNames(pr, 0, hidden)
buildObjectFile(compilerExecName, pr, srcFile, objFile)
buildBitcodeFile(compilerExecName, pr, srcFile, bcFile)
attachBitcodePathToObject(bcFile, objFile)
} else {
for i, srcFile := range pr.InputFiles {
objFile, bcFile := getArtifactNames(pr, i, hidden)
buildObjectFile(compilerExecName, pr, srcFile, objFile)
if hidden {
newObjectFiles = append(newObjectFiles, objFile)
} else if strings.HasSuffix(srcFile, ".bc") {
attachBitcodePathToObject(srcFile, objFile)
} else {
buildBitcodeFile(compilerExecName, pr, srcFile, bcFile)
attachBitcodePathToObject(bcFile, objFile)
}
}
}
if !pr.IsCompileOnly {
linkFiles(compilerExecName, pr, newObjectFiles)
}
}
func attachBitcodePathToObject(bcFile, objFile string) {
2017-06-22 22:48:40 +02:00
// We can only attach a bitcode path to certain file types
switch filepath.Ext(objFile) {
case
".o",
".lo",
".os",
".So",
".po":
// Store bitcode path to temp file
var absBcPath, _= filepath.Abs(bcFile)
tmpContent := []byte(absBcPath+"\n")
tmpFile, err := ioutil.TempFile("", "gowllvm")
if err != nil {
log.Fatal(err)
}
defer os.Remove(tmpFile.Name())
if _, err := tmpFile.Write(tmpContent); err != nil {
log.Fatal(err)
}
if err := tmpFile.Close(); err != nil {
log.Fatal(err)
}
// Let's write the bitcode section
var attachCmd string
var attachCmdArgs []string
if runtime.GOOS == "darwin" {
attachCmd = "ld"
attachCmdArgs = []string{"-r", "-keep_private_externs", objFile, "-sectcreate", DARWIN_SEGMENT_NAME, DARWIN_SECTION_NAME, tmpFile.Name(), "-o", objFile}
} else {
attachCmd = "objcopy"
2017-06-22 23:08:09 +02:00
attachCmdArgs = []string{"--add-section", ELF_SECTION_NAME+"="+tmpFile.Name(), objFile}
2017-06-22 22:48:40 +02:00
}
// Run the attach command and ignore errors
execCmd(attachCmd, attachCmdArgs)
// Copy bitcode file to store, if necessary
if bcStorePath := os.Getenv(BC_STORE_PATH); bcStorePath != "" {
destFilePath := path.Join(bcStorePath, getHashedPath(absBcPath))
in, _ := os.Open(absBcPath)
defer in.Close()
out, _ := os.Create(destFilePath)
defer out.Close()
io.Copy(out, in)
out.Sync()
}
}
2017-06-22 21:14:00 +02:00
}
func linkFiles(compilerExecName string, pr ParserResult, objFiles []string) {
var outputFile = pr.OutputFilename
if outputFile == "" {
outputFile = "a.out"
}
2017-06-23 00:59:19 +02:00
args := append(pr.ObjectFiles, pr.LinkArgs...)
args = append(args, objFiles...)
2017-06-22 21:14:00 +02:00
args = append(args, "-o", outputFile)
if execCmd(compilerExecName, args) {
log.Fatal("Failed to link.")
}
}
// Tries to build the specified source file to object
func buildObjectFile(compilerExecName string, pr ParserResult, srcFile string, objFile string) {
args := pr.CompileArgs[:]
args = append(args, srcFile, "-c", "-o", objFile)
if execCmd(compilerExecName, args) {
log.Fatal("Failed to build object file for ", srcFile)
2017-06-22 21:14:00 +02:00
}
}
// Tries to build the specified source file to bitcode
func buildBitcodeFile(compilerExecName string, pr ParserResult, srcFile string, bcFile string) {
args := pr.CompileArgs[:]
args = append(args, "-emit-llvm", "-c", srcFile, "-o", bcFile)
if execCmd(compilerExecName, args) {
log.Fatal("Failed to build bitcode file for ", srcFile)
2017-06-22 21:14:00 +02:00
}
}
// Tries to build object file
func execCompile(compilerExecName string, pr ParserResult) {
if execCmd(compilerExecName, pr.InputList) {
log.Fatal("Failed to execute compile command.")
}
}
func getCompilerExecName(compilerName string) string {
var compilerPath = os.Getenv(COMPILER_PATH)
switch compilerName {
case "clang":
var clangName = os.Getenv(C_COMPILER_NAME)
if clangName != "" {
return compilerPath + clangName
} else {
return compilerPath + compilerName
}
case "clang++":
var clangppName = os.Getenv(C_COMPILER_NAME)
if clangppName != "" {
return compilerPath + clangppName
} else {
return compilerPath + compilerName
}
default:
log.Fatal("The compiler ", compilerName, " is not supported by this tool.")
2017-06-22 21:14:00 +02:00
return ""
}
2017-06-22 01:34:56 +02:00
}