add concurrent build with sync for bitcode and object files

This commit is contained in:
Loic Gelle 2017-06-26 16:06:51 -07:00
parent b9602ec8fe
commit 14bdcbb812

View File

@ -9,8 +9,14 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"io" "io"
"sync"
) )
type BitcodeToObjectLink struct {
bcPath string
objPath string
}
func compile(args []string, compilerName string) { func compile(args []string, compilerName string) {
var compilerExecName = getCompilerExecName(compilerName) var compilerExecName = getCompilerExecName(compilerName)
var configureOnly bool var configureOnly bool
@ -19,49 +25,63 @@ func compile(args []string, compilerName string) {
} }
var pr = parse(args) var pr = parse(args)
execCompile(compilerExecName, pr) var wg sync.WaitGroup
// If configure only is not set, build bitcode as well // If configure only is set, just execute the compiler
if !configureOnly { if configureOnly {
// Else try to build bitcode wg.Add(1)
buildAndAttachBitcode(compilerExecName, pr) go execCompile(compilerExecName, pr, &wg)
wg.Wait()
// Else try to build bitcode as well
} else {
var bcObjLinks []BitcodeToObjectLink
var newObjectFiles []string
wg.Add(2)
go execCompile(compilerExecName, pr, &wg)
go buildAndAttachBitcode(compilerExecName, pr, &bcObjLinks, &newObjectFiles, &wg)
wg.Wait()
// When objects and bitcode are builtm 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)
}
} }
} }
// Compiles bitcode files and attach path to the object files // Compiles bitcode files and mutates the list of bc->obj links to perform + the list of
func buildAndAttachBitcode(compilerExecName string, pr ParserResult) { // new object files to link
func buildAndAttachBitcode(compilerExecName string, pr ParserResult, bcObjLinks *[]BitcodeToObjectLink, newObjectFiles *[]string, wg *sync.WaitGroup) {
defer (*wg).Done()
// If nothing to do, exit silently // If nothing to do, exit silently
if pr.IsEmitLLVM || pr.IsAssembly || pr.IsAssembleOnly || if !pr.IsEmitLLVM && !pr.IsAssembly && !pr.IsAssembleOnly &&
(pr.IsDependencyOnly && !pr.IsCompileOnly) || pr.IsPreprocessOnly { !(pr.IsDependencyOnly && !pr.IsCompileOnly) && pr.IsPreprocessOnly {
os.Exit(0)
}
var newObjectFiles []string
var hidden = !pr.IsCompileOnly var hidden = !pr.IsCompileOnly
if len(pr.InputFiles) == 1 && pr.IsCompileOnly { if len(pr.InputFiles) == 1 && pr.IsCompileOnly {
var srcFile = pr.InputFiles[0] var srcFile = pr.InputFiles[0]
objFile, bcFile := getArtifactNames(pr, 0, hidden) objFile, bcFile := getArtifactNames(pr, 0, hidden)
buildObjectFile(compilerExecName, pr, srcFile, objFile)
buildBitcodeFile(compilerExecName, pr, srcFile, bcFile) buildBitcodeFile(compilerExecName, pr, srcFile, bcFile)
attachBitcodePathToObject(bcFile, objFile) *bcObjLinks = append(*bcObjLinks, BitcodeToObjectLink{bcPath: bcFile, objPath: objFile})
} else { } else {
for i, srcFile := range pr.InputFiles { for i, srcFile := range pr.InputFiles {
objFile, bcFile := getArtifactNames(pr, i, hidden) objFile, bcFile := getArtifactNames(pr, i, hidden)
buildObjectFile(compilerExecName, pr, srcFile, objFile)
if hidden { if hidden {
newObjectFiles = append(newObjectFiles, objFile) buildObjectFile(compilerExecName, pr, srcFile, objFile)
} else if strings.HasSuffix(srcFile, ".bc") { *newObjectFiles = append(*newObjectFiles, objFile)
attachBitcodePathToObject(srcFile, objFile) }
if strings.HasSuffix(srcFile, ".bc") {
*bcObjLinks = append(*bcObjLinks, BitcodeToObjectLink{bcPath: srcFile, objPath: objFile})
} else { } else {
buildBitcodeFile(compilerExecName, pr, srcFile, bcFile) buildBitcodeFile(compilerExecName, pr, srcFile, bcFile)
attachBitcodePathToObject(bcFile, objFile) *bcObjLinks = append(*bcObjLinks, BitcodeToObjectLink{bcPath: bcFile, objPath: objFile})
} }
} }
} }
if !pr.IsCompileOnly {
compileTimeLinkFiles(compilerExecName, pr, newObjectFiles)
} }
return
} }
func attachBitcodePathToObject(bcFile, objFile string) { func attachBitcodePathToObject(bcFile, objFile string) {
@ -147,9 +167,10 @@ func buildBitcodeFile(compilerExecName string, pr ParserResult, srcFile string,
} }
// Tries to build object file // Tries to build object file
func execCompile(compilerExecName string, pr ParserResult) { func execCompile(compilerExecName string, pr ParserResult, wg *sync.WaitGroup) {
defer (*wg).Done()
if execCmd(compilerExecName, pr.InputList, "") { if execCmd(compilerExecName, pr.InputList, "") {
log.Fatal("Failed to execute compile command.") log.Fatal("Failed to compile using given arguments.")
} }
} }