diff --git a/Makefile b/Makefile index a6a7628..3b8d0d8 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,16 @@ GOROOT := $(shell go env GOPATH) build: - go build + go build ./shared ./cmd/gclang ./cmd/gclang++ ./cmd/get-bc + + install: build - go install - ln -f -s $(GOROOT)/bin/gllvm $(GOROOT)/bin/gclang - ln -f -s $(GOROOT)/bin/gllvm $(GOROOT)/bin/gclang++ - ln -f -s $(GOROOT)/bin/gllvm $(GOROOT)/bin/get-bc + go install ./cmd/gclang ./cmd/gclang++ ./cmd/get-bc clean: go clean + rm -f gclang gclang++ get-bc uninstall: rm -f $(GOROOT)/bin/gclang diff --git a/cmd/gclang++/main.go b/cmd/gclang++/main.go new file mode 100644 index 0000000..ff8e162 --- /dev/null +++ b/cmd/gclang++/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "os" + "github.com/SRI-CSL/gllvm/shared" +) + +func main() { + // Parse command line + var args = os.Args + args = args[1:] + + + exitCode := shared.Compile(args, "clang++") + + + shared.LogInfo("Calling %v returned %v\n", os.Args, exitCode) + + //important to pretend to look like the actual wrapped command + os.Exit(exitCode) + +} diff --git a/cmd/gclang/main.go b/cmd/gclang/main.go new file mode 100644 index 0000000..318e991 --- /dev/null +++ b/cmd/gclang/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "os" + "github.com/SRI-CSL/gllvm/shared" +) + +func main() { + // Parse command line + var args = os.Args + args = args[1:] + + + exitCode := shared.Compile(args, "clang") + + shared.LogInfo("Calling %v returned %v\n", os.Args, exitCode) + + //important to pretend to look like the actual wrapped command + os.Exit(exitCode) + +} diff --git a/cmd/get-bc/main.go b/cmd/get-bc/main.go new file mode 100644 index 0000000..0147cf4 --- /dev/null +++ b/cmd/get-bc/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "os" + "github.com/SRI-CSL/gllvm/shared" +) + +func main() { + // Parse command line + var args = os.Args + + shared.Extract(args) + + shared.LogInfo("Calling %v DID NOT TELL US WHAT HAPPENED\n", os.Args) + + // could be more honest about our success here + os.Exit(0) + +} diff --git a/gllvm.go b/gllvm.go deleted file mode 100644 index 0137fc0..0000000 --- a/gllvm.go +++ /dev/null @@ -1,32 +0,0 @@ -package main - -import ( - "os" - "path" -) - -func main() { - // Parse command line - var args = os.Args - _, callerName := path.Split(args[0]) - args = args[1:] - - var exitCode int - - switch callerName { - case "gclang": - exitCode = compile(args, "clang") - case "gclang++": - exitCode = compile(args, "clang++") - case "get-bc": - extract(args) - default: - logError("You should call %s with a valid name.", callerName) - } - - logInfo("Calling %v returned %v\n", os.Args, exitCode) - - //important to pretend to look like the actual wrapped command - os.Exit(exitCode) - -} diff --git a/compiler.go b/shared/compiler.go similarity index 91% rename from compiler.go rename to shared/compiler.go index 2a9954f..49bdcd9 100644 --- a/compiler.go +++ b/shared/compiler.go @@ -1,4 +1,5 @@ -package main +package shared + import ( "io" @@ -16,7 +17,7 @@ type bitcodeToObjectLink struct { objPath string } -func compile(args []string, compilerName string) (exitCode int) { +func Compile(args []string, compilerName 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. @@ -117,14 +118,14 @@ func attachBitcodePathToObject(bcFile, objFile string) { tmpContent := []byte(absBcPath + "\n") tmpFile, err := ioutil.TempFile("", "gllvm") if err != nil { - logFatal("attachBitcodePathToObject: %v\n", err) + LogFatal("attachBitcodePathToObject: %v\n", err) } defer os.Remove(tmpFile.Name()) if _, err := tmpFile.Write(tmpContent); err != nil { - logFatal("attachBitcodePathToObject: %v\n", err) + LogFatal("attachBitcodePathToObject: %v\n", err) } if err := tmpFile.Close(); err != nil { - logFatal("attachBitcodePathToObject: %v\n", err) + LogFatal("attachBitcodePathToObject: %v\n", err) } // Let's write the bitcode section @@ -164,7 +165,7 @@ func compileTimeLinkFiles(compilerExecName string, pr parserResult, objFiles []s args = append(args, "-o", outputFile) success, err := execCmd(compilerExecName, args, "") if !success { - logFatal("Failed to link: %v.", err) + LogFatal("Failed to link: %v.", err) } } @@ -174,7 +175,7 @@ func buildObjectFile(compilerExecName string, pr parserResult, srcFile string, o 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) + LogFatal("Failed to build object file for %s because: %v\n", srcFile, err) } } @@ -184,7 +185,7 @@ func buildBitcodeFile(compilerExecName string, pr parserResult, srcFile string, 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) + LogFatal("Failed to build bitcode file for %s because: %v\n", srcFile, err) } } @@ -193,7 +194,7 @@ func execCompile(compilerExecName string, pr parserResult, wg *sync.WaitGroup, o defer (*wg).Done() success, err := execCmd(compilerExecName, pr.InputList, "") if !success { - logError("Failed to compile using given arguments: %v\n", err) + LogError("Failed to compile using given arguments: %v\n", err) *ok = false } } @@ -211,7 +212,7 @@ func getCompilerExecName(compilerName string) string { } return filepath.Join(LLVMToolChainBinDir, compilerName) default: - logFatal("The compiler %s is not supported by this tool.", compilerName) + LogFatal("The compiler %s is not supported by this tool.", compilerName) return "" } } diff --git a/constants.go b/shared/constants.go similarity index 98% rename from constants.go rename to shared/constants.go index f10eb04..47109bd 100644 --- a/constants.go +++ b/shared/constants.go @@ -1,4 +1,4 @@ -package main +package shared import ( "os" diff --git a/extractor.go b/shared/extractor.go similarity index 87% rename from extractor.go rename to shared/extractor.go index 690d87a..74aef46 100644 --- a/extractor.go +++ b/shared/extractor.go @@ -1,4 +1,4 @@ -package main +package shared import ( "debug/elf" @@ -25,7 +25,7 @@ type extractionArgs struct { IsBuildBitcodeArchive bool } -func extract(args []string) { +func Extract(args []string) { ea := parseExtractionArgs(args) switch ea.InputType { @@ -39,7 +39,7 @@ func extract(args []string) { case fileTypeARCHIVE: handleArchive(ea) default: - logFatal("Incorrect input file type %v.", ea.InputType) + LogFatal("Incorrect input file type %v.", ea.InputType) } } @@ -84,7 +84,7 @@ func parseExtractionArgs(args []string) extractionArgs { args = args[1:] case "-o": if len(args) < 2 { - logFatal("There was an error parsing the arguments: %v.", origArgs) + LogFatal("There was an error parsing the arguments: %v.", origArgs) } ea.OutputFile = args[1] args = args[2:] @@ -96,14 +96,14 @@ func parseExtractionArgs(args []string) extractionArgs { // Sanity-check the parsed arguments if len(ea.InputFile) == 0 { - logFatal("No input file was given.") + LogFatal("No input file was given.") } if _, err := os.Stat(ea.InputFile); os.IsNotExist(err) { - logFatal("The input file %s does not exist.", ea.InputFile) + LogFatal("The input file %s does not exist.", ea.InputFile) } realPath, err := filepath.EvalSymlinks(ea.InputFile) if err != nil { - logFatal("There was an error getting the real path of %s.", ea.InputFile) + LogFatal("There was an error getting the real path of %s.", ea.InputFile) } ea.InputFile = realPath ea.InputType = getFileType(realPath) @@ -126,7 +126,7 @@ func parseExtractionArgs(args []string) extractionArgs { } ea.ObjectTypeInArchive = fileTypeMACHOBJECT default: - logFatal("Unsupported platform: %s.", platform) + LogFatal("Unsupported platform: %s.", platform) } // Create output filename if not given @@ -169,7 +169,7 @@ func handleArchive(ea extractionArgs) { // Create tmp dir tmpDirName, err := ioutil.TempDir("", "gllvm") if err != nil { - logFatal("The temporary directory in which to extract object files could not be created.") + LogFatal("The temporary directory in which to extract object files could not be created.") } defer os.RemoveAll(tmpDirName) @@ -179,7 +179,7 @@ func handleArchive(ea extractionArgs) { arArgs = append(arArgs, inputAbsPath) 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) + LogFatal("Failed to extract object files from %s to %s because: %v.\n", ea.InputFile, tmpDirName, err) } // Define object file handling closure @@ -232,10 +232,10 @@ func archiveBcFiles(ea extractionArgs, bcFiles []string) { args = append(args, bcFilesInDir...) success, err := execCmd(ea.ArchiverName, args, dir) if !success { - logFatal("There was an error creating the bitcode archive: %v.\n", err) + LogFatal("There was an error creating the bitcode archive: %v.\n", err) } } - logInfo("Built bitcode archive: %s.", ea.OutputFile) + LogInfo("Built bitcode archive: %s.", ea.OutputFile) } func extractTimeLinkFiles(ea extractionArgs, filesToLink []string) { @@ -247,20 +247,20 @@ func extractTimeLinkFiles(ea extractionArgs, filesToLink []string) { linkArgs = append(linkArgs, filesToLink...) success, err := execCmd(ea.LinkerName, linkArgs, "") if !success { - logFatal("There was an error linking input files into %s because %v.\n", ea.OutputFile, err) + LogFatal("There was an error linking input files into %s because %v.\n", ea.OutputFile, err) } - logInfo("Bitcode file extracted to: %s.", ea.OutputFile) + LogInfo("Bitcode file extracted to: %s.", ea.OutputFile) } func extractSectionDarwin(inputFile string) (contents []string) { machoFile, err := macho.Open(inputFile) if err != nil { - logFatal("Mach-O file %s could not be read.", inputFile) + LogFatal("Mach-O file %s could not be read.", inputFile) } section := machoFile.Section(DarwinSectionName) sectionContents, errContents := section.Data() if errContents != nil { - logFatal("Error reading the %s section of Mach-O file %s.", DarwinSectionName, inputFile) + LogFatal("Error reading the %s section of Mach-O file %s.", DarwinSectionName, inputFile) } contents = strings.Split(strings.TrimSuffix(string(sectionContents), "\n"), "\n") return @@ -269,12 +269,12 @@ func extractSectionDarwin(inputFile string) (contents []string) { func extractSectionUnix(inputFile string) (contents []string) { elfFile, err := elf.Open(inputFile) if err != nil { - logFatal("ELF file %s could not be read.", inputFile) + LogFatal("ELF file %s could not be read.", inputFile) } section := elfFile.Section(ELFSectionName) sectionContents, errContents := section.Data() if errContents != nil { - logFatal("Error reading the %s section of ELF file %s.", ELFSectionName, inputFile) + LogFatal("Error reading the %s section of ELF file %s.", ELFSectionName, inputFile) } contents = strings.Split(strings.TrimSuffix(string(sectionContents), "\n"), "\n") return @@ -304,7 +304,7 @@ func writeManifest(ea extractionArgs, bcFiles []string, artifactFiles []string) contents := []byte(section1 + section2) manifestFilename := ea.OutputFile + ".llvm.manifest" if err := ioutil.WriteFile(manifestFilename, contents, 0644); err != nil { - logFatal("There was an error while writing the manifest file: ", err) + LogFatal("There was an error while writing the manifest file: ", err) } - logInfo("Manifest file written to %s.", manifestFilename) + LogInfo("Manifest file written to %s.", manifestFilename) } diff --git a/filetypes.go b/shared/filetypes.go similarity index 95% rename from filetypes.go rename to shared/filetypes.go index 15bc8c4..acaa120 100644 --- a/filetypes.go +++ b/shared/filetypes.go @@ -1,4 +1,4 @@ -package main +package shared import ( "os" @@ -35,7 +35,7 @@ func getFileType(realPath string) (fileType int) { cmd := exec.Command("file", realPath) out, err := cmd.Output() if err != nil { - logFatal("There was an error getting the type of %s. Make sure that the 'file' command is installed.", realPath) + LogFatal("There was an error getting the type of %s. Make sure that the 'file' command is installed.", realPath) } // Test the output diff --git a/logging.go b/shared/logging.go similarity index 80% rename from logging.go rename to shared/logging.go index 776c297..32d20d4 100644 --- a/logging.go +++ b/shared/logging.go @@ -1,4 +1,4 @@ -package main +package shared import ( "fmt" @@ -49,12 +49,12 @@ func makeLogger(lvl int) func(format string, a ...interface{}) { } } -var logDebug = makeLogger(debugV) -var logInfo = makeLogger(infoV) -var logWarning = makeLogger(warningV) -var logError = makeLogger(errorV) +var LogDebug = makeLogger(debugV) +var LogInfo = makeLogger(infoV) +var LogWarning = makeLogger(warningV) +var LogError = makeLogger(errorV) -func logFatal(format string, a ...interface{}) { - logError(format, a...) +func LogFatal(format string, a ...interface{}) { + LogError(format, a...) os.Exit(1) } diff --git a/parser.go b/shared/parser.go similarity index 99% rename from parser.go rename to shared/parser.go index 0559286..0246b6d 100644 --- a/parser.go +++ b/shared/parser.go @@ -1,4 +1,4 @@ -package main +package shared import ( "crypto/sha256" diff --git a/utils.go b/shared/utils.go similarity index 96% rename from utils.go rename to shared/utils.go index 23ee777..a989015 100644 --- a/utils.go +++ b/shared/utils.go @@ -1,4 +1,4 @@ -package main +package shared import ( "os"