mirror of
https://github.com/danog/gllvm.git
synced 2025-01-23 03:11:15 +01:00
add concurrent build with sync for bitcode and object files
This commit is contained in:
parent
b9602ec8fe
commit
14bdcbb812
95
compiler.go
95
compiler.go
@ -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 hidden = !pr.IsCompileOnly
|
||||||
}
|
|
||||||
|
|
||||||
var newObjectFiles []string
|
if len(pr.InputFiles) == 1 && pr.IsCompileOnly {
|
||||||
var hidden = !pr.IsCompileOnly
|
var srcFile = pr.InputFiles[0]
|
||||||
|
objFile, bcFile := getArtifactNames(pr, 0, hidden)
|
||||||
if len(pr.InputFiles) == 1 && pr.IsCompileOnly {
|
buildBitcodeFile(compilerExecName, pr, srcFile, bcFile)
|
||||||
var srcFile = pr.InputFiles[0]
|
*bcObjLinks = append(*bcObjLinks, BitcodeToObjectLink{bcPath: bcFile, objPath: objFile})
|
||||||
objFile, bcFile := getArtifactNames(pr, 0, hidden)
|
} else {
|
||||||
buildObjectFile(compilerExecName, pr, srcFile, objFile)
|
for i, srcFile := range pr.InputFiles {
|
||||||
buildBitcodeFile(compilerExecName, pr, srcFile, bcFile)
|
objFile, bcFile := getArtifactNames(pr, i, hidden)
|
||||||
attachBitcodePathToObject(bcFile, objFile)
|
if hidden {
|
||||||
} else {
|
buildObjectFile(compilerExecName, pr, srcFile, objFile)
|
||||||
for i, srcFile := range pr.InputFiles {
|
*newObjectFiles = append(*newObjectFiles, objFile)
|
||||||
objFile, bcFile := getArtifactNames(pr, i, hidden)
|
}
|
||||||
buildObjectFile(compilerExecName, pr, srcFile, objFile)
|
if strings.HasSuffix(srcFile, ".bc") {
|
||||||
if hidden {
|
*bcObjLinks = append(*bcObjLinks, BitcodeToObjectLink{bcPath: srcFile, objPath: objFile})
|
||||||
newObjectFiles = append(newObjectFiles, objFile)
|
} else {
|
||||||
} else if strings.HasSuffix(srcFile, ".bc") {
|
buildBitcodeFile(compilerExecName, pr, srcFile, bcFile)
|
||||||
attachBitcodePathToObject(srcFile, objFile)
|
*bcObjLinks = append(*bcObjLinks, BitcodeToObjectLink{bcPath: bcFile, objPath: objFile})
|
||||||
} else {
|
}
|
||||||
buildBitcodeFile(compilerExecName, pr, srcFile, bcFile)
|
|
||||||
attachBitcodePathToObject(bcFile, objFile)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return
|
||||||
if !pr.IsCompileOnly {
|
|
||||||
compileTimeLinkFiles(compilerExecName, pr, newObjectFiles)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user