make execCmd more robust, understandable, and easier to debug.

This commit is contained in:
Ian A Mason 2017-06-30 10:00:25 -07:00
parent 06a31effc6
commit 58b6d135ac
3 changed files with 27 additions and 18 deletions

View File

@ -120,7 +120,7 @@ func attachBitcodePathToObject(bcFile, objFile string) {
}
// Run the attach command and ignore errors
execCmd(attachCmd, attachCmdArgs, "")
_, err = execCmd(attachCmd, attachCmdArgs, "")
// Copy bitcode file to store, if necessary
if bcStorePath := os.Getenv(envBCSTOREPATH); bcStorePath != "" {
@ -143,8 +143,9 @@ func compileTimeLinkFiles(compilerExecName string, pr parserResult, objFiles []s
args := append(pr.ObjectFiles, pr.LinkArgs...)
args = append(args, objFiles...)
args = append(args, "-o", outputFile)
if execCmd(compilerExecName, args, "") {
log.Fatal("Failed to link.")
success, err := execCmd(compilerExecName, args, "")
if !success {
logFatal("Failed to link: %v.", err)
}
}
@ -152,8 +153,9 @@ func compileTimeLinkFiles(compilerExecName string, pr parserResult, objFiles []s
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)
success, err := execCmd(compilerExecName, args, "")
if !success {
logFatal("Failed to build object file for %s because: %v\n", srcFile, err)
}
}
@ -161,16 +163,18 @@ func buildObjectFile(compilerExecName string, pr parserResult, srcFile string, o
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)
success, err := execCmd(compilerExecName, args, "")
if !success {
logFatal("Failed to build bitcode file for %s because: %v\n", srcFile, err)
}
}
// Tries to build object file
func execCompile(compilerExecName string, pr parserResult, wg *sync.WaitGroup) {
defer (*wg).Done()
if execCmd(compilerExecName, pr.InputList, "") {
log.Fatal("Failed to compile using given arguments.")
success, err := execCmd(compilerExecName, pr.InputList, "")
if !success {
logFatal("Failed to compile using given arguments: %v\n", err)
}
}

View File

@ -177,8 +177,9 @@ func handleArchive(ea extractionArgs) {
arArgs := ea.ArArgs
inputAbsPath, _ := filepath.Abs(ea.InputFile)
arArgs = append(arArgs, inputAbsPath)
if execCmd("ar", arArgs, tmpDirName) {
log.Fatal("Failed to extract object files from ", ea.InputFile, " to ", tmpDirName, ".")
success, err := execCmd("ar", arArgs, tmpDirName)
if !success {
logFatal("Failed to extract object files from %s to %s because: %v.\n", ea.InputFile, tmpDirName, err)
}
// Define object file handling closure
@ -229,8 +230,9 @@ func archiveBcFiles(ea extractionArgs, bcFiles []string) {
var args []string
args = append(args, "rs", absOutputFile)
args = append(args, bcFilesInDir...)
if execCmd(ea.ArchiverName, args, dir) {
log.Fatal("There was an error creating the bitcode archive.")
success, err := execCmd(ea.ArchiverName, args, dir)
if !success {
logFatal("There was an error creating the bitcode archive: %v.\n", err)
}
}
fmt.Println("Built bitcode archive", ea.OutputFile)
@ -243,8 +245,9 @@ func extractTimeLinkFiles(ea extractionArgs, filesToLink []string) {
}
linkArgs = append(linkArgs, "-o", ea.OutputFile)
linkArgs = append(linkArgs, filesToLink...)
if execCmd(ea.LinkerName, linkArgs, "") {
log.Fatal("There was an error linking input files into ", ea.OutputFile, ".")
success, err := execCmd(ea.LinkerName, linkArgs, "")
if !success {
log.Fatal("There was an error linking input files into %s because %v.\n", ea.OutputFile, err)
}
fmt.Println("Bitcode file extracted to", ea.OutputFile)
}

View File

@ -5,11 +5,13 @@ import (
"os/exec"
)
// Executes a command then returns true if there was an error
func execCmd(cmdExecName string, args []string, workingDir string) bool {
// Executes a command then returns true for success, false if there was an error, err is either nil or the error.
func execCmd(cmdExecName string, args []string, workingDir string) (success bool, err error) {
cmd := exec.Command(cmdExecName, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = workingDir
return cmd.Run() != nil
err = cmd.Run()
success = (err == nil)
return
}